Set config value through command

Discussion in 'Plugin Development' started by KeybordPiano459, Aug 20, 2012.

  1. Offline

    KeybordPiano459

    dev.bukkit.org profile:
    CFUSERNAME
    My Plugins (CFCOUNT)
    I was wondering if there was a way to set a value in the config through a command. For example, /example <message> would set the config value 'money', so when you go to the config, you see money set to whatever <message> was.
  2. Offline

    Digi

    dev.bukkit.org profile:
    CFUSERNAME
    My Plugins (CFCOUNT)
  3. Offline

    KeybordPiano459

    dev.bukkit.org profile:
    CFUSERNAME
    My Plugins (CFCOUNT)
  4. Offline

    KeybordPiano459

    dev.bukkit.org profile:
    CFUSERNAME
    My Plugins (CFCOUNT)
    I'm thinking about this before I actually write the code. The config value that the command will be setting in my plugin would contain multiple words, with spaces. How do I get it to think that the multiple words are one argument?
  5. Offline

    Digi

    dev.bukkit.org profile:
    CFUSERNAME
    My Plugins (CFCOUNT)
    You put them back together.
    Code:
    // in your command
    
    int startArg = 0; // start from first argument, you can change this tough...
    
    if(args.length <= startArg)
        return false; // less args than required ?
    
    StringBuilder str = new StringBuilder(args[startArg]);
    
    for(int i = startArg + 1; i < args.length; i++)
    {
         str.append(' ').append(args[i]);
    }
    
    String message = str.toString(); // all arguments from startArg blend together in one string

    This post has been edited 3 times. It was last edited by Digi Aug 20, 2012.
  6. Offline

    jacklin213

    dev.bukkit.org profile:
    CFUSERNAME
    My Plugins (CFCOUNT)
    Code:
    if (commandLabel.equalsIgnoreCase("money")){
        if (args.length >=1) {
           String money = args[1];
           int moneyamount = Integer.parseInt(money);
           this.getConfig().set("Money",moneyamount);
           this.saveConfig();
           sender.sendMessage("MoneyAmount has been changed to "    + moneyamount);
           return true;
        }
    }
    is how i would do it
  7. Offline

    Digi

    dev.bukkit.org profile:
    CFUSERNAME
    My Plugins (CFCOUNT)
    @jacklin213
    Your code makes the 2nd argument into an integer an sets it into the config... but:

    This post has been edited 1 time. It was last edited by Digi Aug 20, 2012.
  8. Offline

    KeybordPiano459

    dev.bukkit.org profile:
    CFUSERNAME
    My Plugins (CFCOUNT)
    I now have this:
    Code:
    if(sender instanceof Player) {
            if(cmd.getName().equalsIgnoreCase("ad")) {
                Player player = (Player) sender;
                int startArg = 0;
                if(args.length <= startArg)
                    return false;
                StringBuilder str = new StringBuilder(args[startArg]);
                for(int i = startArg + 1; i < args.length; i++)
                {
                    str.append(' ').append(args[i]);
                }
                String message = str.toString();
                if(args.length != 1) {
                    player.sendMessage("[" + ChatColor.AQUA + "Newspaper" + ChatColor.RESET + "] " + ChatColor.RED + "Incorrect usage! Type /ad <message>");
                } else if(args.length == 1) {
                    this.getConfig().set("path.to.string", message);
                }
            }
        } else {
            sender.sendMessage("[" + ChatColor.AQUA + "Newspaper" + ChatColor.RESET + "] " + ChatColor.RED + "This command can only be executed by players.");
        }
    It doesn't work, it returns 'if(args.length != 1)'. Why, though?
  9. Offline

    Digi

    dev.bukkit.org profile:
    CFUSERNAME
    My Plugins (CFCOUNT)
    You're checking if command doesn't have exacly 1 argument...

    You need to move that message before my "return false", that's where it checks if it has at least 1 argument.

    Still, if you need all arguments, this might seem simplier:

    Code:
    if(args.length == 0)
    {
        sender.sendMessage("You must type a message.");
        return true;
    }
    
    StringBuilder str = new StringBuilder(args[0]);
    
    for(int i = 1; i < args.length; i++)
    {
        str.append(' ').append(args[i]);
    }
    
    getConfig().set("node", str.toString());
    Also, why do you limit that command to players, why can't it also be used from console ?

    This post has been edited 2 times. It was last edited by Digi Aug 20, 2012.
  10. Offline

    KeybordPiano459

    dev.bukkit.org profile:
    CFUSERNAME
    My Plugins (CFCOUNT)
    I can't let it be used from console because of a problem with my vault integration.
    EDIT: Thanks, this works, and I'm happy :D

    This post has been edited 1 time. It was last edited by KeybordPiano459 Aug 20, 2012.
  11. Offline

    Digi

    dev.bukkit.org profile:
    CFUSERNAME
    My Plugins (CFCOUNT)
    Ehh... and that's your fix, limiting features ? That's a poor fix.

    You should fix your vault integration and allow command to be used from console.

    This post has been edited 1 time. It was last edited by Digi Aug 20, 2012.
  12. Offline

    KeybordPiano459

    dev.bukkit.org profile:
    CFUSERNAME
    My Plugins (CFCOUNT)
    That's what I really want to do, and I'm working on it. Also, does you or anyone else know a way to fix it? I used the code that you showed me, but the string is only set until the server reloads or restarts. After I typed in the command, I looked at the config before reloading the server, and it was still the string that was there before I typed /ad <message>.
  13. Offline

    jacklin213

    dev.bukkit.org profile:
    CFUSERNAME
    My Plugins (CFCOUNT)
    this.saveConfig();

Share This Page