Solved How to allow a username in command, then get the UUID of username for a console command

Discussion in 'Spigot Plugin Development' started by minecrftbuilder, May 26, 2018.

  1. Hello. I am new at coding and I am trying to make my first plugin.

    I am wanting to allow players to do something like /command {username} in-game and then this would run a command through the console using the UUID of the player.

    How would I put this in the "if command" line?
    How would I take that username and turn it into a UUID to use for a console command?



    This is my code so far, as I said before this is all very new to me and I am not sure if it is right:
    Code (Java):
    if (command.getName().equalsIgnoreCase("command" +  (args[0]))) {
             
                String playerName = args[0];
                // the Player object
                Player p = getServer().getPlayer(playerName);
                // UUID
                UUID uuid = p.getUniqueId();
             
                sender.sendMessage("You ran /command!");
                Bukkit.dispatchCommand(Bukkit.getConsoleSender(), "command" + "placeholder" + uuid +
                "Text Placeholder");

                return true;
            }
            return false;
            }
     
  2. Code (Java):
    if (command.getName().equalsIgnoreCase("command" +  (args[0]))) {
    command.getName() doesnt include the args so you should just check if it equals "command"

    also you should be checking the length of args to make sure there is an args[0]. along the same lines you should check if the player variable is null (it would be null if the player isnt online/no player has that name).

    you get the uuid correctly though if that was your question
     
  3. Okay thank you.

    What if the command has another part to it. Such as /command run {username}
    Would I change it to:
    Code (Java):
    if (command.getName().equalsIgnoreCase("command run"))
     
  4. no the command is still just "command". you would check if args[0] is "run"
     
  5. "Command" is just a placeholder. It would be like /plugin run {username}
     
  6. yeah i know, the command itself is just the first word (whatever you register in the plugin yml). everything else is an argument
    Code (Java):
    if (command.getName().equalsIgnoreCase("plugin")) {
        if (args[0].equalsIgnoreCase("run")) {
            //do stuff
        } else if (args[0].equalsIgnoreCase("reload")) {
            //do stuff
        } else {
            sender.sendMessage("Invalid usage!");
        }
        return true;
    }
    return false
     
    • Informative Informative x 1
  7. That was very helpful, thank you so much!!

    One more question, would it be "registering" that it should convert the username? Like if I entered /plugin run minecrftbuilder - would it know to turn this username into a UUID?
     
  8. im not exactly sure what youre asking. you would do what showed in your first post. Get the player object from the username then get the uuid from the player object
     
  9. @minecrftbuilder check for args.length first. If there are no arguments, you'll get a NullPointerException.
    It won't know how to do anything you don't tell it do. Not sure what your current full code looks like, but you were properly retrieving the player UUID. The odd part was the Bukkit.dispatchCommand.. You separate the first part ("command" + "placeholder") of the command unnecessarily (and without spaces between any of the words, at that) with results I'm uncertain of, since you don't appear to have anything run when the first arg is "placeholder" and the second arg is a UUID.
     
  10. Is this code correct?
    Code (Java):
            if (command.getName().equalsIgnoreCase("plugin")) {
               
                if (args[0].equalsIgnoreCase("run"))) {
               
                String playerName = args[0];
                // the Player object
                Player p = getServer().getPlayer(playerName);
                // UUID
                UUID uuid = p.getUniqueId();
    Here is an example: Player A types in /plugin run PlayerUser. PlayerUser's UUID will then turn into the variable "uuid" that can be used to run a command through console.
     
  11. yes its correct but as i said in my first post you should check the length of args and if p is null
     
    • Like Like x 1
  12. Will do, thank you!
     
  13. If the command the console runs is part of your plugin, rather than making a new command or possible argument solely for the console's use, you should simply run the code after the player uses the command..
    Also, your current code checks that the first argument is run, and if so, the player is the first argument ("run"). You'd need to change the args length check to 2 and define the Player as args[1].

    I feel like this is kind of spoonfeeding. I highly recommend you take a look more into commands and the sorts if you're uncertain.
     
  14. The console command is not part of my plugin.

    So the player is args[2] and then it should be args[1].equalsIgnoreCase ?
     
  15. In that case, ignore what I said.
    What? No. Counting starts from 0. The first argument, args[0], would be "run". The second argument, args[1], would be the player.
     
    • Informative Informative x 1
  16. Strahan

    Benefactor

    This isn't that complicated.

    Code (Text):
    /plugin run somePlayerName someotherarugment blah
    In that above example, /plugin is the actual command so you get that from command.getName(). Everything else is an argument. Arguments increment from zero so whatever is entered is counted up so:

    run = args[0]
    somePlayerName = args[1]
    someotherargument = args[2]
    blah = args[3]

    Now if someone typed in /plugin run blah somePlayerName someotherargument, then it becomes:

    run = args[0]
    blah = args[1]
    somePlayerName = args[2]
    someotherargument = args[3]

    Get it?
     
    • Informative Informative x 1
  17. Makes much more sense. Thank you both so much!