Skip to content

Commit 8d1b498

Browse files
committed
Fix core error creating item with non-item material
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".
1 parent 2d41b43 commit 8d1b498

File tree

5 files changed

+14
-5
lines changed

5 files changed

+14
-5
lines changed

src/main/java/com/laytonsmith/abstraction/bukkit/BukkitConvertor.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -280,7 +280,7 @@ public MCItemStack GetItemStack(String type, int qty) {
280280
if(mat == null) {
281281
mat = Material.matchMaterial(type);
282282
}
283-
if(mat == null) {
283+
if(mat == null || !mat.isItem()) {
284284
return null;
285285
}
286286
return new BukkitMCItemStack(new ItemStack(mat, qty));

src/main/java/com/laytonsmith/abstraction/bukkit/BukkitMCInventory.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ public int hashCode() {
111111

112112
@Override
113113
public Map<Integer, MCItemStack> addItem(MCItemStack stack) {
114-
Map<Integer, ItemStack> h = i.addItem(stack == null ? null : ((BukkitMCItemStack) stack).is);
114+
Map<Integer, ItemStack> h = i.addItem(((BukkitMCItemStack) stack).is);
115115
Map<Integer, MCItemStack> m = new HashMap<>();
116116
for(Map.Entry<Integer, ItemStack> entry : h.entrySet()) {
117117
Integer key = entry.getKey();

src/main/java/com/laytonsmith/core/ObjectGenerator.java

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -373,6 +373,9 @@ public MCItemStack item(Mixed i, Target t, boolean legacy) {
373373
}
374374
}
375375

376+
if(!material.isItem()) {
377+
material = MCMaterial.get("AIR");
378+
}
376379
ret = StaticLayer.GetItemStack(material, qty);
377380
MSLog.GetLogger().w(MSLog.Tags.DEPRECATION, "Converted \"" + mat + "\" with data \""
378381
+ data + "\" to " + material.getName(), t);
@@ -472,7 +475,7 @@ public Construct itemMeta(MCItemStack is, Target t) {
472475
ma.set("modifiers", CNull.NULL, t);
473476
} else {
474477
CArray modifiers = new CArray(t);
475-
for(MCAttributeModifier m : meta.getAttributeModifiers()) {
478+
for(MCAttributeModifier m : modifierList) {
476479
modifiers.push(attributeModifier(m, t), t);
477480
}
478481
ma.set("modifiers", modifiers, t);
@@ -2421,7 +2424,7 @@ public MCRecipe recipe(Mixed c, Target t) {
24212424
*/
24222425
private MCMaterial recipeMaterial(Mixed arg, Target t) {
24232426
MCMaterial mat = StaticLayer.GetMaterial(arg.val());
2424-
if(mat == null || mat.isAir()) {
2427+
if(mat == null || mat.isAir() || !mat.isItem()) {
24252428
throw new CREIllegalArgumentException("Recipe input ingredient is invalid: " + arg.val(), t);
24262429
}
24272430
return mat;

src/main/java/com/laytonsmith/core/Static.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -785,6 +785,9 @@ public static MCItemStack ParseItemNotation(String functionName, String notation
785785
if(mat == null) {
786786
throw new CREFormatException("Invalid item format: " + notation, t);
787787
}
788+
if(!mat.isItem()) {
789+
mat = MCMaterial.get("AIR");
790+
}
788791
MCItemStack is = StaticLayer.GetItemStack(mat, qty);
789792
MSLog.GetLogger().w(MSLog.Tags.DEPRECATION, "Item notation is deprecated."
790793
+ " Converting '" + notation + "' to '" + is.getType().getName() + "'.", t);

src/main/java/com/laytonsmith/core/functions/Minecraft.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1165,6 +1165,8 @@ public Mixed exec(Target t, Environment environment, Mixed... args) throws Confi
11651165
return CBoolean.get(mat.hasGravity());
11661166
case "isBlock":
11671167
return CBoolean.get(mat.isBlock());
1168+
case "isItem":
1169+
return CBoolean.get(mat.isItem());
11681170
case "isBurnable":
11691171
return CBoolean.get(mat.isBurnable());
11701172
case "isEdible":
@@ -1202,6 +1204,7 @@ public Mixed exec(Target t, Environment environment, Mixed... args) throws Confi
12021204
ret.set("maxDurability", new CInt(mat.getMaxDurability(), t), t);
12031205
ret.set("hasGravity", CBoolean.get(mat.hasGravity()), t);
12041206
ret.set("isBlock", CBoolean.get(mat.isBlock()), t);
1207+
ret.set("isItem", CBoolean.get(mat.isItem()), t);
12051208
ret.set("isBurnable", CBoolean.get(mat.isBurnable()), t);
12061209
ret.set("isEdible", CBoolean.get(mat.isEdible()), t);
12071210
ret.set("isFlammable", CBoolean.get(mat.isFlammable()), t);
@@ -1230,7 +1233,7 @@ public Integer[] numArgs() {
12301233
@Override
12311234
public String docs() {
12321235
return "mixed {material, [trait]} Returns an array of info about the material. If a trait is specified,"
1233-
+ " it returns only that trait. Available traits: hasGravity, isBlock, isBurnable, isEdible,"
1236+
+ " it returns only that trait. Available traits: hasGravity, isBlock, isItem, isBurnable, isEdible,"
12341237
+ " isFlammable, isOccluding, isRecord, isSolid, isTransparent, isInteractable, maxDurability,"
12351238
+ " hardness (for block materials only), blastResistance (for block materials only),"
12361239
+ " and maxStacksize. The accuracy of these values depend on the server implementation.";

0 commit comments

Comments
 (0)