Solved Changing config values through config

Discussion in 'Spigot Plugin Development' started by leoyt185owner, Aug 4, 2018.

  1. Hello, i have one method to change value of Join message (true or false), but that method not excludes the player to put another word, the command is /jm true o /jm false and changes succesfully the config but is the same if in a hypothetical case he/she puts /jm another word how can i change this?
    Code (Text):
        @Override
        public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args) {
            if (cmd.getName().equalsIgnoreCase("jm")) {
                Player player = (Player) sender;
                if(player.hasPermission("core.*") && player.hasPermission("core.updatejm") && player.isOp()) {
                if(args.length == 0)
                {
                    sender.sendMessage("§a[MineBoneCore] §7Join Message");
                    sender.sendMessage("§bAvailable options");
                    sender.sendMessage("§etrue §a[Activates join message]");
                    sender.sendMessage("§efalse §c[Desactivates join message]");
                    return true;
                }
                StringBuilder str = new StringBuilder(args[0]);

                for(int i = 1; i < args.length; i++)
                {
                    str.append("  ").append(args[i]);
                }
                player.sendMessage("§a[MineBoneCore] §eJoin message updated");
                getConfig().set("Join-Message", str.toString());
                saveConfig();
                return true;
                }else{
                    sender.sendMessage("§cYou don't have permission to use this command");
            return true;
        }
     
  2. Just make it as boolean not as String.
     
  3. First of all, when setting a config value to true or false, you should be setting it to a boolean value, and in your case, you're using a String.

    If you want to convert a user's input into a boolean true/false, you can use Boolean#valueOf(String) in order to 'make' a boolean out of a String. It will be true only if the String equals 'true' (not case sensitive), otherwise it'll return false.
    Code (Java):
    getConfig().set("Join-Message", Boolean.valueOf(args[0]));
    I also don't understand why you're using a StringBuilder to join together all the arguments. If you only want the one argument (true or false), then just use args[0].
     
  4. Code (Java):
       @Override
        public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args) {
            if (cmd.getName().equalsIgnoreCase("jm")) {
                Player player = (Player) sender;
                if(player.hasPermission("core.*") && player.hasPermission("core.updatejm") && player.isOp()) {
                if(args.length == 0)
                {
                    sender.sendMessage("§a[MineBoneCore] §7Join Message");
                    sender.sendMessage("§bAvailable options");
                    sender.sendMessage("§etrue §a[Activates join message]");
                    sender.sendMessage("§efalse §c[Desactivates join message]");
                    return true;
                }
               
                if(args[0].equalsIngoreCase("true")) {
                getConfig().set("Join-Message", "true");
                saveConfig();
                player.sendMessage("§a[MineBoneCore] §eJoin message set to true");
                } else if(args[0].equalsIngoreCase("false")) {
                getConfig().set("Join-Message", "false");
                saveConfig();
                player.sendMessage("§a[MineBoneCore] §eJoin message set to false");
                } else {
                player.sendMessage("§a[MineBoneCore] §cJoin message must be true or false!");
                }
               
                return true;
                } else {
                    sender.sendMessage("§cYou don't have permission to use this command");
            return true;
        }
     
  5. Thank you!! it works