Permalink
Browse files

BREAKING CHANGE: Move item damage from the "data" key to the "damage"…

… key in the item meta.

This also fixes damage on items with meta.
  • Loading branch information...
PseudoKnight committed Aug 25, 2018
1 parent 39e685f commit 52753f5746f0cd5a40c02addfeb5fc22b2355e96
Showing with 85 additions and 240 deletions.
  1. +0 −10 src/main/java/com/laytonsmith/abstraction/Convertor.java
  2. +4 −0 src/main/java/com/laytonsmith/abstraction/MCItemMeta.java
  3. +0 −5 src/main/java/com/laytonsmith/abstraction/MCItemStack.java
  4. +4 −20 src/main/java/com/laytonsmith/abstraction/StaticLayer.java
  5. +0 −47 src/main/java/com/laytonsmith/abstraction/bukkit/BukkitConvertor.java
  6. +11 −0 src/main/java/com/laytonsmith/abstraction/bukkit/BukkitMCItemMeta.java
  7. +0 −16 src/main/java/com/laytonsmith/abstraction/bukkit/BukkitMCItemStack.java
  8. +2 −6 src/main/java/com/laytonsmith/abstraction/bukkit/events/BukkitInventoryEvents.java
  9. +1 −0 src/main/java/com/laytonsmith/abstraction/enums/bukkit/BukkitMCLegacyMaterial.java
  10. +31 −26 src/main/java/com/laytonsmith/core/ObjectGenerator.java
  11. +4 −2 src/main/java/com/laytonsmith/core/Static.java
  12. +9 −43 src/main/java/com/laytonsmith/core/events/drivers/BlockEvents.java
  13. +5 −0 src/main/java/com/laytonsmith/core/events/drivers/EntityEvents.java
  14. +3 −0 src/main/java/com/laytonsmith/core/events/drivers/InventoryEvents.java
  15. +2 −0 src/main/java/com/laytonsmith/core/events/drivers/PlayerEvents.java
  16. +1 −0 src/main/java/com/laytonsmith/core/events/drivers/VehicleEvents.java
  17. +1 −5 src/main/java/com/laytonsmith/core/functions/InventoryManagement.java
  18. +3 −2 src/main/java/com/laytonsmith/core/functions/ItemMeta.java
  19. +2 −2 src/main/java/com/laytonsmith/core/functions/Minecraft.java
  20. +1 −1 src/main/java/com/laytonsmith/core/functions/MobManagement.java
  21. +1 −1 src/main/java/com/laytonsmith/core/functions/PlayerManagement.java
  22. +0 −25 src/main/java/com/laytonsmith/tools/Interpreter.java
  23. +0 −29 src/test/java/com/laytonsmith/testing/StaticTest.java
