0

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?

Here's the code...

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

Lucan
  • 2,907
  • 2
  • 16
  • 30
  • If you don't think it is possible then say so – GD Vicious bee Apr 27 '21 at 06:00
  • If you are using spigot, and are using a version which supports PersistentDataHolder, then you should check this out: https://www.spigotmc.org/threads/tutorial-storing-custom-data-on-itemstacks-in-1-13-2.354283/#post-3272155 https://www.spigotmc.org/threads/a-guide-to-1-14-persistentdataholder-api.371200/ E.g. use this to store some sort of data on the item, marking it as your nuke, and in the block place event you can check if the material matches, then check if it has the correct persistent data. – Rishaan Gupta Apr 27 '21 at 13:00
  • You should also expand on your question a bit more, what exactly isn't working with your code? Is the event not triggering, is the actual checking that it is a nuke not working, or is there a problem with the explosion. If you don't know, try putting in some breakpoints and stepping through your code, or put in some System.out.println messages, so you know where the problem is. – Rishaan Gupta Apr 27 '21 at 13:08

1 Answers1

1

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.

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

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.

Lucan
  • 2,907
  • 2
  • 16
  • 30