From ec62d3593f0dd5b8bf7c05c518e1834691c4584e Mon Sep 17 00:00:00 2001 From: Eric Date: Thu, 4 Aug 2016 20:19:05 +0200 Subject: [PATCH] Prevent players from fishing shop items --- .../listeners/ItemProtectListener.java | 35 ++++++++++++++++++- 1 file changed, 34 insertions(+), 1 deletion(-) diff --git a/ShopChest/src/main/java/de/epiceric/shopchest/listeners/ItemProtectListener.java b/ShopChest/src/main/java/de/epiceric/shopchest/listeners/ItemProtectListener.java index 2bdf8e3..26817ac 100644 --- a/ShopChest/src/main/java/de/epiceric/shopchest/listeners/ItemProtectListener.java +++ b/ShopChest/src/main/java/de/epiceric/shopchest/listeners/ItemProtectListener.java @@ -3,6 +3,7 @@ package de.epiceric.shopchest.listeners; import de.epiceric.shopchest.ShopChest; import de.epiceric.shopchest.utils.ShopUtils; +import de.epiceric.shopchest.utils.Utils; import org.bukkit.block.Block; import org.bukkit.block.BlockFace; import org.bukkit.block.BlockState; @@ -13,15 +14,19 @@ import org.bukkit.event.Listener; import org.bukkit.event.block.*; import org.bukkit.event.entity.ItemDespawnEvent; import org.bukkit.event.inventory.InventoryPickupItemEvent; -import org.bukkit.event.inventory.InventoryType; import org.bukkit.event.player.PlayerBucketEmptyEvent; +import org.bukkit.event.player.PlayerFishEvent; import org.bukkit.event.player.PlayerPickupItemEvent; +import java.lang.reflect.InvocationTargetException; + public class ItemProtectListener implements Listener { private ShopUtils shopUtils; + private ShopChest plugin; public ItemProtectListener(ShopChest plugin) { + this.plugin = plugin; this.shopUtils = plugin.getShopUtils(); } @@ -98,4 +103,32 @@ public class ItemProtectListener implements Listener { if (shopUtils.isShop(b.getLocation())) e.setCancelled(true); } + @EventHandler(priority = EventPriority.HIGH) + public void onPlayerFish(PlayerFishEvent e) { + if (e.getState() == PlayerFishEvent.State.CAUGHT_ENTITY) { + if (e.getCaught() instanceof Item) { + Item item = (Item) e.getCaught(); + if (item.hasMetadata("shopItem")) { + plugin.debug(e.getPlayer().getName() + " tried to fish a shop item"); + e.setCancelled(true); + + // Use some reflection to get the EntityFishingHook class so the hook can be removed... + try { + Class craftFishClass = Class.forName("org.bukkit.craftbukkit." + Utils.getServerVersion() + ".entity.CraftFish"); + Object craftFish = craftFishClass.cast(e.getHook()); + + Class entityFishingHookClass = Class.forName("net.minecraft.server." + Utils.getServerVersion() + ".EntityFishingHook"); + Object entityFishingHook = craftFishClass.getDeclaredMethod("getHandle").invoke(craftFish); + + entityFishingHookClass.getDeclaredMethod("die").invoke(entityFishingHook); + } catch (IllegalAccessException | InvocationTargetException | NoSuchMethodException | ClassNotFoundException ex) { + plugin.debug("Failed to kill fishing hook with reflection"); + plugin.debug(ex); + ex.printStackTrace(); + } + } + } + } + } + }