Skip to content

Commit

Permalink
Fix player interface drawing when not open
Browse files Browse the repository at this point in the history
  • Loading branch information
Aeltumn committed Oct 25, 2023
1 parent 1189f8b commit 4da8999
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,5 @@ public class PlayerInterfacesInventory(
return playerInventory.setItem(index, item)
}

// Nothing in here counts as the player inventory since this is a fake
// inventory. The purpose of this method is to find which slots should
// not be drawn to until the InventoryOpenEvent, but since this is a fake
// inventory it is never opened this this needs to be false.
override fun isPlayerInventory(row: Int, column: Int): Boolean = false
override fun isPlayerInventory(row: Int, column: Int): Boolean = true
}
Original file line number Diff line number Diff line change
Expand Up @@ -211,12 +211,14 @@ public abstract class AbstractInterfaceView<I : InterfacesInventory, P : Pane>(
semaphore.release()
}

protected open fun drawPaneToInventory(opened: Boolean) {
protected open fun drawPaneToInventory(drawNormalInventory: Boolean, drawPlayerInventory: Boolean) {
var madeChanges = false
pane.forEach { row, column, element ->
// We defer drawing of any elements in the player inventory itself
// for later unless the inventory is already open.
if (!opened && currentInventory.isPlayerInventory(row, column)) return@forEach
val isPlayerInventory = currentInventory.isPlayerInventory(row, column)
if (!isPlayerInventory && drawNormalInventory || isPlayerInventory && drawPlayerInventory) return@forEach

currentInventory.set(row, column, element.itemStack.apply { this?.let { backing.itemPostProcessor?.invoke(it) } })
madeChanges = true
}
Expand All @@ -229,15 +231,7 @@ public abstract class AbstractInterfaceView<I : InterfacesInventory, P : Pane>(
// Whenever we open the inventory we draw all elements in the player inventory
// itself. We do this in this hook because it runs after InventoryCloseEvent so
// it properly happens as the last possible action.
var madeChanges = false
pane.forEach { row, column, element ->
if (!currentInventory.isPlayerInventory(row, column)) return@forEach
currentInventory.set(row, column, element.itemStack.apply { this?.let { backing.itemPostProcessor?.invoke(it) } })
madeChanges = true
}
if (madeChanges) {
Bukkit.getPluginManager().callEvent(DrawPaneEvent(player))
}
drawPaneToInventory(drawNormalInventory = false, drawPlayerInventory = true)
}

protected open fun requiresNewInventory(): Boolean = firstPaint
Expand All @@ -263,7 +257,7 @@ public abstract class AbstractInterfaceView<I : InterfacesInventory, P : Pane>(
// updates on menus that have closed do not affect future menus that actually
// ended up being opened.
val isOpen = isOpen(player)
drawPaneToInventory(isOpen)
drawPaneToInventory(drawNormalInventory = true, drawPlayerInventory = isOpen)
callback(createdInventory)

if ((openIfClosed && !isOpen) || createdInventory) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,8 @@ public class PlayerInterfaceView internal constructor(
}
player.openInventory.cursor = null

// Trigger onOpen manually because there is no real inventory being opened
// Trigger onOpen manually because there is no real inventory being opened,
// this will also re-draw the player inventory parts!
onOpen()
}
}
Expand Down

0 comments on commit 4da8999

Please sign in to comment.