Merge pull request 'Inventory Improvements' (#113) from inventory_2 into master
Reviewed-on: #113 Reviewed-by: Lixfel <lixfel@steamwar.de>
Dieser Commit ist enthalten in:
Commit
d801092061
@ -30,13 +30,19 @@ import org.bukkit.event.inventory.InventoryCloseEvent;
|
||||
import org.bukkit.inventory.Inventory;
|
||||
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 {
|
||||
|
||||
final Player player;
|
||||
final Map<Integer, InvCallback> callbacks = new HashMap<>();
|
||||
final Map<Integer, Consumer<InventoryClickEvent>> callbacks = new HashMap<>();
|
||||
final Inventory inventory;
|
||||
boolean open = false;
|
||||
|
||||
public SWInventory(Player p, int size, String t) {
|
||||
player = p;
|
||||
@ -49,13 +55,26 @@ public class SWInventory implements Listener {
|
||||
open();
|
||||
}
|
||||
|
||||
public SWInventory(Player p, Supplier<Inventory> inventoryConstructor) {
|
||||
player = p;
|
||||
try {
|
||||
inventory = inventoryConstructor.get();
|
||||
} catch (Exception e) {
|
||||
throw new SecurityException("Could not construct inventory", e);
|
||||
}
|
||||
}
|
||||
|
||||
public void addCloseCallback(InvCallback c) {
|
||||
callbacks.put(-1, c);
|
||||
callbacks.put(-1, inventoryClickEvent -> c.clicked(null));
|
||||
}
|
||||
|
||||
public void addCloseRunable(Runnable c) {
|
||||
callbacks.put(-1, inventoryClickEvent -> c.run());
|
||||
}
|
||||
|
||||
public void setItem(int pos, ItemStack itemStack, InvCallback c) {
|
||||
inventory.setItem(pos, itemStack);
|
||||
callbacks.put(pos, c);
|
||||
callbacks.put(pos, inventoryClickEvent -> c.clicked(inventoryClickEvent.getClick()));
|
||||
}
|
||||
|
||||
public void setItem(int pos, SWItem item) {
|
||||
@ -80,12 +99,43 @@ public class SWInventory implements Listener {
|
||||
}
|
||||
|
||||
public void setCallback(int pos, InvCallback c) {
|
||||
callbacks.put(pos, inventoryClickEvent -> c.clicked(inventoryClickEvent.getClick()));
|
||||
}
|
||||
|
||||
public void setItemEvent(int pos, ItemStack itemStack, Consumer<InventoryClickEvent> c) {
|
||||
inventory.setItem(pos, itemStack);
|
||||
callbacks.put(pos, c);
|
||||
}
|
||||
|
||||
public void setItemEvent(int pos, Material m, String name, Consumer<InventoryClickEvent> c) {
|
||||
setItemEvent(pos, m, name, new ArrayList<>(), false, c);
|
||||
}
|
||||
|
||||
public void setItemEvent(int pos, Material m, byte meta, String name, Consumer<InventoryClickEvent> c) {
|
||||
setItemEvent(pos, m, meta, name, new ArrayList<>(), false, c);
|
||||
}
|
||||
|
||||
public void setItemEvent(int pos, Material m, String name, List<String> lore, boolean e, Consumer<InventoryClickEvent> c) {
|
||||
setItemEvent(pos, m, (byte) 0, name, lore, e, c);
|
||||
}
|
||||
|
||||
public void setItemEvent(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);
|
||||
setEventCallback(pos, c);
|
||||
}
|
||||
|
||||
public void setEventCallback(int pos, Consumer<InventoryClickEvent> c) {
|
||||
callbacks.put(pos, c);
|
||||
}
|
||||
|
||||
public void open() {
|
||||
player.openInventory(inventory);
|
||||
if(!open) {
|
||||
Bukkit.getPluginManager().registerEvents(this, Core.getInstance());
|
||||
open = true;
|
||||
}
|
||||
}
|
||||
|
||||
@EventHandler
|
||||
@ -95,7 +145,7 @@ public class SWInventory implements Listener {
|
||||
|
||||
if (callbacks.containsKey(e.getRawSlot()) && callbacks.get(e.getRawSlot()) != null) {
|
||||
e.setCancelled(true);
|
||||
callbacks.get(e.getRawSlot()).clicked(e.getClick());
|
||||
callbacks.get(e.getRawSlot()).accept(e);
|
||||
}
|
||||
}
|
||||
|
||||
@ -107,6 +157,6 @@ public class SWInventory implements Listener {
|
||||
InventoryClickEvent.getHandlerList().unregister(this);
|
||||
InventoryCloseEvent.getHandlerList().unregister(this);
|
||||
if(callbacks.containsKey(-1))
|
||||
callbacks.get(-1).clicked(null);
|
||||
callbacks.get(-1).accept(null);
|
||||
}
|
||||
}
|
||||
|
In neuem Issue referenzieren
Einen Benutzer sperren