项目命令无法识别 Spigot 中的物品。

huangapple go评论74阅读模式
英文:

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 + &quot;You must be a player to run this command!&quot;);
return true;
}
final Player player = (Player) sender;
if (args.length &lt; 1) {
player.sendMessage(ChatColor.RED + &quot;Please specify the item you want!&quot;);
return true;
} else if (args.length &gt; 1) {
player.sendMessage(ChatColor.RED + &quot;Too many arguments!&quot;);
return true;
} else if (args.length == 1) {
if (args[0] == &quot;wand&quot;) {
player.getInventory().addItem(itemManager.wand);
player.sendMessage(ChatColor.GREEN + &quot;You have been given a &quot; + itemManager.wand.getItemMeta().getDisplayName() + ChatColor.GREEN + &quot;!&quot;);
} else if (args[0] == &quot;powerstick&quot;) {
player.getInventory().addItem(itemManager.powerStick);
player.sendMessage(ChatColor.GREEN + &quot;You have been given a &quot; + itemManager.powerStick.getItemMeta().getDisplayName() + ChatColor.GREEN + &quot;!&quot;);
} else if (!(args[0] == &quot;wand&quot; || args[0] == &quot;powerstick&quot;)) {
player.sendMessage(ChatColor.RED + &quot;That item doesn&#39;t exist!&quot;);
}
}
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(&quot;wand&quot;)) {
player.getInventory().addItem(itemManager.wand);
player.sendMessage(ChatColor.GREEN + &quot;You have been given a &quot; + itemManager.wand.getItemMeta().getDisplayName() + ChatColor.GREEN + &quot;!&quot;);
} else if (args[0].equalsIgnoreCase(&quot;powerstick&quot;)) {
player.getInventory().addItem(itemManager.powerStick);
player.sendMessage(ChatColor.GREEN + &quot;You have been given a &quot; + itemManager.powerStick.getItemMeta().getDisplayName() + ChatColor.GREEN + &quot;!&quot;);
} else if (!(args[0].equalsIgnoreCase(&quot;wand&quot;) || args[0].equalsIgnoreCase(&quot;powerstick&quot;))) {
player.sendMessage(ChatColor.RED + &quot;That item doesn&#39;t exist!&quot;);
}

huangapple
  • 本文由 发表于 2020年10月12日 16:11:51
  • 转载请务必保留本文链接:https://go.coder-hub.com/64313998.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定