From 40e2ccc12952adf4b416cfde314d34f5b9a9e593 Mon Sep 17 00:00:00 2001 From: PikaMug <2267126+PikaMug@users.noreply.github.com> Date: Wed, 3 Jul 2024 23:01:58 -0400 Subject: [PATCH] Yet another Paper workaround, fixes #2243 --- .../quests/listeners/BukkitBlockListener.java | 48 ++++++++++++------- .../me/pikamug/quests/util/BukkitLang.java | 4 +- 2 files changed, 33 insertions(+), 19 deletions(-) diff --git a/core/src/main/java/me/pikamug/quests/listeners/BukkitBlockListener.java b/core/src/main/java/me/pikamug/quests/listeners/BukkitBlockListener.java index 3587005df..0e6ce8bb3 100644 --- a/core/src/main/java/me/pikamug/quests/listeners/BukkitBlockListener.java +++ b/core/src/main/java/me/pikamug/quests/listeners/BukkitBlockListener.java @@ -58,8 +58,10 @@ public class BukkitBlockListener implements Listener { } final Player player = event.getPlayer(); if (plugin.canUseQuests(player.getUniqueId())) { - final short durability = getBlockDurability(event.getBlock()); - final ItemStack blockItemStack = new ItemStack(event.getBlock().getType(), 1, durability); + final ItemStack blockItemStack = getItemEquivalent(event.getBlock()); + if (blockItemStack == null) { + return; + } final BukkitQuester quester = plugin.getQuester(player.getUniqueId()); final ObjectiveType breakType = ObjectiveType.BREAK_BLOCK; final ObjectiveType placeType = ObjectiveType.PLACE_BLOCK; @@ -186,8 +188,10 @@ public class BukkitBlockListener implements Listener { } final Player player = event.getPlayer(); if (plugin.canUseQuests(player.getUniqueId())) { - final short durability = getBlockDurability(event.getBlock()); - final ItemStack blockItemStack = new ItemStack(event.getBlock().getType(), 1, durability); + final ItemStack blockItemStack = getItemEquivalent(event.getBlock()); + if (blockItemStack == null) { + return; + } final Quester quester = plugin.getQuester(player.getUniqueId()); final ObjectiveType type = ObjectiveType.DAMAGE_BLOCK; final Set dispatchedQuestIDs = new HashSet<>(); @@ -220,8 +224,10 @@ public class BukkitBlockListener implements Listener { } final Player player = event.getPlayer(); if (plugin.canUseQuests(player.getUniqueId())) { - final short durability = getBlockDurability(event.getBlock()); - final ItemStack blockItemStack = new ItemStack(event.getBlock().getType(), 1, durability); + final ItemStack blockItemStack = getItemEquivalent(event.getBlock()); + if (blockItemStack == null) { + return; + } final BukkitQuester quester = plugin.getQuester(player.getUniqueId()); final ObjectiveType placeType = ObjectiveType.PLACE_BLOCK; final ObjectiveType breakType = ObjectiveType.BREAK_BLOCK; @@ -337,8 +343,10 @@ public class BukkitBlockListener implements Listener { } if (event.getAction().equals(Action.RIGHT_CLICK_BLOCK)) { if (!event.isCancelled() && event.getClickedBlock() != null) { - final short durability = getBlockDurability(event.getClickedBlock()); - final ItemStack blockItemStack = new ItemStack(event.getClickedBlock().getType(), 1, durability); + final ItemStack blockItemStack = getItemEquivalent(event.getClickedBlock()); + if (blockItemStack == null) { + return; + } final ObjectiveType type = ObjectiveType.USE_BLOCK; final Set dispatchedQuestIDs = new HashSet<>(); for (final Quest quest : plugin.getLoadedQuests()) { @@ -366,19 +374,25 @@ public class BukkitBlockListener implements Listener { } /** - * Gets durability for item equivalent of Block, unless server is on certain builds of Paper - * server software, in which case returns 0 + * Gets item equivalent of Block, unless server is on certain builds of Paper + * server software, in which case may return null * - * @param block Block of item - * @return durability or 0 + * @param block Block to convert + * @return item or null */ @SuppressWarnings("deprecation") - private short getBlockDurability(final Block block) { + private ItemStack getItemEquivalent(final Block block) { + short durability = 0; try { - // Occurs with certain builds of Paper server software - return block.getState().getData().toItemStack().getDurability(); - } catch (final IllegalArgumentException ex) { - return 0; + durability = block.getState().getData().toItemStack().getDurability(); + } catch (final IllegalArgumentException e) { + // https://github.com/PikaMug/Quests/issues/2236 } + try { + return new ItemStack(block.getType(), 1, durability); + } catch (final IllegalArgumentException e) { + // https://github.com/PikaMug/Quests/issues/2243 + } + return null; } } diff --git a/core/src/main/java/me/pikamug/quests/util/BukkitLang.java b/core/src/main/java/me/pikamug/quests/util/BukkitLang.java index 834d11123..700d2e3fb 100644 --- a/core/src/main/java/me/pikamug/quests/util/BukkitLang.java +++ b/core/src/main/java/me/pikamug/quests/util/BukkitLang.java @@ -70,7 +70,7 @@ public class BukkitLang { String locale; try { locale = player.getLocale(); - } catch (NoSuchMethodError e) { + } catch (final NoSuchMethodError e) { locale = player.spigot().getLocale(); } final int separator = locale.indexOf("_"); @@ -86,7 +86,7 @@ public class BukkitLang { if (!otherLang.containsKey(locale)) { try { load(plugin, locale); - } catch (Exception e) { + } catch (final Exception e) { return defaultLang.containsKey(key) ? BukkitFormatToken.convertString(player, defaultLang.get(key)) : "NULL"; } }