From 28ec3959786430c3afb204da15dcd59722a9cee8 Mon Sep 17 00:00:00 2001 From: zontreck Date: Sat, 5 Apr 2025 22:48:19 -0700 Subject: [PATCH] Begin to add a pseudo-gui system --- .../ase/commands/CommandRegistry.java | 5 + .../ase/commands/misc/CreditsCommand.java | 23 ++++ .../java/dev/zontreck/ase/guis/ChestGUI.java | 104 ++++++++++++++++++ .../dev/zontreck/ase/guis/CreditsGui.java | 19 ++++ .../dev/zontreck/ase/guis/PrimitiveItem.java | 77 +++++++++++++ 5 files changed, 228 insertions(+) create mode 100644 src/main/java/dev/zontreck/ase/commands/misc/CreditsCommand.java create mode 100644 src/main/java/dev/zontreck/ase/guis/ChestGUI.java create mode 100644 src/main/java/dev/zontreck/ase/guis/CreditsGui.java create mode 100644 src/main/java/dev/zontreck/ase/guis/PrimitiveItem.java diff --git a/src/main/java/dev/zontreck/ase/commands/CommandRegistry.java b/src/main/java/dev/zontreck/ase/commands/CommandRegistry.java index af43ef5..4eda980 100644 --- a/src/main/java/dev/zontreck/ase/commands/CommandRegistry.java +++ b/src/main/java/dev/zontreck/ase/commands/CommandRegistry.java @@ -6,6 +6,7 @@ import dev.zontreck.ase.commands.homes.DelHomeCommand; import dev.zontreck.ase.commands.homes.HomeCommand; import dev.zontreck.ase.commands.homes.HomesCommand; import dev.zontreck.ase.commands.homes.SetHomeCommand; +import dev.zontreck.ase.commands.misc.CreditsCommand; import dev.zontreck.ase.commands.misc.ShareItemCommand; import dev.zontreck.ase.commands.tpa.TPACancelCommand; import dev.zontreck.ase.commands.tpa.TPACommand; @@ -82,5 +83,9 @@ public class CommandRegistry { cmds.register(Commands.literal("shareitem") .requires(x -> x.getSender().isPermissionSet(ShareItemCommand.PERMISSION)) .executes(ctx -> ShareItemCommand.execute(ctx.getSource())).build()); + + cmds.register(Commands.literal("asecredits") + .requires(x -> x.getSender().isPermissionSet(CreditsCommand.PERMISSION)) + .executes(ctx -> CreditsCommand.execute(ctx.getSource())).build()); } } diff --git a/src/main/java/dev/zontreck/ase/commands/misc/CreditsCommand.java b/src/main/java/dev/zontreck/ase/commands/misc/CreditsCommand.java new file mode 100644 index 0000000..1a43711 --- /dev/null +++ b/src/main/java/dev/zontreck/ase/commands/misc/CreditsCommand.java @@ -0,0 +1,23 @@ +package dev.zontreck.ase.commands.misc; + +import org.bukkit.entity.Player; + +import com.mojang.brigadier.Command; + +import dev.zontreck.ase.guis.CreditsGui; +import io.papermc.paper.command.brigadier.CommandSourceStack; +import net.kyori.adventure.text.Component; +import net.kyori.adventure.text.format.NamedTextColor; + +public class CreditsCommand { + public static final String PERMISSION = "ase.commands.asecredits"; + + public static int execute(CommandSourceStack ctx) { + Player player = (Player) ctx.getSender(); + player.sendMessage(Component.text("Opening credits GUI...", NamedTextColor.DARK_GREEN)); + + CreditsGui.openGUI(player); + + return Command.SINGLE_SUCCESS; + } +} diff --git a/src/main/java/dev/zontreck/ase/guis/ChestGUI.java b/src/main/java/dev/zontreck/ase/guis/ChestGUI.java new file mode 100644 index 0000000..fe1f1be --- /dev/null +++ b/src/main/java/dev/zontreck/ase/guis/ChestGUI.java @@ -0,0 +1,104 @@ +package dev.zontreck.ase.guis; + +import java.util.List; + +import org.bukkit.Bukkit; +import org.bukkit.entity.Player; +import org.bukkit.inventory.Inventory; +import org.bukkit.inventory.InventoryView; +import org.bukkit.inventory.ItemStack; + +import net.kyori.adventure.text.Component; + +public class ChestGUI { + // START STATIC REGISTRY + public static final List GUI_LIST = new java.util.ArrayList<>(); + + public static void register(ChestGUI gui) { + GUI_LIST.add(gui); + } + + public static void checkValidity() { + for (int i = 0; i < GUI_LIST.size(); i++) { + ChestGUI gui = GUI_LIST.get(i); + if (!gui.valid()) { + GUI_LIST.remove(gui); + } + } + } + + public static ChestGUI getGUIForPlayer(Player player) { + for (ChestGUI gui : GUI_LIST) { + if (gui.player != null && gui.player.getUniqueId().equals(player.getUniqueId())) { + return gui; + } + } + return null; + } + // END STATIC REGISTRY + + public String guiTitle; + public List buttons; + public int totalSize; + public Player player; + public boolean closed = false; + + public ChestGUI(String title, int size) { + this.guiTitle = title; + this.totalSize = size; + } + + public void setPlayer(Player player) { + this.player = player; + } + + public void fill(int amount, PrimitiveItem item) { + for (int i = 0; i < amount; i++) { + buttons.add(item.generate()); + } + } + + /** + * This method presents the GUI to the player, using the defined layout above. + */ + public void doPresentation() { + Inventory inv = Bukkit.createInventory(null, totalSize, Component.text(guiTitle)); + + for (int i = 0; i < buttons.size(); i++) { + inv.setItem(i, buttons.get(i)); + } + + player.openInventory(inv); + register(this); + } + + public void close() { + // Assume the player is still in this GUI if it has not been invalidated. + if (valid()) { + player.closeInventory(); + closed = true; + } else { + closed = true; // Ensure this is set to true. + } + } + + public boolean valid() { + if (player == null) { + return false; + } + + if (closed) + return false; + + InventoryView inv = player.getOpenInventory(); + if (inv == null) { + return false; + } + + if (inv.title().toString().contains(guiTitle)) { + return true; + } else { + return false; + } + } +} diff --git a/src/main/java/dev/zontreck/ase/guis/CreditsGui.java b/src/main/java/dev/zontreck/ase/guis/CreditsGui.java new file mode 100644 index 0000000..3b4e10f --- /dev/null +++ b/src/main/java/dev/zontreck/ase/guis/CreditsGui.java @@ -0,0 +1,19 @@ +package dev.zontreck.ase.guis; + +import org.bukkit.Material; +import org.bukkit.entity.Player; + +import net.kyori.adventure.text.Component; + +public class CreditsGui { + public static void openGUI(Player player) { + // First step, construct the layout + ChestGUI gui = new ChestGUI("Credits", 9 * 4); + gui.fill(9, new PrimitiveItem(Material.BLACK_STAINED_GLASS_PANE, Component.text(""), true, + Component.text().build())); + + // Second step, present to the player + + gui.doPresentation(); + } +} diff --git a/src/main/java/dev/zontreck/ase/guis/PrimitiveItem.java b/src/main/java/dev/zontreck/ase/guis/PrimitiveItem.java new file mode 100644 index 0000000..3de9fd5 --- /dev/null +++ b/src/main/java/dev/zontreck/ase/guis/PrimitiveItem.java @@ -0,0 +1,77 @@ +package dev.zontreck.ase.guis; + +import java.util.ArrayList; +import java.util.List; + +import org.bukkit.Material; +import org.bukkit.NamespacedKey; +import org.bukkit.inventory.ItemStack; +import org.bukkit.inventory.meta.ItemMeta; +import org.bukkit.persistence.PersistentDataContainer; +import org.bukkit.persistence.PersistentDataType; + +import dev.zontreck.ase.AriasServerEssentials; +import net.kyori.adventure.text.Component; + +/** + * This is a primitive item. + */ +public class PrimitiveItem { + public static final String GUI_TAG = "ase_cgui"; + public Material itemType; + public Component name; + public Component[] itemLore; + public boolean isGUIButton; + + public PrimitiveItem(Material type, Component name, boolean isGUIButton, Component... lore) { + itemType = type; + this.name = name; + itemLore = lore; + this.isGUIButton = isGUIButton; + } + + public PrimitiveItem(Material type, Component name, boolean isGUIButton) { + itemType = type; + this.name = name; + itemLore = new Component[0]; + this.isGUIButton = isGUIButton; + } + + public ItemStack generate() { + ItemStack item = new ItemStack(itemType, 1); + + List lore = new ArrayList<>(); + for (Component component : itemLore) { + lore.add(component); + } + + ItemMeta meta = item.getItemMeta(); + + PersistentDataContainer data = meta.getPersistentDataContainer(); + + meta.lore(lore); + meta.customName(name); + + if (isGUIButton) { + data.set(NamespacedKey.fromString(GUI_TAG, AriasServerEssentials.getSelf()), PersistentDataType.BOOLEAN, + true); + } + + item.setItemMeta(meta); + return item; + } + + public static boolean isGUIButton(ItemStack item) { + if (item == null || item.getType() == Material.AIR) { + return false; + } + ItemMeta meta = item.getItemMeta(); + if (meta == null) { + return false; + } + PersistentDataContainer data = meta.getPersistentDataContainer(); + return data.has(NamespacedKey.fromString(GUI_TAG, AriasServerEssentials.getSelf()), + PersistentDataType.BOOLEAN); + } + +}