diff --git a/src/main/java/fun/ogre/ogredupealias/events/InventoryActionListener.java b/src/main/java/fun/ogre/ogredupealias/events/InventoryActionListener.java index 18e0f09..eaae646 100644 --- a/src/main/java/fun/ogre/ogredupealias/events/InventoryActionListener.java +++ b/src/main/java/fun/ogre/ogredupealias/events/InventoryActionListener.java @@ -1,6 +1,7 @@ package fun.ogre.ogredupealias.events; import fun.ogre.ogredupealias.plugin.custom.forging.CustomTable; +import fun.ogre.ogredupealias.plugin.custom.gui.CustomGui; import fun.ogre.ogredupealias.utils.Text; import org.bukkit.event.EventHandler; import org.bukkit.event.Listener; @@ -14,6 +15,7 @@ public class InventoryActionListener implements Listener { public void onClick(InventoryClickEvent e) { try { this.handleForgeClick(e); + CustomGui.handleRegistriesClick(e); } catch (Exception ignore) {} } @@ -22,6 +24,7 @@ public class InventoryActionListener implements Listener { public void onClose(InventoryCloseEvent e) { try { this.handleForgeClose(e); + CustomGui.handleRegistriesClose(e); } catch (Exception ignore) {} } diff --git a/src/main/java/fun/ogre/ogredupealias/plugin/custom/gui/CustomGui.java b/src/main/java/fun/ogre/ogredupealias/plugin/custom/gui/CustomGui.java new file mode 100644 index 0000000..77370d0 --- /dev/null +++ b/src/main/java/fun/ogre/ogredupealias/plugin/custom/gui/CustomGui.java @@ -0,0 +1,170 @@ +package fun.ogre.ogredupealias.plugin.custom.gui; + +import org.bukkit.Bukkit; +import org.bukkit.event.inventory.InventoryClickEvent; +import org.bukkit.event.inventory.InventoryCloseEvent; +import org.bukkit.inventory.Inventory; +import org.bukkit.inventory.ItemStack; + +import java.util.Comparator; +import java.util.HashMap; +import java.util.Map; + +public class CustomGui { + + private static final Map registry = new HashMap<>(); + private final Map slotActions; + private final Map slotDisplays; + private final InvAction mainAction; + private final CreateAction createAction; + private final CloseAction closeAction; + private final String title; + + public CustomGui(String title, InvAction mainAction, Map slotActions, Map slotDisplays, CreateAction createAction, CloseAction closeAction) { + this.slotActions = slotActions; + this.slotDisplays = slotDisplays; + this.mainAction = mainAction; + this.createAction = createAction; + this.closeAction = closeAction; + this.title = title; + } + + public static CustomGui register(CustomGui gui) { + if (gui != null) { + registry.put(gui.getTitle(), gui); + } + return gui; + } + + public static void handleRegistriesClick(InventoryClickEvent event) { + String title = event.getView().getTitle(); + if (registry.containsKey(title)) { + registry.get(title).onInventoryClick(event); + } + } + + public static void handleRegistriesClose(InventoryCloseEvent event) { + String title = event.getView().getTitle(); + if (registry.containsKey(title)) { + registry.get(title).onInventoryClose(event); + } + } + + public Inventory getInventory() { + int max = slotActions.keySet().stream().sorted(Comparator.comparing(i -> (int)i).reversed()).toList().get(0); + int add = max % 9 == 0 ? 0 : 1; + int value = (int)(Math.floor(max / 9.0) + add) * 9; + + Inventory inv = Bukkit.createInventory(null, value, title); + slotDisplays.forEach(inv::setItem); + + return inv; + } + + public void onInventoryClick(InventoryClickEvent event) { + mainAction.onClick(event); + if (slotActions.containsKey(event.getSlot())) { + slotActions.get(event.getSlot()).onClick(event); + } + } + + public void onInventoryClose(InventoryCloseEvent event) { + closeAction.onClose(event); + } + + public String getTitle() { + return title; + } + + public Map getSlotActions() { + return slotActions; + } + + public Map getSlotDisplays() { + return slotDisplays; + } + + public CreateAction getCreateAction() { + return createAction; + } + + public CloseAction getCloseAction() { + return closeAction; + } + + + + public static GuiBuilder create() { + return new GuiBuilder(); + } + + public static class GuiBuilder { + private InvAction mainAction; + private CreateAction createAction; + private CloseAction closeAction; + private final Map slotActions; + private final Map slotDisplay; + private String title; + + public GuiBuilder() { + this.title = "Untitled Inventory"; + this.mainAction = event -> {}; + this.createAction = inv -> {}; + this.closeAction = event -> {}; + this.slotActions = new HashMap<>(); + this.slotDisplay = new HashMap<>(); + } + + public GuiBuilder title(String text) { + title = text; + return this; + } + + public GuiBuilder onDefine(CreateAction action) { + createAction = action; + return this; + } + + public GuiBuilder onClose(CloseAction action) { + closeAction = action; + return this; + } + + public GuiBuilder defineMain(InvAction mainAction) { + this.mainAction = mainAction; + return this; + } + + public GuiBuilder define(int slot, ItemStack display, InvAction action) { + if (slot < 0 || slot >= 54 || display == null || action == null) return this; + slotActions.put(slot, action); + slotDisplay.put(slot, display); + return this; + } + + public GuiBuilder define(int slot, ItemStack display) { + return define(slot, display, event -> {}); + } + + public CustomGui build() { + CustomGui gui = new CustomGui(title, mainAction, slotActions, slotDisplay, createAction, closeAction); + CustomGui.register(gui); + return gui; + } + } + + @FunctionalInterface + public interface InvAction { + void onClick(InventoryClickEvent event); + } + + @FunctionalInterface + public interface CloseAction { + void onClose(InventoryCloseEvent event); + } + + @FunctionalInterface + public interface CreateAction { + void onCreate(Inventory inv); + } +} diff --git a/src/main/java/fun/ogre/ogredupealias/utils/ServerUtils.java b/src/main/java/fun/ogre/ogredupealias/utils/ServerUtils.java index 0fb8387..afbd1ab 100644 --- a/src/main/java/fun/ogre/ogredupealias/utils/ServerUtils.java +++ b/src/main/java/fun/ogre/ogredupealias/utils/ServerUtils.java @@ -63,4 +63,8 @@ public class ServerUtils { public static boolean hasBlockBelow(Player p, Material type) { return p.getLocation().clone().subtract(0, 1, 0).getBlock().getType() == type; } + + public static boolean dispatch(String command) { + return Bukkit.getServer().dispatchCommand(Bukkit.getConsoleSender(), command); + } }