BlockPlaceEvent 后的自定义爆炸 [英] Custom explosion after BlockPlaceEvent

查看:66
本文介绍了BlockPlaceEvent 后的自定义爆炸的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

So I'm trying to make a nuclear bomb in Minecraft so I tried to make a custom TNT block on placement, but I can't seem to trigger the action of creating the explosion at the block location. May I have some help?

这是代码...

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);
            }
        }
    }

推荐答案

您的问题是您正在检查 Block(e.getBlockPlaced()) 和一个字符串.这两者永远不会相等,因此不满足您的条件.

Your issue is that you're checking for equality between a Block (the result of e.getBlockPlaced()) and a string. These two will never be equal and so your condition is not met.

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

You could change your condition so that it checks the ItemStack in the player's hand when the block was placed. You also didn't check for the block type and so I've added a check for TNT in my example code below but you can just remove that for it to work with any block with the custom name.

    @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.您可能希望手动移除玩家放置的方块,例如,您使用基岩制作核弹",爆炸不会消除它.

However, this will cause the explosion to happen instantaneously on placement, if that's not desired, you can use a runnable like runTaskLater. You may wish to manually remove the block that the player placed as if, for example, you make your 'Nuclear bomb' using bedrock, the explosion won't get rid of it.

这篇关于BlockPlaceEvent 后的自定义爆炸的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