Solved cancellation problem for /deposit GUI.

This commit is contained in:
Ka0rX 2022-10-13 08:53:50 +02:00
parent c573c0abd0
commit c4ccb593c6
3 changed files with 23 additions and 5 deletions

View File

@ -8,6 +8,10 @@ import org.bukkit.inventory.InventoryHolder;
public abstract class PluginInventory implements InventoryHolder { public abstract class PluginInventory implements InventoryHolder {
protected final Player player; protected final Player player;
protected final PlayerData playerData; protected final PlayerData playerData;
/**
* If all the clicks sould be cancelled for the inventory
*/
private boolean shouldCancel = true;
public PluginInventory(PlayerData playerData) { public PluginInventory(PlayerData playerData) {
this.playerData = playerData; this.playerData = playerData;
@ -19,6 +23,15 @@ public abstract class PluginInventory implements InventoryHolder {
this.playerData = player.getOpenInventory() != null && player.getOpenInventory().getTopInventory().getHolder() instanceof PluginInventory ? ((PluginInventory) player.getOpenInventory().getTopInventory().getHolder()).playerData : PlayerData.get(player); this.playerData = player.getOpenInventory() != null && player.getOpenInventory().getTopInventory().getHolder() instanceof PluginInventory ? ((PluginInventory) player.getOpenInventory().getTopInventory().getHolder()).playerData : PlayerData.get(player);
} }
public PluginInventory(Player player, boolean shouldCancel) {
this(player);
this.shouldCancel=shouldCancel;
}
public PluginInventory(PlayerData playerData, boolean shouldCancel) {
this(playerData);
this.shouldCancel=shouldCancel;
}
public PlayerData getPlayerData() { public PlayerData getPlayerData() {
return playerData; return playerData;
} }
@ -27,6 +40,10 @@ public abstract class PluginInventory implements InventoryHolder {
return player; return player;
} }
public boolean shouldCancel() {
return shouldCancel;
}
/** /**
* Opens classic inventory, throws * Opens classic inventory, throws
*/ */

View File

@ -30,7 +30,7 @@ public class DepositMenu extends PluginInventory {
private BukkitRunnable updateRunnable; private BukkitRunnable updateRunnable;
public DepositMenu(Player player) { public DepositMenu(Player player) {
super(player); super(player,false);
} }
@Override @Override

View File

@ -38,11 +38,12 @@ public class PlayerListener implements Listener {
// Register custom inventory clicks // Register custom inventory clicks
@EventHandler @EventHandler
public void b(InventoryClickEvent event) { public void b(InventoryClickEvent event) {
if (event.getInventory().getHolder() instanceof PluginInventory) { if (event.getInventory().getHolder() instanceof PluginInventory pluginInventory) {
if (pluginInventory.shouldCancel()) {
event.setCancelled(true); event.setCancelled(true);
}
if (event.getCurrentItem() != null && event.getCurrentItem().getItemMeta() != null) if (event.getCurrentItem() != null && event.getCurrentItem().getItemMeta() != null)
((PluginInventory) event.getInventory().getHolder()) pluginInventory.whenClicked(new InventoryClickContext(event.getRawSlot(), event.getCurrentItem(), event.getClick(), event, event.getInventory()));
.whenClicked(new InventoryClickContext(event.getRawSlot(), event.getCurrentItem(), event.getClick(), event, event.getInventory()));
} }
} }