我创建了这个插件,但它说错误,我不知道如何解决它,对于每个答案,谢谢。 插件是当命令发送消息预加密时,后缀设置在配置中。
来源:
package cz.denowq.flatrov;
import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.command.Command;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import org.bukkit.plugin.java.JavaPlugin;
public class flatrov extends JavaPlugin{
@Override
public void onEnable() {
getLogger().info("[FlatRov] Načítávaní pluginu na verzi 1.0.1");
getConfig().options().copyDefaults();
saveConfig();
}
@Override
public void onDisable() {
getLogger().info("[FlatRov] Byl vypnut!");
}
@Override
public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
Player player = (Player) sender;
String text = "";
if (player.hasPermission("flatrov.zprava")) {
if (cmd.getName().equalsIgnoreCase("zprava")) {
if((text = getConfig().getString("flatrav.zprava.prikaz")).length() != 0) {
String message = joinArgs(args);
Bukkit.broadcastMessage(ChatColor.translateAlternateColorCodes('&', "" + getConfig().getString("flatrav.zprava.prefix") + " " + getConfig().getString("flatrav.zprava.suffix") + message));
} else {
player.sendMessage(ChatColor.RED + " Použij /zprava <zprava> pro napsání zpravy!");
}
}
} else {
player.sendMessage(ChatColor.RED + " Nemáš povolený používat tento příkaz!");
}
return false;
}
private String joinArgs(String[] args) {
String separator = ""; // empty on first iteration
StringBuilder builder = new StringBuilder();
for (String arg : args) {
builder.append(separator).append(arg);
separator = " "; // space after each arg
}
return builder.toString(); // return result
}
//String message = joinArgs(args);
}
错误:
[20:04:14] [Server thread/ERROR]: null
org.bukkit.command.CommandException: Unhandled exception executing command 'zprava' in plugin FlatRov v1.0.1
at org.bukkit.command.PluginCommand.execute(PluginCommand.java:46) ~[spigot.jar:git-Spigot-1564]
at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:181) ~[spigot.jar:git-Spigot-1564]
at org.bukkit.craftbukkit.v1_7_R4.CraftServer.dispatchCommand(CraftServer.java:767) ~[spigot.jar:git-Spigot-1564]
at net.minecraft.server.v1_7_R4.PlayerConnection.handleCommand(PlayerConnection.java:1016) [spigot.jar:git-Spigot-1564]
at net.minecraft.server.v1_7_R4.PlayerConnection.a(PlayerConnection.java:846) [spigot.jar:git-Spigot-1564]
at net.minecraft.server.v1_7_R4.PacketPlayInChat.a(PacketPlayInChat.java:28) [spigot.jar:git-Spigot-1564]
at net.minecraft.server.v1_7_R4.PacketPlayInChat.handle(PacketPlayInChat.java:65) [spigot.jar:git-Spigot-1564]
at net.minecraft.server.v1_7_R4.NetworkManager.a(NetworkManager.java:184) [spigot.jar:git-Spigot-1564]
at net.minecraft.server.v1_7_R4.ServerConnection.c(ServerConnection.java:81) [spigot.jar:git-Spigot-1564]
at net.minecraft.server.v1_7_R4.MinecraftServer.v(MinecraftServer.java:731) [spigot.jar:git-Spigot-1564]
at net.minecraft.server.v1_7_R4.DedicatedServer.v(DedicatedServer.java:289) [spigot.jar:git-Spigot-1564]
at net.minecraft.server.v1_7_R4.MinecraftServer.u(MinecraftServer.java:584) [spigot.jar:git-Spigot-1564]
at net.minecraft.server.v1_7_R4.MinecraftServer.run(MinecraftServer.java:490) [spigot.jar:git-Spigot-1564]
at net.minecraft.server.v1_7_R4.ThreadServerApplication.run(SourceFile:628) [spigot.jar:git-Spigot-1564]
Caused by: java.lang.NullPointerException
at cz.denowq.flatrov.flatrov.onCommand(flatrov.java:36) ~[?:?]
at org.bukkit.command.PluginCommand.execute(PluginCommand.java:44) ~[spigot.jar:git-Spigot-1564]
... 13 more
[20:04:25] [Server thread/INFO]: DenOwq issued server command: /zprava test
[20:04:25] [Server thread/ERROR]: null
org.bukkit.command.CommandException: Unhandled exception executing command 'zprava' in plugin FlatRov v1.0.1
at org.bukkit.command.PluginCommand.execute(PluginCommand.java:46) ~[spigot.jar:git-Spigot-1564]
at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:181) ~[spigot.jar:git-Spigot-1564]
at org.bukkit.craftbukkit.v1_7_R4.CraftServer.dispatchCommand(CraftServer.java:767) ~[spigot.jar:git-Spigot-1564]
at net.minecraft.server.v1_7_R4.PlayerConnection.handleCommand(PlayerConnection.java:1016) [spigot.jar:git-Spigot-1564]
at net.minecraft.server.v1_7_R4.PlayerConnection.a(PlayerConnection.java:846) [spigot.jar:git-Spigot-1564]
at net.minecraft.server.v1_7_R4.PacketPlayInChat.a(PacketPlayInChat.java:28) [spigot.jar:git-Spigot-1564]
at net.minecraft.server.v1_7_R4.PacketPlayInChat.handle(PacketPlayInChat.java:65) [spigot.jar:git-Spigot-1564]
at net.minecraft.server.v1_7_R4.NetworkManager.a(NetworkManager.java:184) [spigot.jar:git-Spigot-1564]
at net.minecraft.server.v1_7_R4.ServerConnection.c(ServerConnection.java:81) [spigot.jar:git-Spigot-1564]
at net.minecraft.server.v1_7_R4.MinecraftServer.v(MinecraftServer.java:731) [spigot.jar:git-Spigot-1564]
at net.minecraft.server.v1_7_R4.DedicatedServer.v(DedicatedServer.java:289) [spigot.jar:git-Spigot-1564]
at net.minecraft.server.v1_7_R4.MinecraftServer.u(MinecraftServer.java:584) [spigot.jar:git-Spigot-1564]
at net.minecraft.server.v1_7_R4.MinecraftServer.run(MinecraftServer.java:490) [spigot.jar:git-Spigot-1564]
at net.minecraft.server.v1_7_R4.ThreadServerApplication.run(SourceFile:628) [spigot.jar:git-Spigot-1564]
Caused by: java.lang.NullPointerException
at cz.denowq.flatrov.flatrov.onCommand(flatrov.java:36) ~[?:?]
at org.bukkit.command.PluginCommand.execute(PluginCommand.java:44) ~[spigot.jar:git-Spigot-1564]
... 13 more
编辑:删除配置
答案 0 :(得分:1)
你似乎在2年前学过bukkit,或者你只是使用不良做法,因为大约2年前bukkit在你的插件启用或禁用时开始自动记录。
您不需要记录您的插件已启用/禁用,因为bukkit已经这样做了。
此代码可以简化
getConfig().options().copyDefaults();
saveConfig();
可以写成
saveDefaultConfig();
接下来,在命令处理程序中,您不应该检查那里的权限。您应该将权限节点添加到plugin.yml
中的命令读取配置时应指定默认值。由于密钥不存在,因此当您尝试使用getString()访问密钥时,它将返回null。请改用这样的东西。
getString("flatrav.zprava.prikaz"), "")
如果该字符串不存在,则此方法将返回空字符串。
此外,您的joinArgs()函数会在输出中附加一个额外的空格。
在将所有元素附加到一起之后快速解决此问题,使用类似
的内容sb.set length(sb.get length - 1);
答案 1 :(得分:1)
您的堆栈跟踪在第36行显示错误。这是包含您的第一个配置调用的行:getConfig().getString("flatrav.zprava.prikaz")
。
如果您的配置现在包含flatrav.zprava.prikaz
的值,则该方法将返回null。在Java中,您无法在空值上运行方法。您将此null存储为text
这是完全正常的,但是在该空值上调用.length()
的第二个,您将获得NullPointerException。要解决此问题,您应该检查是text == null
还是将getConfig().getString("flatrav.zprava.prikaz")
更改为getConfig().getString("flatrav.zprava.prikaz", "")
,如果在" flatrav.zprava.prikaz"
找不到任何内容,该方法将返回空字符串。
答案 2 :(得分:0)
这里也存在问题
@Override
public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
Player player = (Player) sender;
String text = "";
问题是,您是在立即强制发件人键入Player
而不检查sender
是否为player
。只有在有人从控制台发送命令或者插件在调度命令时将服务器用作CommandSender
时,才会出现此问题。要解决此问题,只需添加一个包含方法其余部分的if语句。
此外,如果return false
方法中的onCommand
,发件人将收到服务器发出的有关命令使用不正确/无效的消息。请尝试返回true
,除非您希望将其发送此消息
@Override
public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
if (sender instanceof Player) {
Player player = (Player) sender;
String text = "";
if (player.hasPermission("flatrov.zprava")) {
if (cmd.getName().equalsIgnoreCase("zprava")) {
if((text = getConfig().getString("flatrav.zprava.prikaz")).length() != 0) {
String message = joinArgs(args);
Bukkit.broadcastMessage(ChatColor.translateAlternateColorCodes('&', "" + getConfig().getString("flatrav.zprava.prefix") + " " + getConfig().getString("flatrav.zprava.suffix") + message));
} else {
player.sendMessage(ChatColor.RED + " Použij /zprava <zprava> pro napsání zpravy!");
}
}
} else {
player.sendMessage(ChatColor.RED + " Nemáš povolený používat tento příkaz!");
}
}
return true;
}