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.
Yes there is a way. How to make commands: http://wiki.bukkit.org/Plugin_Tutorial#Commands How to use configs: http://wiki.bukkit.org/Introduction_to_the_New_Configuration Try to make it and if you get stuck post here :}
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?
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
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
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?
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 ?
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
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.
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>.