我目前正在为我的 Minecraft 服务器开发一个新插件。 我有这个代码:
@EventHandler
fun onPlayerDropItem(event: PlayerDropItemEvent) {
// Record the timestamp when a player drops an item
droppedItemTimestamp[event.player] = System.currentTimeMillis()
}
private fun isShulkerBox(item: ItemStack): Boolean {
return item.type in shulkerBoxTitles.keys
}
所以基本上我想防止用户在手上拿着潜影盒(任何类型的潜影盒)时掉落它。否则,就让他们放弃吧。
我尝试过:
@EventHandler
fun onPlayerDropItem(event: PlayerDropItemEvent) {
val player = event.player
val droppedItem = event.itemDrop.itemStack
// Check if the dropped item is a shulker box
if (isShulkerBox(droppedItem)) {
val mainHandItem = player.inventory.itemInMainHand
val offHandItem = player.inventory.itemInOffHand
// Check if the player is holding the shulker box in either hand
if (mainHandItem == droppedItem || offHandItem == droppedItem) {
event.isCancelled = true // Cancel the item drop event
player.sendMessage(format("§cYou can't drop the shulker box you are holding!"))
}
}
// Record the timestamp when a player drops any item
droppedItemTimestamp[player] = System.currentTimeMillis()
}
但是没有用,有人可以帮助我吗?只是不要弄乱我的时间戳。
问题在于库存在调用 PlayerDropEvent 之前更新。因此,当玩家从快捷栏中删除物品时,条件
mainHandItem == droppedItem || offHandItem == droppedItem
将始终评估为 false。
您可以尝试采取一些技巧来绕过该限制,但没有干净的方法来处理该问题,您最好找到一种解决方案来解决您的问题,并且不允许仅丢弃未持有的物品。