首先,我是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!");
}
}
}
答案 0 :(得分:3)
如您的错误所示,此处:
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
,因为没有被击中的块。
作为一般经验法则,在您从事件中获取某些内容之前,请先检查您所获得的内容是否存在。
答案 1 :(得分:0)
您将获得NullPointerException
,这意味着返回的其中一个值不存在。
尝试添加一些if
个参数,以确保所有内容都存在