From ccd7c47cf6cfe88932a3fd0c0e4229aa13942a5a Mon Sep 17 00:00:00 2001 From: APickledWalrus Date: Tue, 3 Aug 2021 22:51:55 -0400 Subject: [PATCH] Improve map management - Addresses #41 --- .../apickledwalrus/skriptgui/gui/GUI.java | 29 ++++++------------- 1 file changed, 9 insertions(+), 20 deletions(-) diff --git a/src/main/java/io/github/apickledwalrus/skriptgui/gui/GUI.java b/src/main/java/io/github/apickledwalrus/skriptgui/gui/GUI.java index 32021e3..4fb20ae 100644 --- a/src/main/java/io/github/apickledwalrus/skriptgui/gui/GUI.java +++ b/src/main/java/io/github/apickledwalrus/skriptgui/gui/GUI.java @@ -30,7 +30,7 @@ public class GUI { @Override public void onClick(InventoryClickEvent e, int slot) { Character realSlot = convert(slot); - Consumer run = getSlot(realSlot); + Consumer run = slots.get(realSlot); /* * Cancel the event if this GUI slot is a button (it runs a consumer) * If it isn't, check whether items are stealable in this GUI, or if the specific slot is stealable @@ -44,7 +44,7 @@ public void onClick(InventoryClickEvent e, int slot) { @Override public void onDrag(InventoryDragEvent e, int slot) { Character realSlot = convert(slot); - Consumer run = getSlot(realSlot); + Consumer run = slots.get(realSlot); /* * Cancel the event if this GUI slot is a button (it runs a consumer) * If it isn't, check whether items are stealable in this GUI, or if the specific slot is stealable @@ -138,11 +138,8 @@ public void setName(@Nullable String name) { } public void clear(Object slot) { - inventory.clear(); Character realSlot = convert(slot); - setItem(realSlot, new ItemStack(Material.AIR), false, null); // It's okay to convert again (it won't really convert) - slots.remove(realSlot); - stealableSlots.remove(realSlot); + setItem(realSlot, new ItemStack(Material.AIR), false, null); } public void clear() { @@ -220,18 +217,6 @@ public Character nextSlotInverted() { return convert(inventory.firstEmpty() - 1); } - /** - * @param ch The slot in Character form. It is assumed that this Character was already converted through {@link GUI#convert(Object)}. - * @return The slot's button consumer, or an emtpty consumer if it does not have one. - */ - @Nullable - public Consumer getSlot(Character ch) { - if (ch > 0 && slots.containsKey(ch)) { - return slots.get(ch); - } - return null; - } - /** * Sets a slot's item. * @param slot The slot to put the item in. It will be converted by {@link GUI#convert(Object)}. @@ -257,11 +242,15 @@ public void setItem(Object slot, @Nullable ItemStack item, boolean stealable, @N rawShape = rawShape.replaceFirst("\\+", "" + ch2); ch = ch2; } - slots.put(ch, consumer); + if (consumer != null) { + slots.put(ch, consumer); + } else { // Just in case as we may be updating a slot + slots.remove(ch); + } if (stealable) { stealableSlots.add(ch); - } else { // Just in case as we may be updating a slot. + } else { // Just in case as we may be updating a slot stealableSlots.remove(ch); }