gpt4 book ai didi

java - 无法编辑标志 - Bukkit : org. bukkit.command.CommandException:插件 X 中执行命令 'sign' 的未处理异常

转载 作者:行者123 更新时间:2023-11-30 10:46:11 31 4
gpt4 key购买 nike

我是 bukkit/spigot 的新手,我正在制作一个插件,玩家可以在其中键入命令“/sign”,然后会在玩家旁边创建一个附在木 block 上的标志。该标志将显示“你好 PlayerName”。但是,我收到错误:org.bukkit.command.CommandException: Unhandled exception executing command 'sign' in plugin

这是我的部分代码:

        if (cmd.getName().equalsIgnoreCase("sign") && sender instanceof Player){

Player player = (Player) sender;
Location location = player.getLocation();
World someWorld = Bukkit.getServer().getWorld("world");

double playerx = location.getX();
double playery = location.getY();
double playerz = location.getZ();

int px = (int)playerx;
int py = (int)playery;
int pz = (int)playerz;

Location nLoc = new Location(someWorld, px+2, py+1, pz);

Location sLoc = new Location(someWorld, px+1, py+1, pz);


Block block = someWorld.getBlockAt(nLoc);
block.setType(Material.WOOD);

Block block1 = someWorld.getBlockAt(sLoc);
block1.setType(Material.SIGN);
Sign sign = (Sign) block1.getState();
sign.setLine(0, "Hello\n"+player.getName());




}

我该如何修复这个错误?

我在控制台上得到的完整错误是:

[17:46:00 ERROR]: null
org.bukkit.command.CommandException: Unhandled exception executing command 'sign' in plugin FirstPlugin v1.0
at org.bukkit.command.PluginCommand.execute(PluginCommand.java:46) ~[spigot-1.9.jar:git-Spigot-d20369f-7fc5cd8]
at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:141) ~[spigot-1.9.jar:git-Spigot-d20369f-7fc5cd8]
at org.bukkit.craftbukkit.v1_9_R1.CraftServer.dispatchCommand(CraftServer.java:645) ~[spigot-1.9.jar:git-Spigot-d20369f-7fc5cd8]
at net.minecraft.server.v1_9_R1.PlayerConnection.handleCommand(PlayerConnection.java:1350) [spigot-1.9.jar:git-Spigot-d20369f-7fc5cd8]
at net.minecraft.server.v1_9_R1.PlayerConnection.a(PlayerConnection.java:1185) [spigot-1.9.jar:git-Spigot-d20369f-7fc5cd8]
at net.minecraft.server.v1_9_R1.PacketPlayInChat.a(PacketPlayInChat.java:45) [spigot-1.9.jar:git-Spigot-d20369f-7fc5cd8]
at net.minecraft.server.v1_9_R1.PacketPlayInChat.a(PacketPlayInChat.java:1) [spigot-1.9.jar:git-Spigot-d20369f-7fc5cd8]
at net.minecraft.server.v1_9_R1.PlayerConnectionUtils$1.run(SourceFile:13) [spigot-1.9.jar:git-Spigot-d20369f-7fc5cd8]
at java.util.concurrent.Executors$RunnableAdapter.call(Unknown Source) [?:1.8.0_71]
at java.util.concurrent.FutureTask.run(Unknown Source) [?:1.8.0_71]
at net.minecraft.server.v1_9_R1.SystemUtils.a(SourceFile:45) [spigot-1.9.jar:git-Spigot-d20369f-7fc5cd8]
at net.minecraft.server.v1_9_R1.MinecraftServer.D(MinecraftServer.java:721) [spigot-1.9.jar:git-Spigot-d20369f-7fc5cd8]
at net.minecraft.server.v1_9_R1.DedicatedServer.D(DedicatedServer.java:400) [spigot-1.9.jar:git-Spigot-d20369f-7fc5cd8]
at net.minecraft.server.v1_9_R1.MinecraftServer.C(MinecraftServer.java:660) [spigot-1.9.jar:git-Spigot-d20369f-7fc5cd8]
at net.minecraft.server.v1_9_R1.MinecraftServer.run(MinecraftServer.java:559) [spigot-1.9.jar:git-Spigot-d20369f-7fc5cd8]
at java.lang.Thread.run(Unknown Source) [?:1.8.0_71]
Caused by: java.lang.ClassCastException: org.bukkit.craftbukkit.v1_9_R1.block.CraftBlockState cannot be cast to org.bukkit.block.Sign
at zak.firstplugin.FirstPlugin.onCommand(FirstPlugin.java:58) ~[?:?]
at org.bukkit.command.PluginCommand.execute(PluginCommand.java:44) ~[spigot-1.9.jar:git-Spigot-d20369f-7fc5cd8]
... 15 more

