Skip to content

Commit 4bd9c04

Browse files
committed
Add protection against double clicking item types
1 parent 6599c5f commit 4bd9c04

File tree

2 files changed

+25
-3
lines changed

2 files changed

+25
-3
lines changed

examples/src/main/kotlin/com/noxcrew/interfaces/example/ChangingTitleExampleInterface.kt

+1-1
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ public class ChangingTitleExampleInterface : RegistrableInterface {
3232
number += 1
3333
}
3434

35-
pane[0, 6] = StaticElement(drawable(Material.COMPASS))
35+
pane[0, 6] = StaticElement(drawable(Material.ACACIA_SIGN))
3636
}
3737
}
3838
}

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

+24-2
Original file line numberDiff line numberDiff line change
@@ -265,10 +265,32 @@ public class InterfacesListeners private constructor(private val plugin: Plugin)
265265
}
266266
}
267267

268+
// Prevent double-clicking if it involves stealing any items
269+
val topInventory = event.view.topInventory
270+
val bottomInventory = event.view.bottomInventory
271+
if (event.click == ClickType.DOUBLE_CLICK) {
272+
val clickedItem = event.currentItem ?: ItemStack.empty()
273+
val isInPlayerInventory = holder is Player
274+
275+
// Don't check top inventory if we're in the player inventory!
276+
if (
277+
(!isInPlayerInventory && topInventory.withIndex().any { (index, it) ->
278+
// Check if any item is being collected that cannot be moved!
279+
it.isSimilar(clickedItem) && !canFreelyMove(view, requireNotNull(GridPoint.fromBukkitChestSlot(index)), false)
280+
}) ||
281+
bottomInventory.withIndex().any { (index, it) ->
282+
it.isSimilar(clickedItem) &&
283+
// These slots are always in the player inventory and always need to be relativized!
284+
!canFreelyMove(view, view.backing.relativizePlayerInventorySlot(requireNotNull(GridPoint.fromBukkitPlayerSlot(index))), true)
285+
}
286+
) {
287+
event.isCancelled = true
288+
return
289+
}
290+
}
291+
268292
// If it's a shift click we have to detect what slot is being edited
269293
if (event.click.isShiftClick && event.clickedInventory != null) {
270-
val topInventory = event.view.topInventory
271-
val bottomInventory = event.view.bottomInventory
272294
val clickedInventory = event.clickedInventory!!
273295
val otherInventory = if (clickedInventory == topInventory) bottomInventory else topInventory
274296

0 commit comments

Comments
 (0)