我用的是1.8.7spigot API 出现了错误
- public boolean onCommand(CommandSender sender,Command cmd,String lable,String[] args){
- if(lable.equalsIgnoreCase("fly")){
- {
- if(sender instanceof Player){
- if (Bukkit.getServer().getPlayer(args[0])!=null) {
- Player p=Bukkit.getServer().getPlayer(args[0]);
- if(p.getAllowFlight()==false){
- p.setAllowFlight(true);
- p.sendMessage("§2你现在可以飞了");
- }
- else {
- p.setAllowFlight(false);
- p.sendMessage("§c你已被取消飞行");
- }
- }
- else {
- getLogger().info("=null");
- Player p=(Player)sender;
- if(p.getAllowFlight()==false){
- p.setAllowFlight(true);
- p.sendMessage("§2你现在可以飞了");
- }
- else {
- p.setAllowFlight(false);
- p.sendMessage("§c你已被取消飞行");
- }
- }
- }
- else{
- if (Bukkit.getServer().getPlayer(args[0])!=null) {
- Player p=Bukkit.getServer().getPlayer(args[0]);
- if(p.getAllowFlight()==false){
- p.setAllowFlight(true);
- p.sendMessage("§2你现在可以飞了");
- }
- else {
- p.setAllowFlight(false);
- p.sendMessage("§c你已被取消飞行");
- }
- }
- else {
- getLogger().info("控制台无法使用此命令");
- }
- }
- }
- return true;
- }
- return false
- }
复制代码
当在控制台或游戏中输入flyde时候,就是没有args[0]的时候 , 就会报null的错误,错误如下:
>fly
[10:59:19 WARN]: Unexpected exception while parsing console command "fly"
org.bukkit.command.CommandException: Unhandled exception executing command 'fly'
in plugin tskplug1 v1.0
at org.bukkit.command.PluginCommand.execute(PluginCommand.java:46) ~[spi
got-1.8.7-R0.1-SNAPSHOT.jar:git-Spigot-44c59bf-25554dc]
at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:14
1) ~[spigot-1.8.7-R0.1-SNAPSHOT.jar:git-Spigot-44c59bf-25554dc]
at org.bukkit.craftbukkit.v1_8_R3.CraftServer.dispatchCommand(CraftServe
r.java:642) ~[spigot-1.8.7-R0.1-SNAPSHOT.jar:git-Spigot-44c59bf-25554dc]
at org.bukkit.craftbukkit.v1_8_R3.CraftServer.dispatchServerCommand(Craf
tServer.java:628) [spigot-1.8.7-R0.1-SNAPSHOT.jar:git-Spigot-44c59bf-25554dc]
at net.minecraft.server.v1_8_R3.DedicatedServer.aO(DedicatedServer.java:
404) [spigot-1.8.7-R0.1-SNAPSHOT.jar:git-Spigot-44c59bf-25554dc]
at net.minecraft.server.v1_8_R3.DedicatedServer.B(DedicatedServer.java:3
68) [spigot-1.8.7-R0.1-SNAPSHOT.jar:git-Spigot-44c59bf-25554dc]
at net.minecraft.server.v1_8_R3.MinecraftServer.A(MinecraftServer.java:6
57) [spigot-1.8.7-R0.1-SNAPSHOT.jar:git-Spigot-44c59bf-25554dc]
at net.minecraft.server.v1_8_R3.MinecraftServer.run(MinecraftServer.java
:560) [spigot-1.8.7-R0.1-SNAPSHOT.jar:git-Spigot-44c59bf-25554dc]
at java.lang.Thread.run(Unknown Source) [?:1.8.0_45]
Caused by: java.lang.ArrayIndexOutOfBoundsException: 0
at com.github.tskdsb.tskplug1.onCommand(tskplug1.java:69) ~[?:?]
at org.bukkit.command.PluginCommand.execute(PluginCommand.java:44) ~[spi
got-1.8.7-R0.1-SNAPSHOT.jar:git-Spigot-44c59bf-25554dc]
... 8 more
我的插件名叫tsk1,还请大哥解答