Custom GUI builder

This commit is contained in:
ImproperIssues
2023-07-22 18:52:27 -07:00
parent 74b53a5ae2
commit 70190d4cfe
3 changed files with 58 additions and 24 deletions

View File

@@ -1,36 +1,22 @@
package fun.ogre.ogredupealias.events; package fun.ogre.ogredupealias.events;
import fun.ogre.ogredupealias.OgreDupeAlias;
import fun.ogre.ogredupealias.data.PlacedStructures; import fun.ogre.ogredupealias.data.PlacedStructures;
import fun.ogre.ogredupealias.plugin.InventoryPresets; import fun.ogre.ogredupealias.plugin.InventoryPresets;
import fun.ogre.ogredupealias.plugin.ItemPresets; import fun.ogre.ogredupealias.plugin.custom.gui.CustomGuis;
import fun.ogre.ogredupealias.plugin.RecipientList;
import fun.ogre.ogredupealias.plugin.funitems.*; import fun.ogre.ogredupealias.plugin.funitems.*;
import fun.ogre.ogredupealias.utils.*;
import org.bukkit.*;
import org.bukkit.block.Block; import org.bukkit.block.Block;
import org.bukkit.entity.*; import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler; import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener; import org.bukkit.event.Listener;
import org.bukkit.event.block.Action;
import org.bukkit.event.player.PlayerInteractEvent; 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 { public class InteractionListener implements Listener {
@EventHandler @EventHandler
public void onClick(PlayerInteractEvent e) { public void onClick(PlayerInteractEvent e) {
try { try {
e.getPlayer().openInventory(CustomGuis.EXAMPLE.getInventory());
this.processTable(e); this.processTable(e);
NetSkyBlade.handleNetskyBlade(e); NetSkyBlade.handleNetskyBlade(e);
Defender.handleDefender(e); Defender.handleDefender(e);

View File

@@ -19,14 +19,16 @@ public class CustomGui {
private final CreateAction createAction; private final CreateAction createAction;
private final CloseAction closeAction; private final CloseAction closeAction;
private final String title; private final String title;
private final int size;
public CustomGui(String title, InvAction mainAction, Map<Integer, InvAction> slotActions, Map<Integer, ItemStack> slotDisplays, CreateAction createAction, CloseAction closeAction) { public CustomGui(String title, int size, InvAction mainAction, Map<Integer, InvAction> slotActions, Map<Integer, ItemStack> slotDisplays, CreateAction createAction, CloseAction closeAction) {
this.slotActions = slotActions; this.slotActions = slotActions;
this.slotDisplays = slotDisplays; this.slotDisplays = slotDisplays;
this.mainAction = mainAction; this.mainAction = mainAction;
this.createAction = createAction; this.createAction = createAction;
this.closeAction = closeAction; this.closeAction = closeAction;
this.title = title; this.title = title;
this.size = size;
} }
public static CustomGui register(CustomGui gui) { public static CustomGui register(CustomGui gui) {
@@ -51,11 +53,16 @@ public class CustomGui {
} }
public Inventory getInventory() { public Inventory getInventory() {
int max = slotActions.keySet().stream().sorted(Comparator.comparing(i -> (int)i).reversed()).toList().get(0); int size = this.size;
int add = max % 9 == 0 ? 0 : 1;
int value = (int)(Math.floor(max / 9.0) + add) * 9;
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); slotDisplays.forEach(inv::setItem);
return inv; return inv;
@@ -92,6 +99,10 @@ public class CustomGui {
return closeAction; return closeAction;
} }
public int getInvSize() {
return size;
}
public static GuiBuilder create() { public static GuiBuilder create() {
@@ -105,9 +116,11 @@ public class CustomGui {
private final Map<Integer, InvAction> slotActions; private final Map<Integer, InvAction> slotActions;
private final Map<Integer, ItemStack> slotDisplay; private final Map<Integer, ItemStack> slotDisplay;
private String title; private String title;
private int size;
public GuiBuilder() { public GuiBuilder() {
this.title = "Untitled Inventory"; this.title = "Untitled Inventory";
this.size = -1;
this.mainAction = event -> {}; this.mainAction = event -> {};
this.createAction = inv -> {}; this.createAction = inv -> {};
this.closeAction = event -> {}; this.closeAction = event -> {};
@@ -120,6 +133,11 @@ public class CustomGui {
return this; return this;
} }
public GuiBuilder size(int size) {
this.size = size;
return this;
}
public GuiBuilder onDefine(CreateAction action) { public GuiBuilder onDefine(CreateAction action) {
createAction = action; createAction = action;
return this; return this;
@@ -147,7 +165,7 @@ public class CustomGui {
} }
public CustomGui build() { 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); CustomGui.register(gui);
return gui; return gui;
} }

View File

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