Hello I am trying to get the integer argument from a command, and if my plugin finds the strings that match that number in the config, clear them. My code: Code:java public boolean onCommand(CommandSender sender, Command cmd, String CommandLabel, String[] args){ Player p = null; if ((sender instanceof Player)) { p = (Player)sender; } { if (!p.hasPermission("plugin.clearpoint")) { p.sendMessage(ChatColor.RED + " You do not have permission."); } if (args.length == 1) { p.sendMessage(ChatColor.RED + " Error: Too many Arguments"); return true; } if (args.length == 0){ int Tutnumber = Integer.parseInt(args[0]); plugin.getConfig().set("Point." + Tutnumber + ".Message", ""); plugin.getConfig().set("Point." + Tutnumber + ".Location.world", ""); plugin.getConfig().set("Point." + Tutnumber + ".Location.x", ""); plugin.getConfig().set("Point." + Tutnumber + ".Location.y", ""); plugin.getConfig().set("Point." + Tutnumber + ".Location.z", ""); plugin.getConfig().set("Point." + Tutnumber + ".Location.yaw",""); plugin.getConfig().set("Point." + Tutnumber + ".Location.pitch", ""); TutPlus.getInstance().saveConfig(); p.sendMessage(ChatColor.DARK_AQUA + " Tutorial Point number " +ChatColor.RED + Tutnumber +ChatColor.DARK_AQUA+ " cleared!"); plugin.reloadConfig(); return true; } p.sendMessage(ChatColor.RED + " Unknown argument: " + args[0] + "Make sure you are using an Integer!"); return true; } } { I know this command is completely wrong, so if anyone could re format it and add in the integer stuff i would love you forever! thanks guys Updated my code a little bit, still not working EDIT by Moderator: merged posts, please use the edit button instead of double posting.
You have two problems (that I can see). One, you check to make sure it's a player using the command. If it is, you set "p" to the sender, otherwise you do nothing. You need to change it to something like this: Code:java Player p = null;if (sender instanceof Player) { p = (Player) sender;} else { sender.sendMessage("Player only command!"); return true; //This will exit the method.} Basically, if it isn't a player calling the command (read: console), it will send the sender a message and exit. Two, you want this command to have 1 argument based on this code Code:java int int Tutnumber = Integer.parseInt(args[0]); (computers start counting at 0 instead of 1, so think of args[0] as args[0 + 1].) But that code is inside your if statement that makes sure there is NO arguments, the first if statement (args.length == 1) should read Code:java if (args.length != 1) { and the second statement (args.length == 0) should read Code:java if (args.length == 1) {
It's sometimes needed if you define the object in brackets but you want to use it behind the brackets like this: int i = null; try { i = Integer.parseInt(args[0] } catch { player.sendMessage("Invalid Number") } And do here you need i!
Alternatively you can just do: Code:java Player p;if (sender instanceof Player) { p = (Player) sender;} else { sender.sendMessage("Player-only command"); return true;} But I don't see the point of this being a user-only command? Why couldn't the console use it?
1Rogue thats true, i might change that, and thanks for the help EvilKanoa That worked One question, any ideas on how i would be able to send a message saying "No point INTEGER found" if the command doesn't find it in the config?