getPlayer by uuid

Discussion in 'Plugin Development' started by Maximvdw, Jan 13, 2014.

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

    Maximvdw

    Dear,

    How can I get a Player from their uuid? I stored a uuid in a database but Bukkit.getServer().getPlayer() only works for names.

    Code:
    Code:java
    1. /**
    2.   * Get all warps from the database
    3.   */
    4. public static void getWarps() {
    5. final WarpPlugin wp = WarpPlugin.getInstance();
    6. // Thread the database connection
    7. Bukkit.getScheduler().runTaskAsynchronously(wp, new Runnable() {
    8. public void run() {
    9. String query = getQuery("WARPS_SELECT");
    10. query = query.replace("{PREFIX}", Configuration.prefix);
    11. ResultSet result = db.query(query);
    12. SendConsole.info("Loading warps from database ..."); // Log to
    13. // console
    14. // Get the result
    15. try {
    16. while (result.next()) {
    17. try {
    18. String name = result.getString("name");
    19. String uuid = result.getString("owner");
    20. Player owner = Bukkit.getServer().getPlayer(uuid);
    21. int id = result.getInt("id");
    22. float x = result.getFloat("x");
    23. float y = result.getFloat("y");
    24. float z = result.getFloat("z");
    25. float pitch = result.getFloat("pitch");
    26. float yaw = result.getFloat("yaw");
    27. World world = wp.getServer().getWorld(
    28. result.getString("world"));
    29. Location location = new Location(world, x, y, z,
    30. yaw, pitch);
    31. Warp warp = new Warp(name, owner, location, id);
    32. warp.saveWarpCache();
    33. } catch (Exception ex) {
    34. ex.printStackTrace();
    35. }
    36. }
    37. } catch (SQLException e) {
    38. e.printStackTrace();
    39. }
    40. }
    41. });
    42. }


    Best Regards,
    Maximvdw
     
  2. Maximvdw
    Code:
    public Entity getEntityByUUID(UUID id) {
        for (World w : Bukkit.getServer().getWorlds()) {
            for (Entity e : w.getEntities()) {
                if (e.getUUID() == id) {
                    return e;
                }
            }
        }
     
        return null;
    }
     
  3. Offline

    Maximvdw

    Thanks ;)

    My code now:
    Code:java
    1. public static Player getPlayerByUUID(UUID id) {
    2. for (World w : Bukkit.getServer().getWorlds()) {
    3. for (Entity e : w.getEntities()) {
    4. if (e.getUniqueId() == id) {
    5. if (e instanceof Player){
    6. return (Player)e;
    7. }
    8. }
    9. }
    10. }
    11. return null;
    12. }
     
  4. Offline

    metalhedd

    don't use == to compare 2 UUIDs, use uuid.equals(otherUUID)
     
  5. metalhedd
    I might be mistaken, but isn't == used to compare object instances? Or is there something different in UUID?
     
  6. Offline

    maxben34

    It is used to compare primitive types.
     
  7. Offline

    metalhedd

    == will ensure they are the exact same instance of the same UUID, which is not what you want, you only want to make sure the values are equivalent, not that they are the same object in memory
     
  8. Offline

    RawCode

    maxben34
    primitive types and enums.
     
Thread Status:
Not open for further replies.

Share This Page