Skip to content

Commit dda8a37

Browse files
committed
Fix up inherited items and shift-click moving in player inventories
1 parent ae089a5 commit dda8a37

File tree

2 files changed

+47
-9
lines changed

2 files changed

+47
-9
lines changed

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

Lines changed: 45 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ import org.bukkit.event.inventory.InventoryCloseEvent
3838
import org.bukkit.event.inventory.InventoryCloseEvent.Reason
3939
import org.bukkit.event.inventory.InventoryDragEvent
4040
import org.bukkit.event.inventory.InventoryOpenEvent
41+
import org.bukkit.event.inventory.InventoryType
4142
import org.bukkit.event.player.PlayerDropItemEvent
4243
import org.bukkit.event.player.PlayerInteractEvent
4344
import org.bukkit.event.player.PlayerQuitEvent
@@ -433,17 +434,52 @@ public class InterfacesListeners private constructor(private val plugin: Plugin)
433434
// If it's a shift click we have to detect what slot is being edited
434435
if (event.click.isShiftClick && event.clickedInventory != null) {
435436
val clickedInventory = event.clickedInventory!!
436-
val otherInventory = if (clickedInventory == topInventory) bottomInventory else topInventory
437+
var specialInventory = false
438+
val otherInventory =
439+
if (clickedInventory == topInventory) {
440+
bottomInventory
441+
} else if (topInventory.type == InventoryType.CRAFTING) {
442+
specialInventory = true
443+
bottomInventory
444+
} else {
445+
topInventory
446+
}
437447

438448
// Ideally we predict which slot got shift clicked into! We start by finding any
439449
// stack that this item can be added onto, after that we find the first empty slot.
440450
val isMovingIntoPlayerInventory = otherInventory.getHolder(false) is Player
441-
val firstEmptySlot = otherInventory.indexOfFirst {
442-
it != null && !it.isEmpty && it.isSimilar(event.currentItem ?: ItemStack.empty())
443-
}.takeIf { it != -1 } ?: otherInventory.indexOfFirst { it == null || it.isEmpty }
451+
val clickedItem = event.currentItem ?: ItemStack.empty()
452+
val firstEmptySlot = if (specialInventory) {
453+
// If this is the player inventory you tab between the hotbar and the inventory's insides
454+
val allowedIndices = if (clickedPoint.x != 3) {
455+
// Moving into hotbar
456+
otherInventory.contents.indices.filter { it < 9 }
457+
} else {
458+
// Moving into main inventory
459+
otherInventory.contents.indices.filter { it in 9 until 36 }
460+
}
461+
462+
allowedIndices.firstOrNull { index ->
463+
val it = otherInventory.contents[index]
464+
it != null && !it.isEmpty && it.isSimilar(clickedItem)
465+
}.takeIf { it != -1 } ?: allowedIndices.firstOrNull { index ->
466+
val it = otherInventory.contents[index]
467+
it == null || it.isEmpty
468+
} ?: -1
469+
} else {
470+
otherInventory.indexOfFirst {
471+
it != null && !it.isEmpty && it.isSimilar(clickedItem)
472+
}.takeIf { it != -1 } ?: otherInventory.indexOfFirst { it == null || it.isEmpty }
473+
}
444474

445475
if (firstEmptySlot != -1) {
446-
val targetSlot = requireNotNull(GridPoint.fromBukkitChestSlot(firstEmptySlot))
476+
val targetSlot = requireNotNull(
477+
if (isMovingIntoPlayerInventory) {
478+
GridPoint.fromBukkitPlayerSlot(firstEmptySlot)
479+
} else {
480+
GridPoint.fromBukkitChestSlot(firstEmptySlot)
481+
},
482+
)
447483

448484
if (!canFreelyMove(
449485
view,
@@ -664,10 +700,12 @@ public class InterfacesListeners private constructor(private val plugin: Plugin)
664700
}
665701

666702
// If this inventory has no player inventory then the player inventory is always allowed to be edited
667-
if (!view.backing.includesPlayerInventory && isPlayerInventory) return true
703+
if (!view.backing.includesPlayerInventory && isPlayerInventory) {
704+
return true
705+
}
668706

669707
// If there is no item here we allow editing
670-
return view.completedPane?.getRaw(clickedPoint) == null
708+
return view.completedPane?.getRaw(clickedPoint)?.itemStack == null
671709
}
672710

673711
/** Handles a [view] being clicked at [clickedPoint] through some [event]. */

interfaces/src/main/kotlin/com/noxcrew/interfaces/view/AbstractInterfaceView.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -823,8 +823,8 @@ public abstract class AbstractInterfaceView<I : InterfacesInventory, T : Interfa
823823
// If the menu has since been requested to close we ignore all this
824824
if (!shouldBeOpened.get()) return@executeSync
825825

826-
// Save persistent items if the view is currently opened
827-
if (isOpen() && builder.inheritExistingItems) {
826+
// Save persistent items before we render
827+
if (builder.inheritExistingItems) {
828828
savePersistentItems(player.inventory)
829829
}
830830

0 commit comments

Comments
 (0)