From 75b1ab8e0aaf06c721009dbed801bbd560f76334 Mon Sep 17 00:00:00 2001 From: montlikadani Date: Mon, 20 May 2019 16:38:16 +0200 Subject: [PATCH] Fixed NPE, when break a block and checks air in hand instead of item, #427 - Lets try to ignore recently updated deprecated methods --- .../jobs/config/GeneralConfigManager.java | 4 +-- .../jobs/listeners/JobsListener.java | 18 ++++++++-- .../jobs/listeners/JobsPaymentListener.java | 34 ++++++++++--------- 3 files changed, 35 insertions(+), 21 deletions(-) diff --git a/src/main/java/com/gamingmesh/jobs/config/GeneralConfigManager.java b/src/main/java/com/gamingmesh/jobs/config/GeneralConfigManager.java index 5ea98c42..0f3572f8 100644 --- a/src/main/java/com/gamingmesh/jobs/config/GeneralConfigManager.java +++ b/src/main/java/com/gamingmesh/jobs/config/GeneralConfigManager.java @@ -531,9 +531,9 @@ public class GeneralConfigManager { c.addComment("add-xp-player", "Adds the Jobs xp received to the player's Minecraft XP bar"); addXpPlayer = c.get("add-xp-player", false); - c.addComment("enable-pay-when-item-durability-loss", "Allows, when losing maximum durability of item then it does not pay the player until it is repaired.", + c.addComment("allow-pay-for-durability-loss", "Allows, when losing maximum durability of item then it does not pay the player until it is repaired.", "E.g. the player wants to enchant a item with enchanting table and the item has durability loss then not paying."); - payItemDurabilityLoss = c.get("enable-pay-when-item-durability-loss", true); + payItemDurabilityLoss = c.get("allow-pay-for-durability-loss", true); c.addComment("modify-chat", "Modifys chat to add chat titles. If you're using a chat manager, you may add the tag {jobs} to your chat format and disable this."); modifyChat = c.get("modify-chat.use", false); diff --git a/src/main/java/com/gamingmesh/jobs/listeners/JobsListener.java b/src/main/java/com/gamingmesh/jobs/listeners/JobsListener.java index 6fdf942e..378a7f97 100644 --- a/src/main/java/com/gamingmesh/jobs/listeners/JobsListener.java +++ b/src/main/java/com/gamingmesh/jobs/listeners/JobsListener.java @@ -72,6 +72,7 @@ import org.bukkit.plugin.PluginManager; import com.gamingmesh.jobs.Jobs; import com.gamingmesh.jobs.CMILib.ItemManager.CMIMaterial; +import com.gamingmesh.jobs.CMILib.VersionChecker.Version; import com.gamingmesh.jobs.Gui.GuiInfoList; import com.gamingmesh.jobs.api.JobsAreaSelectionEvent; import com.gamingmesh.jobs.api.JobsChunkChangeEvent; @@ -878,6 +879,7 @@ public class JobsListener implements Listener { ArmorTypes type = ArmorTypes.matchType(item); if (ArmorTypes.matchType(item) == null) return; + Location loc = event.getBlock().getLocation(); for (Player p : loc.getWorld().getPlayers()) { Location ploc = p.getLocation(); @@ -890,9 +892,19 @@ public class JobsListener implements Listener { if (!(event.getBlock().getState() instanceof Dispenser)) continue; - Dispenser dispenser = (Dispenser) event.getBlock().getState(); - org.bukkit.material.Dispenser dis = (org.bukkit.material.Dispenser) dispenser.getData(); - BlockFace directionFacing = dis.getFacing(); + + Dispenser dispenser = null; + BlockFace directionFacing = null; + if (Version.isCurrentEqualOrLower(Version.v1_13_R2)) { + dispenser = (Dispenser) event.getBlock().getState(); + org.bukkit.material.Dispenser dis = (org.bukkit.material.Dispenser) dispenser.getData(); + directionFacing = dis.getFacing(); + } else { + dispenser = (Dispenser) event.getBlock().getState(); + org.bukkit.block.data.type.Dispenser dis = (org.bukkit.block.data.type.Dispenser) dispenser.getBlockData(); + directionFacing = dis.getFacing(); + } + if (directionFacing == BlockFace.EAST && ploc.getBlockX() != loc.getBlockX() && ploc.getX() <= loc.getX() + 2.3 && diff --git a/src/main/java/com/gamingmesh/jobs/listeners/JobsPaymentListener.java b/src/main/java/com/gamingmesh/jobs/listeners/JobsPaymentListener.java index abb36431..ceec541b 100644 --- a/src/main/java/com/gamingmesh/jobs/listeners/JobsPaymentListener.java +++ b/src/main/java/com/gamingmesh/jobs/listeners/JobsPaymentListener.java @@ -399,18 +399,19 @@ public class JobsPaymentListener implements Listener { // Item in hand ItemStack item = Jobs.getNms().getItemInMainHand(player); + if (item != null && !item.getType().equals(Material.AIR)) { + // Prevent item durability loss + if (!Jobs.getGCManager().payItemDurabilityLoss && item.getType().getMaxDurability() + - Jobs.getNms().getDurability(item) != item.getType().getMaxDurability()) + return; - // Prevent item durability loss - if (!Jobs.getGCManager().payItemDurabilityLoss && item.getType().getMaxDurability() - - Jobs.getNms().getDurability(item) != item.getType().getMaxDurability()) - return; - - // Protection for block break with silktouch - if (Jobs.getGCManager().useSilkTouchProtection && item != null) { - for (Entry one : item.getEnchantments().entrySet()) { - if (one.getKey().getName().equalsIgnoreCase("SILK_TOUCH")) { - if (Jobs.getBpManager().isInBp(block)) - return; + // Protection for block break with silktouch + if (Jobs.getGCManager().useSilkTouchProtection) { + for (Entry one : item.getEnchantments().entrySet()) { + if (one.getKey().getName().equalsIgnoreCase("SILK_TOUCH")) { + if (Jobs.getBpManager().isInBp(block)) + return; + } } } } @@ -1115,11 +1116,12 @@ public class JobsPaymentListener implements Listener { return; ItemStack item = Jobs.getNms().getItemInMainHand(pDamager); - - // Prevent item durability loss - if (!Jobs.getGCManager().payItemDurabilityLoss && item.getType().getMaxDurability() - - Jobs.getNms().getDurability(item) != item.getType().getMaxDurability()) - return; + if (item != null && !item.getType().equals(Material.AIR)) { + // Prevent item durability loss + if (!Jobs.getGCManager().payItemDurabilityLoss && item.getType().getMaxDurability() + - Jobs.getNms().getDurability(item) != item.getType().getMaxDurability()) + return; + } // pay JobsPlayer jDamager = Jobs.getPlayerManager().getJobsPlayer(pDamager);