从服务器弃用 getPlayer()
Deprecated getPlayer() from Server
下面的一段代码给出了一个错误,说明它已被弃用。
Player target = Bukkit.getServer().getPlayer(args[0]);
这是我的插件:
package me.herobrine112211.ulticmdpack;
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 System extends JavaPlugin {
public void onEnable() {
Bukkit.getServer().getLogger().info("Ultimate Command Pack Plugin has been actived!");
}
public void onDisable() {
Bukkit.getServer().getLogger().info("Ultimate Command Pack Plugin has been disabled! This will give big problems to server.");
}
public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args) {
if (!(sender instanceof Player)) {
sender.sendMessage(ChatColor.RED + "Console will never need owner support.");
return true;
}
Player player = (Player) sender;
Player target = Bukkit.getServer().getPlayer(args[0]);
if (cmd.getName().equalsIgnoreCase("contactowner")) {
player.sendMessage(ChatColor.GOLD + "Owner: Herobrine112211. Live chat skype: gangsteris33.");
}
if (cmd.getName().equalsIgnoreCase("heal")) {
if (args.length == 0) {
player.setHealth(20f);
player.sendMessage(ChatColor.GREEN + "Your health is now full!");
return true;
}
if (target == null) {
player.sendMessage(ChatColor.RED + "Player must be online to be healed!");
return true;
}
target.setHealth(20f);
target.sendMessage(ChatColor.GREEN + player.getName() + " healed you!");
player.sendMessage(ChatColor.GREEN + target.getName() + " is healed!");
}
if (cmd.getName().equalsIgnoreCase("feed")) {
if (args.length == 0) {
player.setFoodLevel(20);
player.sendMessage(ChatColor.GREEN + "Your hunger is now full!");
return true;
}
if (target == null) {
player.sendMessage(ChatColor.RED + "Player must be online to be feeded!");
return true;
}
target.setFoodLevel(20);
target.sendMessage(ChatColor.GREEN + player.getName() + " fed you!");
player.sendMessage(ChatColor.GREEN + target.getName() + " hunger is now full!");
}
return true;
}
}
我得到这个堆栈跟踪:
5:02:27 AM [ERROR] null
5:02:27 AM org.bukkit.command.CommandException: Unhandled exception executing command 'heal' in plugin UltimateCommandPack v1.0.0
5:02:27 AM at org.bukkit.command.PluginCommand.execute(PluginCommand.java:46) ~[bukkit.jar:git-Bukkit-61ef214]
5:02:27 AM at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:140) ~[bukkit.jar:git-Bukkit-61ef214]
5:02:27 AM at org.bukkit.craftbukkit.v1_8_R2.CraftServer.dispatchCommand(CraftServer.java:625) ~[bukkit.jar:git-Bukkit-61ef214]
5:02:27 AM at net.minecraft.server.v1_8_R2.PlayerConnection.handleCommand(PlayerConnection.java:1077) [bukkit.jar:git-Bukkit-61ef214]
5:02:27 AM at net.minecraft.server.v1_8_R2.PlayerConnection.a(PlayerConnection.java:937) [bukkit.jar:git-Bukkit-61ef214]
5:02:27 AM at net.minecraft.server.v1_8_R2.PacketPlayInChat.a(SourceFile:37) [bukkit.jar:git-Bukkit-61ef214]
5:02:27 AM at net.minecraft.server.v1_8_R2.PacketPlayInChat.a(SourceFile:9) [bukkit.jar:git-Bukkit-61ef214]
5:02:27 AM at net.minecraft.server.v1_8_R2.PlayerConnectionUtils.run(SourceFile:13) [bukkit.jar:git-Bukkit-61ef214]
5:02:27 AM at java.util.concurrent.Executors$RunnableAdapter.call(Unknown Source) [?:1.7.0_75]
5:02:27 AM at java.util.concurrent.FutureTask.run(Unknown Source) [?:1.7.0_75]
5:02:27 AM at net.minecraft.server.v1_8_R2.SystemUtils.a(SourceFile:60) [bukkit.jar:git-Bukkit-61ef214]
5:02:27 AM at net.minecraft.server.v1_8_R2.MinecraftServer.A(MinecraftServer.java:670) [bukkit.jar:git-Bukkit-61ef214]
5:02:27 AM at net.minecraft.server.v1_8_R2.DedicatedServer.A(DedicatedServer.java:336) [bukkit.jar:git-Bukkit-61ef214]
5:02:27 AM at net.minecraft.server.v1_8_R2.MinecraftServer.z(MinecraftServer.java:626) [bukkit.jar:git-Bukkit-61ef214]
5:02:27 AM at net.minecraft.server.v1_8_R2.MinecraftServer.run(MinecraftServer.java:534) [bukkit.jar:git-Bukkit-61ef214]
5:02:27 AM at java.lang.Thread.run(Unknown Source) [?:1.7.0_75]
5:02:27 AM Caused by: java.lang.ArrayIndexOutOfBoundsException: 0
5:02:27 AM at me.herobrine112211.ulticmdpack.System.onCommand(System.java:29) ~[?:?]
5:02:27 AM at org.bukkit.command.PluginCommand.execute(PluginCommand.java:44) ~[bukkit.jar:git-Bukkit-61ef214]
5:02:27 AM ... 15 more
您遇到的错误与那段代码无关。
java.lang.ArrayIndexOutOfBoundsException
:
Thrown to indicate that an array has been accessed with an illegal index. The index is either negative or greater than or equal to the size of the array.
args
可能会根据用户输入而变化。
参见 Check the arguments length from the official plugin tutorial。
if (args.length == 0) {
player.sendMessage("Please specify the target.");
return true;
}
弃用消息不是错误,而是警告。
你总是可以忽略它,代码无论如何都会编译。
您可能想要隐藏它:将鼠标悬停在黄色突出显示的带交叉文本上,然后单击 添加 @SuppressWarnings 'deprecation'.
来自 Spigot Javadoc, note that Server.getPlayer(String)
未标记为 @Deprecated
。
也许是 IDE 故障?试试 File >> Save All
.
您的堆栈跟踪问题可能是这一行:
Player target = Bukkit.getServer().getPlayer(args[0]);
在执行此操作之前,您需要检查 args.length > 0,args 数组可能为空,而您正在尝试访问其中的元素。
Bukkit.getPlayer(String)
是 not deprecated in new Spigot
versions, but, it is deprecated in older Bukkit
versions,因为在 1.8.
中从用户名切换到 UUIDs
在 Bukkit 中,弃用的方法几乎从未被删除。他们只是在那里通知您新 Minecraft 版本的变化。因此,忽略警告是安全的,因为 Bukkit.getPlayer(String)
将继续工作。
如果你想隐藏警告,你可以使用:
@SuppressWarnings("deprecation")
在获取播放器的代码上方:
@SuppressWarnings("deprecation")
Player target = Bukkit.getServer().getPlayer(args[0]);
您收到 ArrayIndexOutOfBoundsException
错误:
Caused by: java.lang.ArrayIndexOutOfBoundsException: 0
哪些被扔到:
Indicate that an array has been accessed with an illegal index. The index is either negative or greater than or equal to the size of the array.
(from the JavaDocs)
每当 运行 命令的玩家没有任何参数时(例如,如果他们 运行 /heal
而不是 /heal Notch
)
要解决此问题,如果玩家未输入任何参数,您应该向玩家发送一条错误消息:
if(args.length == 0){
//send the sender an error message, and return true
sender.sendMessage(ChatColor.RED + "Correct usage: /" + commandLabel + " <player>"
return true;
}
因此,您的代码可能如下所示:
public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args) {
if(!(sender instanceof Player)) {
sender.sendMessage(ChatColor.RED + "Console will never need owner support.");
return true;
}
Player player = (Player) sender;
if(args.length == 0){
//send the sender an error message, and return true
sender.sendMessage(ChatColor.RED + "Correct usage: /" + commandLabel + " <player>"
return true;
}
@SuppressWarnings("deprecation")
Player target = Bukkit.getServer().getPlayer(args[0]);
//the rest of your code
}
下面的一段代码给出了一个错误,说明它已被弃用。
Player target = Bukkit.getServer().getPlayer(args[0]);
这是我的插件:
package me.herobrine112211.ulticmdpack;
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 System extends JavaPlugin {
public void onEnable() {
Bukkit.getServer().getLogger().info("Ultimate Command Pack Plugin has been actived!");
}
public void onDisable() {
Bukkit.getServer().getLogger().info("Ultimate Command Pack Plugin has been disabled! This will give big problems to server.");
}
public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args) {
if (!(sender instanceof Player)) {
sender.sendMessage(ChatColor.RED + "Console will never need owner support.");
return true;
}
Player player = (Player) sender;
Player target = Bukkit.getServer().getPlayer(args[0]);
if (cmd.getName().equalsIgnoreCase("contactowner")) {
player.sendMessage(ChatColor.GOLD + "Owner: Herobrine112211. Live chat skype: gangsteris33.");
}
if (cmd.getName().equalsIgnoreCase("heal")) {
if (args.length == 0) {
player.setHealth(20f);
player.sendMessage(ChatColor.GREEN + "Your health is now full!");
return true;
}
if (target == null) {
player.sendMessage(ChatColor.RED + "Player must be online to be healed!");
return true;
}
target.setHealth(20f);
target.sendMessage(ChatColor.GREEN + player.getName() + " healed you!");
player.sendMessage(ChatColor.GREEN + target.getName() + " is healed!");
}
if (cmd.getName().equalsIgnoreCase("feed")) {
if (args.length == 0) {
player.setFoodLevel(20);
player.sendMessage(ChatColor.GREEN + "Your hunger is now full!");
return true;
}
if (target == null) {
player.sendMessage(ChatColor.RED + "Player must be online to be feeded!");
return true;
}
target.setFoodLevel(20);
target.sendMessage(ChatColor.GREEN + player.getName() + " fed you!");
player.sendMessage(ChatColor.GREEN + target.getName() + " hunger is now full!");
}
return true;
}
}
我得到这个堆栈跟踪:
5:02:27 AM [ERROR] null
5:02:27 AM org.bukkit.command.CommandException: Unhandled exception executing command 'heal' in plugin UltimateCommandPack v1.0.0
5:02:27 AM at org.bukkit.command.PluginCommand.execute(PluginCommand.java:46) ~[bukkit.jar:git-Bukkit-61ef214]
5:02:27 AM at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:140) ~[bukkit.jar:git-Bukkit-61ef214]
5:02:27 AM at org.bukkit.craftbukkit.v1_8_R2.CraftServer.dispatchCommand(CraftServer.java:625) ~[bukkit.jar:git-Bukkit-61ef214]
5:02:27 AM at net.minecraft.server.v1_8_R2.PlayerConnection.handleCommand(PlayerConnection.java:1077) [bukkit.jar:git-Bukkit-61ef214]
5:02:27 AM at net.minecraft.server.v1_8_R2.PlayerConnection.a(PlayerConnection.java:937) [bukkit.jar:git-Bukkit-61ef214]
5:02:27 AM at net.minecraft.server.v1_8_R2.PacketPlayInChat.a(SourceFile:37) [bukkit.jar:git-Bukkit-61ef214]
5:02:27 AM at net.minecraft.server.v1_8_R2.PacketPlayInChat.a(SourceFile:9) [bukkit.jar:git-Bukkit-61ef214]
5:02:27 AM at net.minecraft.server.v1_8_R2.PlayerConnectionUtils.run(SourceFile:13) [bukkit.jar:git-Bukkit-61ef214]
5:02:27 AM at java.util.concurrent.Executors$RunnableAdapter.call(Unknown Source) [?:1.7.0_75]
5:02:27 AM at java.util.concurrent.FutureTask.run(Unknown Source) [?:1.7.0_75]
5:02:27 AM at net.minecraft.server.v1_8_R2.SystemUtils.a(SourceFile:60) [bukkit.jar:git-Bukkit-61ef214]
5:02:27 AM at net.minecraft.server.v1_8_R2.MinecraftServer.A(MinecraftServer.java:670) [bukkit.jar:git-Bukkit-61ef214]
5:02:27 AM at net.minecraft.server.v1_8_R2.DedicatedServer.A(DedicatedServer.java:336) [bukkit.jar:git-Bukkit-61ef214]
5:02:27 AM at net.minecraft.server.v1_8_R2.MinecraftServer.z(MinecraftServer.java:626) [bukkit.jar:git-Bukkit-61ef214]
5:02:27 AM at net.minecraft.server.v1_8_R2.MinecraftServer.run(MinecraftServer.java:534) [bukkit.jar:git-Bukkit-61ef214]
5:02:27 AM at java.lang.Thread.run(Unknown Source) [?:1.7.0_75]
5:02:27 AM Caused by: java.lang.ArrayIndexOutOfBoundsException: 0
5:02:27 AM at me.herobrine112211.ulticmdpack.System.onCommand(System.java:29) ~[?:?]
5:02:27 AM at org.bukkit.command.PluginCommand.execute(PluginCommand.java:44) ~[bukkit.jar:git-Bukkit-61ef214]
5:02:27 AM ... 15 more
您遇到的错误与那段代码无关。
java.lang.ArrayIndexOutOfBoundsException
:
Thrown to indicate that an array has been accessed with an illegal index. The index is either negative or greater than or equal to the size of the array.
args
可能会根据用户输入而变化。
参见 Check the arguments length from the official plugin tutorial。
if (args.length == 0) {
player.sendMessage("Please specify the target.");
return true;
}
弃用消息不是错误,而是警告。 你总是可以忽略它,代码无论如何都会编译。 您可能想要隐藏它:将鼠标悬停在黄色突出显示的带交叉文本上,然后单击 添加 @SuppressWarnings 'deprecation'.
来自 Spigot Javadoc, note that Server.getPlayer(String)
未标记为 @Deprecated
。
也许是 IDE 故障?试试 File >> Save All
.
您的堆栈跟踪问题可能是这一行:
Player target = Bukkit.getServer().getPlayer(args[0]);
在执行此操作之前,您需要检查 args.length > 0,args 数组可能为空,而您正在尝试访问其中的元素。
Bukkit.getPlayer(String)
是 not deprecated in new Spigot
versions, but, it is deprecated in older Bukkit
versions,因为在 1.8.
UUIDs
在 Bukkit 中,弃用的方法几乎从未被删除。他们只是在那里通知您新 Minecraft 版本的变化。因此,忽略警告是安全的,因为 Bukkit.getPlayer(String)
将继续工作。
如果你想隐藏警告,你可以使用:
@SuppressWarnings("deprecation")
在获取播放器的代码上方:
@SuppressWarnings("deprecation")
Player target = Bukkit.getServer().getPlayer(args[0]);
您收到 ArrayIndexOutOfBoundsException
错误:
Caused by: java.lang.ArrayIndexOutOfBoundsException: 0
哪些被扔到:
Indicate that an array has been accessed with an illegal index. The index is either negative or greater than or equal to the size of the array.
(from the JavaDocs)
每当 运行 命令的玩家没有任何参数时(例如,如果他们 运行 /heal
而不是 /heal Notch
)
要解决此问题,如果玩家未输入任何参数,您应该向玩家发送一条错误消息:
if(args.length == 0){
//send the sender an error message, and return true
sender.sendMessage(ChatColor.RED + "Correct usage: /" + commandLabel + " <player>"
return true;
}
因此,您的代码可能如下所示:
public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args) {
if(!(sender instanceof Player)) {
sender.sendMessage(ChatColor.RED + "Console will never need owner support.");
return true;
}
Player player = (Player) sender;
if(args.length == 0){
//send the sender an error message, and return true
sender.sendMessage(ChatColor.RED + "Correct usage: /" + commandLabel + " <player>"
return true;
}
@SuppressWarnings("deprecation")
Player target = Bukkit.getServer().getPlayer(args[0]);
//the rest of your code
}