Problem with setting config with command

Discussion in 'Plugin Development' started by stimoze, Jul 14, 2016.

Thread Status:
Not open for further replies.
  1. So there's my code:

    Code:
                    if (args[0].contains("a") || args[0].contains("b") || args[0].contains("c") || args[0].contains("d") || args[0].contains("e") || args[0].contains("f") || args[0].contains("g") || args[0].contains("h") || args[0].contains("i") || args[0].contains("j") || args[0].contains("k") || args[0].contains("l") || args[0].contains("m") || args[0].contains("n") || args[0].contains("o") || args[0].contains("p") || args[0].contains("q") || args[0].contains("r") || args[0].contains("s") || args[0].contains("t") || args[0].contains("u") || args[0].contains("v") || args[0].contains("w") || args[0].contains("x") || args[0].contains("y") || args[0].contains("z"))
                    {
                        player.sendMessage(prefix + " " + "§cThis command doesn't exist");
                    }
                    else
                    {
                    getConfig().set("healamount", args[0]);
                    saveConfig();
                    reloadConfig();
                    player.sendMessage(prefix + " " + "§aHeal amount has been set to §b" + args[0] + "§a!");
                    }
    It sets the "healamount" option in the config.
    But it looks like this:
    Code:
    healamount: '5'
    It doesn't work with this " ' "
     
  2. Offline

    Hyper_

    First, that is a completely inefficient way to check your args. Keep in mind you can also use characters like !@$% and so on. I would do it like this:
    Code:
    int i = getConfig().getInt("healamount");
    try {
      i = Integer.parseInt(args[0]);
    } catch (NumberFormatException e) {
      sender.sendMessage("§c" + args[0] + "§4 is not a number.");
    } finally {
      getConfig().set("healamount", i);
    }
    
    Second, I don't completely understand what you're trying to do, but if you don't want the single quotes/apostrophes, then set the config with an int like this one-liner (I recommend you use the one above):
    Code:
    getConfig().set("healamount", Integer.parseInt(args[0]));
    
    Third, if you're trying to use the ' character in YAML (config language), then just make a note to use two apostrophes (it will return one when using get(); )
     
  3. ' ' indicates that it is a string. You probably want it to be an int (without ' ')

    So instead of setting args[0] (a String) to the config, make it an int with Integer.parseInt(args[0]), (put this in a try-catch block if it's not an int)
    And remove reloadConfig()

    I just noticed that you check for every letter in args[0]. That can be simplified with regular expressions :D
    Code:
    if (args[0].matches(".*[a-z].*")) {
    .* matches any character.
    [a-z] matches a single character in the range between a and z (case sensitive). Use [a-zA-Z] if you don't want it to be case sensitive.

    It works like this: string must have any character, then a letter [a-z] and then again any character. (any character can be nothing too, "a.-." will work, "..,.-" doesn't)
     
    Last edited: Jul 14, 2016
  4. Thanks for helping, it works now :)
     
Thread Status:
Not open for further replies.

Share This Page