mirror of
https://github.com/PaperMC/Paper.git
synced 2025-01-01 05:47:45 +01:00
Improve CraftEntity and CraftPlayer equals and hashCode
Make sure the hash code does not change and also remove outdated equals logic from CraftPlayer. Long-term, the override there should be entirely removed, but this is good enough for now. Replacing some getHandle method calls with direct field access will also reduce overhead from casts that the overridden methods come with, at least until those are changed later on as well.
This commit is contained in:
parent
809fa1fbea
commit
a70953dfb0
@ -437,7 +437,7 @@ public abstract class CraftEntity implements org.bukkit.entity.Entity {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public UUID getUniqueId() {
|
public UUID getUniqueId() {
|
||||||
return this.getHandle().getUUID();
|
return this.entity.getUUID();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@ -496,21 +496,17 @@ public abstract class CraftEntity implements org.bukkit.entity.Entity {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean equals(Object obj) {
|
public boolean equals(Object obj) {
|
||||||
if (obj == null) {
|
if (this == obj) return true;
|
||||||
return false;
|
if (obj == null || getClass() != obj.getClass()) return false;
|
||||||
}
|
|
||||||
if (this.getClass() != obj.getClass()) {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
final CraftEntity other = (CraftEntity) obj;
|
final CraftEntity other = (CraftEntity) obj;
|
||||||
return (this.getEntityId() == other.getEntityId());
|
return this.entity == other.entity; // There should never be duplicate entities with differing references
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public int hashCode() {
|
public int hashCode() {
|
||||||
int hash = 7;
|
// The UUID and thus hash code should never change (unlike the entity id)
|
||||||
hash = 29 * hash + this.getEntityId();
|
return this.getUniqueId().hashCode();
|
||||||
return hash;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
@ -626,22 +626,10 @@ public class CraftPlayer extends CraftHumanEntity implements Player {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean equals(Object obj) {
|
public boolean equals(Object obj) {
|
||||||
if (!(obj instanceof OfflinePlayer)) {
|
// Long-term, this should just use the super equals... for now, check the UUID
|
||||||
return false;
|
if (obj == this) return true;
|
||||||
}
|
if (!(obj instanceof OfflinePlayer other)) return false;
|
||||||
OfflinePlayer other = (OfflinePlayer) obj;
|
return this.getUniqueId().equals(other.getUniqueId());
|
||||||
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;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@ -2052,7 +2040,7 @@ public class CraftPlayer extends CraftHumanEntity implements Player {
|
|||||||
@Override
|
@Override
|
||||||
public int hashCode() {
|
public int hashCode() {
|
||||||
if (this.hash == 0 || this.hash == 485) {
|
if (this.hash == 0 || this.hash == 485) {
|
||||||
this.hash = 97 * 5 + (this.getUniqueId() != null ? this.getUniqueId().hashCode() : 0);
|
this.hash = 97 * 5 + this.getUniqueId().hashCode();
|
||||||
}
|
}
|
||||||
return this.hash;
|
return this.hash;
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user