【问题标题】:Custom explosion after BlockPlaceEventBlockPlaceEvent 之后的自定义爆炸
【发布时间】:2021-04-27 01:22:46
【问题描述】:

所以我试图在 Minecraft 中制造核弹,因此我尝试在放置时制作自定义 TNT 方块,但我似乎无法触发在方块位置创建爆炸的动作。我可以帮忙吗?

这是代码...

package com.TheRealBee.Bows.Event9;

import org.bukkit.ChatColor;
import org.bukkit.Location;
import org.bukkit.block.Block;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.block.BlockPlaceEvent;

public class EventManager9 implements Listener {
        @EventHandler
        public static void onBlockPlace(BlockPlaceEvent e) {

            Block b = e.getBlock();
            Location blocklocation = b.getLocation();
            if (e.getBlockPlaced().equals(ChatColor.AQUA+"Nuclear bomb")){
                blocklocation.getWorld().createExplosion(blocklocation, 5, true);
            }
        }
    }

【问题讨论】:

  • 如果你认为不可能,那么就说出来
  • 如果您正在使用 spigot,并且正在使用支持 PersistentDataHolder 的版本,那么您应该检查一下:spigotmc.org/threads/…spigotmc.org/threads/… E.g.使用它在物品上存储某种数据,将其标记为您的核弹,并在方块放置事件中检查材料是否匹配,然后检查它是否具有正确的持久数据。
  • 您还应该进一步扩展您的问题,您的代码到底有什么问题?事件是否未触发,是否实际检查它是否是核弹不起作用,或者爆炸是否存在问题。如果您不知道,请尝试放置一些断点并单步执行您的代码,或者放置一些 System.out.println 消息,这样您就知道问题出在哪里了。
  • 好的,感谢您的反馈!

标签: events event-handling minecraft bukkit


【解决方案1】:

您的问题是您正在检查 Blocke.getBlockPlaced() 的结果)和字符串之间的相等性。这两个永远不会相等,因此不满足您的条件。

您可以更改条件,以便在放置方块时检查玩家手中的ItemStack。您也没有检查块类型,因此我在下面的示例代码中添加了对 TNT 的检查,但您可以删除它以使其与具有自定义名称的任何块一起使用。

    @EventHandler
    public void onNukePlace(BlockPlaceEvent e){
        // Return if it's not TNT, doesn't have ItemMeta or doesn't have a custom dispaly name
        if(!e.getBlock().getType().equals(Material.TNT) || !e.getItemInHand().hasItemMeta() || !e.getItemInHand().getItemMeta().hasDisplayName())
            return;
        // Return if the item display name is not correct
        if(!e.getItemInHand().getItemMeta().getDisplayName().equals(ChatColor.AQUA + "Nuclear bomb"))
            return;
        // Create the explosion
        e.getBlock().getLocation().getWorld().createExplosion(e.getBlock().getLocation(), 5, true);
    }

但是,这会导致爆炸在放置时立即发生,如果不需要,您可以使用类似runTaskLater 的可运行文件。您可能希望手动移除玩家放置的方块,例如,您使用基岩制造“核弹”,爆炸不会消除它。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2010-12-18
    • 2016-06-26
    • 1970-01-01
    • 2023-01-20
    • 2021-01-20
    • 1970-01-01
    • 1970-01-01
    • 2013-04-22
    相关资源
    最近更新 更多