Skip to content

Commit 9acd92e

Browse files
committed
fix: Fix player inventories breaking when opening vanilla containers
Items would stop functioning when in a chest, furnace, or horse's GUI
1 parent 20a5281 commit 9acd92e

File tree

2 files changed

+26
-24
lines changed

2 files changed

+26
-24
lines changed

build.gradle.kts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ subprojects {
5454

5555
// Configure any existing RunServerTasks
5656
tasks.withType<RunServer> {
57-
minecraftVersion("1.21.5")
57+
minecraftVersion("1.21.8")
5858
jvmArgs("-Dio.papermc.paper.suppress.sout.nags=true")
5959
}
6060

interfaces/src/main/kotlin/com/noxcrew/interfaces/InterfacesListeners.kt

Lines changed: 25 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -283,7 +283,7 @@ public class InterfacesListeners private constructor(private val plugin: Plugin)
283283
saveInventoryContentsIfOpened(event.player)
284284

285285
val holder = event.inventory.getHolder(false)
286-
val view = convertHolderToInterfaceView(holder) ?: return
286+
val view = convertHolderToInterfaceView(holder)
287287

288288
// Close the previous view first with open new as the reason, unless we
289289
// are currently opening this view!
@@ -309,17 +309,19 @@ public class InterfacesListeners private constructor(private val plugin: Plugin)
309309
}
310310
}
311311

312-
// Set the new open inventory
313-
openInventory[event.player] = view
314-
315-
// If there is an open view left, destroy it as something went wrong!
316-
withoutReopen {
317-
openPlayerInterfaceViews.remove(event.player.uniqueId)?.markClosed(SCOPE, Reason.OPEN_NEW)
312+
// If the view is not owned by interfaces, deal with that properly!
313+
if (view == null) {
314+
openInventory -= event.player
315+
} else {
316+
// Set the new open inventory
317+
openInventory[event.player] = view
318318
}
319319

320320
// Abort any previous query the player had
321321
abortQuery(event.player.uniqueId, null)
322-
view.onOpen()
322+
323+
// Handle opening the new view
324+
view?.onOpen()
323325
}
324326

325327
@EventHandler
@@ -344,16 +346,23 @@ public class InterfacesListeners private constructor(private val plugin: Plugin)
344346
if (opened != null) {
345347
opened.markClosed(SCOPE, reason)
346348
} else if (reason in REOPEN_REASONS && !event.player.isDead) {
347-
// If the opened menu didn't trigger a re-open, do it manually!
349+
// If the opened menu didn't trigger a re-open, do it manually! This is necessary if it wasn't
350+
// an interfaces inventory but instead a crafting table, furnace, horse, etc.
348351
reopenInventory(event.player as Player)
349352
}
350353
}
351354

352355
@EventHandler(priority = EventPriority.LOW, ignoreCancelled = true)
353356
public fun onClick(event: InventoryClickEvent) {
354-
val holder = event.inventory.getHolder(false)
355-
val view = convertHolderToInterfaceView(holder) ?: return
356-
val clickedPoint = view.backing.mapper.toGridPoint(event.rawSlot) ?: return
357+
// Try to get the view of the top inventory first but fall back to the clicked inventory in specific
358+
val mainView = convertHolderToInterfaceView(event.inventory.getHolder(false))
359+
val view = mainView ?: convertHolderToInterfaceView(
360+
(event.clickedInventory ?: event.inventory).getHolder(false),
361+
) ?: return
362+
363+
// If we are using a fallback player view we need to adjust the raw slot to include the 9 regular player inventory top slots
364+
val clickedPoint =
365+
view.backing.mapper.toGridPoint(if (mainView == null) (event.rawSlot - event.inventory.size + 9) else event.rawSlot) ?: return
357366
val isPlayerInventory = (event.clickedInventory ?: event.inventory).getHolder(false) is Player
358367

359368
// Run base click handling
@@ -396,7 +405,7 @@ public class InterfacesListeners private constructor(private val plugin: Plugin)
396405
val bottomInventory = event.view.bottomInventory
397406
if (event.click == ClickType.DOUBLE_CLICK) {
398407
val clickedItem = event.cursor
399-
val isInPlayerInventory = holder is Player
408+
val isInPlayerInventory = event.inventory.getHolder(false) is Player
400409

401410
// Don't check top inventory if we're in the player inventory!
402411
if (
@@ -666,14 +675,6 @@ public class InterfacesListeners private constructor(private val plugin: Plugin)
666675
event.isCancelled = true
667676
}
668677

669-
/** Extracts the clicked point from an inventory click event. */
670-
private fun clickedPoint(view: AbstractInterfaceView<*, *, *>, event: InventoryClickEvent): GridPoint? {
671-
if (event.inventory.getHolder(false) is Player) {
672-
return GridPoint.fromBukkitPlayerSlot(event.slot)?.let { view.backing.relativizePlayerInventorySlot(it) }
673-
}
674-
return GridPoint.fromBukkitChestSlot(event.rawSlot)
675-
}
676-
677678
/**
678679
* Converts an inventory holder to an [AbstractInterfaceView] if possible. If the holder is a player
679680
* their currently open player interface is returned.
@@ -684,8 +685,9 @@ public class InterfacesListeners private constructor(private val plugin: Plugin)
684685
// If it's an abstract view use that one
685686
if (holder is AbstractInterfaceView<*, *, *>) return holder
686687

687-
// If it's the player's own inventory use the held one
688-
if (holder is HumanEntity) return getOpenPlayerInterface(holder.uniqueId)
688+
// If it's the player's own inventory use the held one, it might be the background one if
689+
// this is the bottom of a combined inventory
690+
if (holder is HumanEntity) return getOpenPlayerInterface(holder.uniqueId) ?: getBackgroundPlayerInterface(holder.uniqueId)
689691

690692
return null
691693
}

0 commit comments

Comments
 (0)