如何阻止玩家手持潜影盒时丢弃它?Minecraft插件开发求助
解决手持潜影盒无法阻止丢弃的问题
你的代码无法生效的核心原因是直接用==比较ItemStack对象——在Spigot/Bukkit中,玩家手中的物品栈和即将丢弃的物品栈是不同的实例,哪怕是同一个物品,对象引用也不一样,所以==判断永远不成立。
修正后的代码
@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 // 改用isSimilar()判断物品是否相同(类型、数据、NBT等一致) if (mainHandItem.isSimilar(droppedItem) || offHandItem.isSimilar(droppedItem)) { event.isCancelled = true // Cancel the item drop event player.sendMessage(format("§cYou can't drop the shulker box you are holding!")) } } // 保留原时间戳代码,未做改动 droppedItemTimestamp[player] = System.currentTimeMillis() } private fun isShulkerBox(item: ItemStack): Boolean { return item.type in shulkerBoxTitles.keys }
关键修改说明
- 把
mainHandItem == droppedItem替换为mainHandItem.isSimilar(droppedItem):isSimilar()是Bukkit提供的方法,用于判断两个ItemStack是否是“相同的物品”(包括类型、耐久、NBT标签等),这才是正确的物品比较方式。 - 如果你的需求简化为“只要玩家手持潜影盒,就不能丢弃任何潜影盒”,可以进一步优化判断逻辑:直接检查主手/副手是否是潜影盒,不需要和丢弃的物品比较,逻辑更简洁:
if (isShulkerBox(mainHandItem) || isShulkerBox(offHandItem)) { event.isCancelled = true player.sendMessage(format("§cYou can't drop shulker boxes while holding one!")) }
内容的提问来源于stack exchange,提问作者xuanlam2007
相关产品推荐
相关产品推荐

