1.16.5 Comparing two players isn't working.

Discussion in 'Spigot Plugin Development' started by MiloAppleChief, Jun 2, 2021.

  1. In one of my plugins I need to cycle through a list of players and see if any of them match my "Main Player". To do that I run this if statement:
    Code (Text):
    if(gamePlayer.player.equals(runTime.player)) {
    and on the line right above it I put
    Code (Text):
    Bukkit.broadcastMessage("" + runTime.player.getName() + " - " + gamePlayer.player.getName());
    The print returns "MiloAppleCheif - MiloAppleChief" but the if statement still fails. Anyone have any idea why?
     
  2. maybe check uuid instead?
     
    • Agree Agree x 2
  3. Try == instead.
     
  4. This should only be used when comparing primitive values (int, double ....) for all objects, .equals() should be used (even for strings!)

    The equals method for CraftPlayer s looks like this:
    Code (Java):
    @Override
        public boolean equals(Object obj) {
            if (!(obj instanceof OfflinePlayer)) {
                return false;
            }
            OfflinePlayer other = (OfflinePlayer) obj;
            if ((this.getUniqueId() == null) || (other.getUniqueId() == null)) {
                return false;
            }

            boolean uuidEquals = this.getUniqueId().equals(other.getUniqueId());
            boolean idEquals = true;

            if (other instanceof CraftPlayer) {
                idEquals = this.getEntityId() == ((CraftPlayer) other).getEntityId();
            }

            return uuidEquals && idEquals;
        }
    so try to compare the UUID and if that is equal, there is an underlying issue.
     
  5. And for reference comparison...
     
    • Agree Agree x 1
  6. If you know what you are doing you can just as well make use of ==
     
  7. Strahan

    Benefactor

    I assume you mean you do know that, so I'm curious; how would you make == work if you wanted to, say, check if an item's displayname String is "Sword of Destiny"?
     
  8. He meant in this scenario, not strings.
     
  9. Strahan

    Benefactor

    That makes more sense.