From e3f388e485e9c291ae54862aad299ef596c1e63b Mon Sep 17 00:00:00 2001 From: BuildTools Date: Mon, 7 Jan 2019 12:33:59 -0500 Subject: [PATCH] Only compare ItemFlags above MC 1.11, fixes #609 --- src/main/java/me/blackvein/quests/Quest.java | 26 ++++++++++++------- .../java/me/blackvein/quests/Quester.java | 20 +++++++------- .../me/blackvein/quests/util/ItemUtil.java | 21 +++++++++++---- .../me/blackvein/quests/util/LocaleQuery.java | 6 ++++- 4 files changed, 48 insertions(+), 25 deletions(-) diff --git a/src/main/java/me/blackvein/quests/Quest.java b/src/main/java/me/blackvein/quests/Quest.java index e9a27158c..d32b5034f 100644 --- a/src/main/java/me/blackvein/quests/Quest.java +++ b/src/main/java/me/blackvein/quests/Quest.java @@ -573,12 +573,16 @@ public class Quest { if (i.getEnchantments().isEmpty()) { text = "- " + ChatColor.DARK_AQUA + ChatColor.ITALIC + i.getItemMeta().getDisplayName() + ChatColor.RESET + ChatColor.GRAY + " x " + i.getAmount(); } else { - text = "- " + ChatColor.DARK_AQUA + ChatColor.ITALIC + i.getItemMeta().getDisplayName() + ChatColor.RESET; - if (!i.getItemMeta().hasItemFlag(ItemFlag.HIDE_ENCHANTS)) { - text += ChatColor.GRAY + " " + Lang.get(player, "with") + ChatColor.DARK_PURPLE; - for (Entry e : i.getEnchantments().entrySet()) { - text += " " + Quester.prettyEnchantmentString(e.getKey()) + ":" + e.getValue(); + text = "- " + ChatColor.DARK_AQUA + ChatColor.ITALIC + i.getItemMeta().getDisplayName() + ChatColor.RESET; + try { + if (!i.getItemMeta().hasItemFlag(ItemFlag.HIDE_ENCHANTS)) { + text += ChatColor.GRAY + " " + Lang.get(player, "with") + ChatColor.DARK_PURPLE; + for (Entry e : i.getEnchantments().entrySet()) { + text += " " + Quester.prettyEnchantmentString(e.getKey()) + ":" + e.getValue(); + } } + } catch (Throwable tr) { + // Do nothing, hasItemFlag() not introduced until 1.8.6 } text += ChatColor.GRAY + " x " + i.getAmount(); } @@ -597,11 +601,15 @@ public class Quest { text = "- " + ChatColor.DARK_GREEN + ItemUtil.getName(i) + ChatColor.GRAY + " x " + i.getAmount(); } else { text = "- " + ChatColor.DARK_GREEN + ItemUtil.getName(i); - if (!i.getItemMeta().hasItemFlag(ItemFlag.HIDE_ENCHANTS)) { - text += ChatColor.GRAY + " " + Lang.get(player, "with"); - for (Entry e : i.getEnchantments().entrySet()) { - text += " " + Quester.prettyEnchantmentString(e.getKey()) + ":" + e.getValue(); + try { + if (!i.getItemMeta().hasItemFlag(ItemFlag.HIDE_ENCHANTS)) { + text += ChatColor.GRAY + " " + Lang.get(player, "with"); + for (Entry e : i.getEnchantments().entrySet()) { + text += " " + Quester.prettyEnchantmentString(e.getKey()) + ":" + e.getValue(); + } } + } catch (Throwable tr) { + // Do nothing, hasItemFlag() not introduced until 1.8.6 } text += ChatColor.GRAY + " x " + i.getAmount(); } diff --git a/src/main/java/me/blackvein/quests/Quester.java b/src/main/java/me/blackvein/quests/Quester.java index 5c2bf8491..d2eb9577b 100644 --- a/src/main/java/me/blackvein/quests/Quester.java +++ b/src/main/java/me/blackvein/quests/Quester.java @@ -915,7 +915,7 @@ public class Quester { //Blocks are solid so check for durability if (m.getDurability() == is.getDurability()) { broken = is; - } else if (!LocaleQuery.oldVersion) { + } else if (!LocaleQuery.isBelow113(Quests.bukkitVersion)) { //Ignore durability for 1.13+ broken = is; } @@ -932,7 +932,7 @@ public class Quester { //Blocks are solid so check for durability if (m.getDurability() == is.getDurability()) { toBreak = is; - } else if (!LocaleQuery.oldVersion) { + } else if (!LocaleQuery.isBelow113(Quests.bukkitVersion)) { //Ignore durability for 1.13+ toBreak = is; } @@ -966,7 +966,7 @@ public class Quester { //Blocks are solid so check for durability if (m.getDurability() == is.getDurability()) { damaged = is; - } else if (!LocaleQuery.oldVersion) { + } else if (!LocaleQuery.isBelow113(Quests.bukkitVersion)) { //Ignore durability for 1.13+ damaged = is; } @@ -982,7 +982,7 @@ public class Quester { //Blocks are solid so check for durability if (m.getDurability() == is.getDurability()) { toDamage = is; - } else if (!LocaleQuery.oldVersion) { + } else if (!LocaleQuery.isBelow113(Quests.bukkitVersion)) { //Ignore durability for 1.13+ toDamage = is; } @@ -1016,7 +1016,7 @@ public class Quester { //Blocks are solid so check for durability if (m.getDurability() == is.getDurability()) { placed = is; - } else if (!LocaleQuery.oldVersion) { + } else if (!LocaleQuery.isBelow113(Quests.bukkitVersion)) { //Ignore durability for 1.13+ placed = is; } @@ -1032,7 +1032,7 @@ public class Quester { //Blocks are solid so check for durability if (m.getDurability() == is.getDurability()) { toPlace = is; - } else if (!LocaleQuery.oldVersion) { + } else if (!LocaleQuery.isBelow113(Quests.bukkitVersion)) { //Ignore durability for 1.13+ toPlace = is; } @@ -1066,7 +1066,7 @@ public class Quester { //Blocks are solid so check for durability if (m.getDurability() == is.getDurability()) { used = is; - } else if (!LocaleQuery.oldVersion) { + } else if (!LocaleQuery.isBelow113(Quests.bukkitVersion)) { //Ignore durability for 1.13+ used = is; } @@ -1082,7 +1082,7 @@ public class Quester { //Blocks are solid, so check durability if (m.getDurability() == is.getDurability()) { toUse = is; - } else if (!LocaleQuery.oldVersion) { + } else if (!LocaleQuery.isBelow113(Quests.bukkitVersion)) { //Ignore durability for 1.13+ toUse = is; } @@ -1116,7 +1116,7 @@ public class Quester { //Blocks are solid so check for durability if (m.getDurability() == is.getDurability()) { cut = is; - } else if (!LocaleQuery.oldVersion) { + } else if (!LocaleQuery.isBelow113(Quests.bukkitVersion)) { //Ignore durability for 1.13+ cut = is; } @@ -1132,7 +1132,7 @@ public class Quester { //Blocks are solid so check for durability if (m.getDurability() == is.getDurability()) { toCut = is; - } else if (!LocaleQuery.oldVersion) { + } else if (!LocaleQuery.isBelow113(Quests.bukkitVersion)) { //Ignore durability for 1.13+ toCut = is; } diff --git a/src/main/java/me/blackvein/quests/util/ItemUtil.java b/src/main/java/me/blackvein/quests/util/ItemUtil.java index 91fd53eb9..a720a55ab 100644 --- a/src/main/java/me/blackvein/quests/util/ItemUtil.java +++ b/src/main/java/me/blackvein/quests/util/ItemUtil.java @@ -82,10 +82,17 @@ public class ItemUtil { } else if (one.getItemMeta().hasLore() && two.getItemMeta().hasLore() && one.getItemMeta().getLore().equals(two.getItemMeta().getLore()) == false) { return -4; } - for (ItemFlag flag : ItemFlag.values()) { - if (one.getItemMeta().hasItemFlag(flag) == false && two.getItemMeta().hasItemFlag(flag)) { - return -7; + try { + ItemMeta test = one.getItemMeta(); + test.setUnbreakable(true); + // We're on 1.11+ so check ItemFlags + for (ItemFlag flag : ItemFlag.values()) { + if (one.getItemMeta().hasItemFlag(flag) == false && two.getItemMeta().hasItemFlag(flag)) { + return -7; + } } + } catch (Throwable tr) { + // We're below 1.11 so don't check ItemFlags } } if (one.getEnchantments().equals(two.getEnchantments()) == false) { @@ -153,7 +160,6 @@ public class ItemUtil { String display = null; LinkedList lore = new LinkedList(); String[] flags = new String[10]; - //ItemFlag[] flags = new ItemFlag[ItemFlag.values().length]; LinkedHashMap stored = new LinkedHashMap(); LinkedHashMap extra = new LinkedHashMap(); ItemMeta meta = null; @@ -200,7 +206,6 @@ public class ItemUtil { String[] mapping = value.replace("[", "").replace("]", "").split(", "); int index = 0; for (String s : mapping) { - //flags[index] = ItemFlag.valueOf(s); flags[index] = s; index++; } @@ -255,6 +260,8 @@ public class ItemUtil { meta.setUnbreakable(true); } } catch (Throwable tr) { + // ItemMeta.setUnbrekable() not introduced until 1.11 + // However, NBT tags could be set by Spigot-only methods, so show error Bukkit.getLogger().info("You are running a version of CraftBukkit" + " for which Quests cannot set the NBT tag " + key); } @@ -299,6 +306,10 @@ public class ItemUtil { meta.addItemFlags(ItemFlag.valueOf(flag)); } catch (NullPointerException npe) { Bukkit.getLogger().severe(flag + " is not a valid ItemFlag"); + } catch (Throwable tr) { + // ItemMeta.addItemFlags() not introduced until 1.8.3 + Bukkit.getLogger().info("You are running a version of CraftBukkit" + + " for which Quests cannot add the item flag " + flag); } } } diff --git a/src/main/java/me/blackvein/quests/util/LocaleQuery.java b/src/main/java/me/blackvein/quests/util/LocaleQuery.java index c09b8beda..2da5f6dcf 100644 --- a/src/main/java/me/blackvein/quests/util/LocaleQuery.java +++ b/src/main/java/me/blackvein/quests/util/LocaleQuery.java @@ -31,7 +31,7 @@ public class LocaleQuery { private static Class craftMagicNumbers = null; private static Class itemClazz = null; private final Quests plugin; - public static boolean oldVersion = false; + private static boolean oldVersion = false; public LocaleQuery(Quests plugin){ this.plugin = plugin; @@ -158,6 +158,10 @@ public class LocaleQuery { } public static boolean isBelow113(String bukkitVersion) { + return _isBelow113(bukkitVersion); + } + + private static boolean _isBelow113(String bukkitVersion) { if (bukkitVersion.matches("^[0-9.]+$")) { switch(bukkitVersion) { case "1.12" :