Solved Help needed with command arguments

Discussion in 'Plugin Development' started by technerder, Aug 22, 2016.

Thread Status:
Not open for further replies.
  1. Offline

    technerder

    hi, i am trying to create a name changer plugin however i cant seem to make the arguments work within the command, i am trying to do this: /cn <newname> so that whenever a player types this into chat it will change their name.

    Code:
    package technerder.shadowtech.namechanger;
    
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
    import org.bukkit.plugin.java.JavaPlugin;
    
    public class NameChanger extends JavaPlugin {
    
        @Override
        public void onEnable() {
            getLogger().info("NameChanger Plugin Has Been Enabled!");
        }
    
        @Override
        public void onDisable() {
            getLogger().info("NameChanger Plugin Has Been Disabled!");   
        }
       
        public boolean onCommand(CommandSender sender, Command command, String alias, String[] args) {
           
            Player player = (Player) sender;
           
            if (alias.equalsIgnoreCase("cn")) {
            if (args.length == 10) {player.setDisplayName(args[0]); player.setPlayerListName(args[0]);} else {} } return false;    
    
    }   
    }
    do i need to add a args.length == 10 then 11 then 12 and so on?
     
  2. @technerder
    And why do you need to check if the amount of args is 10? To me it only seems like you need 1.
     
  3. Offline

    technerder

    @AlvinB i want the player to change their name to a ign with anywhere near 1 to 16 characters long
     
  4. @technerder
    Well, then check the length of the string returned from the array not the length of the array itself.
     
  5. Offline

    technerder

    @AlvinB would you mind telling me how i would be able to do that.
     
  6. @technerder
    What you need to do is:
    • Get the first argument the user put in (the first entry in the "args" array).
    • Check if the Strings ".length()" method returns less than 16, if it does, add their prefix
     
  7. Offline

    technerder

    @AlvinB so like this? : if (args.length > 16) {player.setDisplayName(args[0]); player.setPlayerListName(args[0]);} else {} } return false;
     
  8. @technerder
    No, you are still checking the length of the args array. You need to first get a value from it, then check the .length() method.
     
  9. Offline

    technerder

    @AlvinB i am seriously confused, sorry if i am wasting your time.
     
  10. Offline

    Zombie_Striker

    @technerder
    1. Please don't stack lines. It makes it harder to read and debug.
    2. You need to check if there are any args before you get the first one. Do that by checking if args.length >0
    3. Use args[0].length to get the length of a string. After that, check if is is less than or equal to 16.
     
  11. Offline

    technerder

    thx i did


    Code:
    package technerder.shadowtech.namechanger;
    
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
    import org.bukkit.plugin.java.JavaPlugin;
    
    public class NameChanger extends JavaPlugin {
    
        @Override
        public void onEnable() {
            getLogger().info("NameChanger Plugin Has Been Enabled!");
        }
    
        @Override
        public void onDisable() {
            getLogger().info("NameChanger Plugin Has Been Disabled!");   
        }
       
        public boolean onCommand(CommandSender sender, Command command, String alias, String[] args) {
           
            Player player = (Player) sender;
           
            if (alias.equalsIgnoreCase("cn")) {
            if (args.length > 0) {player.setDisplayName(args[0]); player.setPlayerListName(args[0]);} else {} } return false;    
    
    }   
    }
    and it worked! thx for both of your help:

    @AlvinB @Zombie_Striker
     
Thread Status:
Not open for further replies.

Share This Page