From 336d697d9e28cf346a78653c6d011f8c613e50b6 Mon Sep 17 00:00:00 2001 From: Chaoscaot Date: Wed, 25 Aug 2021 15:40:51 +0200 Subject: [PATCH 1/4] Inventory Improvements --- .../de/steamwar/inventory/SWInventory.java | 84 ++++++++++++++----- 1 file changed, 65 insertions(+), 19 deletions(-) diff --git a/SpigotCore_Main/src/de/steamwar/inventory/SWInventory.java b/SpigotCore_Main/src/de/steamwar/inventory/SWInventory.java index 7de1c87..731eb03 100644 --- a/SpigotCore_Main/src/de/steamwar/inventory/SWInventory.java +++ b/SpigotCore_Main/src/de/steamwar/inventory/SWInventory.java @@ -30,35 +30,49 @@ 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 callbacks = new HashMap<>(); + final Map> callbacks = new HashMap<>(); final Inventory inventory; - public SWInventory(Player p, int size, String t){ + public SWInventory(Player p, int size, String t) { player = p; inventory = Bukkit.createInventory(p, size, t); } - public SWInventory(Player p, int size, String t, Map items){ + public SWInventory(Player p, int size, String t, Map items) { this(p, size, t); items.forEach(this::setItem); open(); } - public void addCloseCallback(InvCallback c){ - callbacks.put(-1, c); + public SWInventory(Player p, Supplier inventoryConstructor) { + 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); - 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()); } @@ -70,32 +84,64 @@ public class SWInventory implements Listener { setItem(pos, m, meta, name, new ArrayList<>(), false, c); } - public void setItem(int pos, Material m, String name, List lore, boolean e, InvCallback c){ - setItem(pos, m, (byte)0, name, lore, e, c); + public void setItem(int pos, Material m, String name, List lore, boolean e, InvCallback c) { + setItem(pos, m, (byte) 0, name, lore, e, c); } - public void setItem(int pos, Material m, byte meta, String name, List lore, boolean e, InvCallback c){ + public void setItem(int pos, Material m, byte meta, String name, List lore, boolean e, InvCallback c) { SWItem item = new SWItem(m, meta, name, lore, e, c); 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 c) { + inventory.setItem(pos, itemStack); callbacks.put(pos, c); } - public void open(){ + public void setItem(int pos, Material m, String name, Consumer c) { + setItem(pos, m, name, new ArrayList<>(), false, c); + } + + public void setItem(int pos, Material m, byte meta, String name, Consumer c) { + setItem(pos, m, meta, name, new ArrayList<>(), false, c); + } + + public void setItem(int pos, Material m, String name, List lore, boolean e, Consumer c) { + setItem(pos, m, (byte) 0, name, lore, e, c); + } + + public void setItem(int pos, Material m, byte meta, String name, List lore, boolean e, Consumer c) { + SWItem item = new SWItem(m, meta, name, lore, e, click -> { + }); + setItem(pos, item); + setCallback(pos, c); + } + + public void setCallback(int pos, Consumer c) { + callbacks.put(pos, c); + } + + public void open() { player.openInventory(inventory); Bukkit.getPluginManager().registerEvents(this, Core.getInstance()); } + public void reOpen() { + player.openInventory(inventory); + } + @EventHandler - public void onInventoryClick(InventoryClickEvent e){ - if(!player.equals(e.getWhoClicked())) + public void onInventoryClick(InventoryClickEvent e) { + if (!player.equals(e.getWhoClicked())) return; - if(callbacks.containsKey(e.getRawSlot()) && callbacks.get(e.getRawSlot()) != null) { + 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 +153,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); } } -- 2.39.2 From 8fbb16557cb1c2ad13163def2599eb066dec815a Mon Sep 17 00:00:00 2001 From: Chaoscaot Date: Wed, 25 Aug 2021 18:31:08 +0200 Subject: [PATCH 2/4] Backwards --- .../de/steamwar/inventory/SWInventory.java | 20 +++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/SpigotCore_Main/src/de/steamwar/inventory/SWInventory.java b/SpigotCore_Main/src/de/steamwar/inventory/SWInventory.java index 731eb03..6862697 100644 --- a/SpigotCore_Main/src/de/steamwar/inventory/SWInventory.java +++ b/SpigotCore_Main/src/de/steamwar/inventory/SWInventory.java @@ -97,31 +97,31 @@ public class SWInventory implements Listener { callbacks.put(pos, inventoryClickEvent -> c.clicked(inventoryClickEvent.getClick())); } - public void setItem(int pos, ItemStack itemStack, Consumer c) { + public void setItemEvent(int pos, ItemStack itemStack, Consumer c) { inventory.setItem(pos, itemStack); callbacks.put(pos, c); } - public void setItem(int pos, Material m, String name, Consumer c) { - setItem(pos, m, name, new ArrayList<>(), false, c); + public void setItemEvent(int pos, Material m, String name, Consumer c) { + setItemEvent(pos, m, name, new ArrayList<>(), false, c); } - public void setItem(int pos, Material m, byte meta, String name, Consumer c) { - setItem(pos, m, meta, name, new ArrayList<>(), false, c); + public void setItemEvent(int pos, Material m, byte meta, String name, Consumer c) { + setItemEvent(pos, m, meta, name, new ArrayList<>(), false, c); } - public void setItem(int pos, Material m, String name, List lore, boolean e, Consumer c) { - setItem(pos, m, (byte) 0, name, lore, e, c); + public void setItemEvent(int pos, Material m, String name, List lore, boolean e, Consumer c) { + setItemEvent(pos, m, (byte) 0, name, lore, e, c); } - public void setItem(int pos, Material m, byte meta, String name, List lore, boolean e, Consumer c) { + public void setItemEvent(int pos, Material m, byte meta, String name, List lore, boolean e, Consumer c) { SWItem item = new SWItem(m, meta, name, lore, e, click -> { }); setItem(pos, item); - setCallback(pos, c); + setEventCallback(pos, c); } - public void setCallback(int pos, Consumer c) { + public void setEventCallback(int pos, Consumer c) { callbacks.put(pos, c); } -- 2.39.2 From 30ffe3e471a2632562b6f75ddc81abb43ee15f2a Mon Sep 17 00:00:00 2001 From: Chaoscaot Date: Thu, 9 Sep 2021 20:43:32 +0200 Subject: [PATCH 3/4] Open Check --- .../src/de/steamwar/inventory/SWInventory.java | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/SpigotCore_Main/src/de/steamwar/inventory/SWInventory.java b/SpigotCore_Main/src/de/steamwar/inventory/SWInventory.java index 6862697..74a599b 100644 --- a/SpigotCore_Main/src/de/steamwar/inventory/SWInventory.java +++ b/SpigotCore_Main/src/de/steamwar/inventory/SWInventory.java @@ -42,6 +42,7 @@ public class SWInventory implements Listener { final Player player; final Map> callbacks = new HashMap<>(); final Inventory inventory; + boolean open = false; public SWInventory(Player p, int size, String t) { player = p; @@ -127,11 +128,10 @@ public class SWInventory implements Listener { public void open() { player.openInventory(inventory); - Bukkit.getPluginManager().registerEvents(this, Core.getInstance()); - } - - public void reOpen() { - player.openInventory(inventory); + if(!open) { + Bukkit.getPluginManager().registerEvents(this, Core.getInstance()); + open = true; + } } @EventHandler -- 2.39.2 From e0a86269e0da88665db05074f3912b4d7c8ea036 Mon Sep 17 00:00:00 2001 From: Chaoscaot Date: Mon, 27 Sep 2021 17:06:50 +0200 Subject: [PATCH 4/4] Add --- SpigotCore_Main/src/de/steamwar/inventory/SWInventory.java | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/SpigotCore_Main/src/de/steamwar/inventory/SWInventory.java b/SpigotCore_Main/src/de/steamwar/inventory/SWInventory.java index 74a599b..be75f28 100644 --- a/SpigotCore_Main/src/de/steamwar/inventory/SWInventory.java +++ b/SpigotCore_Main/src/de/steamwar/inventory/SWInventory.java @@ -68,6 +68,10 @@ public class SWInventory implements Listener { 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, inventoryClickEvent -> c.clicked(inventoryClickEvent.getClick())); -- 2.39.2