Skip to content

Commit

Permalink
Minor reorganization
Browse files Browse the repository at this point in the history
  • Loading branch information
cjburkey01 committed May 29, 2024
1 parent 2f6449e commit 627ffc4
Show file tree
Hide file tree
Showing 6 changed files with 102 additions and 85 deletions.
44 changes: 25 additions & 19 deletions src/main/java/com/cjburkey/claimchunk/gui/CCGuiHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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);
}
Expand All @@ -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) {
Expand Down
55 changes: 28 additions & 27 deletions src/main/java/com/cjburkey/claimchunk/gui/GuiMenuScreen.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,39 +35,37 @@ 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.
*
* <p>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.
* @param itemLore The lore attached to the item.
* @param action The on-click callback
*/
protected void addInteractiveButton(
@NotNull Inventory inventory,
int slot,
@NotNull Material itemType,
@NotNull String itemName,
@NotNull List<String> 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) {
Expand All @@ -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) {
Expand All @@ -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() {
Expand All @@ -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.
*
Expand Down Expand Up @@ -166,13 +154,26 @@ 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()
.guiChunkOwner
.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;
Expand Down
25 changes: 14 additions & 11 deletions src/main/java/com/cjburkey/claimchunk/gui/ICCGui.java
Original file line number Diff line number Diff line change
Expand Up @@ -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
* <p>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);
Expand All @@ -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();
}
35 changes: 18 additions & 17 deletions src/main/java/com/cjburkey/claimchunk/gui/screens/MainMenu.java
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -21,29 +21,27 @@ 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<String> lore = new ArrayList<>();

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);
}
Expand All @@ -54,21 +52,22 @@ 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
.getMainHandler()
.unclaimChunk(
false,
false,
player,
getPlayer(),
chunkPos.world(),
chunkPos.x(),
chunkPos.z());
Expand All @@ -77,20 +76,22 @@ 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,
Collections.singletonList(claimChunk.getMessages().guiMapDescription),
(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,
Expand Down
Loading

0 comments on commit 627ffc4

Please sign in to comment.