Solved Player UUID Fails :P

Discussion in 'Plugin Help/Development/Requests' started by 2008Choco, Mar 15, 2015.

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

    2008Choco

    Hello :) I'm new to the developing world of Bukkit, and I am completely bombing it. Haha. I've been trying to get just a very very simple plugin to test, and I always have the same issue. UUID's! I have no idea how to get a players UUID and use it the same way as I would a player name. Here is the code that I have right now:

    Code:
    package me.choco.test;
    
    import java.util.UUID;
    
    import org.bukkit.Bukkit;
    import org.bukkit.Color;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
    import org.bukkit.plugin.java.JavaPlugin;
    
    public class Test extends JavaPlugin{
     
        @Override
        public void onEnable() {
            getLogger().info("Hello :)");
        }
    
        @Override
        public void onDisable() {
            getLogger().info("The plugin has been successfully disabled");
        }
    
        @SuppressWarnings("deprecation")
        public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args) {
            if (commandLabel.equalsIgnoreCase("namelength")){
                UUID uuid = Bukkit.getServer().getPlayer(args[0]).getUniqueId();
                Player player = Bukkit.getServer().getPlayer(uuid);
                player.sendMessage("Your name is " + sender.getName().length() + " characters long");
                return true;
            }
            if (commandLabel.equalsIgnoreCase("")){
               
            }
            return false;
        }
    }
    If someone could please explain to me how to use UUID's instead of Player Names? I tried suppressing the deprecation on Bukkit.getServer().getPlayer(args[0]) but it still didn't help >.< I just need a little help :3 Help a new developer out please :D I'd appreciate it
     
  2. Offline

    pie_flavor

    @2008Choco First of all, don't use commandLabel. Use cmd.getName(). cmd.getName() is always the same for that command, whereas commandLabel is the alias that the person used like /w instead of /warp.
    Second, you're doing fine. The deprecation bit was when you tried to use getPlayer(args[0]) because it's a gentle nudge to get devs to switch over to uuids.
    Is there anything specific that isn't working about your plugin?
     
  3. Offline

    2008Choco

    Thanks for the tip on the cmd.getName(). That'll be helpful for the future :D And as for the getPlayer(args[0]), I seem to have figured out a work around temporarily.
    Player player = (Player) sender;
    That works fine for player names. I believe that with the code I was using before, I got a null pointer exception (again, new to this. I can only assume it was that). But lets say I wanted to store information and had to use UUID's incase a player changed their names. How would I go about using UUID's instead?

    NOTE: The test plugin I'm doing now is okay because it's just using names. I'd like to know ^ for the future
     
    Last edited by a moderator: Mar 15, 2015
  4. Offline

    pie_flavor

    @2008Choco You can still do getPlayer(args[0]). It's deprecated to nudge devs toward uuids for player storage. And to get a player with a UUID, you just do Bukkit.getPlayer(id) or Server#getPlayer(id).
    The player will be null if a player with that name / uuid isn't on the server. Alternatively, you can do getOfflinePlayer, and then check if (player.hasPlayedBefore()). This method is useful if the player is not necessarily on the server.
    Also, if you want to use a uuid in a command, to get the uuid, use UUID.fromString().
     
    2008Choco likes this.
  5. Offline

    2008Choco

    @pie_flavor Okay I'm doing SOMETHING wrong, and I can't seem to figure out what it is >.> Sorry for the really basic questions. I'm teaching myself java so it's not the best.
    Code:
        public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args) {
            UUID playerUUID = UUID.fromString(sender.getName());
            Player player = Bukkit.getPlayer(playerUUID);
            //Name length command
            if (commandLabel.equalsIgnoreCase("namelength")){
                player.sendMessage(ChatColor.LIGHT_PURPLE + "Your name is " + ChatColor.DARK_PURPLE + player.getName().length() + ChatColor.LIGHT_PURPLE + " characters long");
                return true;
            }
    That's just the namelength command. But it had the Bukkit.getPlayer(id) and UUID.fromString thing. Before you blast me about "It's UUID.fromString(getName());!!!", I tried that and it didn't work. I tried this as an alternate and this didn't work either. It's just giving me a null pointer exception.
     
    Last edited: Mar 16, 2015
  6. Offline

    UniqueNameDen

    You need to get the player first...
    Player playerToDoStuffWith = Bukkit.getPlayer(args[1]);
    and: playerToDoStuffWith.getUniqueId();
     
  7. Offline

    2008Choco

    Thank you! I think that's exactly what I needed, @UniqueNameDen! Thank you a bunch for the help you two :) I appreciate it. Very helpful community. Especially to new developers ;P
     
  8. Offline

    pie_flavor

    @2008Choco Perhaps you misunderstood me. UUID.fromString() doesn't take a player name and convert it to UUID. fromString() reverses the process used to make toString().
     
Thread Status:
Not open for further replies.

Share This Page