我创建了这个插件,但是它说错误并且我不知道如何解决它,对于每个答案,谢谢。
插件是命令发送消息前缀时,配置中的后缀设置。
资源:
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
编辑:删除配置
最佳答案
您的堆栈跟踪在第36行显示错误。此行包含您的第一个配置调用:getConfig().getString("flatrav.zprava.prikaz")
。
如果您的配置现在确实包含flatrav.zprava.prikaz
的值,则该方法将返回null。在Java中,不能对空值运行方法。您可以将此空值存储为text
,这是完全可以的,但是第二次在该空值上调用.length()
时,您会得到NullPointerException。要解决此问题,您应该检查text == null
还是将getConfig().getString("flatrav.zprava.prikaz")
更改为getConfig().getString("flatrav.zprava.prikaz", "")
,如果在" flatrav.zprava.prikaz"
上找不到任何内容,该方法将返回一个空字符串。