Question

Well to start off, I am new to Java. This is my first ever bukkit plugin and the only error is this (Click Here). The goal of the plugin is that when you right click with the "Bedrock Breaker", it breaks the bedrock.

package me.jrneulight.bedrockbreaker;


import org.bukkit.ChatColor;
import org.bukkit.Material;
import org.bukkit.command.Command;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority;
import org.bukkit.event.Listener;
import org.bukkit.event.block.BlockBreakEvent;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.PlayerInventory;
import org.bukkit.inventory.meta.ItemMeta;
import org.bukkit.plugin.java.JavaPlugin;

public final class Main extends JavaPlugin implements Listener {
    public static void main(String[] args) {

    }
    @Override
    public void onEnable() {
        getLogger().info("Breakrock Breaker Enabled!");
        getServer().getPluginManager().registerEvents(this, this);
    }
    @Override
    public void onDisable() {
        getLogger().info("Bedrock Breaker Disabled!");
    }
    @Override
    public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args){
        if(cmd.getName().equalsIgnoreCase("bedrockbreaker")){ 
            Player player = (Player) sender;
            PlayerInventory inventory = player.getInventory();

            ItemStack bedrockbreaker = new ItemStack(Material.DIAMOND_HOE,1);

            ItemMeta im = bedrockbreaker.getItemMeta();

            im.setDisplayName(ChatColor.DARK_AQUA + "Bedrock Breaker");
            bedrockbreaker.setItemMeta(im);
            inventory.addItem(bedrockbreaker);
            return true;
        } 

        return false; 
    }
    @EventHandler(priority=EventPriority.HIGH)
    public void onPlayerUse (org.bukkit.event.player.PlayerInteractEvent evnt) {
        org.bukkit.block.Block block = evnt.getClickedBlock();
        org.bukkit.inventory.ItemStack item = evnt.getItem();
        Player player = evnt.getPlayer();
        org.bukkit.World world = block.getWorld();
        ItemMeta itemmeta = item.getItemMeta();
            if (block.getType() == Material.BEDROCK && item.getType() == Material.DIAMOND_HOE && itemmeta.getDisplayName() == ChatColor.DARK_AQUA + "Bedrock Breaker") {

                BlockBreakEvent breakEvent = new BlockBreakEvent(block, player);
                getServer().getPluginManager().callEvent(breakEvent);
                ItemStack drop = new ItemStack(block.getType());
                drop.setAmount(1);
                drop.setType(Material.BEDROCK);
                block.setType(Material.AIR);

                world.dropItemNaturally(block.getLocation(), drop);
                player.sendMessage("Bedrock Broken!");
            }
    }
}
Was it helpful?

Solution

As seen in your error, here:

Caused by: java.lang.NullPointerException
    at me.jrneulight.bedrockbreaker.Main.onPlayerUse(Main.java:54)

you are getting a NullPointer in your Main file on line 54. Meaning the value of something on line 54 is null. First, Let me start out by making your code in the beginning of onPlayerUse better. You don't have to use org.bukkit.block.Block block, you just have to use Block block = evnt.getClickedBlock(); same goes for org.bukkit.inventory.ItemStack, you only need to use ItemStack.

The error is being caused by your method trying to get the block clicked, when no block was clicked, or trying to get the item used, when none was used. Here is the code you should be using instead:

@EventHandler(priority=EventPriority.HIGH)
public void onPlayerUse (PlayerInteractEvent evnt) {
    if(evnt.getAction().equals(Action.LEFT_CLICK_BLOCK){//make sure that they are infact hitting a block
        if(evt.getItem() == new ItemStack(Material.DIAMOND_HOE)){ //make sure they are using a diamond hoe
            Block block = evnt.getClickedBlock();
            //ItemStack item = evnt.getItem();
            //we don't need the above line any more
            Player player = evnt.getPlayer();

            World world = block.getWorld();
            ItemMeta itemmeta = item.getItemMeta();
         }
    }
}

So, for your whole onPlayerUse method, you should be using this code:

    @EventHandler(priority=EventPriority.HIGH)
    public void onPlayerUse (PlayerInteractEvent evnt) {
        if(evnt.getAction().equals(Action.LEFT_CLICK_BLOCK)){//make sure that they are infact hitting a block
            if(evnt.getItem() == new ItemStack(Material.DIAMOND_HOE)){ //make sure they are using a diamond hoe
               if(evnt.getItem().getItemMeta().getDisplayName().equals(ChatColor.DARK_AQUA + "Bedrock Breaker")){ //make sure the name of the diamond hoe is "Bedrock Breaker" in DARK_AQUA

                Block block = evnt.getClickedBlock();
                World world = block.getWorld();
                Player player = evnt.getPlayer();
                if(block.getType() == Material.BEDROCK){ //make sure the block clicked is bedrock

                    BlockBreakEvent breakEvent = new BlockBreakEvent(block, player);
                    Bukkit.getServer().getPluginManager().callEvent(breakEvent);
                    ItemStack drop = new ItemStack(block.getType());
                    drop.setAmount(1);
                    drop.setType(Material.BEDROCK);
                    block.setType(Material.AIR);

                    world.dropItemNaturally(block.getLocation(), drop);
                    player.sendMessage("Bedrock Broken!");
                }
            }
        }
    }
    }

You're going to want to check for the name with .equals(String) instead of == String. Also, you should check for most of the requirements initially to reduce lag.

The reason that you were getting that error was because you weren't making sure that they were hitting a block, so, in your code, when the player did any action (stepping on a pressure plate, right-clicking an item, hitting the air), it called you're code. That's why you need to make sure that they are left clicking a block, with Action.LEFT_CLICK_BLOCK, then get the block that they are hitting. Before you would try to get the block they were hitting, even when they pressed a button, or stepped on a pressure plate, so it returned a NullPointerException, because there was no block being hit.

As a general rule of thumb, before you get something from an event, first check if that something that you are getting exists.

OTHER TIPS

You are getting a NullPointerException, which means that one of the values returned didn't exist.

Try adding some if statments to make sure that everything exists first

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top