Custom GUI builder
This commit is contained in:
@@ -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);
|
||||
|
||||
@@ -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<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.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 size = this.size;
|
||||
|
||||
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;
|
||||
int value = (int)(Math.floor(max / 9.0) + add) * 9;
|
||||
size = (int)(Math.floor(max / 9.0) + add) * 9;
|
||||
}
|
||||
|
||||
Inventory inv = Bukkit.createInventory(null, value, title);
|
||||
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<Integer, InvAction> slotActions;
|
||||
private final Map<Integer, ItemStack> 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;
|
||||
}
|
||||
|
||||
@@ -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!!
|
||||
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user