Added state watch functionality

This commit is contained in:
bluefireoly
2020-10-21 17:59:34 +02:00
parent 014ee6e279
commit ff7c5940d8
2 changed files with 55 additions and 30 deletions

View File

@@ -27,13 +27,23 @@ abstract class InventoryGUI<T : ForInventory>(
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<Int, InventoryGUISlot<*>>,
offsetHorizontally: Int = 0,
offsetVertically: Int = 0
)
/**
* @return True, if the [inventory] belongs to this GUI.
@@ -85,43 +95,59 @@ class InventoryGUIShared<T : ForInventory>(
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<Int, InventoryGUISlot<*>>,
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<T : ForInventory>(
}
override fun loadPageUnsafe(page: Int, offsetHorizontally: Int, offsetVertically: Int) {
data.pages[page]?.let { loadPageUnsafe(it, offsetHorizontally, offsetVertically) }
}
override operator fun set(slot: InventorySlotCompound<T>, value: ItemStack) {
slot.realSlotsWithInvType(data.inventoryType).forEach {
bukkitInventory.setItem(it, value)

View File

@@ -19,4 +19,7 @@ abstract class InventoryGUIElement<T : ForInventory> : InventoryGUISlot<T>() {
protected abstract fun onClickElement(clickEvent: InventoryGUIClickEvent<T>)
internal open fun startUsing(gui: InventoryGUI<*>) { }
internal open fun stopUsing(gui: InventoryGUI<*>) { }
}