@@ -39,24 +39,14 @@
MCServer GetServer();
MCItemStack GetItemStack(int type, int qty);
MCItemStack GetItemStack(int type, int data, int qty);
MCItemStack GetItemStack(MCMaterial type, int qty);
MCItemStack GetItemStack(MCMaterial type, int data, int qty);
MCItemStack GetItemStack(String type, int qty);
MCItemStack GetItemStack(String type, int data, int qty);
MCPotionData GetPotionData(MCPotionType type, boolean extended, boolean upgraded);
void Startup(CommandHelperPlugin chp);
MCMaterial getMaterial(int id);
MCMaterial GetMaterialFromLegacy(String name, int data);
MCMaterial GetMaterialFromLegacy(int id, int data);
@@ -106,4 +106,8 @@
boolean isUnbreakable();
void setUnbreakable(boolean unbreakable);
int getDamage();
void setDamage(int damage);
}
@@ -4,17 +4,12 @@
import java.util.Map;
public interface MCItemStack extends AbstractionObject {
short getDurability();
/**
* @deprecated Magic value
*/
@Deprecated
int getTypeId();
void setDurability(short data);
void addEnchantment(MCEnchantment e, int level);
void addUnsafeEnchantment(MCEnchantment e, int level);
@@ -64,38 +64,22 @@ public static MCLocation GetLocation(MCWorld w, double x, double y, double z) {
return convertor.GetServerEventMixin();
}
public static MCMaterial GetMaterialFromLegacy(int type, int qty) {
return convertor.GetMaterialFromLegacy(type, qty);
public static MCMaterial GetMaterialFromLegacy(int type, int data) {
return convertor.GetMaterialFromLegacy(type, data);
}
public static MCMaterial GetMaterialFromLegacy(String name, int qty) {
return convertor.GetMaterialFromLegacy(name, qty);
}
public static MCItemStack GetItemStack(int type, int qty) {
return convertor.GetItemStack(type, qty);
}
public static MCItemStack GetItemStack(int type, int data, int qty) {
return convertor.GetItemStack(type, data, qty);
public static MCMaterial GetMaterialFromLegacy(String name, int data) {
return convertor.GetMaterialFromLegacy(name, data);
}
public static MCItemStack GetItemStack(String type, int qty) {
return convertor.GetItemStack(type, qty);
}
public static MCItemStack GetItemStack(String type, int data, int qty) {
return convertor.GetItemStack(type, data, qty);
}
public static MCItemStack GetItemStack(MCMaterial type, int qty) {
return convertor.GetItemStack(type, qty);
}
public static MCItemStack GetItemStack(MCMaterial type, int data, int qty) {
return convertor.GetItemStack(type, data, qty);
}
public static MCPotionData GetPotionData(MCPotionType type, boolean extended, boolean upgraded) {
return convertor.GetPotionData(type, extended, upgraded);
}
@@ -196,12 +196,6 @@ public MCServer GetServer() {
return BukkitMCServer.Get();
}
@Override
public MCMaterial getMaterial(int id) {
Material mat = BukkitMCLegacyMaterial.getMaterial(id);
return mat == null ? null : new BukkitMCMaterial(mat);
}
@Override
public MCMaterial GetMaterialFromLegacy(String mat, int data) {
Material m = BukkitMCLegacyMaterial.getMaterial(mat, data);
@@ -234,34 +228,11 @@ public MCMaterial GetMaterial(String name) {
return null;
}
@Override
public MCItemStack GetItemStack(int id, int qty) {
Material mat = BukkitMCLegacyMaterial.getMaterial(id);
if(mat == null) {
return null;
}
return new BukkitMCItemStack(new ItemStack(mat, qty));
}
@Override
public MCItemStack GetItemStack(int id, int data, int qty) {
Material mat = BukkitMCLegacyMaterial.getMaterial(id, data);
if(mat == null) {
return null;
}
return new BukkitMCItemStack(new ItemStack(mat, qty, (short) data));
}
@Override
public MCItemStack GetItemStack(MCMaterial type, int qty) {
return new BukkitMCItemStack(new ItemStack(((BukkitMCMaterial) type).getHandle(), qty));
}
@Override
public MCItemStack GetItemStack(MCMaterial type, int data, int qty) {
return new BukkitMCItemStack(new ItemStack(((BukkitMCMaterial) type).getHandle(), qty, (short) data));
}
@Override
public MCItemStack GetItemStack(String type, int qty) {
Material mat = Material.getMaterial(type);
@@ -274,24 +245,6 @@ public MCItemStack GetItemStack(String type, int qty) {
return new BukkitMCItemStack(new ItemStack(mat, qty));
}
@Override
public MCItemStack GetItemStack(String type, int data, int qty) {
Material mat = Material.getMaterial(type);
if(mat == null) {
mat = BukkitMCLegacyMaterial.getMaterial(type, data);
} else if(mat.getMaxDurability() == 0) {
// only do this for non-damageable items with data values, which don't exist for modern materials
Material converted = BukkitMCLegacyMaterial.getMaterial(type, data);
if(converted != null) {
mat = converted;
}
}
if(mat == null) {
return null;
}
return new BukkitMCItemStack(new ItemStack(mat, qty, (short) data));
}
@Override
public MCPotionData GetPotionData(MCPotionType type, boolean extended, boolean upgraded) {
return new BukkitMCPotionData(new PotionData(
@@ -16,6 +16,7 @@
import java.util.Set;
import org.bukkit.enchantments.Enchantment;
import org.bukkit.inventory.ItemFlag;
import org.bukkit.inventory.meta.Damageable;
import org.bukkit.inventory.meta.ItemMeta;
import org.bukkit.inventory.meta.Repairable;
@@ -154,4 +155,14 @@ public boolean isUnbreakable() {
public void setUnbreakable(boolean unbreakable) {
im.setUnbreakable(unbreakable);
}
@Override
public int getDamage() {
return ((Damageable) im).getDamage();
}
@Override
public void setDamage(int damage) {
((Damageable) im).setDamage(damage);
}
}
@@ -36,14 +36,6 @@ public Object getHandle() {
return is;
}
@Override
public short getDurability() {
if(is == null) {
return 0;
}
return is.getDurability();
}
@Override
public int getTypeId() {
if(is == null) {
@@ -52,14 +44,6 @@ public int getTypeId() {
return Bukkit.getUnsafe().toLegacy(is.getType()).getId();
}
@Override
public void setDurability(short data) {
if(is == null) {
return;
}
is.setDurability(data);
}
@Override
public void addEnchantment(MCEnchantment e, int level) {
if(is == null) {
@@ -374,10 +374,8 @@ public void setItem(MCItemStack i) {
ItemStack is = ((BukkitMCItemStack) i).asItemStack();
item.setAmount(is.getAmount());
item.setData(is.getData());
item.setDurability(is.getDurability());
item.setItemMeta(is.getItemMeta());
item.setType(is.getType());
item.setItemMeta(is.getItemMeta());
}
@Override
@@ -436,10 +434,8 @@ public void setItem(MCItemStack i) {
ItemStack is = ((BukkitMCItemStack) i).asItemStack();
item.setAmount(is.getAmount());
item.setData(is.getData());
item.setDurability(is.getDurability());
item.setItemMeta(is.getItemMeta());
item.setType(is.getType());
item.setItemMeta(is.getItemMeta());
}
}
@@ -7,6 +7,7 @@
import java.util.HashMap;
@SuppressWarnings("deprecation")
public enum BukkitMCLegacyMaterial {
AIR(0),
STONE(1),
@@ -231,7 +231,6 @@ public Construct item(MCItemStack is, Target t) {
CArray ret = CArray.GetAssociativeArray(t);
ret.set("name", new CString(is.getType().getName(), t), t);
ret.set("data", new CInt(is.getDurability(), t), t);
ret.set("qty", new CInt(is.getAmount(), t), t);
ret.set("meta", itemMeta(is, t), t);
return ret;
@@ -268,14 +267,13 @@ public MCItemStack item(Construct i, Target t, boolean legacy) {
}
}
if(item.containsKey("data")) {
data = Static.getInt32(item.get("data", t), t);
}
legacy = legacy || item.containsKey("type");
legacy = legacy || item.containsKey("type") || item.containsKey("data");
if(legacy) {
// Do legacy item conversion
if(item.containsKey("data")) {
data = Static.getInt32(item.get("data", t), t);
}
MCMaterial material;
if(item.containsKey("name")) {
mat = item.get("name", t).val();
@@ -335,20 +333,13 @@ public MCItemStack item(Construct i, Target t, boolean legacy) {
}
}
ret = StaticLayer.GetItemStack(material, data, qty);
ret = StaticLayer.GetItemStack(material, qty);
CHLog.GetLogger().w(CHLog.Tags.DEPRECATION, "Converted \"" + mat + "\" with data \""
+ data + "\" to " + material.getName(), t);
} else if(item.containsKey("name")) {
mat = item.get("name", t).val();
if(data > 0) {
ret = StaticLayer.GetItemStack(mat, data, qty);
} else {
ret = StaticLayer.GetItemStack(mat, qty);
}
} else {
throw new CREFormatException("Could not find item name key in array!", t);
mat = item.get("name", t).val();
ret = StaticLayer.GetItemStack(mat, qty);
}
if(ret == null) {
@@ -369,6 +360,10 @@ public MCItemStack item(Construct i, Target t, boolean legacy) {
MCMapMeta meta = (MCMapMeta) ret.getItemMeta();
meta.setMapId(data);
ret.setItemMeta(meta);
} else if(data > 0 && ret.getType().getMaxDurability() > 0) {
MCItemMeta meta = ret.getItemMeta();
meta.setDamage(data);
ret.setItemMeta(meta);
}
}
@@ -415,7 +410,6 @@ public Construct itemMeta(MCItemStack is, Target t) {
ma.set("display", display, t);
ma.set("lore", lore, t);
ma.set("enchants", enchants(meta.getEnchants(), t), t);
ma.set("repair", new CInt(meta.getRepairCost(), t), t);
Set<MCItemFlag> itemFlags = meta.getItemFlags();
CArray flagArray = new CArray(t);
@@ -425,7 +419,12 @@ public Construct itemMeta(MCItemStack is, Target t) {
}
}
ma.set("flags", flagArray, t);
ma.set("unbreakable", CBoolean.get(meta.isUnbreakable()), t);
if(is.getType().getMaxDurability() > 0) {
ma.set("damage", new CInt(meta.getDamage(), t), t);
ma.set("unbreakable", CBoolean.get(meta.isUnbreakable()), t);
ma.set("repair", new CInt(meta.getRepairCost(), t), t);
}
// Specific ItemMeta
if(meta instanceof MCBlockStateMeta) {
@@ -650,9 +649,6 @@ public MCItemMeta itemMeta(Construct c, MCMaterial mat, Target t) throws ConfigR
throw new CREFormatException("Enchants field was expected to be an array of Enchantment arrays", t);
}
}
if(ma.containsKey("repair") && !(ma.get("repair", t) instanceof CNull)) {
meta.setRepairCost(Static.getInt32(ma.get("repair", t), t));
}
if(ma.containsKey("flags")) {
Construct flags = ma.get("flags", t);
if(flags instanceof CArray) {
@@ -665,8 +661,17 @@ public MCItemMeta itemMeta(Construct c, MCMaterial mat, Target t) throws ConfigR
throw new CREFormatException("Itemflags was expected to be an array of flags.", t);
}
}
if(ma.containsKey("unbreakable")) {
meta.setUnbreakable(Static.getBoolean(ma.get("unbreakable", t), t));
if(mat.getMaxDurability() > 0) {
if(ma.containsKey("damage")) {
meta.setDamage(Static.getInt32(ma.get("damage", t), t));
}
if(ma.containsKey("unbreakable")) {
meta.setUnbreakable(Static.getBoolean(ma.get("unbreakable", t), t));
}
if(ma.containsKey("repair") && !(ma.get("repair", t) instanceof CNull)) {
meta.setRepairCost(Static.getInt32(ma.get("repair", t), t));
}
}
// Specific ItemMeta
@@ -1491,13 +1496,13 @@ public MCRecipe recipe(Construct c, Target t) {
String[] split = ingredient.val().split(":");
mat = StaticLayer.GetMaterialFromLegacy(Integer.valueOf(split[0]), Integer.valueOf(split[1]));
} else {
mat = StaticLayer.GetConvertor().getMaterial(Integer.valueOf(ingredient.val()));
mat = StaticLayer.GetMaterialFromLegacy(Integer.valueOf(ingredient.val()), 0);
}
CHLog.GetLogger().w(CHLog.Tags.DEPRECATION, "Numeric item formats (eg. \"0:0\" are deprecated.", t);
} catch (NumberFormatException ex) {}
}
} else if(ingredient instanceof CInt) {
mat = StaticLayer.GetConvertor().getMaterial(Static.getInt32(ingredient, t));
mat = StaticLayer.GetMaterialFromLegacy(Static.getInt32(ingredient, t), 0);
CHLog.GetLogger().w(CHLog.Tags.DEPRECATION, "Numeric item ingredients are deprecated.", t);
} else if(ingredient instanceof CArray) {
mat = item(ingredient, t).getType();
@@ -1524,7 +1529,7 @@ public MCRecipe recipe(Construct c, Target t) {
String[] split = ingredient.val().split(":");
mat = StaticLayer.GetMaterialFromLegacy(Integer.valueOf(split[0]), Integer.valueOf(split[1]));
} else {
mat = StaticLayer.GetConvertor().getMaterial(Integer.valueOf(ingredient.val()));
mat = StaticLayer.GetMaterialFromLegacy(Integer.valueOf(ingredient.val()), 0);
}
CHLog.GetLogger().w(CHLog.Tags.DEPRECATION, "Numeric item formats (eg. \"0:0\" are deprecated.", t);
} catch (NumberFormatException ex) {}
Oops, something went wrong.

0 comments on commit 52753f5

Please sign in to comment.