Permalink
Browse files

Update enchantment arrays in item meta arrays to be associative, and …

…update a few enchantment functions.

Old enchantment arrays will still be accepted and applied to the item. The functions enchant_inv(), enchant_rm_inv(), and get_enchant_inv() have been deprecated for enchant_item(), remove_item_enchant() and get_item_enchants() respectively. The sandbox function enchant_inv_unsafe() has been removed due to obsolescence.
  • Loading branch information...
PseudoKnight committed Aug 13, 2018
1 parent 9422e90 commit 90a38b5dee5e469c46e2d75ddb98aac9da4689a9
@@ -7,4 +7,6 @@
int getMaxLevel();
String getName();
String getKey();
}
@@ -183,9 +183,12 @@ public Class GetServerEventMixin() {
@Override
public MCEnchantment GetEnchantmentByName(String name) {
Enchantment enchant = Enchantment.getByName(name);
Enchantment enchant = Enchantment.getByKey(NamespacedKey.minecraft(name.toLowerCase()));
if(enchant == null) {
return null;
enchant = Enchantment.getByName(name.toUpperCase());
if(enchant == null) {
return null;
}
}
return new BukkitMCEnchantment(enchant);
}
@@ -52,6 +52,11 @@ public String getName() {
return e.getName();
}
@Override
public String getKey() {
return e.getKey().getKey();
}
@Override
public String toString() {
return e.toString();
@@ -1083,23 +1083,28 @@ public Vector3D vector(Vector3D v, Construct c, Target t) {
}
public CArray enchants(Map<MCEnchantment, Integer> map, Target t) {
CArray ret = new CArray(t);
CArray ret = CArray.GetAssociativeArray(t);
for(Map.Entry<MCEnchantment, Integer> entry : map.entrySet()) {
CArray eObj = CArray.GetAssociativeArray(t);
eObj.set("etype", new CString(entry.getKey().getName(), t), t);
eObj.set("elevel", new CInt(entry.getValue(), t), t);
ret.push(eObj, t);
ret.set(entry.getKey().getKey(), new CInt(entry.getValue(), t), t);
}
return ret;
}
public Map<MCEnchantment, Integer> enchants(CArray enchantArray, Target t) {
Map<MCEnchantment, Integer> ret = new HashMap<>();
for(String key : enchantArray.stringKeySet()) {
try {
CArray ea = (CArray) enchantArray.get(key, t);
if(enchantArray.isAssociative()) {
MCEnchantment etype = StaticLayer.GetEnchantmentByName(key);
if(etype == null) {
throw new CREEnchantmentException("Unknown enchantment type: " + key, t);
}
int elevel = Static.getInt32(enchantArray.get(key, t), t);
ret.put(etype, elevel);
} else {
// legacy
CArray ea = Static.getArray(enchantArray.get(key, t), t);
String setype = ea.get("etype", t).val();
MCEnchantment etype = StaticLayer.GetConvertor().GetEnchantmentByName(setype);
MCEnchantment etype = StaticLayer.GetEnchantmentByName(setype);
int elevel = Static.getInt32(ea.get("elevel", t), t);
if(etype == null) {
if(setype.equals("SWEEPING")) {
@@ -1111,8 +1116,6 @@ public CArray enchants(Map<MCEnchantment, Integer> map, Target t) {
}
}
ret.put(etype, elevel);
} catch (ClassCastException cce) {
throw new CREFormatException("Expected an array at index " + key, t);
}
}
return ret;
Oops, something went wrong.

0 comments on commit 90a38b5

Please sign in to comment.