Comparing UUID's

Discussion in 'Plugin Development' started by RickyBGamez, Mar 22, 2015.

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

    RickyBGamez

    I'm trying to send a message to everyone, if a UUID matches. Here is the code below:

    Code:
    UUID playerUUID = player.getUniqueId();
            String rbgUUID = "f4e97005-b497-44ad-9f45-94ac5ffd2ddd";
    
            if(playerUUID == rbgUUID){
                player.sendMessage("Something Here");
            }else{
                player.sendMessage("Something Else");
            }
    
     
  2. Invisible

    nverdier

    @RickyBGamez You are checking if a UUID is equal to a String. This will never be true. Either convert playerUUID to a String, or convert rbdUUID to a UUID. If you are comparing the Strings, then use Object#equals(Object object)
     
  3. Offline

    uksspy

    @RickyBGamez
    '==' only works for comparing primitive times and enums. For Strings use .equals()
     
  4. Invisible

    nverdier

    @uksspy The == operator works in a different way. It will almost never work for Strings. For those you should use #equals(). But I've ninja'd you :p
     
    Last edited by a moderator: Mar 24, 2015
  5. Offline

    Konato_K

    @uksspy Just for the record, == works for objects if and only if they are exactly the same object, if it's another object with the same contents it will return false
     
  6. Offline

    RickyBGamez

    Ok, but how would I do this? How would I make rbgUUID into a UUID instead of String.
     
  7. Invisible

    nverdier

  8. Offline

    RickyBGamez

    Alright, thanks!
     
  9. Invisible

    nverdier

  10. Offline

    uksspy

    @Konato_K Right, thanks for clearing that up, I should have mentioned it.
     
  11. Invisible

    nverdier

  12. Offline

    decontamin4t0R

    Nope, if you want to directly compare them, you've got two ways:
    a) Get both as String and make equal:
    Code:
            UUID playerUUID = player.getUniqueId();
            String rbgUUID = "f4e97005-b497-44ad-9f45-94ac5ffd2ddd";
    
            if(playerUUID.toString() == rbgUUID){
                player.sendMessage("Something Here");
            }else{
                player.sendMessage("Something Else");
            }
    
    or
    b) Get both as UUID and check for equality with UUID#equals:
    Code:
            UUID playerUUID = player.getUniqueId();
            String rbgUUID = "f4e97005-b497-44ad-9f45-94ac5ffd2ddd";
            UUID rbgUUIDo = UUID.fromString(rbgUUID);
    
            if(playerUUID.equals(rbgUUIDo)){
                player.sendMessage("Something Here");
            }else{
                player.sendMessage("Something Else");
            }
    
    Because new Object("as") and new Object("as") aren't the same. (Except Exceptions.)
     
  13. Offline

    Konato_K

  14. Offline

    mythbusterma

    @decontamin4t0R

    Why are you posting something that is completely wrong? Strings will (almost) never be equal using the equality operator, as it will compare the equality of the references of the Objects, not the contents of the Objects. Use String#equals(Object) to compare Strings.


    @Konato_K

    Unless you never want the comparison to be true, I suppose.
     
Thread Status:
Not open for further replies.

Share This Page