Skip to content

Commit

Permalink
Move EntityPickupItemEvent listener
Browse files Browse the repository at this point in the history
Should fix issue #98 and 1.8 compatibility
  • Loading branch information
Trophonix committed Dec 17, 2022
1 parent 9dba256 commit 41b711c
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 12 deletions.
Original file line number Diff line number Diff line change
@@ -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);
}
}

}
24 changes: 12 additions & 12 deletions src/main/java/com/trophonix/tradeplus/trade/Trade.java
Original file line number Diff line number Diff line change
Expand Up @@ -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<Integer> mySlots, theirSlots, myExtraSlots, theirExtraSlots;
private final List<Extra> extras = new ArrayList<>();
Expand All @@ -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;
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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) {
Expand Down

0 comments on commit 41b711c

Please sign in to comment.