From d0d4d9b4f12d1c1e1f3fed4f1daf65d615580c8e Mon Sep 17 00:00:00 2001 From: fullwall Date: Sat, 7 Apr 2018 16:01:13 +0800 Subject: [PATCH] 1.8.8 backwards compatibility --- .../api/trait/trait/Equipment.java | 14 +++++++++---- .../api/trait/trait/Inventory.java | 12 +++++++---- .../citizensnpcs/api/util/ItemStorage.java | 20 ++++++++++++------- 3 files changed, 31 insertions(+), 15 deletions(-) diff --git a/src/main/java/net/citizensnpcs/api/trait/trait/Equipment.java b/src/main/java/net/citizensnpcs/api/trait/trait/Equipment.java index 587045bb..62eeb9b3 100644 --- a/src/main/java/net/citizensnpcs/api/trait/trait/Equipment.java +++ b/src/main/java/net/citizensnpcs/api/trait/trait/Equipment.java @@ -122,13 +122,16 @@ public void onSpawn() { } else { EntityEquipment equip = getEquipmentFromEntity(npc.getEntity()); if (equipment[0] != null) { - equip.setItemInMainHand(equipment[0]); + equip.setItemInHand(equipment[0]); } equip.setHelmet(equipment[1]); equip.setChestplate(equipment[2]); equip.setLeggings(equipment[3]); equip.setBoots(equipment[4]); - equip.setItemInOffHand(equipment[5]); + try { + equip.setItemInOffHand(equipment[5]); + } catch (NoSuchMethodError e) { + } } if (npc.getEntity() instanceof Player) { ((Player) npc.getEntity()).updateInventory(); @@ -186,7 +189,7 @@ public void set(int slot, ItemStack item) { EntityEquipment equip = getEquipmentFromEntity(npc.getEntity()); switch (slot) { case 0: - equip.setItemInMainHand(item); + equip.setItemInHand(item); break; case 1: equip.setHelmet(item); @@ -201,7 +204,10 @@ public void set(int slot, ItemStack item) { equip.setBoots(item); break; case 5: - equip.setItemInOffHand(item); + try { + equip.setItemInOffHand(item); + } catch (NoSuchMethodError e) { + } break; default: throw new IllegalArgumentException("Slot must be between 0 and 5"); diff --git a/src/main/java/net/citizensnpcs/api/trait/trait/Inventory.java b/src/main/java/net/citizensnpcs/api/trait/trait/Inventory.java index fcd88582..28eb6280 100644 --- a/src/main/java/net/citizensnpcs/api/trait/trait/Inventory.java +++ b/src/main/java/net/citizensnpcs/api/trait/trait/Inventory.java @@ -67,8 +67,13 @@ public void inventoryCloseEvent(InventoryCloseEvent event) { } } if (npc.getEntity() instanceof InventoryHolder) { - int maxSize = ((InventoryHolder) npc.getEntity()).getInventory().getStorageContents().length; - ((InventoryHolder) npc.getEntity()).getInventory().setStorageContents(Arrays.copyOf(contents, maxSize)); + try { + int maxSize = ((InventoryHolder) npc.getEntity()).getInventory().getStorageContents().length; + ((InventoryHolder) npc.getEntity()).getInventory().setStorageContents(Arrays.copyOf(contents, maxSize)); + } catch (NoSuchMethodError e) { + int maxSize = ((InventoryHolder) npc.getEntity()).getInventory().getContents().length; + ((InventoryHolder) npc.getEntity()).getInventory().setContents(Arrays.copyOf(contents, maxSize)); + } } views.remove(event.getView()); } @@ -88,8 +93,7 @@ public void onSpawn() { setContents(contents); int size = npc.getEntity() instanceof Player ? 36 : npc.getEntity() instanceof InventoryHolder - ? ((InventoryHolder) npc.getEntity()).getInventory().getSize() - : contents.length; + ? ((InventoryHolder) npc.getEntity()).getInventory().getSize() : contents.length; int rem = size % 9; if (rem != 0) { size += 9 - rem; // round up to nearest multiple of 9 diff --git a/src/main/java/net/citizensnpcs/api/util/ItemStorage.java b/src/main/java/net/citizensnpcs/api/util/ItemStorage.java index 68a1745e..d64a719c 100644 --- a/src/main/java/net/citizensnpcs/api/util/ItemStorage.java +++ b/src/main/java/net/citizensnpcs/api/util/ItemStorage.java @@ -176,9 +176,12 @@ private static void deserialiseMeta(DataKey root, ItemStack res) { if (root.keyExists("potion")) { PotionMeta meta = ensureMeta(res); - PotionData data = new PotionData(PotionType.valueOf(root.getString("potion.data.type")), - root.getBoolean("potion.data.extended"), root.getBoolean("potion.data.upgraded")); - meta.setBasePotionData(data); + try { + PotionData data = new PotionData(PotionType.valueOf(root.getString("potion.data.type")), + root.getBoolean("potion.data.extended"), root.getBoolean("potion.data.upgraded")); + meta.setBasePotionData(data); + } catch (Throwable t) { + } for (DataKey sub : root.getRelative("potion.effects").getIntegerSubKeys()) { int duration = sub.getInt("duration"); int amplifier = sub.getInt("amplifier"); @@ -392,11 +395,14 @@ private static void serialiseMeta(DataKey key, ItemMeta meta) { if (meta instanceof PotionMeta) { PotionMeta potion = (PotionMeta) meta; - PotionData data = potion.getBasePotionData(); List effects = potion.getCustomEffects(); - key.setBoolean("potion.data.extended", data.isExtended()); - key.setBoolean("potion.data.upgraded", data.isUpgraded()); - key.setString("potion.data.type", data.getType().name()); + try { + PotionData data = potion.getBasePotionData(); + key.setBoolean("potion.data.extended", data.isExtended()); + key.setBoolean("potion.data.upgraded", data.isUpgraded()); + key.setString("potion.data.type", data.getType().name()); + } catch (Throwable t) { + } key.removeKey("potion.effects"); DataKey effectKey = key.getRelative("potion.effects"); for (int i = 0; i < effects.size(); i++) {