From 627ffc4072c18538d16eccd5d90079ae39f80130 Mon Sep 17 00:00:00 2001 From: CJ Burkey Date: Tue, 28 May 2024 23:41:08 -0400 Subject: [PATCH] Minor reorganization --- .../cjburkey/claimchunk/gui/CCGuiHandler.java | 44 ++++++++------- .../claimchunk/gui/GuiMenuScreen.java | 55 ++++++++++--------- .../com/cjburkey/claimchunk/gui/ICCGui.java | 25 +++++---- .../claimchunk/gui/screens/MainMenu.java | 35 ++++++------ .../claimchunk/gui/screens/MapMenu.java | 26 +++++---- .../smartcommand/sub/ply/GuiCmd.java | 2 +- 6 files changed, 102 insertions(+), 85 deletions(-) diff --git a/src/main/java/com/cjburkey/claimchunk/gui/CCGuiHandler.java b/src/main/java/com/cjburkey/claimchunk/gui/CCGuiHandler.java index c7b813a..806f220 100644 --- a/src/main/java/com/cjburkey/claimchunk/gui/CCGuiHandler.java +++ b/src/main/java/com/cjburkey/claimchunk/gui/CCGuiHandler.java @@ -10,6 +10,7 @@ import org.bukkit.event.inventory.InventoryCloseEvent; import org.bukkit.inventory.Inventory; import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; import java.util.HashMap; import java.util.UUID; @@ -32,11 +33,7 @@ public void onGuiClick(InventoryClickEvent e) { if (e.getInventory().equals(openGui.inventory())) { openGui.gui() .onClick( - openGui.inventory(), - (Player) e.getWhoClicked(), - e.getSlot(), - e.getClick(), - e.getCurrentItem()); + openGui.inventory(), e.getSlot(), e.getClick(), e.getCurrentItem()); } e.setCancelled(true); } @@ -49,26 +46,35 @@ public void onGuiClose(InventoryCloseEvent e) { } } - public void openGui(@NotNull Player player, @NotNull ICCGui gui) { - if (openGuis.containsKey(player.getUniqueId())) { - closeGui(player); + public void openOrRefreshGui(@NotNull ICCGui gui) { + Player player = gui.getPlayer(); + CCOpenGui openGui = closeGui(player, false); + + final Inventory inventory; + if (openGui != null && openGui.gui() == gui) { + inventory = openGui.inventory(); + inventory.clear(); + } else { + inventory = createAndShowGui(player, gui); } - openGuis.put(player.getUniqueId(), new CCOpenGui(gui, createAndShowGui(player, gui))); - gui.onOpen(openGuis.get(player.getUniqueId()).inventory(), player); + + openGuis.put(player.getUniqueId(), new CCOpenGui(gui, inventory)); + gui.onOpen(openGuis.get(player.getUniqueId()).inventory()); } public void closeGui(@NotNull Player player) { - if (openGuis.containsKey(player.getUniqueId())) { - openGuis.get(player.getUniqueId()) - .gui() - .onClose(openGuis.get(player.getUniqueId()).inventory(), player); - openGuis.remove(player.getUniqueId()); - } + closeGui(player, true); } - public void refreshGui(@NotNull Player player, @NotNull ICCGui gui) { - closeGui(player); - openGui(player, gui); + private @Nullable CCOpenGui closeGui(@NotNull Player player, boolean removeFromMap) { + CCOpenGui openGui = openGuis.get(player.getUniqueId()); + if (openGui != null) { + openGui.gui().onClose(openGuis.get(player.getUniqueId()).inventory()); + if (removeFromMap) { + openGuis.remove(player.getUniqueId()); + } + } + return openGui; } private static Inventory createAndShowGui(@NotNull Player player, @NotNull ICCGui gui) { diff --git a/src/main/java/com/cjburkey/claimchunk/gui/GuiMenuScreen.java b/src/main/java/com/cjburkey/claimchunk/gui/GuiMenuScreen.java index a5d5dc5..0ececbd 100644 --- a/src/main/java/com/cjburkey/claimchunk/gui/GuiMenuScreen.java +++ b/src/main/java/com/cjburkey/claimchunk/gui/GuiMenuScreen.java @@ -35,25 +35,24 @@ public interface GuiItemAction { record GuiItemWrapper(@NotNull ItemStack stack, @NotNull GuiItemAction action) {} protected final ClaimChunk claimChunk; + private final Player player; private final int rowCount; - private final GuiItemWrapper[] actions; private final String name; + private final GuiItemWrapper[] actions; - protected @Nullable Inventory inventory; - protected @Nullable Player player; - - protected GuiMenuScreen(ClaimChunk claimChunk, int rowCount, @NotNull String name) { + protected GuiMenuScreen( + ClaimChunk claimChunk, @NotNull Player player, int rowCount, @NotNull String name) { this.claimChunk = claimChunk; + this.player = player; this.rowCount = Math.min(Math.max(rowCount, 1), 6); - this.actions = new GuiItemWrapper[this.rowCount * 9]; this.name = name; + this.actions = new GuiItemWrapper[this.rowCount * 9]; } /** * Add an interactive button item to this inventory GUI. * - *

Must be called in {@link this#onBuildGui()}, - * + * @param inventory This GUI's inventory. * @param slot The slot of the inventory in which to put the item. * @param itemType The material of the item. * @param itemName The display name of the item stack. @@ -61,13 +60,12 @@ protected GuiMenuScreen(ClaimChunk claimChunk, int rowCount, @NotNull String nam * @param action The on-click callback */ protected void addInteractiveButton( + @NotNull Inventory inventory, int slot, @NotNull Material itemType, @NotNull String itemName, @NotNull List itemLore, @NotNull GuiItemAction action) { - if (inventory == null) return; - Utils.debug("Made GUI stack in slot: " + slot); Utils.debug("Max: " + this.actions.length); if (slot >= 0 && slot < this.actions.length && itemType != Material.AIR) { @@ -90,29 +88,14 @@ protected void addInteractiveButton( return stack; } - @Override - public void onOpen(@NotNull Inventory inventory, @NotNull Player player) { - this.inventory = inventory; - this.player = player; - - onBuildGui(); - } - - /** - * Called when the GUI is opened so you don't have to override {@link this#onOpen(Inventory, - * Player)} - */ - protected abstract void onBuildGui(); - /** Method to reopen this gui (to update item names, etc.) */ protected void refresh() { - if (player != null) claimChunk.getGuiHandler().refreshGui(player, this); + claimChunk.getGuiHandler().openOrRefreshGui(this); } @Override public void onClick( @NotNull Inventory inventory, - @NotNull Player player, int slot, @NotNull ClickType clickType, @Nullable ItemStack stack) { @@ -125,7 +108,7 @@ public void onClick( } @Override - public void onClose(@NotNull Inventory inventory, @NotNull Player player) {} + public void onClose(@NotNull Inventory inventory) {} @Override public @NotNull String getName() { @@ -137,6 +120,11 @@ public int getRows() { return rowCount; } + @Override + public Player getPlayer() { + return player; + } + /** * Get the given item name's associated {@link Material} enum variant. * @@ -166,6 +154,13 @@ public int getRows() { .replaceAll(Pattern.quote("%%Z%%"), chunkPos.z() + ""); } + /** + * Generate localized chunk owner name GUI text. + * + * @param chunkName The non-null name of the chunk owner. This replaces {@code %%NAME%%} + * verbatim. + * @return Player-facing localized chunk owner text. + */ protected @NotNull String guiChunkOwnerNameText(@NotNull String chunkName) { return claimChunk .getMessages() @@ -173,6 +168,12 @@ public int getRows() { .replaceAll(Pattern.quote("%%NAME%%"), chunkName); } + /** + * Get the name for this given chunk owner, or return the localized unknown player text. + * + * @param chunkOwner The non-null owner of the chunk. + * @return The name for the chunk's owner that can be shown to a player in the GUI. + */ protected @NotNull String chunkNameOrUnknown(@NotNull UUID chunkOwner) { String chunkName = claimChunk.getPlayerHandler().getChunkName(chunkOwner); return chunkName != null ? chunkName : claimChunk.getMessages().unknownChunkOwner; diff --git a/src/main/java/com/cjburkey/claimchunk/gui/ICCGui.java b/src/main/java/com/cjburkey/claimchunk/gui/ICCGui.java index 344ca60..05858f7 100644 --- a/src/main/java/com/cjburkey/claimchunk/gui/ICCGui.java +++ b/src/main/java/com/cjburkey/claimchunk/gui/ICCGui.java @@ -15,33 +15,31 @@ public interface ICCGui { /** - * Called when this screen is shown to a player. + * Called when the GUI is being constructed to be shown to the player. * - * @param inventory The inventory behind this GUI - * @param player The player the GUI is shown to + *

Modify the inventory inside this method + * + * @param inventory The inventory being shown. */ - void onOpen(@NotNull Inventory inventory, @NotNull Player player); + void onOpen(@NotNull Inventory inventory); /** - * Called when this screen is being closed. + * Called when the GUI is closed; nothing usually needs to happen, but just in case, you know? * - * @param inventory The inventory behind this GUI - * @param player The player closing the GUI + * @param inventory The inventory being shown. */ - void onClose(@NotNull Inventory inventory, @NotNull Player player); + void onClose(@NotNull Inventory inventory); /** * Called when the player clicks on a given slot within the inventory. * - * @param inventory The inventory behind this GUI - * @param player The player clicking in the GUI being shown to them. + * @param inventory The inventory being shown. * @param slot The index of the slot being clicked. * @param clickType Which type of click the player performed. * @param stack The stack on which the player clicked. */ void onClick( @NotNull Inventory inventory, - @NotNull Player player, int slot, @NotNull ClickType clickType, @Nullable ItemStack stack); @@ -56,4 +54,9 @@ void onClick( * @return The number of rows this GUI should have */ int getRows(); + + /** + * @return The player this GUI is going to be shown to. + */ + Player getPlayer(); } diff --git a/src/main/java/com/cjburkey/claimchunk/gui/screens/MainMenu.java b/src/main/java/com/cjburkey/claimchunk/gui/screens/MainMenu.java index 58a9e6f..fa8b15e 100644 --- a/src/main/java/com/cjburkey/claimchunk/gui/screens/MainMenu.java +++ b/src/main/java/com/cjburkey/claimchunk/gui/screens/MainMenu.java @@ -5,11 +5,11 @@ import com.cjburkey.claimchunk.gui.GuiMenuScreen; import org.bukkit.entity.Player; +import org.bukkit.inventory.Inventory; import org.jetbrains.annotations.NotNull; import java.util.ArrayList; import java.util.Collections; -import java.util.Objects; import java.util.UUID; public class MainMenu extends GuiMenuScreen { @@ -21,21 +21,19 @@ public class MainMenu extends GuiMenuScreen { // 4: Chunk map // 6: Chunk permissions - public MainMenu(ClaimChunk claimChunk) { - super(claimChunk, 1, claimChunk.getMessages().guiMainMenuTitle); + public MainMenu(ClaimChunk claimChunk, Player player) { + super(claimChunk, player, 1, claimChunk.getMessages().guiMainMenuTitle); } @Override - protected void onBuildGui() { - if (player == null) return; - - addCurrentChunkItem(player); - addMapItem(); - addPermsItem(); + public void onOpen(@NotNull Inventory inventory) { + addCurrentChunkItem(inventory); + addMapItem(inventory); + addPermsItem(inventory); } - private void addCurrentChunkItem(@NotNull Player player) { - ChunkPos chunkPos = new ChunkPos(player.getLocation().getChunk()); + private void addCurrentChunkItem(@NotNull Inventory inventory) { + ChunkPos chunkPos = new ChunkPos(getPlayer().getLocation().getChunk()); UUID chunkOwner = claimChunk.getChunkHandler().getOwner(chunkPos); ArrayList lore = new ArrayList<>(); @@ -43,7 +41,7 @@ private void addCurrentChunkItem(@NotNull Player player) { lore.add(guiChunkPosText(chunkPos)); if (chunkOwner != null) { lore.add(guiChunkOwnerNameText(chunkNameOrUnknown(chunkOwner))); - if (chunkOwner.equals(player.getUniqueId())) { + if (chunkOwner.equals(getPlayer().getUniqueId())) { lore.add(""); lore.add(claimChunk.getMessages().guiClickToUnclaim); } @@ -54,13 +52,14 @@ private void addCurrentChunkItem(@NotNull Player player) { } addInteractiveButton( + inventory, 2, materialFromStr(claimChunk.getConfigHandler().getGuiMainMenuCurrentChunkItem()), claimChunk.getMessages().guiMainMenuCurrentChunkItemName, lore, (clickType, stack) -> { if (clickType.isLeftClick()) { - claimChunk.getMainHandler().claimChunk(player, chunkPos); + claimChunk.getMainHandler().claimChunk(getPlayer(), chunkPos); refresh(); } else if (clickType.isRightClick()) { claimChunk @@ -68,7 +67,7 @@ private void addCurrentChunkItem(@NotNull Player player) { .unclaimChunk( false, false, - player, + getPlayer(), chunkPos.world(), chunkPos.x(), chunkPos.z()); @@ -77,8 +76,9 @@ private void addCurrentChunkItem(@NotNull Player player) { }); } - private void addMapItem() { + private void addMapItem(@NotNull Inventory inventory) { addInteractiveButton( + inventory, 4, materialFromStr(claimChunk.getConfigHandler().getGuiMainMenuChunkMapItem()), claimChunk.getMessages().guiMainMenuMapItemName, @@ -86,11 +86,12 @@ private void addMapItem() { (clickType, stack) -> claimChunk .getGuiHandler() - .openGui(Objects.requireNonNull(player), new MapMenu(claimChunk))); + .openOrRefreshGui(new MapMenu(claimChunk, getPlayer()))); } - private void addPermsItem() { + private void addPermsItem(@NotNull Inventory inventory) { addInteractiveButton( + inventory, 6, materialFromStr(claimChunk.getConfigHandler().getGuiMainMenuPermFlagsItem()), claimChunk.getMessages().guiMainMenuPermFlagsItemName, diff --git a/src/main/java/com/cjburkey/claimchunk/gui/screens/MapMenu.java b/src/main/java/com/cjburkey/claimchunk/gui/screens/MapMenu.java index 7ab7d71..04c9f0d 100644 --- a/src/main/java/com/cjburkey/claimchunk/gui/screens/MapMenu.java +++ b/src/main/java/com/cjburkey/claimchunk/gui/screens/MapMenu.java @@ -7,6 +7,10 @@ import com.cjburkey.claimchunk.gui.GuiMenuScreen; import com.cjburkey.claimchunk.i18n.V2JsonMessages; +import org.bukkit.entity.Player; +import org.bukkit.inventory.Inventory; +import org.jetbrains.annotations.NotNull; + import java.util.ArrayList; import java.util.Collections; import java.util.UUID; @@ -15,31 +19,32 @@ public class MapMenu extends GuiMenuScreen { private static final int MAP_HEIGHT = 5; - public MapMenu(ClaimChunk claimChunk) { + public MapMenu(@NotNull ClaimChunk claimChunk, @NotNull Player player) { // Offset to make it 6 tall so that we can have the top row be a menu bar. - super(claimChunk, MAP_HEIGHT + 1, claimChunk.getMessages().guiMapMenuTitle); + super(claimChunk, player, MAP_HEIGHT + 1, claimChunk.getMessages().guiMainMenuTitle); } @Override - protected void onBuildGui() { - if (player == null) return; - + public void onOpen(@NotNull Inventory inventory) { int halfHeight = Math.floorDiv(MAP_HEIGHT, 2); ChunkHandler chunkHandler = claimChunk.getChunkHandler(); V2JsonMessages messages = claimChunk.getMessages(); ClaimChunkConfig config = claimChunk.getConfigHandler(); - ChunkPos centerChunk = new ChunkPos(player.getLocation().getChunk()); + ChunkPos centerChunk = new ChunkPos(getPlayer().getLocation().getChunk()); boolean enableClaimingFromMap = config.getGuiMapMenuAllowClaimOtherChunks(); // Add the back button addInteractiveButton( + inventory, 0, materialFromStr(config.getGuiMenuBackButtonItem()), messages.guiMenuBackButtonName, Collections.singletonList(messages.guiMenuBackButtonDesc), (clickType, stack) -> - claimChunk.getGuiHandler().openGui(player, new MainMenu(claimChunk))); + claimChunk + .getGuiHandler() + .openOrRefreshGui(new MainMenu(claimChunk, getPlayer()))); // Add the map items // Inventory width is 9, so go 4 eastward and westward @@ -57,7 +62,7 @@ protected void onBuildGui() { ArrayList lore = new ArrayList<>(); UUID chunkOwner = chunkHandler.getOwner(offsetChunk); - boolean isOwner = player.getUniqueId().equals(chunkOwner); + boolean isOwner = getPlayer().getUniqueId().equals(chunkOwner); if (isCenter) lore.add(messages.guiMapMenuInsideThisChunk); @@ -93,13 +98,14 @@ protected void onBuildGui() { } addInteractiveButton( + inventory, slot, materialFromStr(mapItemMaterialStr), guiChunkPosText(offsetChunk), lore, (clickType, stack) -> { if (clickType.isLeftClick()) { - claimChunk.getMainHandler().claimChunk(player, offsetChunk); + claimChunk.getMainHandler().claimChunk(getPlayer(), offsetChunk); refresh(); } else if (clickType.isRightClick()) { claimChunk @@ -107,7 +113,7 @@ protected void onBuildGui() { .unclaimChunk( false, false, - player, + getPlayer(), offsetChunk.world(), offsetChunk.x(), offsetChunk.z()); diff --git a/src/main/java/com/cjburkey/claimchunk/smartcommand/sub/ply/GuiCmd.java b/src/main/java/com/cjburkey/claimchunk/smartcommand/sub/ply/GuiCmd.java index 05aebad..598f6b9 100644 --- a/src/main/java/com/cjburkey/claimchunk/smartcommand/sub/ply/GuiCmd.java +++ b/src/main/java/com/cjburkey/claimchunk/smartcommand/sub/ply/GuiCmd.java @@ -37,7 +37,7 @@ public int getRequiredArguments() { @Override public boolean onCall(@NotNull String cmdUsed, @NotNull CommandSender executor, String[] args) { - claimChunk.getGuiHandler().openGui((Player) executor, new MainMenu(claimChunk)); + claimChunk.getGuiHandler().openOrRefreshGui(new MainMenu(claimChunk, (Player) executor)); return true; } }