Skip to content

Commit

Permalink
Fix core error creating item with non-item material
Browse files Browse the repository at this point in the history
Until recently, creating an item stack with a material that is not an item type would work but act like an empty item stack when added to an inventory. Paper now validates if it's an item type on creation. This makes CH throw an exception on invalid item types, but continues to convert legacy block-only items to air. material_info() can now be used to check if a material "isItem".
  • Loading branch information
PseudoKnight committed Dec 26, 2024
1 parent 2d41b43 commit 8d1b498
Show file tree
Hide file tree
Showing 5 changed files with 14 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -280,7 +280,7 @@ public MCItemStack GetItemStack(String type, int qty) {
if(mat == null) {
mat = Material.matchMaterial(type);
}
if(mat == null) {
if(mat == null || !mat.isItem()) {
return null;
}
return new BukkitMCItemStack(new ItemStack(mat, qty));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ public int hashCode() {

@Override
public Map<Integer, MCItemStack> addItem(MCItemStack stack) {
Map<Integer, ItemStack> h = i.addItem(stack == null ? null : ((BukkitMCItemStack) stack).is);
Map<Integer, ItemStack> h = i.addItem(((BukkitMCItemStack) stack).is);
Map<Integer, MCItemStack> m = new HashMap<>();
for(Map.Entry<Integer, ItemStack> entry : h.entrySet()) {
Integer key = entry.getKey();
Expand Down
7 changes: 5 additions & 2 deletions src/main/java/com/laytonsmith/core/ObjectGenerator.java
Original file line number Diff line number Diff line change
Expand Up @@ -373,6 +373,9 @@ public MCItemStack item(Mixed i, Target t, boolean legacy) {
}
}

if(!material.isItem()) {
material = MCMaterial.get("AIR");
}
ret = StaticLayer.GetItemStack(material, qty);
MSLog.GetLogger().w(MSLog.Tags.DEPRECATION, "Converted \"" + mat + "\" with data \""
+ data + "\" to " + material.getName(), t);
Expand Down Expand Up @@ -472,7 +475,7 @@ public Construct itemMeta(MCItemStack is, Target t) {
ma.set("modifiers", CNull.NULL, t);
} else {
CArray modifiers = new CArray(t);
for(MCAttributeModifier m : meta.getAttributeModifiers()) {
for(MCAttributeModifier m : modifierList) {
modifiers.push(attributeModifier(m, t), t);
}
ma.set("modifiers", modifiers, t);
Expand Down Expand Up @@ -2421,7 +2424,7 @@ public MCRecipe recipe(Mixed c, Target t) {
*/
private MCMaterial recipeMaterial(Mixed arg, Target t) {
MCMaterial mat = StaticLayer.GetMaterial(arg.val());
if(mat == null || mat.isAir()) {
if(mat == null || mat.isAir() || !mat.isItem()) {
throw new CREIllegalArgumentException("Recipe input ingredient is invalid: " + arg.val(), t);
}
return mat;
Expand Down
3 changes: 3 additions & 0 deletions src/main/java/com/laytonsmith/core/Static.java
Original file line number Diff line number Diff line change
Expand Up @@ -785,6 +785,9 @@ public static MCItemStack ParseItemNotation(String functionName, String notation
if(mat == null) {
throw new CREFormatException("Invalid item format: " + notation, t);
}
if(!mat.isItem()) {
mat = MCMaterial.get("AIR");
}
MCItemStack is = StaticLayer.GetItemStack(mat, qty);
MSLog.GetLogger().w(MSLog.Tags.DEPRECATION, "Item notation is deprecated."
+ " Converting '" + notation + "' to '" + is.getType().getName() + "'.", t);
Expand Down
5 changes: 4 additions & 1 deletion src/main/java/com/laytonsmith/core/functions/Minecraft.java
Original file line number Diff line number Diff line change
Expand Up @@ -1165,6 +1165,8 @@ public Mixed exec(Target t, Environment environment, Mixed... args) throws Confi
return CBoolean.get(mat.hasGravity());
case "isBlock":
return CBoolean.get(mat.isBlock());
case "isItem":
return CBoolean.get(mat.isItem());
case "isBurnable":
return CBoolean.get(mat.isBurnable());
case "isEdible":
Expand Down Expand Up @@ -1202,6 +1204,7 @@ public Mixed exec(Target t, Environment environment, Mixed... args) throws Confi
ret.set("maxDurability", new CInt(mat.getMaxDurability(), t), t);
ret.set("hasGravity", CBoolean.get(mat.hasGravity()), t);
ret.set("isBlock", CBoolean.get(mat.isBlock()), t);
ret.set("isItem", CBoolean.get(mat.isItem()), t);
ret.set("isBurnable", CBoolean.get(mat.isBurnable()), t);
ret.set("isEdible", CBoolean.get(mat.isEdible()), t);
ret.set("isFlammable", CBoolean.get(mat.isFlammable()), t);
Expand Down Expand Up @@ -1230,7 +1233,7 @@ public Integer[] numArgs() {
@Override
public String docs() {
return "mixed {material, [trait]} Returns an array of info about the material. If a trait is specified,"
+ " it returns only that trait. Available traits: hasGravity, isBlock, isBurnable, isEdible,"
+ " it returns only that trait. Available traits: hasGravity, isBlock, isItem, isBurnable, isEdible,"
+ " isFlammable, isOccluding, isRecord, isSolid, isTransparent, isInteractable, maxDurability,"
+ " hardness (for block materials only), blastResistance (for block materials only),"
+ " and maxStacksize. The accuracy of these values depend on the server implementation.";
Expand Down

0 comments on commit 8d1b498

Please sign in to comment.