121 lines
3.1 KiB
Java
121 lines
3.1 KiB
Java
package dev.zontreck.ase.guis;
|
|
|
|
import java.util.ArrayList;
|
|
import java.util.Iterator;
|
|
import java.util.List;
|
|
|
|
import org.bukkit.Bukkit;
|
|
import org.bukkit.entity.Player;
|
|
import org.bukkit.inventory.Inventory;
|
|
import org.bukkit.inventory.InventoryView;
|
|
import org.bukkit.inventory.ItemStack;
|
|
|
|
import dev.zontreck.ase.AriasServerEssentials;
|
|
import net.kyori.adventure.text.Component;
|
|
|
|
public class ChestGUI {
|
|
// START STATIC REGISTRY
|
|
public static final List<ChestGUI> GUI_LIST = new java.util.ArrayList<>();
|
|
|
|
public static void register(ChestGUI gui) {
|
|
GUI_LIST.add(gui);
|
|
}
|
|
|
|
public static void checkValidity() {
|
|
Iterator<ChestGUI> it = GUI_LIST.iterator();
|
|
|
|
while (it.hasNext()) {
|
|
ChestGUI gui = it.next();
|
|
if (!gui.valid()) {
|
|
|
|
AriasServerEssentials.getSelf().getLogger().info("A gui has been invalidated, removing from caches");
|
|
it.remove();
|
|
}
|
|
}
|
|
}
|
|
|
|
public static ChestGUI getGUIForPlayer(Player player) {
|
|
for (ChestGUI gui : GUI_LIST) {
|
|
if (gui.player != null && gui.player.getUniqueId().equals(player.getUniqueId())) {
|
|
return gui;
|
|
}
|
|
}
|
|
|
|
AriasServerEssentials.getSelf().getLogger().info("No such GUI, returning null");
|
|
return null;
|
|
}
|
|
|
|
public static void onTick() {
|
|
checkValidity();
|
|
}
|
|
// END STATIC REGISTRY
|
|
|
|
public String guiTitle;
|
|
public List<ItemStack> buttons = new ArrayList<>();
|
|
public int totalSize;
|
|
public Player player;
|
|
public boolean closed = false;
|
|
|
|
public ChestGUI(String title, int size) {
|
|
this.guiTitle = title;
|
|
this.totalSize = size;
|
|
}
|
|
|
|
public void setPlayer(Player player) {
|
|
this.player = player;
|
|
}
|
|
|
|
public void fill(int amount, PrimitiveItem item) {
|
|
for (int i = 0; i < amount; i++) {
|
|
buttons.add(item.generate());
|
|
}
|
|
}
|
|
|
|
public void add(PrimitiveItem item) {
|
|
buttons.add(item.generate());
|
|
}
|
|
|
|
/**
|
|
* This method presents the GUI to the player, using the defined layout above.
|
|
*/
|
|
public void doPresentation() {
|
|
Inventory inv = Bukkit.createInventory(null, totalSize, Component.text(guiTitle));
|
|
|
|
for (int i = 0; i < buttons.size(); i++) {
|
|
inv.setItem(i, buttons.get(i));
|
|
}
|
|
|
|
player.openInventory(inv);
|
|
register(this);
|
|
}
|
|
|
|
public void close() {
|
|
// Assume the player is still in this GUI if it has not been invalidated.
|
|
if (valid()) {
|
|
player.closeInventory();
|
|
closed = true;
|
|
} else {
|
|
closed = true; // Ensure this is set to true.
|
|
}
|
|
}
|
|
|
|
public boolean valid() {
|
|
if (player == null) {
|
|
return false;
|
|
}
|
|
|
|
if (closed)
|
|
return false;
|
|
|
|
InventoryView inv = player.getOpenInventory();
|
|
if (inv == null) {
|
|
return false;
|
|
}
|
|
|
|
if (inv.title().toString().contains(guiTitle)) {
|
|
return true;
|
|
} else {
|
|
return false;
|
|
}
|
|
}
|
|
}
|