Prevent players from fishing shop items

This commit is contained in:
Eric 2016-08-04 20:19:05 +02:00
parent ee90215dd6
commit ec62d3593f

View File

@ -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();
}
}
}
}
}
}