Begin to add a pseudo-gui system

This commit is contained in:
zontreck 2025-04-05 22:48:19 -07:00
parent 875e557fad
commit 28ec395978
5 changed files with 228 additions and 0 deletions

View file

@ -6,6 +6,7 @@ import dev.zontreck.ase.commands.homes.DelHomeCommand;
import dev.zontreck.ase.commands.homes.HomeCommand; import dev.zontreck.ase.commands.homes.HomeCommand;
import dev.zontreck.ase.commands.homes.HomesCommand; import dev.zontreck.ase.commands.homes.HomesCommand;
import dev.zontreck.ase.commands.homes.SetHomeCommand; 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.misc.ShareItemCommand;
import dev.zontreck.ase.commands.tpa.TPACancelCommand; import dev.zontreck.ase.commands.tpa.TPACancelCommand;
import dev.zontreck.ase.commands.tpa.TPACommand; import dev.zontreck.ase.commands.tpa.TPACommand;
@ -82,5 +83,9 @@ public class CommandRegistry {
cmds.register(Commands.literal("shareitem") cmds.register(Commands.literal("shareitem")
.requires(x -> x.getSender().isPermissionSet(ShareItemCommand.PERMISSION)) .requires(x -> x.getSender().isPermissionSet(ShareItemCommand.PERMISSION))
.executes(ctx -> ShareItemCommand.execute(ctx.getSource())).build()); .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());
} }
} }

View file

@ -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;
}
}

View file

@ -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<ChestGUI> 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<ItemStack> 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;
}
}
}

View file

@ -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();
}
}

View file

@ -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<Component> 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);
}
}