Cleaned up plugin inv listener

This commit is contained in:
Indyuce 2022-11-10 16:49:55 +01:00
parent c8977a0ec9
commit cbe1a06947
2 changed files with 19 additions and 11 deletions

View File

@ -4,26 +4,31 @@ import org.bukkit.event.Cancellable;
import org.bukkit.event.inventory.ClickType;
import org.bukkit.inventory.Inventory;
import org.bukkit.inventory.ItemStack;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
public class InventoryClickContext {
private final int slot;
private final ItemStack itemStack;
@Nullable
private final ItemStack currentItem;
@NotNull
private final ClickType clickType;
@NotNull
private final Cancellable event;
private Inventory inv;
public InventoryClickContext(int slot, ItemStack itemStack, ClickType clickType, Cancellable event) {
this.slot = slot;
this.itemStack = itemStack;
this.clickType = clickType;
this.event = event;
@Nullable
private final Inventory inv;
public InventoryClickContext(int slot, ItemStack currentItem, ClickType clickType, Cancellable event) {
this(slot, currentItem, clickType, event, null);
}
public InventoryClickContext(int slot, ItemStack itemStack, ClickType clickType, Cancellable event, Inventory inv) {
public InventoryClickContext(int slot, ItemStack currentItem, ClickType clickType, Cancellable event, Inventory inv) {
this.slot = slot;
this.itemStack = itemStack;
this.currentItem = currentItem;
this.clickType = clickType;
this.event = event;
this.inv = inv;
@ -48,13 +53,15 @@ public class InventoryClickContext {
*/
@Nullable
public ItemStack getClickedItem() {
return itemStack;
return currentItem;
}
@NotNull
public Cancellable getEvent() {
return event;
}
@Nullable
public Inventory getInventory() {
return inv;
}
@ -63,6 +70,7 @@ public class InventoryClickContext {
return inv != null;
}
@NotNull
public ClickType getClickType() {
return clickType;
}

View File

@ -39,7 +39,7 @@ public class PlayerListener implements Listener {
*/
@EventHandler
public void b(InventoryClickEvent event) {
if (event.getInventory().getHolder() instanceof PluginInventory&&event.getCurrentItem()!=null)
if (event.getInventory().getHolder() instanceof PluginInventory)
((PluginInventory) event.getInventory().getHolder())
.whenClicked(new InventoryClickContext(event.getRawSlot(), event.getCurrentItem(), event.getClick(), event, event.getInventory()));
}