From 1fc55f47c78f499a34247b418b1007ee5f4e52b0 Mon Sep 17 00:00:00 2001 From: bluefireoly Date: Wed, 21 Oct 2020 21:28:50 +0200 Subject: [PATCH] Simplified usage state watching --- .../net/axay/kspigot/inventory/InventoryGUI.kt | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-) diff --git a/src/main/kotlin/net/axay/kspigot/inventory/InventoryGUI.kt b/src/main/kotlin/net/axay/kspigot/inventory/InventoryGUI.kt index b049faed..f8a4e6b3 100644 --- a/src/main/kotlin/net/axay/kspigot/inventory/InventoryGUI.kt +++ b/src/main/kotlin/net/axay/kspigot/inventory/InventoryGUI.kt @@ -29,6 +29,8 @@ abstract class InventoryGUI( internal var isInMove: Boolean = false + internal val currentElements = HashSet>() + internal abstract fun loadPageUnsafe( page: Int, offsetHorizontally: Int = 0, @@ -116,7 +118,14 @@ class InventoryGUIShared( if (!ifOffset) { // unregister this inv from all elements on the previous page - HashSet(currentPage.slots.values).forEach { if (it is InventoryGUIElement) it.stopUsing(this) } + currentElements.forEach { it.stopUsing(this) } + currentElements.clear() + + // register this inv for all new elements + HashSet(page.slots.values).forEach { if (it is InventoryGUIElement) { + currentElements += it + it.startUsing(this) + } } currentPageInt = page.number @@ -144,6 +153,7 @@ class InventoryGUIShared( } } else bukkitInventory.clear() + // render the given content content.forEach { val slot = it.value @@ -155,10 +165,7 @@ class InventoryGUIShared( val offsetSlot = invSlot.add(offsetHorizontally, offsetVertically).realSlotIn(dimensions) if (offsetSlot != null) bukkitInventory.setItem(offsetSlot, slot.getItemStack(offsetSlot)) } - } else { - bukkitInventory.setItem(it.key, slot.getItemStack(it.key)) - slot.startUsing(this) - } + } else bukkitInventory.setItem(it.key, slot.getItemStack(it.key)) }