From dca9ec385f1f57fa55b9f7326b527bcf5516395d Mon Sep 17 00:00:00 2001 From: Pierre Maurice Schwang Date: Sat, 4 Jun 2022 16:30:33 +0200 Subject: [PATCH] chore/feat: cleanup gui logic, extendable inventories / guis --- .../bukkit/util/gui/BukkitPlotInventory.java | 110 ++++------------ .../util/gui/BukkitPlotInventoryListener.java | 87 +++++++++++++ .../util/gui/BukkitPlotInventoryProvider.java | 4 +- .../com/plotsquared/core/command/Music.java | 95 +------------- .../core/gui/PlotMusicInventory.java | 120 ++++++++++++++++++ .../util/gui/ExtendablePlotInventory.java | 49 +++++++ .../core/util/gui/PlotInventory.java | 36 +++--- .../util/gui/PlotInventoryClickHandler.java | 5 +- .../core/util/gui/PlotInventoryClickType.java | 12 ++ .../core/util/gui/PlotInventoryProvider.java | 3 +- 10 files changed, 325 insertions(+), 196 deletions(-) create mode 100644 Bukkit/src/main/java/com/plotsquared/bukkit/util/gui/BukkitPlotInventoryListener.java create mode 100644 Core/src/main/java/com/plotsquared/core/gui/PlotMusicInventory.java create mode 100644 Core/src/main/java/com/plotsquared/core/util/gui/ExtendablePlotInventory.java create mode 100644 Core/src/main/java/com/plotsquared/core/util/gui/PlotInventoryClickType.java diff --git a/Bukkit/src/main/java/com/plotsquared/bukkit/util/gui/BukkitPlotInventory.java b/Bukkit/src/main/java/com/plotsquared/bukkit/util/gui/BukkitPlotInventory.java index 471e3f41e..5b1406a81 100644 --- a/Bukkit/src/main/java/com/plotsquared/bukkit/util/gui/BukkitPlotInventory.java +++ b/Bukkit/src/main/java/com/plotsquared/bukkit/util/gui/BukkitPlotInventory.java @@ -41,9 +41,7 @@ import net.kyori.adventure.text.minimessage.tag.resolver.TagResolver; import org.bukkit.Bukkit; import org.bukkit.Material; import org.bukkit.entity.Player; -import org.bukkit.event.EventHandler; import org.bukkit.event.Listener; -import org.bukkit.event.inventory.InventoryCloseEvent; import org.bukkit.inventory.Inventory; import org.bukkit.inventory.ItemStack; import org.bukkit.inventory.meta.ItemMeta; @@ -61,85 +59,45 @@ import java.util.WeakHashMap; public class BukkitPlotInventory extends PlotInventory { private static Listener INVENTORY_LISTENER; - private final ItemStack[] items; - private final PlotInventoryClickHandler[] clickHandlers; - private final Map viewers = new WeakHashMap<>(); private static final Map INVENTORIES = new WeakHashMap<>(); + private final ItemStack[] items; + final PlotInventoryClickHandler[] clickHandlers; + Inventory nativeInventory; + /** * {@inheritDoc} */ - protected BukkitPlotInventory(final int size, final Caption titleCaption, final TagResolver... titleResolvers) { - super(size, titleCaption, titleResolvers); + protected BukkitPlotInventory( + PlotPlayer player, final int size, final Caption titleCaption, + final TagResolver... titleResolvers + ) { + super(player, size, titleCaption, titleResolvers); this.items = new ItemStack[size]; this.clickHandlers = new PlotInventoryClickHandler[size]; + this.nativeInventory = Bukkit.createInventory(player.getPlatformPlayer(), size(), + BukkitUtil.LEGACY_COMPONENT_SERIALIZER.serialize(MiniMessage.miniMessage().deserialize( + titleCaption().getComponent(player), titleResolvers() + )) + ); - if (INVENTORY_LISTENER != null) { - return; + if (INVENTORY_LISTENER == null) { + INVENTORY_LISTENER = new BukkitPlotInventoryListener(INVENTORIES); + BukkitPlatform bukkitPlatform = ((BukkitPlatform) PlotSquared.platform()); + bukkitPlatform.getServer().getPluginManager().registerEvents(INVENTORY_LISTENER, bukkitPlatform); } - INVENTORY_LISTENER = new Listener() { - @EventHandler - public void onInventoryClick(final org.bukkit.event.inventory.InventoryClickEvent event) { - final PlotPlayer player = BukkitUtil.adapt((Player) event.getWhoClicked()); - - BukkitPlotInventory currentInventory = INVENTORIES.get(player.getUUID()); - if (currentInventory == null) { - return; - } - if (!Objects.equals(event.getClickedInventory(), currentInventory.viewers.get(player.getUUID()))) { - return; - } - - final int slot = event.getRawSlot(); - if (slot < 0 || slot >= currentInventory.size()) { - return; - } - final PlotInventoryClickHandler clickHandler = currentInventory.clickHandlers[slot]; - if (clickHandler == null) { - return; - } - event.setCancelled(true); - final ItemStack item = event.getCurrentItem(); - if (item == null) { - clickHandler.handle(null, player); - return; - } - clickHandler.handle(new PlotItemStack( - BukkitAdapter.asItemType(item.getType()), - item.getAmount(), - item.getItemMeta().getDisplayName(), - item.getItemMeta().hasLore() ? item.getItemMeta().getLore().toArray(String[]::new) : new String[0] - ), player); - } - - @EventHandler - public void onInventoryClose(InventoryCloseEvent event) { - final PlotPlayer player = BukkitUtil.adapt((Player) event.getPlayer()); - BukkitPlotInventory currentInventory = INVENTORIES.get(player.getUUID()); - if (currentInventory == null) { - return; - } - currentInventory.viewers.remove(player.getUUID()); - INVENTORIES.remove(player.getUUID()); - } - - }; - BukkitPlatform bukkitPlatform = ((BukkitPlatform) PlotSquared.platform()); - bukkitPlatform.getServer().getPluginManager().registerEvents(INVENTORY_LISTENER, bukkitPlatform); } @Override - public void setItem(final int slot, final PlotItemStack item, final PlotInventoryClickHandler onClick) { + public void setItem(final int slot, final PlotItemStack item, final PlotInventoryClickHandler onClick) { Preconditions.checkElementIndex(slot, size(), "Slot must be in range (0, " + size() + ")"); this.items[slot] = toPlatformItem(item); this.clickHandlers[slot] = onClick; - for (final Inventory value : viewers.values()) { - value.setItem(slot, this.items[slot]); - } + this.nativeInventory.setItem(slot, this.items[slot]); } @Override - public void addItem(final PlotItemStack item, final PlotInventoryClickHandler onClick) { + public void addItem(final PlotItemStack item, final PlotInventoryClickHandler onClick) { // TODO: probably needs more love (who doesn't) int slot = -1; // try to fill stacks @@ -161,31 +119,19 @@ public class BukkitPlotInventory extends PlotInventory { Preconditions.checkElementIndex(slot, size()); this.items[slot] = toPlatformItem(item); this.clickHandlers[slot] = onClick; - for (final Inventory value : viewers.values()) { - value.setItem(slot, this.items[slot]); - } + this.nativeInventory.setItem(slot, this.items[slot]); } @Override - protected void openPlatformPlayer(final PlotPlayer player) { - Component title = MiniMessage.miniMessage().deserialize( - titleCaption().getComponent(player), titleResolvers() - ); - Inventory inventory = Bukkit.createInventory(player.getPlatformPlayer(), size(), - BukkitUtil.LEGACY_COMPONENT_SERIALIZER.serialize(title) - ); - for (int i = 0; i < items.length; i++) { - inventory.setItem(i, items[i]); - } - INVENTORIES.put(player.getUUID(), this); - viewers.put(player.getUUID(), inventory); - player.getPlatformPlayer().openInventory(inventory); + public void open() { + INVENTORIES.put(player().getUUID(), this); + player().getPlatformPlayer().openInventory(this.nativeInventory); } @Override - protected void closePlatformPlayer(final PlotPlayer player) { - if (player.getPlatformPlayer().getOpenInventory().getTopInventory().equals(INVENTORIES.get(player.getUUID()))) { - player.getPlatformPlayer().closeInventory(); + public void close() { + if (Objects.equals(player().getPlatformPlayer().getOpenInventory().getTopInventory(), this.nativeInventory)) { + player().getPlatformPlayer().closeInventory(); } } diff --git a/Bukkit/src/main/java/com/plotsquared/bukkit/util/gui/BukkitPlotInventoryListener.java b/Bukkit/src/main/java/com/plotsquared/bukkit/util/gui/BukkitPlotInventoryListener.java new file mode 100644 index 000000000..61e7f8af8 --- /dev/null +++ b/Bukkit/src/main/java/com/plotsquared/bukkit/util/gui/BukkitPlotInventoryListener.java @@ -0,0 +1,87 @@ +package com.plotsquared.bukkit.util.gui; + +import com.plotsquared.bukkit.util.BukkitUtil; +import com.plotsquared.core.player.PlotPlayer; +import com.plotsquared.core.plot.PlotItemStack; +import com.plotsquared.core.util.gui.PlotInventoryClickHandler; +import com.plotsquared.core.util.gui.PlotInventoryClickType; +import com.sk89q.worldedit.bukkit.BukkitAdapter; +import org.bukkit.entity.Player; +import org.bukkit.event.EventHandler; +import org.bukkit.event.Listener; +import org.bukkit.event.inventory.ClickType; +import org.bukkit.event.inventory.InventoryCloseEvent; +import org.bukkit.inventory.ItemStack; + +import java.util.EnumMap; +import java.util.Map; +import java.util.Objects; +import java.util.UUID; + +public class BukkitPlotInventoryListener implements Listener { + + private static final EnumMap CLICK_MAPPING = new EnumMap<>(ClickType.class); + + static { + CLICK_MAPPING.put(ClickType.LEFT, PlotInventoryClickType.LEFT); + CLICK_MAPPING.put(ClickType.RIGHT, PlotInventoryClickType.RIGHT); + CLICK_MAPPING.put(ClickType.SHIFT_LEFT, PlotInventoryClickType.SHIFT_LEFT); + CLICK_MAPPING.put(ClickType.SHIFT_RIGHT, PlotInventoryClickType.SHIFT_RIGHT); + CLICK_MAPPING.put(ClickType.MIDDLE, PlotInventoryClickType.MIDDLE); + } + + private final Map inventories; + + public BukkitPlotInventoryListener(final Map inventories) { + this.inventories = inventories; + } + + @EventHandler + public void onInventoryClick(final org.bukkit.event.inventory.InventoryClickEvent event) { + final PlotPlayer player = BukkitUtil.adapt((Player) event.getWhoClicked()); + + BukkitPlotInventory currentInventory = inventories.get(player.getUUID()); + if (currentInventory == null) { + return; + } + if (!Objects.equals(event.getClickedInventory(), currentInventory.nativeInventory)) { + return; + } + + final int slot = event.getRawSlot(); + if (slot < 0 || slot >= currentInventory.size()) { + return; + } + PlotInventoryClickType clickType = CLICK_MAPPING.getOrDefault(event.getClick(), PlotInventoryClickType.OTHER); + event.setCancelled(true); + + final PlotInventoryClickHandler clickHandler = currentInventory.clickHandlers[slot]; + if (clickHandler == null) { + return; + } + final ItemStack item = event.getCurrentItem(); + if (item == null) { + clickHandler.handle(null, clickType); + return; + } + clickHandler.handle(new PlotItemStack( + BukkitAdapter.asItemType(item.getType()), + item.getAmount(), + item.getItemMeta().getDisplayName(), + item.getItemMeta().hasLore() ? item.getItemMeta().getLore().toArray(String[]::new) : new String[0] + ), clickType); + } + + @EventHandler + public void onInventoryClose(InventoryCloseEvent event) { + final PlotPlayer player = BukkitUtil.adapt((Player) event.getPlayer()); + BukkitPlotInventory currentInventory = inventories.get(player.getUUID()); + if (currentInventory == null) { + return; + } + currentInventory.nativeInventory = null; + inventories.remove(player.getUUID()); + } + + +} diff --git a/Bukkit/src/main/java/com/plotsquared/bukkit/util/gui/BukkitPlotInventoryProvider.java b/Bukkit/src/main/java/com/plotsquared/bukkit/util/gui/BukkitPlotInventoryProvider.java index bfe712ad2..270f9a671 100644 --- a/Bukkit/src/main/java/com/plotsquared/bukkit/util/gui/BukkitPlotInventoryProvider.java +++ b/Bukkit/src/main/java/com/plotsquared/bukkit/util/gui/BukkitPlotInventoryProvider.java @@ -26,6 +26,7 @@ package com.plotsquared.bukkit.util.gui; import com.plotsquared.core.configuration.caption.Caption; +import com.plotsquared.core.player.PlotPlayer; import com.plotsquared.core.util.gui.PlotInventory; import com.plotsquared.core.util.gui.PlotInventoryProvider; import net.kyori.adventure.text.minimessage.tag.resolver.TagResolver; @@ -36,11 +37,12 @@ public class BukkitPlotInventoryProvider implements PlotInventoryProvider createInventory( + PlotPlayer player, final int size, final Caption titleCaption, final TagResolver... titleResolvers ) { - return new BukkitPlotInventory(size, titleCaption, titleResolvers); + return new BukkitPlotInventory((PlotPlayer) player, size, titleCaption, titleResolvers); } } diff --git a/Core/src/main/java/com/plotsquared/core/command/Music.java b/Core/src/main/java/com/plotsquared/core/command/Music.java index fb160705d..408921834 100644 --- a/Core/src/main/java/com/plotsquared/core/command/Music.java +++ b/Core/src/main/java/com/plotsquared/core/command/Music.java @@ -27,29 +27,18 @@ package com.plotsquared.core.command; import com.google.inject.Inject; import com.plotsquared.core.configuration.caption.TranslatableCaption; -import com.plotsquared.core.events.PlotFlagAddEvent; -import com.plotsquared.core.events.PlotFlagRemoveEvent; -import com.plotsquared.core.events.Result; +import com.plotsquared.core.gui.PlotMusicInventory; import com.plotsquared.core.location.Location; import com.plotsquared.core.permissions.Permission; import com.plotsquared.core.player.PlotPlayer; import com.plotsquared.core.plot.Plot; -import com.plotsquared.core.plot.PlotItemStack; -import com.plotsquared.core.plot.flag.PlotFlag; -import com.plotsquared.core.plot.flag.implementations.MusicFlag; import com.plotsquared.core.util.EventDispatcher; import com.plotsquared.core.util.Permissions; -import com.plotsquared.core.util.gui.PlotInventory; import com.plotsquared.core.util.gui.PlotInventoryProvider; -import com.sk89q.worldedit.world.item.ItemTypes; -import net.kyori.adventure.text.Component; import net.kyori.adventure.text.minimessage.tag.Tag; import net.kyori.adventure.text.minimessage.tag.resolver.TagResolver; import org.checkerframework.checker.nullness.qual.NonNull; -import java.util.List; -import java.util.stream.Stream; - @CommandDeclaration(command = "music", permission = "plots.music", usage = "/plot music", @@ -57,30 +46,13 @@ import java.util.stream.Stream; requiredType = RequiredType.PLAYER) public class Music extends SubCommand { - private static final List DISCS = Stream.of( - "music_disc_13", - "music_disc_cat", - "music_disc_blocks", - "music_disc_chirp", - "music_disc_far", - "music_disc_mall", - "music_disc_mellohi", - "music_disc_stal", - "music_disc_strad", - "music_disc_ward", - "music_disc_11", - "music_disc_wait", - "music_disc_otherside", - "music_disc_pigstep" - ).filter(s -> ItemTypes.get(s) != null).toList(); - private final EventDispatcher eventDispatcher; private final PlotInventoryProvider inventoryProvider; @Inject public Music( - final @NonNull EventDispatcher eventDispatcher, final - PlotInventoryProvider inventoryProvider + final @NonNull EventDispatcher eventDispatcher, + final PlotInventoryProvider inventoryProvider ) { this.eventDispatcher = eventDispatcher; this.inventoryProvider = inventoryProvider; @@ -110,66 +82,7 @@ public class Music extends SubCommand { return true; } - final PlotInventory inventory = inventoryProvider.createInventory( - 9 * 2, - TranslatableCaption.of("plotjukebox.jukebox_header") - ); - - for (final String disc : DISCS) { - PlotItemStack itemStack = new PlotItemStack( - disc, 1, String.format("%s", disc), - TranslatableCaption.of("plotjukebox.click_to_play").getComponent(player) - ); - inventory.addItem(itemStack, (item, clicker) -> { - clicker.closeInventory(); - PlotFlag plotFlag = plot.getFlagContainer().getFlag(MusicFlag.class) - .createFlagInstance(item.getType()); - PlotFlagAddEvent event = eventDispatcher.callFlagAdd(plotFlag, plot); - if (event.getEventResult() == Result.DENY) { - clicker.sendMessage( - TranslatableCaption.of("events.event_denied"), - TagResolver.resolver("value", Tag.inserting(Component.text("Music addition"))) - ); - return; - } - plot.setFlag(event.getFlag()); - clicker.sendMessage( - TranslatableCaption.of("flag.flag_added"), - TagResolver.builder() - .tag("flag", Tag.inserting(Component.text("music"))) - .tag("value", Tag.inserting(Component.text(event.getFlag().getValue().toString()))) - .build() - ); - }); - } - - PlotItemStack cancelItem = new PlotItemStack( - ItemTypes.BEDROCK, 1, - TranslatableCaption.of("plotjukebox.cancel_music").getComponent(player), - TranslatableCaption.of("plotjukebox.reset_music").getComponent(player) - ); - inventory.setItem(inventory.size() - 1, cancelItem, (item, clicker) -> { - clicker.closeInventory(); - PlotFlag plotFlag = plot.getFlagContainer().getFlag(MusicFlag.class) - .createFlagInstance(item.getType()); - PlotFlagRemoveEvent event = eventDispatcher.callFlagRemove(plotFlag, plot); - if (event.getEventResult() == Result.DENY) { - clicker.sendMessage( - TranslatableCaption.of("events.event_denied"), - TagResolver.resolver("value", Tag.inserting(Component.text("Music removal"))) - ); - return; - } - plot.removeFlag(event.getFlag()); - clicker.sendMessage( - TranslatableCaption.of("flag.flag_removed"), - TagResolver.builder() - .tag("flag", Tag.inserting(Component.text("music"))) - .tag("value", Tag.inserting(Component.text("music_disc"))) - .build() - ); - }); - inventory.open(player); + new PlotMusicInventory<>(inventoryProvider, player, plot, eventDispatcher).open(); return true; } diff --git a/Core/src/main/java/com/plotsquared/core/gui/PlotMusicInventory.java b/Core/src/main/java/com/plotsquared/core/gui/PlotMusicInventory.java new file mode 100644 index 000000000..a3b324f1a --- /dev/null +++ b/Core/src/main/java/com/plotsquared/core/gui/PlotMusicInventory.java @@ -0,0 +1,120 @@ +package com.plotsquared.core.gui; + +import com.plotsquared.core.configuration.caption.TranslatableCaption; +import com.plotsquared.core.events.PlotFlagAddEvent; +import com.plotsquared.core.events.PlotFlagRemoveEvent; +import com.plotsquared.core.events.Result; +import com.plotsquared.core.player.PlotPlayer; +import com.plotsquared.core.plot.Plot; +import com.plotsquared.core.plot.PlotItemStack; +import com.plotsquared.core.plot.flag.PlotFlag; +import com.plotsquared.core.plot.flag.implementations.MusicFlag; +import com.plotsquared.core.util.EventDispatcher; +import com.plotsquared.core.util.gui.ExtendablePlotInventory; +import com.plotsquared.core.util.gui.PlotInventoryProvider; +import com.sk89q.worldedit.world.item.ItemTypes; +import net.kyori.adventure.text.Component; +import net.kyori.adventure.text.minimessage.tag.Tag; +import net.kyori.adventure.text.minimessage.tag.resolver.TagResolver; + +import java.util.List; +import java.util.stream.Stream; + +public class PlotMusicInventory extends ExtendablePlotInventory { + + private static final List DISCS = Stream.of( + "music_disc_13", + "music_disc_cat", + "music_disc_blocks", + "music_disc_chirp", + "music_disc_far", + "music_disc_mall", + "music_disc_mellohi", + "music_disc_stal", + "music_disc_strad", + "music_disc_ward", + "music_disc_11", + "music_disc_wait", + "music_disc_otherside", + "music_disc_pigstep" + ).filter(s -> ItemTypes.get(s) != null).toList(); + private final Plot plot; + private final EventDispatcher eventDispatcher; + + public PlotMusicInventory( + final PlotInventoryProvider provider, + PlotPlayer player, + Plot plot, + EventDispatcher eventDispatcher + ) { + super( + provider, player, 2 * 9, + TranslatableCaption.of("plotjukebox.jukebox_header") + ); + this.plot = plot; + this.eventDispatcher = eventDispatcher; + setDiscs(); + setCancelButton(); + } + + private void setDiscs() { + for (final String disc : DISCS) { + PlotItemStack itemStack = new PlotItemStack( + disc, 1, String.format("%s", disc), + TranslatableCaption.of("plotjukebox.click_to_play").getComponent(player()) + ); + addItem(itemStack, (item, type) -> { + close(); + PlotFlag plotFlag = plot.getFlagContainer().getFlag(MusicFlag.class) + .createFlagInstance(item.getType()); + PlotFlagAddEvent event = eventDispatcher.callFlagAdd(plotFlag, plot); + if (event.getEventResult() == Result.DENY) { + player().sendMessage( + TranslatableCaption.of("events.event_denied"), + TagResolver.resolver("value", Tag.inserting(Component.text("Music addition"))) + ); + return; + } + plot.setFlag(event.getFlag()); + player().sendMessage( + TranslatableCaption.of("flag.flag_added"), + TagResolver.builder() + .tag("flag", Tag.inserting(Component.text("music"))) + .tag("value", Tag.inserting(Component.text(event.getFlag().getValue().toString()))) + .build() + ); + }); + } + } + + + private void setCancelButton() { + PlotItemStack cancelItem = new PlotItemStack( + ItemTypes.BEDROCK, 1, + TranslatableCaption.of("plotjukebox.cancel_music").getComponent(player()), + TranslatableCaption.of("plotjukebox.reset_music").getComponent(player()) + ); + setItem(size() - 1, cancelItem, (item, type) -> { + close(); + PlotFlag plotFlag = plot.getFlagContainer().getFlag(MusicFlag.class) + .createFlagInstance(item.getType()); + PlotFlagRemoveEvent event = eventDispatcher.callFlagRemove(plotFlag, plot); + if (event.getEventResult() == Result.DENY) { + player().sendMessage( + TranslatableCaption.of("events.event_denied"), + TagResolver.resolver("value", Tag.inserting(Component.text("Music removal"))) + ); + return; + } + plot.removeFlag(event.getFlag()); + player().sendMessage( + TranslatableCaption.of("flag.flag_removed"), + TagResolver.builder() + .tag("flag", Tag.inserting(Component.text("music"))) + .tag("value", Tag.inserting(Component.text("music_disc"))) + .build() + ); + }); + } + +} diff --git a/Core/src/main/java/com/plotsquared/core/util/gui/ExtendablePlotInventory.java b/Core/src/main/java/com/plotsquared/core/util/gui/ExtendablePlotInventory.java new file mode 100644 index 000000000..3a018cd38 --- /dev/null +++ b/Core/src/main/java/com/plotsquared/core/util/gui/ExtendablePlotInventory.java @@ -0,0 +1,49 @@ +package com.plotsquared.core.util.gui; + +import com.plotsquared.core.configuration.caption.Caption; +import com.plotsquared.core.player.PlotPlayer; +import com.plotsquared.core.plot.PlotItemStack; +import net.kyori.adventure.text.minimessage.tag.resolver.TagResolver; + +public class ExtendablePlotInventory extends PlotInventory { + + private final PlotInventory delegate; + + public ExtendablePlotInventory(PlotInventory delegate) { + super(delegate.player(), delegate.size(), delegate.titleCaption(), delegate.titleResolvers()); + this.delegate = delegate; + } + + public ExtendablePlotInventory( + PlotInventoryProvider provider, PlotPlayer player, int size, Caption title, + TagResolver... titleResolver + ) { + this(provider.createInventory(player, size, title, titleResolver)); + } + + @Override + public void setItem(final int slot, final PlotItemStack item, final PlotInventoryClickHandler onClick) { + delegate.setItem(slot, item, onClick); + } + + @Override + public void addItem(final PlotItemStack item, final PlotInventoryClickHandler onClick) { + delegate.addItem(item, onClick); + } + + @Override + public void open() { + delegate.open(); + } + + @Override + public void close() { + delegate.close(); + } + + @Override + public I toPlatformItem(final PlotItemStack item) { + return delegate.toPlatformItem(item); + } + +} diff --git a/Core/src/main/java/com/plotsquared/core/util/gui/PlotInventory.java b/Core/src/main/java/com/plotsquared/core/util/gui/PlotInventory.java index 7b73e6f6d..cacd55137 100644 --- a/Core/src/main/java/com/plotsquared/core/util/gui/PlotInventory.java +++ b/Core/src/main/java/com/plotsquared/core/util/gui/PlotInventory.java @@ -37,9 +37,10 @@ import net.kyori.adventure.text.minimessage.tag.resolver.TagResolver; */ public abstract class PlotInventory { - private final PlotInventoryClickHandler

NOOP_CLICK_HANDLER = (x, y) -> { + private final PlotInventoryClickHandler NOOP_CLICK_HANDLER = (x, y) -> { }; + private final PlotPlayer

player; private final int size; private final Caption titleCaption; private final TagResolver[] titleResolvers; @@ -52,7 +53,8 @@ public abstract class PlotInventory { * @param titleResolvers The tag resolvers to use for the title * @since 7.0.0 */ - protected PlotInventory(int size, Caption titleCaption, TagResolver... titleResolvers) { + protected PlotInventory(PlotPlayer

player, int size, Caption titleCaption, TagResolver... titleResolvers) { + this.player = player; this.size = size; this.titleCaption = titleCaption; this.titleResolvers = titleResolvers; @@ -67,7 +69,7 @@ public abstract class PlotInventory { * @param onClick The handler to call when clicking this item * @since TODO */ - public abstract void setItem(int slot, PlotItemStack item, PlotInventoryClickHandler

onClick); + public abstract void setItem(int slot, PlotItemStack item, PlotInventoryClickHandler onClick); /** * Set an item in this inventory at a specific slot / index. @@ -88,7 +90,7 @@ public abstract class PlotInventory { * @param onClick The handler to call when clicking this item * @since TODO */ - public abstract void addItem(PlotItemStack item, PlotInventoryClickHandler

onClick); + public abstract void addItem(PlotItemStack item, PlotInventoryClickHandler onClick); /** * Add an item to this inventory, at the first slot possible (first empty slot, or first slot with the exact same item) @@ -101,28 +103,18 @@ public abstract class PlotInventory { } /** - * Opens this inventory for a specific {@link PlotPlayer} + * Opens this inventory * - * @param player The player to open this inventory for * @since TODO */ - public void open(PlotPlayer player) { - this.openPlatformPlayer((PlotPlayer

) player); - } - - protected abstract void openPlatformPlayer(PlotPlayer

player); + public abstract void open(); /** - * Close this inventory for a specific {@link PlotPlayer} + * Close this inventory * - * @param player The player to close this inventory for * @since TODO */ - public void close(PlotPlayer player) { - this.closePlatformPlayer((PlotPlayer

) player); - } - - protected abstract void closePlatformPlayer(PlotPlayer

player); + public abstract void close(); public abstract I toPlatformItem(PlotItemStack item); @@ -134,6 +126,14 @@ public abstract class PlotInventory { return size; } + /** + * Get the associated player of this inventory + * @return {@link PlotPlayer} + */ + public PlotPlayer

player() { + return player; + } + protected Caption titleCaption() { return titleCaption; } diff --git a/Core/src/main/java/com/plotsquared/core/util/gui/PlotInventoryClickHandler.java b/Core/src/main/java/com/plotsquared/core/util/gui/PlotInventoryClickHandler.java index 0f08d24c0..425fac0ff 100644 --- a/Core/src/main/java/com/plotsquared/core/util/gui/PlotInventoryClickHandler.java +++ b/Core/src/main/java/com/plotsquared/core/util/gui/PlotInventoryClickHandler.java @@ -25,11 +25,10 @@ */ package com.plotsquared.core.util.gui; -import com.plotsquared.core.player.PlotPlayer; import com.plotsquared.core.plot.PlotItemStack; -public interface PlotInventoryClickHandler

{ +public interface PlotInventoryClickHandler { - void handle(PlotItemStack itemStack, PlotPlayer

plotPlayer); + void handle(PlotItemStack itemStack, PlotInventoryClickType clickType); } diff --git a/Core/src/main/java/com/plotsquared/core/util/gui/PlotInventoryClickType.java b/Core/src/main/java/com/plotsquared/core/util/gui/PlotInventoryClickType.java new file mode 100644 index 000000000..47f35c6e4 --- /dev/null +++ b/Core/src/main/java/com/plotsquared/core/util/gui/PlotInventoryClickType.java @@ -0,0 +1,12 @@ +package com.plotsquared.core.util.gui; + +public enum PlotInventoryClickType { + + LEFT, + SHIFT_LEFT, + RIGHT, + SHIFT_RIGHT, + MIDDLE, + OTHER + +} diff --git a/Core/src/main/java/com/plotsquared/core/util/gui/PlotInventoryProvider.java b/Core/src/main/java/com/plotsquared/core/util/gui/PlotInventoryProvider.java index ede28bf1a..94e9d0d8d 100644 --- a/Core/src/main/java/com/plotsquared/core/util/gui/PlotInventoryProvider.java +++ b/Core/src/main/java/com/plotsquared/core/util/gui/PlotInventoryProvider.java @@ -26,6 +26,7 @@ package com.plotsquared.core.util.gui; import com.plotsquared.core.configuration.caption.Caption; +import com.plotsquared.core.player.PlotPlayer; import net.kyori.adventure.text.minimessage.tag.resolver.TagResolver; /** @@ -44,6 +45,6 @@ public interface PlotInventoryProvider { * @param titleResolvers The (optional) placeholder resolvers for the inventory * @return The platform inventory */ - PlotInventory createInventory(int size, Caption titleCaption, TagResolver... titleResolvers); + PlotInventory createInventory(PlotPlayer player, int size, Caption titleCaption, TagResolver... titleResolvers); }