diff --git a/src/main/kotlin/net/axay/kspigot/inventory/InventoryGUI.kt b/src/main/kotlin/net/axay/kspigot/inventory/InventoryGUI.kt index 6a17488a..5e1a7056 100644 --- a/src/main/kotlin/net/axay/kspigot/inventory/InventoryGUI.kt +++ b/src/main/kotlin/net/axay/kspigot/inventory/InventoryGUI.kt @@ -27,13 +27,23 @@ abstract class InventoryGUI( internal abstract val bukkitInventory: Inventory + internal abstract fun loadPageUnsafe( + page: Int, + offsetHorizontally: Int = 0, + offsetVertically: Int = 0 + ) + internal abstract fun loadPageUnsafe( page: InventoryGUIPage<*>, offsetHorizontally: Int = 0, offsetVertically: Int = 0 ) - internal abstract fun loadPageUnsafe(page: Int, offsetHorizontally: Int = 0, offsetVertically: Int = 0) + internal abstract fun loadContent( + content: Map>, + offsetHorizontally: Int = 0, + offsetVertically: Int = 0 + ) /** * @return True, if the [inventory] belongs to this GUI. @@ -85,43 +95,59 @@ class InventoryGUIShared( override fun isThisInv(inventory: Inventory) = inventory == bukkitInventory + override fun loadPageUnsafe(page: Int, offsetHorizontally: Int, offsetVertically: Int) { + data.pages[page]?.let { loadPageUnsafe(it, offsetHorizontally, offsetVertically) } + } + override fun loadPageUnsafe(page: InventoryGUIPage<*>, offsetHorizontally: Int, offsetVertically: Int) { val ifOffset = offsetHorizontally != 0 || offsetVertically != 0 - if (!ifOffset) + if (!ifOffset) { + + // unregister this inv from all elements on the previous page + HashSet(currentPage.slots.values).forEach { if (it is InventoryGUIElement) it.stopUsing(this) } + currentPageInt = page.number - page.slots.let { slots -> + } - val dimensions = data.inventoryType.dimensions + loadContent(page.slots, offsetHorizontally, offsetVertically) - if (ifOffset) { - dimensions.invSlots.forEach { - dimensions.invSlotsWithRealSlots[it.add(offsetHorizontally, offsetVertically)]?.let { slotToClear -> - if (dimensions.realSlots.contains(slotToClear)) - bukkitInventory.clear(slotToClear) - } - } - } else { - bukkitInventory.clear() + } + + override fun loadContent( + content: Map>, + offsetHorizontally: Int, + offsetVertically: Int + ) { + + val ifOffset = offsetHorizontally != 0 || offsetVertically != 0 + + val dimensions = data.inventoryType.dimensions + + // clear the space which will be redefined + if (ifOffset) { + dimensions.invSlots.forEach { + val slotToClear = dimensions.invSlotsWithRealSlots[it.add(offsetHorizontally, offsetVertically)] + if (slotToClear != null) bukkitInventory.clear(slotToClear) } + } else bukkitInventory.clear() - slots.forEach { - val slot = it.value - if (slot is InventoryGUIElement) { + content.forEach { - if (ifOffset) { - val invSlot = InventorySlot.fromRealSlot(it.key, dimensions) - if (invSlot != null) { - 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)) + val slot = it.value + if (slot is InventoryGUIElement) { + + if (ifOffset) { + val invSlot = InventorySlot.fromRealSlot(it.key, dimensions) + if (invSlot != null) { + 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) } } @@ -130,10 +156,6 @@ class InventoryGUIShared( } - override fun loadPageUnsafe(page: Int, offsetHorizontally: Int, offsetVertically: Int) { - data.pages[page]?.let { loadPageUnsafe(it, offsetHorizontally, offsetVertically) } - } - override operator fun set(slot: InventorySlotCompound, value: ItemStack) { slot.realSlotsWithInvType(data.inventoryType).forEach { bukkitInventory.setItem(it, value) diff --git a/src/main/kotlin/net/axay/kspigot/inventory/InventoryGUIElements.kt b/src/main/kotlin/net/axay/kspigot/inventory/InventoryGUIElements.kt index da06e632..5e25a874 100644 --- a/src/main/kotlin/net/axay/kspigot/inventory/InventoryGUIElements.kt +++ b/src/main/kotlin/net/axay/kspigot/inventory/InventoryGUIElements.kt @@ -19,4 +19,7 @@ abstract class InventoryGUIElement : InventoryGUISlot() { protected abstract fun onClickElement(clickEvent: InventoryGUIClickEvent) + internal open fun startUsing(gui: InventoryGUI<*>) { } + internal open fun stopUsing(gui: InventoryGUI<*>) { } + } \ No newline at end of file