Custom GUI builder

This commit is contained in:
ImproperIssues
2023-07-22 18:30:21 -07:00
parent 648e50e2b7
commit 74b53a5ae2
3 changed files with 177 additions and 0 deletions

View File

@@ -1,6 +1,7 @@
package fun.ogre.ogredupealias.events; package fun.ogre.ogredupealias.events;
import fun.ogre.ogredupealias.plugin.custom.forging.CustomTable; import fun.ogre.ogredupealias.plugin.custom.forging.CustomTable;
import fun.ogre.ogredupealias.plugin.custom.gui.CustomGui;
import fun.ogre.ogredupealias.utils.Text; import fun.ogre.ogredupealias.utils.Text;
import org.bukkit.event.EventHandler; import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener; import org.bukkit.event.Listener;
@@ -14,6 +15,7 @@ public class InventoryActionListener implements Listener {
public void onClick(InventoryClickEvent e) { public void onClick(InventoryClickEvent e) {
try { try {
this.handleForgeClick(e); this.handleForgeClick(e);
CustomGui.handleRegistriesClick(e);
} }
catch (Exception ignore) {} catch (Exception ignore) {}
} }
@@ -22,6 +24,7 @@ public class InventoryActionListener implements Listener {
public void onClose(InventoryCloseEvent e) { public void onClose(InventoryCloseEvent e) {
try { try {
this.handleForgeClose(e); this.handleForgeClose(e);
CustomGui.handleRegistriesClose(e);
} }
catch (Exception ignore) {} catch (Exception ignore) {}
} }

View File

@@ -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<String, CustomGui> registry = new HashMap<>();
private final Map<Integer, InvAction> slotActions;
private final Map<Integer, ItemStack> slotDisplays;
private final InvAction mainAction;
private final CreateAction createAction;
private final CloseAction closeAction;
private final String title;
public CustomGui(String title, InvAction mainAction, Map<Integer, InvAction> slotActions, Map<Integer, ItemStack> 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<Integer, InvAction> getSlotActions() {
return slotActions;
}
public Map<Integer, ItemStack> 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<Integer, InvAction> slotActions;
private final Map<Integer, ItemStack> 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);
}
}

View File

@@ -63,4 +63,8 @@ public class ServerUtils {
public static boolean hasBlockBelow(Player p, Material type) { public static boolean hasBlockBelow(Player p, Material type) {
return p.getLocation().clone().subtract(0, 1, 0).getBlock().getType() == 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);
}
} }