gpt4 book ai didi

java - Bukkit 插件编码不起作用?

转载 作者:行者123 更新时间:2023-12-01 23:13:55 26 4
gpt4 key购买 nike

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

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)

您将在 Main 文件的第 54 行获得一个 NullPointer。意味着 54 行上的某些内容的值为 null。首先,让我先让 onPlayerUse 开头的代码变得更好。您不必使用 org.bukkit.block.Block block,只需使用 Block block = evnt.getClickedBlock(); 同样适用于 org.bukkit.inventory.ItemStack,只需要使用ItemStack即可。

该错误是由于您的方法在没有单击任何 block 时尝试单击该 block ,或者在未使用任何 block 时尝试获取所使用的项目而引起的。这是您应该使用的代码:

@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 检查名称。此外,您应该首先检查大多数要求以减少延迟。

您收到该错误的原因是因为您不确定他们是否击中了方 block ,因此,在您的代码中,当玩家执行任何操作时(踩在压力板上,右键单击一个项目) ,击中空气),它称你为代码。这就是为什么您需要确保他们使用 Action.LEFT_CLICK_BLOCK 左键单击一个 block ,然后获取他们正在点击的 block 。在你尝试获取他们正在击中的方 block 之前,即使他们按下按钮或踩在压力板上,也会返回一个 NullPointerException,因为没有任何方 block 被击中。

作为一般经验法则,在您从事件中获取某些内容之前,首先检查您要获取的内容是否存在。

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

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com