Java Minecraft 插件问题 - 不响应 if 语句?

标签 java plugins minecraft

所以我正在为 Minecraft 服务器制作一个简单的代码兑换插件。奇怪的是,当我输入/redeem(有效代码)时,没有任何反应,尽管它应该...有效代码是用户输入到插件配置中的代码。

这是我的代码...

public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args)
{   


    //Assigns the commands chosen in config to strings
    String commandChosen1 = this.getConfig().getString("Command for code 1");
    String commandChosen2 = this.getConfig().getString("Command for code 2");
    String commandChosen3 = this.getConfig().getString("Command for code 3");

    //Assigns the codes to strings
    String validCode1 = this.getConfig().getString("Valid Code 1");
    String validCode2 = this.getConfig().getString("Valid Code 2");
    String validCode3 = this.getConfig().getString("Valid Code 3");

    //If the redeem command is sent from a player
    if(cmd.getName().equalsIgnoreCase("redeem") && sender instanceof Player)
    {
        //Casts the sender to a new player.
        Player player = (Player) sender;

        //Creates object hasUSed to store whether or not the player has already redeemed a code
        Object hasUsed = this.getConfig().get(player.getName());


        //Gives an error message of the arguments don't equal 1.
        if(args.length != 1)
        {
            player.sendMessage(ChatColor.DARK_RED + "Please enter a valid promo code. Find them on our twitter!");
        }



        if(args.length == 1)
        {
            //If the player hasn't used the code yet and the arguments given are equal to a code then give them the reward...
            if(args[0] == validCode1 && hasUsed == null)
            {
                this.getConfig().set(player.getName(), 1);
                player.sendMessage(ChatColor.GREEN + "Promo code successfully entered!");
                if(commandChosen1 == "xp")
                {
                Bukkit.dispatchCommand(player, commandChosen1 + getConfig().getString("XP Given") + "L" + " " + player.getName());
                }
            }

        }

        return true;

    }
    return false;
}

问题出现在“if (args[0] == validCode1 && hasUsed == null)”上。如果这两件事都检查出来,应该发生的代码没有发生,我不知道为什么。

最佳答案

确保在比较字符串时使用equals()。使用 commandChosen1 == "xp" 比较字符串引用而不是值;使用 commandChosen1.equals("xp") 或者如果您更喜欢 "xp".equals(commandChosen1)

此外,

虽然可以使用包含空格的键值的 this.getConfig().getString(),但它会使配置文件难以阅读和困惑。每当我设计插件时,我都会这样设计我的 config.yml

VoteGUI:
  message: 'hello'

然后运行 ​​this.getConfig().getString("VoteGUI.message");

对于你,我会建议这样的东西

Promo-Codes:
    validCode1: 'insert code here'
    validCode2: 'insert code here'
    validCode3: 'insert code here'

然后将其放入您的 onCommand 方法中:

String validCode1 = this.getConfig().getString("Promo-Codes.validCode1");
    String validCode2 = this.getConfig().getString("Promo-Codes.validCode2");
    String validCode3 = this.getConfig().getString("Promo-Codes.validCode3");

如果这不能解决问题,请复制并粘贴从控制台抛出的异常,我可能会提供进一步的帮助

关于Java Minecraft 插件问题 - 不响应 if 语句?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41938056/

相关文章:

Lua 参数问题

java - 使用 Mockito/PowerMock 验证日志是否已写入

java - 如何从 Windows 运行 ZooInspector

java - 为 Jersey 资源提供 lambda 上下文

java - jar执行期间资源路径改变

eclipse - 有没有办法在 Minecraft 客户端连接到服务器时设置超时时间?这是由于Eclipse调试造成的

java - 如何获取从单个 IP 登录的所有 UUID(MySQL 数据库 [Minecraft])

java - Eclipse 代码推荐错误

ruby - 管理 ruby​​ gem 的冲突版本

plugins - 如何为 Sublime Text 2 编辑器安装插件?