如何防止用户掉落潜影盒?

问题描述 投票:0回答:2

我目前正在为我的 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()
}

但是没有用,有人可以帮助我吗?只是不要弄乱我的时间戳。

kotlin maven plugins minecraft
2个回答
0
投票
  1. 您无需检查任何一只手中的物品。只需检查掉落的物品是否是潜影盒(正如您所做的那样)
  2. 我们没有看到您的 ShulkerBoxTitles.keys,因此可能存在错误。
  3. 确保插件正在运行并且监听器已正确注册。

0
投票

问题在于库存在调用 PlayerDropEvent 之前更新。因此,当玩家从快捷栏中删除物品时,条件

mainHandItem == droppedItem || offHandItem == droppedItem
将始终评估为 false。

您可以尝试采取一些技巧来绕过该限制,但没有干净的方法来处理该问题,您最好找到一种解决方案来解决您的问题,并且不允许仅丢弃未持有的物品。

© www.soinside.com 2019 - 2024. All rights reserved.