From 41b711cb6c9726d367ee3181834c26b49ece4c8e Mon Sep 17 00:00:00 2001 From: Lucas Cosolo Date: Sat, 17 Dec 2022 11:53:58 -0600 Subject: [PATCH] Move EntityPickupItemEvent listener Should fix issue #98 and 1.8 compatibility --- .../trade/EntityPickupItemEventListener.java | 27 +++++++++++++++++++ .../com/trophonix/tradeplus/trade/Trade.java | 24 ++++++++--------- 2 files changed, 39 insertions(+), 12 deletions(-) create mode 100644 src/main/java/com/trophonix/tradeplus/trade/EntityPickupItemEventListener.java diff --git a/src/main/java/com/trophonix/tradeplus/trade/EntityPickupItemEventListener.java b/src/main/java/com/trophonix/tradeplus/trade/EntityPickupItemEventListener.java new file mode 100644 index 0000000..927dd4d --- /dev/null +++ b/src/main/java/com/trophonix/tradeplus/trade/EntityPickupItemEventListener.java @@ -0,0 +1,27 @@ +package com.trophonix.tradeplus.trade; + +import com.trophonix.tradeplus.TradePlus; +import org.bukkit.entity.Player; +import org.bukkit.event.EventHandler; +import org.bukkit.event.Listener; +import org.bukkit.event.entity.EntityPickupItemEvent; + +public class EntityPickupItemEventListener implements Listener { + + private final Trade trade; + + public EntityPickupItemEventListener(Trade trade) { + this.trade = trade; + } + + @EventHandler + public void onPickup(EntityPickupItemEvent event) { + if (trade.isCancelled()) return; + if (!(event.getEntity() instanceof Player)) return; + Player player = (Player) event.getEntity(); + if (player.equals(trade.getPlayer1()) || player.equals(trade.getPlayer2())) { + event.setCancelled(true); + } + } + +} diff --git a/src/main/java/com/trophonix/tradeplus/trade/Trade.java b/src/main/java/com/trophonix/tradeplus/trade/Trade.java index 6e40fa3..5ac9121 100644 --- a/src/main/java/com/trophonix/tradeplus/trade/Trade.java +++ b/src/main/java/com/trophonix/tradeplus/trade/Trade.java @@ -36,8 +36,7 @@ public class Trade implements Listener { - public final Player player1; - public final Player player2; + @Getter public final Player player1, player2; private final TradePlus pl = TradePlus.getPlugin(TradePlus.class); private List mySlots, theirSlots, myExtraSlots, theirExtraSlots; private final List extras = new ArrayList<>(); @@ -51,6 +50,7 @@ public class Trade implements Listener { private boolean forced = false; private BukkitTask task; @Getter private boolean cancelled; + private EntityPickupItemEventListener entityPickupListener; public Trade(Player p1, Player p2) { player1 = p1; @@ -136,6 +136,12 @@ public Trade(Player p1, Player p2) { .sync( () -> { Bukkit.getServer().getPluginManager().registerEvents(this, pl); + try { + Class.forName("org.bukkit.event.entity.EntityPickupItemEvent"); + entityPickupListener = new EntityPickupItemEventListener(this); + Bukkit.getServer().getPluginManager().registerEvents(entityPickupListener, pl); + } catch (ClassNotFoundException ignored) { + } this.mySlots = pl.getTradeConfig().getMySlots(); this.theirSlots = pl.getTradeConfig().getTheirSlots(); @@ -478,6 +484,8 @@ public void onClose(InventoryCloseEvent event) { && inv2.getViewers().isEmpty() && spectatorInv.getViewers().isEmpty()) { HandlerList.unregisterAll(this); + if (entityPickupListener != null) + HandlerList.unregisterAll(entityPickupListener); } }, 1L); @@ -506,6 +514,8 @@ public void onClose(InventoryCloseEvent event) { && inv2.getViewers().isEmpty() && spectatorInv.getViewers().isEmpty()) { HandlerList.unregisterAll(this); + if (entityPickupListener != null) + HandlerList.unregisterAll(entityPickupListener); } }, 1L); @@ -574,16 +584,6 @@ public void onDisable(PluginDisableEvent event) { } } - @EventHandler - public void onPickup(EntityPickupItemEvent event) { - if (cancelled) return; - if (!(event.getEntity() instanceof Player)) return; - Player player = (Player) event.getEntity(); - if (player.equals(player1) || player.equals(player2)) { - event.setCancelled(true); - } - } - @EventHandler public void onInteract(PlayerInteractAtEntityEvent event) { if (event.getPlayer() == player1 || event.getPlayer() == player2) {