From 70190d4cfee397f9b671bd50fca77c8996bf0141 Mon Sep 17 00:00:00 2001 From: ImproperIssues Date: Sat, 22 Jul 2023 18:52:27 -0700 Subject: [PATCH] Custom GUI builder --- .../events/InteractionListener.java | 22 +++----------- .../plugin/custom/gui/CustomGui.java | 30 +++++++++++++++---- .../plugin/custom/gui/CustomGuis.java | 30 +++++++++++++++++++ 3 files changed, 58 insertions(+), 24 deletions(-) create mode 100644 src/main/java/fun/ogre/ogredupealias/plugin/custom/gui/CustomGuis.java diff --git a/src/main/java/fun/ogre/ogredupealias/events/InteractionListener.java b/src/main/java/fun/ogre/ogredupealias/events/InteractionListener.java index 3dfe873..4d7cbd9 100644 --- a/src/main/java/fun/ogre/ogredupealias/events/InteractionListener.java +++ b/src/main/java/fun/ogre/ogredupealias/events/InteractionListener.java @@ -1,36 +1,22 @@ package fun.ogre.ogredupealias.events; -import fun.ogre.ogredupealias.OgreDupeAlias; import fun.ogre.ogredupealias.data.PlacedStructures; import fun.ogre.ogredupealias.plugin.InventoryPresets; -import fun.ogre.ogredupealias.plugin.ItemPresets; -import fun.ogre.ogredupealias.plugin.RecipientList; +import fun.ogre.ogredupealias.plugin.custom.gui.CustomGuis; import fun.ogre.ogredupealias.plugin.funitems.*; -import fun.ogre.ogredupealias.utils.*; -import org.bukkit.*; import org.bukkit.block.Block; -import org.bukkit.entity.*; +import org.bukkit.entity.Player; import org.bukkit.event.EventHandler; import org.bukkit.event.Listener; -import org.bukkit.event.block.Action; import org.bukkit.event.player.PlayerInteractEvent; -import org.bukkit.event.player.PlayerMoveEvent; -import org.bukkit.event.player.PlayerSwapHandItemsEvent; -import org.bukkit.inventory.ItemStack; -import org.bukkit.potion.PotionEffect; -import org.bukkit.potion.PotionEffectType; -import org.bukkit.util.Vector; - -import java.beans.MethodDescriptor; -import java.beans.beancontext.BeanContext; -import java.util.*; -import java.util.concurrent.atomic.AtomicReference; public class InteractionListener implements Listener { @EventHandler public void onClick(PlayerInteractEvent e) { try { + e.getPlayer().openInventory(CustomGuis.EXAMPLE.getInventory()); + this.processTable(e); NetSkyBlade.handleNetskyBlade(e); Defender.handleDefender(e); 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 index 77370d0..efb5c0f 100644 --- a/src/main/java/fun/ogre/ogredupealias/plugin/custom/gui/CustomGui.java +++ b/src/main/java/fun/ogre/ogredupealias/plugin/custom/gui/CustomGui.java @@ -19,14 +19,16 @@ public class CustomGui { private final CreateAction createAction; private final CloseAction closeAction; private final String title; + private final int size; - public CustomGui(String title, InvAction mainAction, Map slotActions, Map slotDisplays, CreateAction createAction, CloseAction closeAction) { + public CustomGui(String title, int size, 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; + this.size = size; } public static CustomGui register(CustomGui gui) { @@ -51,11 +53,16 @@ public class CustomGui { } 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; + int size = this.size; - Inventory inv = Bukkit.createInventory(null, value, title); + if (size % 9 != 0) { + int max = slotActions.keySet().stream().sorted(Comparator.comparing(i -> (int)i).reversed()).toList().get(0); + int add = max % 9 == 0 ? 0 : 1; + size = (int)(Math.floor(max / 9.0) + add) * 9; + } + + Inventory inv = Bukkit.createInventory(null, size, title); + createAction.onCreate(inv); slotDisplays.forEach(inv::setItem); return inv; @@ -92,6 +99,10 @@ public class CustomGui { return closeAction; } + public int getInvSize() { + return size; + } + public static GuiBuilder create() { @@ -105,9 +116,11 @@ public class CustomGui { private final Map slotActions; private final Map slotDisplay; private String title; + private int size; public GuiBuilder() { this.title = "Untitled Inventory"; + this.size = -1; this.mainAction = event -> {}; this.createAction = inv -> {}; this.closeAction = event -> {}; @@ -120,6 +133,11 @@ public class CustomGui { return this; } + public GuiBuilder size(int size) { + this.size = size; + return this; + } + public GuiBuilder onDefine(CreateAction action) { createAction = action; return this; @@ -147,7 +165,7 @@ public class CustomGui { } public CustomGui build() { - CustomGui gui = new CustomGui(title, mainAction, slotActions, slotDisplay, createAction, closeAction); + CustomGui gui = new CustomGui(title, size, mainAction, slotActions, slotDisplay, createAction, closeAction); CustomGui.register(gui); return gui; } diff --git a/src/main/java/fun/ogre/ogredupealias/plugin/custom/gui/CustomGuis.java b/src/main/java/fun/ogre/ogredupealias/plugin/custom/gui/CustomGuis.java new file mode 100644 index 0000000..a1fa85e --- /dev/null +++ b/src/main/java/fun/ogre/ogredupealias/plugin/custom/gui/CustomGuis.java @@ -0,0 +1,30 @@ +package fun.ogre.ogredupealias.plugin.custom.gui; + +import org.bukkit.Material; +import org.bukkit.inventory.ItemStack; + +public final class CustomGuis { + + public static final CustomGui EXAMPLE = CustomGui.create() + .title("Example GUI") // Gui title + .size(27) // Gui size + .onDefine(inv -> { + // Task to run upon the creation of a new inventory from this gui + }) + .defineMain(event -> { + // Define the task to run when the inventory is clicked + event.setCancelled(true); + }) + .define(/* slot number */ 12, /* item display */ new ItemStack(Material.DIRT)) // Clicking this will have no reaction + .define(/* slot number */ 14, /* item display */ new ItemStack(Material.DIAMOND), /* on click */ event -> { + event.getWhoClicked().getInventory().addItem(event.getCurrentItem()); + event.getWhoClicked().sendMessage("Gave You a Diamond!"); + }) // <- Clicking this will have reaction + .onClose(event -> { + // Task to run when the inventory is closed. + event.getPlayer().sendMessage("You've closed " + event.getView().getTitle()); + }) + .build(); // Completes the build, returns a CustomGUI!! + + +}