Solved UUID is very confusing to me

Discussion in 'Plugin Development' started by iBecameALoaf, Jun 8, 2014.

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

    iBecameALoaf

    So I just updated my craftbukkit to 1.7.9 R0.2 and I noticed all of my player.getNameExact()'s were deprecated. I did some research and figured out it was due to he new UUID stystem upcoming in minecraft 1.8. Well, I have a slight problem. I have a custom banning feature and it uses player.getNameExact() and it has functionality with offline names (I just made a config list of all players who have joined before.). I thought I was unable to get a UUID from an offline names, so it would end up screwing my whole banning system over, but then I saw this neat little thing called UUIDFetcher. But I still have a problem... I'm slightly confused on how exactly I can get an offline players UUID. If anyone could provide an example of getting an offline players UUID, that would be great.
     
  2. Offline

    MOMOTHEREAL

    Use the UUIDFetcher to get a UUID from a player name...?
     
  3. Offline

    teej107

    Code:java
    1. OfflinePlayer player = plugin.getServer().getOfflinePlayer(args[1]);
    I was able to use player.getUniqueId() using that and I was using a 1.7.5 build so 1.7.9 should have that too. This was code taken from one of my plugins.
     
  4. Offline

    iBecameALoaf

    teej107
    I'll try this :)

    EDIT: Doesn't work :/
    Here's my code for an online player that I just realized won''t work in 1.8:

    Code:
        if (commandLabel.equalsIgnoreCase("kick")) {
                    if (player.hasPermission("restrike.rank.mod")) {
                        if (args.length >= 1) {
                            Player kicked = plugin.getServer().getPlayerExact(args[0]); // Will break stuff
                            if (kicked != null) {
                                StringBuilder sb = new StringBuilder();
                                for (int i = 1; i < args.length; i++) {
                                    sb.append(args[i]).append(" ");
                                }
                                String reason;
     
                                if (args.length != 1) {
                                    reason = sb.toString().trim();
                                } else {
                                    reason = "Kicked by a staff member.";
                                }
     
                                kicked.kickPlayer("§cKicked for:§6 " + reason
                                + " \n §cKicked by:§6 " + player.getName());
                            } else {
                                player.sendMessage(plugin.getPrefix(PrefixType.BAD)
                                + "Player is null.");
                            }
                        } else {
                            player.sendMessage(plugin.getPrefix(PrefixType.BAD)
                            + "Incorrect Syntax. /kick (player) (reason)");
     
                        }
                    } else {
                        player.sendMessage(plugin.getPrefix(PrefixType.BAD)
                        + "You don't have permission.");
                    }
                } 
    Well... Here's the thing, I can't figure out HOW to use UUIDFetcher, so I was looking for an example of using it :)

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 8, 2016
  5. Offline

    MOMOTHEREAL

    https://forums.bukkit.org/threads/player-name-uuid-fetcher.250926/
    He explains it in his thread.

     
  6. Offline

    BlueMustache

    I somewhat also find this confusing, but here, take this.
    I haven't tried it out yet, but I slapped it together. Hope it helps! :D

    Code:java
    1. public static UUID getUUID(Player p) {
    2. String key = "9YSlLJpyKpFrm412chs7CrF3N1NsAx";
    3. //this is my API key for theminecraftapi.com
    4. //please get your own!
    5. URL url;
    6. try {
    7. url = new URL("[url]http://theminecraftapi.com/v1/[/url]" + p +"&apiKey=" + key);
    8. BufferedReader br = new BufferedReader(new InputStreamReader(url.openStream()));
    9. String output = br.readLine();
    10. br.close();
    11. UUID finaloutput = UUID.fromString(output);
    12. return finaloutput;
    13. } catch (MalformedURLException e) {
    14. e.printStackTrace();
    15. } catch (IOException e) {
    16. e.printStackTrace();
    17. }
    18. return null;
    19. }


    -Blue
    P.S. No thanks needed. :p
     
  7. Offline

    Garris0n

    Why won't it work? Nothing in that code is broken.
     
  8. Offline

    iBecameALoaf

    It works right now, but in 1.8 the getPlayerExact will be. I need to find a way to make a player variable from a UUID whether it is offline or online, and I need help with that.
     
  9. Offline

    BlueMustache

    iBecameALoaf
    I just gave you a chunk of code.
    That does that.
    No limit on the API I don't think. Also, it will work whether they are online or off.
     
  10. iBecameALoaf getPlayerExact() is only for online players regardless of the version. That part of your plugin shows no compatibility with offline players.
     
  11. Offline

    iBecameALoaf

    I know it does that, but the method requires that you enter a player name, and what I'm trying to do is get a player variable.

    I'll try to explain this in a more simple way, I want to kick a player using an argument from a command. It works fine in the current update, but it will be broken in the next update due to bukkit removing getPlayerExact() and I need another way to do this.

    It would really help if you could just modify my code that I posted to work with the 1.8 UUID's and I can go from that :)

    Not the kicking part, but my banning part does. It bans a player and stores their username in a file regardlessly, I just need to make a player variable out of an argument in a command using UUID's.

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 8, 2016
  12. Offline

    BlueMustache

    iBecameALoaf

    I don't understand.
    So you want to input a "Player Name", which is converted to a UUID, then get the Player from the UUID?

    EDIT: What needs to be the input. And what is the output?
     
  13. Offline

    iBecameALoaf

    Yes :) Input is player name, output is actual player (so I can kick them). This is possible as of now, but it won't be in 1.8 as I said before.
     
  14. iBecameALoaf In that case the "will break stuff" is incorrect - you'll still be able to get by name in 1.8
     
  15. Offline

    iBecameALoaf

    but they will be able to change it... and it will mess up offline functionality of my ban plugin .
     
  16. iBecameALoaf You're just kicking them in this code, not storing anything long term. They can't change their name while they're online, so it doesn't matter.
     
  17. Offline

    iBecameALoaf

    just noticed that, i'll post my ban code :)

    Code:
    if (commandLabel.equalsIgnoreCase("ban")) {
                    if (player.hasPermission("restrike.rank.mod")) {
                        if (args.length >= 1) {
                            if (plugin.getConfig().getStringList("JoinedBefore")
                            .contains(args[0])) {
                                StringBuilder sb = new StringBuilder();
                                for (int i = 1; i < args.length; i++) {
                                    sb.append(args[i]).append(" ");
                                }
                                String reason;
     
                                if (args.length != 1) {
                                    reason = sb.toString().trim();
                                } else {
                                    reason = "Banned by a staff member.";
                                }
     
                                if (plugin.getConfig().contains(
                                "Banned-Players." + args[0])) {
                                    plugin.getConfig()
                                    .set("Banned-Players." + args[0] + ".reason",
                                    reason);
                                    plugin.getConfig().set(
                                    "Banned-Players." + args[0] + ".banner",
                                    player.getName());
                                    plugin.getConfig().options().copyDefaults(true);
                                    plugin.saveConfig();
                                } else {
                                    plugin
                                    .getConfig()
                                    .addDefault(
                                    "Banned-Players." + args[0] + ".reason", reason);
                                    plugin.getConfig().addDefault(
                                    "Banned-Players." + args[0] + ".banner",
                                    player.getName());
                                    plugin.getConfig().options().copyDefaults(true);
                                    plugin.saveConfig();
                                }
                                Player banned = plugin.getServer().getPlayer(
                                args[0]);
                                if (banned != null) {
                                    banned.kickPlayer("§cBanned for:§6 " + reason
                                    + "\n" + "§cBanned by:§6 " + player.getName());
                                }
     
                                for (Player players : plugin.getServer()
                                .getOnlinePlayers()) {
                                    if (players == player) {
                                        players.sendMessage(plugin
                                        .getPrefix(PrefixType.GOOD)
                                        + "§bYou§3 banned"
                                        + "§b "
                                        + args[0]
                                        + "§3 for " + "§b" + reason);
     
                                    } else {
                                        players.sendMessage(plugin
                                        .getPrefix(PrefixType.GOOD)
                                        + "§b"
                                        + player.getName()
                                        + " §3banned §b"
                                        + args[0] + "§3 for " + "§b" + reason);
                                    }
                                }
                            } else {
                                player.sendMessage(plugin.getPrefix(PrefixType.BAD)
                                + "Player has never joined before.");
                            }
                        } else {
                            player.sendMessage(plugin.getPrefix(PrefixType.BAD)
                            + "Incorrect Syntax. /kick (player) (reason)");
                        }
                    }
                }
     
  18. Offline

    Garris0n

  19. Offline

    BlueMustache

    iBecameALoaf

    This should work.
    Just get an API key.
    Now, I want you to learn from this.
    Most people won't "Spoonfeed" you.
    This should work though, but I haven't tested it.

    Code:java
    1. public static Player getPlayerFromName(String p) {
    2. String key = "9YSlLJpyKpFrm412chs7CrF3N1NsAx";
    3. //this is my API key for theminecraftapi.com
    4. //please get your own!
    5. URL url;
    6. try {
    7. url = new URL("[URL]http://theminecraftapi.com/v1/[/URL]" + p +"&apiKey=" + key);
    8. BufferedReader br = new BufferedReader(new InputStreamReader(url.openStream()));
    9. String output = br.readLine();
    10. br.close();
    11. UUID uuid = UUID.fromString(output);
    12. Player outputplayer = Bukkit.getServer().getPlayer(uuid);
    13. return outputplayer;
    14. } catch (MalformedURLException e) {
    15. e.printStackTrace();
    16. } catch (IOException e) {
    17. e.printStackTrace();
    18. }
    19. return null;
    20. }


    -Blue
    Hope it helps!

    iBecameALoaf
    To re-iterate, learn from this!
    Don't make me regret it.
    And finally, good luck! :p

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 8, 2016
  20. Offline

    iBecameALoaf

    Alright, I'll try this. And... Where can I find an API key, I've never done anything with them before.

    EDIT: I found something weird... When I use plugin.getServer().getPlayerExact(), it is deprecated, but when I use Bukkit.getServer().getPlayerExact(), it's not... Any ideas why?
     
  21. Offline

    BlueMustache

Thread Status:
Not open for further replies.

Share This Page