Added state watch functionality
This commit is contained in:
@@ -27,13 +27,23 @@ abstract class InventoryGUI<T : ForInventory>(
|
|||||||
|
|
||||||
internal abstract val bukkitInventory: Inventory
|
internal abstract val bukkitInventory: Inventory
|
||||||
|
|
||||||
|
internal abstract fun loadPageUnsafe(
|
||||||
|
page: Int,
|
||||||
|
offsetHorizontally: Int = 0,
|
||||||
|
offsetVertically: Int = 0
|
||||||
|
)
|
||||||
|
|
||||||
internal abstract fun loadPageUnsafe(
|
internal abstract fun loadPageUnsafe(
|
||||||
page: InventoryGUIPage<*>,
|
page: InventoryGUIPage<*>,
|
||||||
offsetHorizontally: Int = 0,
|
offsetHorizontally: Int = 0,
|
||||||
offsetVertically: 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.
|
* @return True, if the [inventory] belongs to this GUI.
|
||||||
@@ -85,29 +95,47 @@ class InventoryGUIShared<T : ForInventory>(
|
|||||||
|
|
||||||
override fun isThisInv(inventory: Inventory) = inventory == bukkitInventory
|
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) {
|
override fun loadPageUnsafe(page: InventoryGUIPage<*>, offsetHorizontally: Int, offsetVertically: Int) {
|
||||||
|
|
||||||
val ifOffset = offsetHorizontally != 0 || offsetVertically != 0
|
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
|
currentPageInt = page.number
|
||||||
|
|
||||||
page.slots.let { slots ->
|
}
|
||||||
|
|
||||||
|
loadContent(page.slots, offsetHorizontally, offsetVertically)
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun loadContent(
|
||||||
|
content: Map<Int, InventoryGUISlot<*>>,
|
||||||
|
offsetHorizontally: Int,
|
||||||
|
offsetVertically: Int
|
||||||
|
) {
|
||||||
|
|
||||||
|
val ifOffset = offsetHorizontally != 0 || offsetVertically != 0
|
||||||
|
|
||||||
val dimensions = data.inventoryType.dimensions
|
val dimensions = data.inventoryType.dimensions
|
||||||
|
|
||||||
|
// clear the space which will be redefined
|
||||||
if (ifOffset) {
|
if (ifOffset) {
|
||||||
dimensions.invSlots.forEach {
|
dimensions.invSlots.forEach {
|
||||||
dimensions.invSlotsWithRealSlots[it.add(offsetHorizontally, offsetVertically)]?.let { slotToClear ->
|
val slotToClear = dimensions.invSlotsWithRealSlots[it.add(offsetHorizontally, offsetVertically)]
|
||||||
if (dimensions.realSlots.contains(slotToClear))
|
if (slotToClear != null) bukkitInventory.clear(slotToClear)
|
||||||
bukkitInventory.clear(slotToClear)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
bukkitInventory.clear()
|
|
||||||
}
|
}
|
||||||
|
} else bukkitInventory.clear()
|
||||||
|
|
||||||
|
content.forEach {
|
||||||
|
|
||||||
slots.forEach {
|
|
||||||
val slot = it.value
|
val slot = it.value
|
||||||
if (slot is InventoryGUIElement) {
|
if (slot is InventoryGUIElement) {
|
||||||
|
|
||||||
@@ -115,11 +143,11 @@ class InventoryGUIShared<T : ForInventory>(
|
|||||||
val invSlot = InventorySlot.fromRealSlot(it.key, dimensions)
|
val invSlot = InventorySlot.fromRealSlot(it.key, dimensions)
|
||||||
if (invSlot != null) {
|
if (invSlot != null) {
|
||||||
val offsetSlot = invSlot.add(offsetHorizontally, offsetVertically).realSlotIn(dimensions)
|
val offsetSlot = invSlot.add(offsetHorizontally, offsetVertically).realSlotIn(dimensions)
|
||||||
if (offsetSlot != null)
|
if (offsetSlot != null) bukkitInventory.setItem(offsetSlot, slot.getItemStack(offsetSlot))
|
||||||
bukkitInventory.setItem(offsetSlot, slot.getItemStack(offsetSlot))
|
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
bukkitInventory.setItem(it.key, slot.getItemStack(it.key))
|
bukkitInventory.setItem(it.key, slot.getItemStack(it.key))
|
||||||
|
slot.startUsing(this)
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
@@ -128,12 +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) {
|
override operator fun set(slot: InventorySlotCompound<T>, value: ItemStack) {
|
||||||
slot.realSlotsWithInvType(data.inventoryType).forEach {
|
slot.realSlotsWithInvType(data.inventoryType).forEach {
|
||||||
bukkitInventory.setItem(it, value)
|
bukkitInventory.setItem(it, value)
|
||||||
|
@@ -19,4 +19,7 @@ abstract class InventoryGUIElement<T : ForInventory> : InventoryGUISlot<T>() {
|
|||||||
|
|
||||||
protected abstract fun onClickElement(clickEvent: InventoryGUIClickEvent<T>)
|
protected abstract fun onClickElement(clickEvent: InventoryGUIClickEvent<T>)
|
||||||
|
|
||||||
|
internal open fun startUsing(gui: InventoryGUI<*>) { }
|
||||||
|
internal open fun stopUsing(gui: InventoryGUI<*>) { }
|
||||||
|
|
||||||
}
|
}
|
Reference in New Issue
Block a user