首先,我是Java的新手。这是我第一个bukkit插件,唯一的错误是这个(Click Here)。该插件的目的是当您右键单击“基岩破坏者”时,它会破坏基岩。

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

最佳答案

如您的错误所示,这里:

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


您在NullPointer行的Main文件中得到一个54。表示54行上某物的值是null。首先,让我开始改进onPlayerUse开头的代码。您不必使用org.bukkit.block.Block block,仅需使用Block block = evnt.getClickedBlock(); org.bukkit.inventory.ItemStack,只需使用ItemStack

该错误是由于您的方法试图单击该块而未单击任何块,或试图使该项目被使用(未使用任何项)引起的。这是您应该使用的代码:

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


因此,对于整个onPlayerUse方法,您应该使用以下代码:

    @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!");
                }
            }
        }
    }
    }


您将要使用.equals(String)而不是== String检查名称。另外,您最初应该检查大多数需求以减少延迟。

出现该错误的原因是,您不确定自己是否碰到了障碍物,因此,在您的代码中,玩家进行任何操作(踩压板,右键单击某个物品,空气),它称您为代码。这就是为什么您需要确保使用Action.LEFT_CLICK_BLOCK左键单击一个块,然后获取它们所击中的块。在您尝试获得它们被击中的块之前,即使他们按下按钮或踩在压板上,由于没有块被击中,所以它返回了NullPointerException

作为一般的经验法则,在从事件中获取某些东西之前,请先检查您所获取的东西是否存在。

关于java - Bukkit插件编码不起作用?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/21514927/

10-14 07:41