Permalink
Browse files

Partially revert item meta enchantments array changes. It is now an a…

…rray of arrays again, but the new enchantment keys still exist.

This should improve compatibility a bit with legacy scripts reading new enchanted item meta arrays, as well as future proof for possible enchantment data additions.
  • Loading branch information...
PseudoKnight committed Aug 28, 2018
1 parent 568e8cd commit 03e94bfba5e2fe08027443c61649a0230f26a24e
@@ -410,6 +410,7 @@ 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);
@@ -423,7 +424,6 @@ public Construct itemMeta(MCItemStack is, Target 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
@@ -650,6 +650,9 @@ 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) {
@@ -670,9 +673,6 @@ public MCItemMeta itemMeta(Construct c, MCMaterial mat, Target t) throws ConfigR
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
@@ -1159,38 +1159,45 @@ public Vector3D vector(Vector3D v, Construct c, Target t) {
public CArray enchants(Map<MCEnchantment, Integer> map, Target t) {
CArray ret = CArray.GetAssociativeArray(t);
for(Map.Entry<MCEnchantment, Integer> entry : map.entrySet()) {
ret.set(entry.getKey().getKey(), new CInt(entry.getValue(), t), t);
CArray enchant = CArray.GetAssociativeArray(t);
enchant.set("etype", new CString(entry.getKey().getName(), t), t);
enchant.set("elevel", new CInt(entry.getValue(), t), t);
ret.set(entry.getKey().getKey(), enchant, t);
}
return ret;
}
public Map<MCEnchantment, Integer> enchants(CArray enchantArray, Target t) {
Map<MCEnchantment, Integer> ret = new HashMap<>();
for(String key : enchantArray.stringKeySet()) {
MCEnchantment etype = null;
int elevel;
Construct value = enchantArray.get(key, t);
if(enchantArray.isAssociative()) {
MCEnchantment etype = StaticLayer.GetEnchantmentByName(key);
if(etype == null) {
throw new CREEnchantmentException("Unknown enchantment type: " + key, t);
etype = StaticLayer.GetEnchantmentByName(key);
if(etype != null && value instanceof CInt) {
ret.put(etype, Static.getInt32(value, t));
continue;
}
int elevel = Static.getInt32(enchantArray.get(key, t), t);
ret.put(etype, elevel);
} else {
// legacy
CArray ea = Static.getArray(enchantArray.get(key, t), t);
}
CArray ea = Static.getArray(value, t);
if(etype == null) {
String setype = ea.get("etype", t).val();
MCEnchantment etype = StaticLayer.GetEnchantmentByName(setype);
int elevel = Static.getInt32(ea.get("elevel", t), t);
etype = StaticLayer.GetEnchantmentByName(setype);
if(etype == null) {
if(setype.equals("SWEEPING")) {
// data from 1.11.2, changed in 1.12
etype = StaticLayer.GetEnchantmentByName("SWEEPING_EDGE");
}
if(etype == null) {
throw new CREEnchantmentException("Unknown enchantment type at " + key, t);
throw new CREEnchantmentException("Unknown enchantment type: " + setype, t);
}
}
ret.put(etype, elevel);
}
elevel = Static.getInt32(ea.get("elevel", t), t);
ret.put(etype, elevel);
}
return ret;
}
@@ -41,7 +41,8 @@ public static String docs() {
private static final String APPLICABLE_ITEM_META = "<ul>"
+ "<li>All items - \"display\" (string), \"lore\" (array of strings), \"enchants\" (An associative array of"
+ " enchantments with the type of enchantments as the key and the level as the value),"
+ " enchantments with the vanilla name of enchantment as the key, and value being an enchantment array with"
+ " the key \"elevel\" for the level of the enchantment),"
+ " \"flags\" (array). Possible flags: " + StringUtils.Join(MCItemFlag.values(), ", ", " or ") + "</li>"
+ "<li>Damageable: \"damage\" on the item (0 is undamaged; each item type has its own max durability),"
+ " \"unbreakable\" (boolean) and \"repair\" (int, repair cost)</li>"

1 comment on commit 03e94bf

@LadyCailin

This comment has been minimized.

Show comment
Hide comment
@LadyCailin

LadyCailin Aug 28, 2018

Contributor

👍

Contributor

LadyCailin commented on 03e94bf Aug 28, 2018

👍

Please sign in to comment.