最佳答案

如果你投了一个BlockStateBlock对于它不是实例的子类,将抛出此错误(如在您的堆栈跟踪中):org.bukkit.craftbukkit.[version].block.CraftBlockState cannot be cast to org.bukkit.block.[YourState] .换句话说,您的代码会抛出该错误,因为该 block 实际上不是 Sign。 , 因为它是 BlockState不是 Sign 的实例.以后可以随时使用instanceof查看确保父类(super class)可以转换为特定的子类:

if (block.getState() instanceof Sign) {
Sign sign = (Sign) block.getState();
// ... Your code
}

即使您将其 Material 设置为 Material.SIGN,您的方 block 也不是标志的原因, 是 Material.SIGN enumerator 实际上指的是符号 item 类型,而不是 block 类型,其中实际上有两个( Material.SIGN_POSTMaterial.WALL_SIGN )。令人困惑的是,当您设置 Block 时,Bukkit/Spigot 不会警告您的类型为项目类型的类型,而是设置 Block的类型或 Material 到空气(因此抛出 ClassCastException)。我猜既然你正在为标志生成一个实心 block ,你可能想要后者的枚举器或 Material.WALL_SIGN .

此外,为了确保标牌上的文字出现,您需要使用 state.update() 更新 BlockState。 (甚至可能使用 state.update(true) 强制更新)。

要在下一行写下玩家的名字,您需要添加 String使用 sign.setLine(1, player.getName()) 到第 2 行(索引 1) , 不使用 \n换行符,Minecraft 标志不处理。

最后但并非最不重要的一点是,您案例中的墙上标志将朝向错误的方向,这可以通过更改标志的旋转来解决。这当然取决于木 block 相对于标志的放置位置,因此在您的情况下,标志需要旋转面向西。要设置标志的旋转,我们可以使用已弃用的 setData(byte data) Block 的方法s,但是如果你想用不被弃用的、更容易阅读的方式来做,我们将不得不处理另一个小问题:有一个 org.bukkit.block.Sign接口(interface),和一个org.bukkit.material.Sign Sign 的所有用法以上引用org.bukkit.block.Sign接口(interface)是 BlockState 的子类型.这用于例如设置标志的文本。 org.bukkit.material.SignMaterialData 的子类型我们可以使用 state.getData() 访问的类并用于更改标志的方向,因为该类实现了 Directional接口(interface)(特别是 Attachable 接口(interface))。所以要设置文本 旋转标志,我们必须同时使用类和接口(interface)。下面是一些示例代码:

Block sign = world.getBlockAt(signLoc); // Get the block
sign.setType(Material.WALL_SIGN); // Set the type to "WALL_SIGN", now it's BlockState is an instance of "Sign"

BlockState signState = sign.getState(); // Get the general state

if (signState instanceof org.bukkit.block.Sign) { // Make sure the sign block really does have the "Sign" BlockState (this isn't really necessary, more of a double check)
org.bukkit.block.Sign signBlock = (org.bukkit.block.Sign) signState; // Note that this is the org.bukkit.block.Sign interface
signBlock.setLine(0, "Hello"); // Set the first line
signBlock.setLine(1, player.getName()); // Set the second line
if (signState.getData() instanceof org.bukkit.material.Sign) { // Now get the "MaterialData" from the BlockState...
org.bukkit.material.Sign signMaterialData = (org.bukkit.material.Sign) signState.getData(); // And cast it to org.bukkit.material.Sign
signMaterialData.setFacingDirection(BlockFace.WEST); // Use this to change the direction of the sign, in this case WEST (sign is placed + X direction of player)
}
signBlock.update(); // Update the sign's state
}

关于java - 无法编辑标志 - Bukkit : org. bukkit.command.CommandException:插件 X 中执行命令 'sign' 的未处理异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36679175/

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