Spigot 1.11.2: I wrote lots of 1.8 Plugins but now I am trying 1.11.2. In 1.11.2 is command args[0] = command.getName? or is args.lenght always 1 becaus args[0] is the command?
As far as I'm aware the commands api hasn't changed, just do whatever you used to do. args is the arguments the user used, and will never include the actual command (which is accessed through command.getName()).
In command "/test player simple" Part "test" is command.getName(); Part "player" is args[0]; Part "simple" is args[1]
//Commands public boolean onComand(CommandSender sender, Command cmd, String label, String[] args) { // sender --> Player Player p = null; if (sender instanceof Player) { p = (Player) sender; } //Command /setSpawn if (cmd.getName().equalsIgnoreCase("setSpawn")) { if (p != null) { if (p.isOp()) { if (args.length != 0) { return false; } Location pLoc = p.getLocation(); this.getConfig().set("Hub.spawn.X", pLoc.getX()); this.getConfig().set("Hub.spawn.Y", pLoc.getY()); this.getConfig().set("Hub.spawn.Z", pLoc.getZ()); this.getConfig().set("Hub.spawn.yaw", pLoc.getYaw()); this.getConfig().set("Hub.spawn.pitch", pLoc.getPitch()); this.getConfig().set("Hub.spawn.world", pLoc.getWorld()); this.saveConfig(); this.getConfig().options().copyDefaults(true); p.sendMessage(ChatColor.GREEN + "[CatBu] Erfolgreich Spawn auf " + pLoc.toString() + " gesetzt"); return true; } else { p.sendMessage(ChatColor.RED + "[CatBu] Du hast keine Recht, um diesen Command auszuführen!!!"); return true; } } else { System.out.println("[Hub] Spawns können nur ingame verändert werden!!!"); return true; } } return false; }
Why do you copyDefaults after you have already saved the config? Also: @dexelOK is correct /com subcom Command cmd - This is your base command "com" args[0] - This is your first subcommand "subcom" In the example above: args.length() == 1 as args contains 1 element at index 0
If its spaming your command back at you and not throwing any errors in the console or chat, that means you probably have your plugin.yml configured correctly, however if its not spamming usage, you might want to put that in your plugin.yml. Here is an example of how: http://pastebin.com/raw/qDb50K8R As your plugin.yml appears to be configured correctly, its most likely that your main class is not taking over control of the command. For this to happen, you need to set the executor for each and every command in your onEnable method Code (Text): @Override public void onEnable() { new events(this); initConfig(); getCommand("setSpawn").setExecutor(this); getCommand("spawn").setExecutor(this); getCommand("sethome").setExecutor(this); getCommand("home").setExecutor(this); } Ive done a few for you to give you the idea. My CommandInterpreted example code shows an easier way of doing this. https://www.spigotmc.org/resources/command-interpreter.37560/ Basically you could load up a list type with your commands and loop through them to set the executor.
Code (Text): if (args.length != 0) { return false; } When you return false, for any reason at all, then your command and usage is placed in chat. This is because returning false is telling the server that it was not a valid command. https://hub.spigotmc.org/javadocs/spigot/org/bukkit/command/CommandExecutor.html#onCommand(org.bukkit.command.CommandSender, org.bukkit.command.Command, java.lang.String, java.lang.String[]) Always always always return true. You can handle sending errors to the user yourself simply with sender#sendMessage(String) and then return true, then you wont have your command/usage spammed back at you in chat.
You mean I have to write:? if (args.lenght != 0) { p.sendmessage("the command is not correct!!!"); return true; }
Yes. Unless you want/need to specifically show the command syntax/usage to the user (like already stated). When you use "return false" sparingly then you know by the error message which parts of your code is failing. If you just return false everywhere anything could go wrong, then you have no idea which one is the cause. See this simple snippet for example: Code (Text): public class GetIP implements CommandExecutor { @Override public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) { // check to make sure its a player using the command if (!(sender instanceof Player)) { sender.sendMessage("Player only command"); return true; // we handled the error output ourselves } // check to make sure the player has permission to use the command if (!sender.hasPermission("command.getip")) { sender.sendMessage("No permission for this command"); return true; // we handled the error output ourselves } // check to see the required syntax is correct if (args.length < 1) { // we need 1 arg, so who cares if there 10 args supplied. we only want the first one sender.sendMessage(cmd.getDescription()); // if no args supplied, lets send them the command description return false; // then show the player the command syntax as registered in plugin.yml } // finally lets check to make sure the supplied arg is an online player Player target = Bukkit.getPlayer(args[0]); if (target == null) { sender.sendMessage("Player not online"); return true; // handled error message ourselves } // that wasn't so hard. lets get the info about the player we want sender.sendMessage(target.getName() + "'s IP: " + target.getAddress().getAddress().getHostAddress()); return true; // handled command output as expected } }
Yes, assuming you want to not allow arguments and assuming you use proper spelling and capitalization. Really, there is no point though. If the user passes arguments when the command takes none, it won't hurt anything. In your code, you are doing player detection in a rather awkward way. Rather than setting p to null then checking if it's a player and casting p if they are, just check if sender isn't a player and kick back a "can't be used at console" type msg then return. That saves you from having to test it over and over down the road.