英文:
Item command doesn't recognise the items Spigot
问题
public class giveCommand implements CommandExecutor {
@Override
public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
if (!(sender instanceof Player)) {
sender.sendMessage(ChatColor.RED + "You must be a player to run this command!");
return true;
}
final Player player = (Player) sender;
if (args.length < 1) {
player.sendMessage(ChatColor.RED + "Please specify the item you want!");
return true;
} else if (args.length > 1) {
player.sendMessage(ChatColor.RED + "Too many arguments!");
return true;
} else if (args.length == 1) {
if (args[0].equals("wand")) {
player.getInventory().addItem(itemManager.wand);
player.sendMessage(ChatColor.GREEN + "You have been given a " + itemManager.wand.getItemMeta().getDisplayName() + ChatColor.GREEN + "!");
} else if (args[0].equals("powerstick")) {
player.getInventory().addItem(itemManager.powerStick);
player.sendMessage(ChatColor.GREEN + "You have been given a " + itemManager.powerStick.getItemMeta().getDisplayName() + ChatColor.GREEN + "!");
} else {
player.sendMessage(ChatColor.RED + "That item doesn't exist!");
}
}
return true;
}
}
在运行 /i wand
或 /i powerstick
命令时,总是显示无效的物品。修复此问题并使将来添加新物品更加方便的改进将不胜感激。
英文:
I am developing a spigot plugin for fun and it has a couple items. I tried making an item command so that I can get the items easier.
public class giveCommand implements CommandExecutor {
@Override
public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
if (!(sender instanceof Player )) {
sender.sendMessage(ChatColor.RED + "You must be a player to run this command!");
return true;
}
final Player player = (Player) sender;
if (args.length < 1) {
player.sendMessage(ChatColor.RED + "Please specify the item you want!");
return true;
} else if (args.length > 1) {
player.sendMessage(ChatColor.RED + "Too many arguments!");
return true;
} else if (args.length == 1) {
if (args[0] == "wand") {
player.getInventory().addItem(itemManager.wand);
player.sendMessage(ChatColor.GREEN + "You have been given a " + itemManager.wand.getItemMeta().getDisplayName() + ChatColor.GREEN + "!");
} else if (args[0] == "powerstick") {
player.getInventory().addItem(itemManager.powerStick);
player.sendMessage(ChatColor.GREEN + "You have been given a " + itemManager.powerStick.getItemMeta().getDisplayName() + ChatColor.GREEN + "!");
} else if (!(args[0] == "wand" || args[0] == "powerstick")) {
player.sendMessage(ChatColor.RED + "That item doesn't exist!");
}
}
return true;
}
}
Every time I run /i wand
or /i powerstick
, it just says invalid items. The fix and any improvements to easily add future items would be greatly appreciated.
答案1
得分: 1
请检查您的if语句。您不应该使用==
来检查字符串,而应该使用String#equals(anotherString)或String#equalsIgnoreCase(anotherString)。
String#equalsIgnoreCase(..)会忽略大小写。
请尝试:
if (args[0].equalsIgnoreCase("wand")) {
player.getInventory().addItem(itemManager.wand);
player.sendMessage(ChatColor.GREEN + "You have been given a " + itemManager.wand.getItemMeta().getDisplayName() + ChatColor.GREEN + "!");
} else if (args[0].equalsIgnoreCase("powerstick")) {
player.getInventory().addItem(itemManager.powerStick);
player.sendMessage(ChatColor.GREEN + "You have been given a " + itemManager.powerStick.getItemMeta().getDisplayName() + ChatColor.GREEN + "!");
} else if (!(args[0].equalsIgnoreCase("wand") || args[0].equalsIgnoreCase("powerstick"))) {
player.sendMessage(ChatColor.RED + "That item doesn't exist!");
}
英文:
Check your if statements. You should not check a string with ==
but with String#equals(anotherString) or with String#equalsIgnoreCase(anotherString)
String#equalsIgnoreCase(..) simply ignores the caps.
Try :
if (args[0].equalsIgnoreCase("wand")) {
player.getInventory().addItem(itemManager.wand);
player.sendMessage(ChatColor.GREEN + "You have been given a " + itemManager.wand.getItemMeta().getDisplayName() + ChatColor.GREEN + "!");
} else if (args[0].equalsIgnoreCase("powerstick")) {
player.getInventory().addItem(itemManager.powerStick);
player.sendMessage(ChatColor.GREEN + "You have been given a " + itemManager.powerStick.getItemMeta().getDisplayName() + ChatColor.GREEN + "!");
} else if (!(args[0].equalsIgnoreCase("wand") || args[0].equalsIgnoreCase("powerstick"))) {
player.sendMessage(ChatColor.RED + "That item doesn't exist!");
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论