SteamWar/SpigotCore
Archiviert
13
0

Inventory Improvements

Dieser Commit ist enthalten in:
Chaoscaot 2021-08-25 15:40:51 +02:00
Ursprung c032baf879
Commit 336d697d9e

Datei anzeigen

@ -30,35 +30,49 @@ import org.bukkit.event.inventory.InventoryCloseEvent;
import org.bukkit.inventory.Inventory; import org.bukkit.inventory.Inventory;
import org.bukkit.inventory.ItemStack; import org.bukkit.inventory.ItemStack;
import java.util.*; import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.function.Consumer;
import java.util.function.Supplier;
public class SWInventory implements Listener { public class SWInventory implements Listener {
final Player player; final Player player;
final Map<Integer, InvCallback> callbacks = new HashMap<>(); final Map<Integer, Consumer<InventoryClickEvent>> callbacks = new HashMap<>();
final Inventory inventory; final Inventory inventory;
public SWInventory(Player p, int size, String t){ public SWInventory(Player p, int size, String t) {
player = p; player = p;
inventory = Bukkit.createInventory(p, size, t); inventory = Bukkit.createInventory(p, size, t);
} }
public SWInventory(Player p, int size, String t, Map<Integer, SWItem> items){ public SWInventory(Player p, int size, String t, Map<Integer, SWItem> items) {
this(p, size, t); this(p, size, t);
items.forEach(this::setItem); items.forEach(this::setItem);
open(); open();
} }
public void addCloseCallback(InvCallback c){ public SWInventory(Player p, Supplier<Inventory> inventoryConstructor) {
callbacks.put(-1, c); player = p;
try {
inventory = inventoryConstructor.get();
} catch (Exception e) {
throw new SecurityException("Could not construct inventory", e);
}
} }
public void setItem(int pos, ItemStack itemStack, InvCallback c){ public void addCloseCallback(InvCallback c) {
callbacks.put(-1, inventoryClickEvent -> c.clicked(null));
}
public void setItem(int pos, ItemStack itemStack, InvCallback c) {
inventory.setItem(pos, itemStack); inventory.setItem(pos, itemStack);
callbacks.put(pos, c); callbacks.put(pos, inventoryClickEvent -> c.clicked(inventoryClickEvent.getClick()));
} }
public void setItem(int pos, SWItem item){ public void setItem(int pos, SWItem item) {
setItem(pos, item.getItemStack(), item.getCallback()); setItem(pos, item.getItemStack(), item.getCallback());
} }
@ -70,32 +84,64 @@ public class SWInventory implements Listener {
setItem(pos, m, meta, name, new ArrayList<>(), false, c); setItem(pos, m, meta, name, new ArrayList<>(), false, c);
} }
public void setItem(int pos, Material m, String name, List<String> lore, boolean e, InvCallback c){ public void setItem(int pos, Material m, String name, List<String> lore, boolean e, InvCallback c) {
setItem(pos, m, (byte)0, name, lore, e, c); setItem(pos, m, (byte) 0, name, lore, e, c);
} }
public void setItem(int pos, Material m, byte meta, String name, List<String> lore, boolean e, InvCallback c){ public void setItem(int pos, Material m, byte meta, String name, List<String> lore, boolean e, InvCallback c) {
SWItem item = new SWItem(m, meta, name, lore, e, c); SWItem item = new SWItem(m, meta, name, lore, e, c);
setItem(pos, item); setItem(pos, item);
} }
public void setCallback(int pos, InvCallback c){ public void setCallback(int pos, InvCallback c) {
callbacks.put(pos, inventoryClickEvent -> c.clicked(inventoryClickEvent.getClick()));
}
public void setItem(int pos, ItemStack itemStack, Consumer<InventoryClickEvent> c) {
inventory.setItem(pos, itemStack);
callbacks.put(pos, c); callbacks.put(pos, c);
} }
public void open(){ public void setItem(int pos, Material m, String name, Consumer<InventoryClickEvent> c) {
setItem(pos, m, name, new ArrayList<>(), false, c);
}
public void setItem(int pos, Material m, byte meta, String name, Consumer<InventoryClickEvent> c) {
setItem(pos, m, meta, name, new ArrayList<>(), false, c);
}
public void setItem(int pos, Material m, String name, List<String> lore, boolean e, Consumer<InventoryClickEvent> c) {
setItem(pos, m, (byte) 0, name, lore, e, c);
}
public void setItem(int pos, Material m, byte meta, String name, List<String> lore, boolean e, Consumer<InventoryClickEvent> c) {
SWItem item = new SWItem(m, meta, name, lore, e, click -> {
});
setItem(pos, item);
setCallback(pos, c);
}
public void setCallback(int pos, Consumer<InventoryClickEvent> c) {
callbacks.put(pos, c);
}
public void open() {
player.openInventory(inventory); player.openInventory(inventory);
Bukkit.getPluginManager().registerEvents(this, Core.getInstance()); Bukkit.getPluginManager().registerEvents(this, Core.getInstance());
} }
public void reOpen() {
player.openInventory(inventory);
}
@EventHandler @EventHandler
public void onInventoryClick(InventoryClickEvent e){ public void onInventoryClick(InventoryClickEvent e) {
if(!player.equals(e.getWhoClicked())) if (!player.equals(e.getWhoClicked()))
return; return;
if(callbacks.containsKey(e.getRawSlot()) && callbacks.get(e.getRawSlot()) != null) { if (callbacks.containsKey(e.getRawSlot()) && callbacks.get(e.getRawSlot()) != null) {
e.setCancelled(true); e.setCancelled(true);
callbacks.get(e.getRawSlot()).clicked(e.getClick()); callbacks.get(e.getRawSlot()).accept(e);
} }
} }
@ -107,6 +153,6 @@ public class SWInventory implements Listener {
InventoryClickEvent.getHandlerList().unregister(this); InventoryClickEvent.getHandlerList().unregister(this);
InventoryCloseEvent.getHandlerList().unregister(this); InventoryCloseEvent.getHandlerList().unregister(this);
if(callbacks.containsKey(-1)) if(callbacks.containsKey(-1))
callbacks.get(-1).clicked(null); callbacks.get(-1).accept(null);
} }
} }