Skip to content

Commit

Permalink
Always use cheaper getHolder(false)
Browse files Browse the repository at this point in the history
Avoids creating copies of the opened block entity
  • Loading branch information
Aeltumn committed Jan 23, 2025
1 parent 980352c commit 387e4f0
Show file tree
Hide file tree
Showing 6 changed files with 14 additions and 14 deletions.
2 changes: 1 addition & 1 deletion build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ val javaVersion: Int = 21

allprojects {
group = "com.noxcrew.interfaces"
version = "1.3.2"
version = "1.3.3-SNAPSHOT"

tasks.withType<JavaCompile> {
sourceCompatibility = javaVersion.toString()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -236,7 +236,7 @@ public class InterfacesListeners private constructor(private val plugin: Plugin)
public fun saveInventoryContentsIfOpened(player: HumanEntity) {
// Saves any persistent items stored in the main inventory whenever we are currently
// showing a combined or player inventory before we draw the new one over-top
val currentlyShown = convertHolderToInterfaceView(player.openInventory.topInventory.holder)
val currentlyShown = convertHolderToInterfaceView(player.openInventory.topInventory.getHolder(false))
?: getBackgroundPlayerInterface(player.uniqueId)
if (currentlyShown != null && currentlyShown !is ChestInterfaceView) {
currentlyShown.savePersistentItems(player.inventory)
Expand All @@ -248,7 +248,7 @@ public class InterfacesListeners private constructor(private val plugin: Plugin)
// Save previous inventory contents before we open the new one
saveInventoryContentsIfOpened(event.player)

val holder = event.inventory.holder
val holder = event.inventory.getHolder(false)
val view = convertHolderToInterfaceView(holder) ?: return

// Move the current open inventory to the background to indicate
Expand All @@ -265,7 +265,7 @@ public class InterfacesListeners private constructor(private val plugin: Plugin)

@EventHandler
public fun onClose(event: InventoryCloseEvent) {
val holder = event.inventory.holder
val holder = event.inventory.getHolder(false)
val view = holder as? AbstractInterfaceView<*, *, *> ?: return
val reason = event.reason

Expand Down Expand Up @@ -294,10 +294,10 @@ public class InterfacesListeners private constructor(private val plugin: Plugin)

@EventHandler(priority = EventPriority.LOW, ignoreCancelled = true)
public fun onClick(event: InventoryClickEvent) {
val holder = event.inventory.holder
val holder = event.inventory.getHolder(false)
val view = convertHolderToInterfaceView(holder) ?: return
val clickedPoint = clickedPoint(view, event) ?: return
val isPlayerInventory = (event.clickedInventory ?: event.inventory).holder is Player
val isPlayerInventory = (event.clickedInventory ?: event.inventory).getHolder(false) is Player

// Run base click handling
if (handleClick(view, clickedPoint, event.click, event.hotbarButton, isPlayerInventory, false)) {
Expand Down Expand Up @@ -375,7 +375,7 @@ public class InterfacesListeners private constructor(private val plugin: Plugin)

// Ideally we predict which slot got shift clicked into! We start by finding any
// stack that this item can be added onto, after that we find the first empty slot.
val isMovingIntoPlayerInventory = otherInventory.holder is Player
val isMovingIntoPlayerInventory = otherInventory.getHolder(false) is Player
val firstEmptySlot = otherInventory.indexOfFirst {
it != null && !it.isEmpty && it.isSimilar(event.currentItem ?: ItemStack.empty())
}.takeIf { it != -1 } ?: otherInventory.indexOfFirst { it == null || it.isEmpty }
Expand Down Expand Up @@ -409,7 +409,7 @@ public class InterfacesListeners private constructor(private val plugin: Plugin)

@EventHandler(priority = EventPriority.LOW, ignoreCancelled = true)
public fun onDrag(event: InventoryDragEvent) {
val holder = event.inventory.holder
val holder = event.inventory.getHolder(false)
val view = convertHolderToInterfaceView(holder) ?: return
for (slot in event.rawSlots) {
val clickedPoint = GridPoint.fromBukkitChestSlot(slot) ?: continue
Expand Down Expand Up @@ -506,7 +506,7 @@ public class InterfacesListeners private constructor(private val plugin: Plugin)
@EventHandler(priority = EventPriority.HIGH, ignoreCancelled = true)
public fun onDeath(event: PlayerDeathEvent) {
// Determine the holder of the top inventory being shown (can be open player inventory)
val view = convertHolderToInterfaceView(event.player.openInventory.topInventory.holder) ?: return
val view = convertHolderToInterfaceView(event.player.openInventory.topInventory.getHolder(false)) ?: return

// Ignore inventories that do not use the player inventory!
if (!view.backing.includesPlayerInventory) return
Expand Down Expand Up @@ -559,7 +559,7 @@ public class InterfacesListeners private constructor(private val plugin: Plugin)

/** Extracts the clicked point from an inventory click event. */
private fun clickedPoint(view: AbstractInterfaceView<*, *, *>, event: InventoryClickEvent): GridPoint? {
if (event.inventory.holder is Player) {
if (event.inventory.getHolder(false) is Player) {
return GridPoint.fromBukkitPlayerSlot(event.slot)?.let { view.backing.relativizePlayerInventorySlot(it) }
}
return GridPoint.fromBukkitChestSlot(event.rawSlot)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public interface Interface<I : Interface<I, P>, P : Pane> {
public suspend fun open(
player: Player,
parent: InterfaceView? =
InterfacesListeners.INSTANCE.convertHolderToInterfaceView(player.openInventory.topInventory.holder)
InterfacesListeners.INSTANCE.convertHolderToInterfaceView(player.openInventory.topInventory.getHolder(false))
): InterfaceView

/** Returns the [gridPoint] relative to the player's inventory within this inventory. */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -443,7 +443,7 @@ public abstract class AbstractInterfaceView<I : InterfacesInventory, T : Interfa
if (!shouldBeOpened.get()) return@runSync

// Save persistent items if the view is currently opened
if (player.openInventory.topInventory.holder == this) {
if (player.openInventory.topInventory.getHolder(false) == this) {
savePersistentItems(player.openInventory.topInventory)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,5 +43,5 @@ public class ChestInterfaceView internal constructor(

override fun getInventory(): Inventory = currentInventory.chestInventory

override fun isOpen(): Boolean = player.openInventory.topInventory.holder == this
override fun isOpen(): Boolean = player.openInventory.topInventory.getHolder(false) == this
}
Original file line number Diff line number Diff line change
Expand Up @@ -44,5 +44,5 @@ public class CombinedInterfaceView internal constructor(

override fun getInventory(): Inventory = currentInventory.chestInventory

override fun isOpen(): Boolean = player.openInventory.topInventory.holder == this
override fun isOpen(): Boolean = player.openInventory.topInventory.getHolder(false) == this
}

0 comments on commit 387e4f0

Please sign in to comment.