Started Base For Custom GUI Handling
This commit is contained in:
parent
faf245b870
commit
0a60a581f5
@ -0,0 +1,59 @@
|
||||
package com.massivecraft.factions.util.serializable;
|
||||
|
||||
import com.massivecraft.factions.util.ItemBuilder;
|
||||
import org.bukkit.event.inventory.ClickType;
|
||||
import org.bukkit.event.inventory.InventoryClickEvent;
|
||||
import org.bukkit.inventory.ItemStack;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* @author Driftay
|
||||
*/
|
||||
public class InventoryItem {
|
||||
private ItemStack item;
|
||||
private Map<ClickType, Runnable> clickMap;
|
||||
private Runnable runnable;
|
||||
|
||||
public InventoryItem(ItemStack original) {
|
||||
this.clickMap = new HashMap();
|
||||
this.item = original;
|
||||
}
|
||||
|
||||
public InventoryItem(ItemBuilder original) { this(original.build()); }
|
||||
|
||||
public InventoryItem click(ClickType type, Runnable runnable) {
|
||||
this.clickMap.put(type, runnable);
|
||||
return this;
|
||||
}
|
||||
|
||||
public InventoryItem click(Runnable runnable) {
|
||||
this.runnable = runnable;
|
||||
return this;
|
||||
}
|
||||
|
||||
public void handleClick(InventoryClickEvent event) {
|
||||
if (clickMap.isEmpty() && runnable != null) {
|
||||
runnable.run();
|
||||
} else {
|
||||
Runnable found = this.clickMap.get(event.getClick());
|
||||
if (found != null) {
|
||||
found.run();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public ItemStack getItem() {
|
||||
return this.item;
|
||||
}
|
||||
|
||||
public Map<ClickType, Runnable> getClickMap() {
|
||||
return this.clickMap;
|
||||
}
|
||||
|
||||
public Runnable getRunnable() {
|
||||
return this.runnable;
|
||||
}
|
||||
}
|
@ -1,40 +0,0 @@
|
||||
package com.massivecraft.factions.util.serializable;
|
||||
|
||||
|
||||
import com.massivecraft.factions.util.ItemBuilder;
|
||||
import com.massivecraft.factions.util.XMaterial;
|
||||
import org.bukkit.inventory.ItemStack;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public class Item {
|
||||
private String name;
|
||||
private List<String> lore;
|
||||
private XMaterial material;
|
||||
private int amount;
|
||||
private int slot;
|
||||
|
||||
public Item(String name, List<String> lore, XMaterial material, int amount, int slot) {
|
||||
this.name = name;
|
||||
this.lore = lore;
|
||||
this.material = material;
|
||||
this.amount = amount;
|
||||
this.slot = slot;
|
||||
}
|
||||
|
||||
public ItemStack buildItemStack(boolean isSelected) {
|
||||
return new ItemBuilder(material.parseItem()).name(name).lore(lore).glowing(isSelected).amount(amount).build();
|
||||
}
|
||||
|
||||
public int getAmount() {
|
||||
return this.amount;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return this.name;
|
||||
}
|
||||
|
||||
public int getSlot() {
|
||||
return this.slot;
|
||||
}
|
||||
}
|
@ -0,0 +1,151 @@
|
||||
package com.massivecraft.factions.util.serializable;
|
||||
|
||||
/**
|
||||
* @author Driftay
|
||||
*/
|
||||
import com.massivecraft.factions.FactionsPlugin;
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.bukkit.event.inventory.InventoryClickEvent;
|
||||
import org.bukkit.event.inventory.InventoryType;
|
||||
import org.bukkit.inventory.Inventory;
|
||||
import org.bukkit.inventory.ItemStack;
|
||||
import org.bukkit.plugin.java.JavaPlugin;
|
||||
|
||||
import java.util.*;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
import java.util.function.Consumer;
|
||||
|
||||
|
||||
public abstract class SaberGUI {
|
||||
public static Set<String> allGUINames = new HashSet();
|
||||
public static Map<UUID, SaberGUI> activeGUIs = new ConcurrentHashMap();
|
||||
public SaberGUI parentGUI;
|
||||
protected String title;
|
||||
protected int size;
|
||||
protected Player player;
|
||||
protected Inventory inventory;
|
||||
private Map<Integer, InventoryItem> inventoryItems;
|
||||
private String owningPluginName;
|
||||
private Runnable closeRunnable;
|
||||
|
||||
public SaberGUI(Player player, String title, int size) {
|
||||
this(player, title, size, InventoryType.CHEST);
|
||||
}
|
||||
|
||||
public SaberGUI(Player player, String title, int size, InventoryType type) {
|
||||
this.inventoryItems = new HashMap();
|
||||
this.inventory = type == InventoryType.CHEST ? Bukkit.createInventory(null, size, title) : Bukkit.createInventory(null, type, title);
|
||||
this.player = player;
|
||||
this.size = size;
|
||||
this.title = title;
|
||||
allGUINames.add(this.title);
|
||||
}
|
||||
|
||||
public SaberGUI setParentGUI(SaberGUI parent) {
|
||||
this.parentGUI = parent;
|
||||
return this;
|
||||
}
|
||||
|
||||
public void onUnknownItemClick(InventoryClickEvent event) {
|
||||
}
|
||||
|
||||
public abstract void redraw();
|
||||
|
||||
public void openGUI(JavaPlugin owning) {
|
||||
this.owningPluginName = owning.getName();
|
||||
SaberGUI currentlyActive = activeGUIs.get(this.player.getUniqueId());
|
||||
if (currentlyActive != null) {
|
||||
Bukkit.getLogger().info("Closing already open menu first!");
|
||||
Bukkit.getScheduler().scheduleSyncDelayedTask(owning, () -> {
|
||||
currentlyActive.close();
|
||||
activeGUIs.put(this.player.getUniqueId(), this);
|
||||
this.redraw();
|
||||
this.player.openInventory(this.inventory);
|
||||
});
|
||||
} else {
|
||||
activeGUIs.put(this.player.getUniqueId(), this);
|
||||
this.redraw();
|
||||
this.player.openInventory(this.inventory);
|
||||
}
|
||||
}
|
||||
|
||||
public void setItem(int slot, InventoryItem inventoryItem) {
|
||||
if (inventoryItem != null && inventoryItem.getItem() != null) {
|
||||
this.inventoryItems.put(slot, inventoryItem);
|
||||
this.inventory.setItem(slot, inventoryItem.getItem());
|
||||
} else {
|
||||
this.removeItem(slot);
|
||||
}
|
||||
}
|
||||
|
||||
public void closeWithDelay() {
|
||||
this.closeWithDelay(null);
|
||||
}
|
||||
|
||||
public void closeWithDelay(Consumer<Player> afterClose) {
|
||||
Bukkit.getScheduler().scheduleSyncDelayedTask(FactionsPlugin.instance, () -> {
|
||||
this.player.closeInventory();
|
||||
if (afterClose != null) {
|
||||
afterClose.accept(this.player);
|
||||
}
|
||||
|
||||
}, 1L);
|
||||
}
|
||||
|
||||
public void setItem(int slot, ItemStack item, Runnable runnable) {
|
||||
this.setItem(slot, (new InventoryItem(item)).click(runnable));
|
||||
}
|
||||
|
||||
public void onInventoryClose() {
|
||||
if (this.closeRunnable != null) {
|
||||
this.closeRunnable.run();
|
||||
}
|
||||
}
|
||||
|
||||
public void close() {
|
||||
this.onInventoryClose();
|
||||
this.player.closeInventory();
|
||||
}
|
||||
|
||||
public void removeItem(int slot) {
|
||||
this.inventory.setItem(slot, null);
|
||||
this.inventoryItems.remove(slot);
|
||||
}
|
||||
|
||||
public SaberGUI setOnClose(Runnable runnable) {
|
||||
this.closeRunnable = runnable;
|
||||
return this;
|
||||
}
|
||||
|
||||
public boolean isInventory(Inventory inventory) {
|
||||
return this.inventory.equals(inventory);
|
||||
}
|
||||
|
||||
public static SaberGUI getActiveGUI(UUID uuid) { return activeGUIs.get(uuid); }
|
||||
|
||||
public static void removeGUI(UUID uuid) {
|
||||
activeGUIs.remove(uuid);
|
||||
}
|
||||
|
||||
public SaberGUI getParentGUI() {
|
||||
return this.parentGUI;
|
||||
}
|
||||
|
||||
public Map<Integer, InventoryItem> getInventoryItems() {
|
||||
return this.inventoryItems;
|
||||
}
|
||||
|
||||
public String getOwningPluginName() {
|
||||
return this.owningPluginName;
|
||||
}
|
||||
|
||||
public void setOwningPluginName(String owningPluginName) {
|
||||
this.owningPluginName = owningPluginName;
|
||||
}
|
||||
|
||||
public Runnable getCloseRunnable() {
|
||||
return this.closeRunnable;
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user