我的Bukkit插件命令在运行时总是抛出异常。
当我输入我的命令时:/config set
(它应该将信息保存到文件/配置),我在聊天中收到此错误:
尝试执行此命令时发生内部错误。
为什么会这样?
控制台日志:
[13:23:24 INFO]: whispereq issued server command: /config set
[13:23:24 ERROR]: null
org.bukkit.command.CommandException: Unhandled exception executing command 'config' in plugin registyPlayer v1.0
at org.bukkit.command.PluginCommand.execute(PluginCommand.java:46) ~[src.jar:git-Bukkit-1.7.2-R0.3-b3020jnks]
at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:175) ~[src.jar:git-Bukkit-1.7.2-R0.3-b3020jnks]
at org.bukkit.craftbukkit.v1_7_R1.CraftServer.dispatchCommand(CraftServer.java:683) ~[src.jar:git-Bukkit-1.7.2-R0.3-b3020jnks]
at net.minecraft.server.v1_7_R1.PlayerConnection.handleCommand(PlayerConnection.java:952) [src.jar:git-Bukkit-1.7.2-R0.3-b3020jnks]
at net.minecraft.server.v1_7_R1.PlayerConnection.a(PlayerConnection.java:814) [src.jar:git-Bukkit-1.7.2-R0.3-b3020jnks]
at net.minecraft.server.v1_7_R1.PacketPlayInChat.a(PacketPlayInChat.java:28) [src.jar:git-Bukkit-1.7.2-R0.3-b3020jnks]
at net.minecraft.server.v1_7_R1.PacketPlayInChat.handle(PacketPlayInChat.java:47) [src.jar:git-Bukkit-1.7.2-R0.3-b3020jnks]
at net.minecraft.server.v1_7_R1.NetworkManager.a(NetworkManager.java:146) [src.jar:git-Bukkit-1.7.2-R0.3-b3020jnks]
at net.minecraft.server.v1_7_R1.ServerConnection.c(SourceFile:134) [src.jar:git-Bukkit-1.7.2-R0.3-b3020jnks]
at net.minecraft.server.v1_7_R1.MinecraftServer.u(MinecraftServer.java:655) [src.jar:git-Bukkit-1.7.2-R0.3-b3020jnks]
at net.minecraft.server.v1_7_R1.DedicatedServer.u(DedicatedServer.java:250) [src.jar:git-Bukkit-1.7.2-R0.3-b3020jnks]
at net.minecraft.server.v1_7_R1.MinecraftServer.t(MinecraftServer.java:545) [src.jar:git-Bukkit-1.7.2-R0.3-b3020jnks]
at net.minecraft.server.v1_7_R1.MinecraftServer.run(MinecraftServer.java:457) [src.jar:git-Bukkit-1.7.2-R0.3-b3020jnks]
at net.minecraft.server.v1_7_R1.ThreadServerApplication.run(SourceFile:617) [src.jar:git-Bukkit-1.7.2-R0.3-b3020jnks]
Caused by: java.lang.ArrayIndexOutOfBoundsException: 1
at whispereq.saver.onCommand(saver.java:14) ~[?:?]
at org.bukkit.command.PluginCommand.execute(PluginCommand.java:44) ~[src.jar:git-Bukkit-1.7.2-R0.3-b3020jnks]
... 13 more
来自saver
类的代码:
package whispereq;
import org.bukkit.Bukkit;
import org.bukkit.GameMode;
import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
public class saver implements CommandExecutor {
@Override
public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
if (cmd.getName().equalsIgnoreCase("config")) {
if (sender instanceof Player) {
if (args.length == 1) {
if (args[1].equalsIgnoreCase("set")) {
sender.sendMessage("§6Registering player§c " + args[0] + "§6 to Server....");
Player p = (Player) sender;
String nick = p.getName().toLowerCase();
String uid = p.getUniqueId().toString();
boolean op = p.isOp();
GameMode gm = p.getGameMode();
GameMode dgm = Bukkit.getDefaultGameMode();
float exp = p.getExp();
float explvl = p.getExpToLevel();
Main.getInst().getConfig().set("players." + nick + ".uuid", uid);
Main.getInst().getConfig().set("players." + nick + ".isOpped", op);
Main.getInst().getConfig().set("players." + nick + ".CurrentGameMode", gm);
Main.getInst().getConfig().set("players." + nick + ".DefaultGameMode", dgm);
Main.getInst().getConfig().set("players." + nick + ".Exp", exp);
Main.getInst().getConfig().set("players." + nick + ".ExpLevel", explvl);
Main.getInst().saveConfig();
sender.sendMessage("§6Finished!, yours Current in-game Status was SUccesfully registered to the config.yml File in Plugin's Directory. use §c/registy getMe§6 to view your Property.");
return true;
} else if (args[1].equalsIgnoreCase("get")) {
Player p = (Player) sender;
String nick = p.getName().toLowerCase();
if (Main.getInst().getConfig().get("players." + nick) != null) {
p.sendMessage("§8_____________________________________________________");
p.sendMessage(Main.getInst().getConfig().getString("players." + nick + ".uuid"));
p.sendMessage(Main.getInst().getConfig().getString("players." + nick + ".isOpped"));
p.sendMessage(Main.getInst().getConfig().getString("players." + nick + ".CurrentGameMode"));
p.sendMessage(Main.getInst().getConfig().getString("players." + nick + ".DefaultGameMode"));
p.sendMessage(Main.getInst().getConfig().getString("players." + nick + ".Exp"));
p.sendMessage(Main.getInst().getConfig().getString("players." + nick + "ExpLevel"));
p.sendMessage("§8_____________________________________________________");
}
}
}
}
}
return false;
}
}
Main
上课:
package whispereq;
import org.bukkit.plugin.java.JavaPlugin;
public class Main extends JavaPlugin {
public static Main instance;
public void onEnable() {
instance = this;
System.out.println("Loading RegistyPlayer ..");
getCommand("config").setExecutor(new saver());
saveDefaultConfig();
}
public static Main getInst() {
return instance;
}
}
配置文件(空):
#-----------------------------------------------------------#
plugin.yml:
name: registyPlayer
version: 1.0
main: whispereq.Main
commands:
config:
Craftbukkit:1.7.2 R03
Bukkit API:1.7.2 R03
Server Craftbukkit:1.7.2 R03
答案 0 :(得分:1)
数组总是从索引0开始。因此,要访问玩家是否指定“获取”或“设置”,您应该替换
args[1].equalsIgnoreCase("set")
args[1].equalsIgnoreCase("get")
与
args[0].equalsIgnoreCase("set")
args[0].equalsIgnoreCase("get")
另外,不确定你用线路做什么
sender.sendMessage("§6Registering player§c " + args[0] + "§6 to Server....");
args [0]指的是“get”或“set”。据推测,你应该用“nick”替换它并在
之后插入行String nick = p.getName().toLowerCase();