From 3ff2a7bc12f05f96363e05125511f9151153314e Mon Sep 17 00:00:00 2001 From: CraftBukkit/Spigot Date: Fri, 22 Sep 2023 07:36:30 +1000 Subject: [PATCH] SPIGOT-7486: Alternate approach to null profile names By: md_5 --- .../craftbukkit/CraftOfflinePlayer.java | 2 +- .../org/bukkit/craftbukkit/CraftServer.java | 3 +- .../bukkit/craftbukkit/block/CraftSkull.java | 8 +-- .../craftbukkit/inventory/CraftMetaSkull.java | 14 ++--- .../craftbukkit/profile/CraftGameProfile.java | 63 ------------------- .../profile/CraftPlayerProfile.java | 24 +++---- .../profile/PlayerProfileTest.java | 5 +- 7 files changed, 28 insertions(+), 91 deletions(-) delete mode 100644 paper-server/src/main/java/org/bukkit/craftbukkit/profile/CraftGameProfile.java diff --git a/paper-server/src/main/java/org/bukkit/craftbukkit/CraftOfflinePlayer.java b/paper-server/src/main/java/org/bukkit/craftbukkit/CraftOfflinePlayer.java index 34e025dd57..3814b72100 100644 --- a/paper-server/src/main/java/org/bukkit/craftbukkit/CraftOfflinePlayer.java +++ b/paper-server/src/main/java/org/bukkit/craftbukkit/CraftOfflinePlayer.java @@ -60,7 +60,7 @@ public class CraftOfflinePlayer implements OfflinePlayer, ConfigurationSerializa } // This might not match lastKnownName but if not it should be more correct - if (profile.getName() != null) { + if (!profile.getName().isEmpty()) { return profile.getName(); } diff --git a/paper-server/src/main/java/org/bukkit/craftbukkit/CraftServer.java b/paper-server/src/main/java/org/bukkit/craftbukkit/CraftServer.java index 2acdf4052f..c4d839843a 100644 --- a/paper-server/src/main/java/org/bukkit/craftbukkit/CraftServer.java +++ b/paper-server/src/main/java/org/bukkit/craftbukkit/CraftServer.java @@ -189,7 +189,6 @@ import org.bukkit.craftbukkit.metadata.PlayerMetadataStore; import org.bukkit.craftbukkit.metadata.WorldMetadataStore; import org.bukkit.craftbukkit.packs.CraftDataPackManager; import org.bukkit.craftbukkit.potion.CraftPotionBrewer; -import org.bukkit.craftbukkit.profile.CraftGameProfile; import org.bukkit.craftbukkit.profile.CraftPlayerProfile; import org.bukkit.craftbukkit.scheduler.CraftScheduler; import org.bukkit.craftbukkit.scoreboard.CraftCriteria; @@ -1676,7 +1675,7 @@ public final class CraftServer implements Server { if (result == null) { result = offlinePlayers.get(id); if (result == null) { - result = new CraftOfflinePlayer(this, new CraftGameProfile(id, null)); + result = new CraftOfflinePlayer(this, new GameProfile(id, "")); offlinePlayers.put(id, result); } } else { diff --git a/paper-server/src/main/java/org/bukkit/craftbukkit/block/CraftSkull.java b/paper-server/src/main/java/org/bukkit/craftbukkit/block/CraftSkull.java index dbed8a91a5..29a9cace15 100644 --- a/paper-server/src/main/java/org/bukkit/craftbukkit/block/CraftSkull.java +++ b/paper-server/src/main/java/org/bukkit/craftbukkit/block/CraftSkull.java @@ -2,6 +2,7 @@ package org.bukkit.craftbukkit.block; import com.google.common.base.Preconditions; import com.mojang.authlib.GameProfile; +import net.minecraft.SystemUtils; import net.minecraft.resources.MinecraftKey; import net.minecraft.server.MinecraftServer; import net.minecraft.world.level.block.entity.TileEntitySkull; @@ -16,7 +17,6 @@ import org.bukkit.block.data.BlockData; import org.bukkit.block.data.Directional; import org.bukkit.block.data.Rotatable; import org.bukkit.craftbukkit.entity.CraftPlayer; -import org.bukkit.craftbukkit.profile.CraftGameProfile; import org.bukkit.craftbukkit.profile.CraftPlayerProfile; import org.bukkit.craftbukkit.util.CraftNamespacedKey; import org.bukkit.profile.PlayerProfile; @@ -84,11 +84,11 @@ public class CraftSkull extends CraftBlockEntityState implement @Override public OfflinePlayer getOwningPlayer() { if (profile != null) { - if (profile.getId() != null) { + if (!profile.getId().equals(SystemUtils.NIL_UUID)) { return Bukkit.getOfflinePlayer(profile.getId()); } - if (profile.getName() != null) { + if (!profile.getName().isEmpty()) { return Bukkit.getOfflinePlayer(profile.getName()); } } @@ -103,7 +103,7 @@ public class CraftSkull extends CraftBlockEntityState implement if (player instanceof CraftPlayer) { this.profile = ((CraftPlayer) player).getProfile(); } else { - this.profile = new CraftGameProfile(player.getUniqueId(), player.getName()); + this.profile = new GameProfile(player.getUniqueId(), player.getName()); } } diff --git a/paper-server/src/main/java/org/bukkit/craftbukkit/inventory/CraftMetaSkull.java b/paper-server/src/main/java/org/bukkit/craftbukkit/inventory/CraftMetaSkull.java index e335076e09..5852fcd9c1 100644 --- a/paper-server/src/main/java/org/bukkit/craftbukkit/inventory/CraftMetaSkull.java +++ b/paper-server/src/main/java/org/bukkit/craftbukkit/inventory/CraftMetaSkull.java @@ -7,6 +7,7 @@ import java.util.Map; import java.util.Objects; import java.util.Set; import java.util.UUID; +import net.minecraft.SystemUtils; import net.minecraft.nbt.GameProfileSerializer; import net.minecraft.nbt.NBTTagCompound; import net.minecraft.resources.MinecraftKey; @@ -18,7 +19,6 @@ import org.bukkit.OfflinePlayer; import org.bukkit.configuration.serialization.DelegateDeserialization; import org.bukkit.craftbukkit.entity.CraftPlayer; import org.bukkit.craftbukkit.inventory.CraftMetaItem.SerializableMeta; -import org.bukkit.craftbukkit.profile.CraftGameProfile; import org.bukkit.craftbukkit.profile.CraftPlayerProfile; import org.bukkit.craftbukkit.util.CraftMagicNumbers; import org.bukkit.craftbukkit.util.CraftNamespacedKey; @@ -75,7 +75,7 @@ class CraftMetaSkull extends CraftMetaItem implements SkullMeta { if (tag.contains(SKULL_OWNER.NBT, CraftMagicNumbers.NBT.TAG_COMPOUND)) { this.setProfile(GameProfileSerializer.readGameProfile(tag.getCompound(SKULL_OWNER.NBT))); } else if (tag.contains(SKULL_OWNER.NBT, CraftMagicNumbers.NBT.TAG_STRING) && !tag.getString(SKULL_OWNER.NBT).isEmpty()) { - this.setProfile(new CraftGameProfile(null, tag.getString(SKULL_OWNER.NBT))); + this.setProfile(new GameProfile(SystemUtils.NIL_UUID, tag.getString(SKULL_OWNER.NBT))); } if (tag.contains(BLOCK_ENTITY_TAG.NBT, CraftMagicNumbers.NBT.TAG_COMPOUND)) { @@ -179,7 +179,7 @@ class CraftMetaSkull extends CraftMetaItem implements SkullMeta { @Override public boolean hasOwner() { - return profile != null && profile.getName() != null; + return profile != null && !profile.getName().isEmpty(); } @Override @@ -190,11 +190,11 @@ class CraftMetaSkull extends CraftMetaItem implements SkullMeta { @Override public OfflinePlayer getOwningPlayer() { if (hasOwner()) { - if (profile.getId() != null) { + if (!profile.getId().equals(SystemUtils.NIL_UUID)) { return Bukkit.getOfflinePlayer(profile.getId()); } - if (profile.getName() != null) { + if (!profile.getName().isEmpty()) { return Bukkit.getOfflinePlayer(profile.getName()); } } @@ -211,7 +211,7 @@ class CraftMetaSkull extends CraftMetaItem implements SkullMeta { if (name == null) { setProfile(null); } else { - setProfile(new CraftGameProfile(null, name)); + setProfile(new GameProfile(SystemUtils.NIL_UUID, name)); } return true; @@ -224,7 +224,7 @@ class CraftMetaSkull extends CraftMetaItem implements SkullMeta { } else if (owner instanceof CraftPlayer) { setProfile(((CraftPlayer) owner).getProfile()); } else { - setProfile(new CraftGameProfile(owner.getUniqueId(), owner.getName())); + setProfile(new GameProfile(owner.getUniqueId(), owner.getName())); } return true; diff --git a/paper-server/src/main/java/org/bukkit/craftbukkit/profile/CraftGameProfile.java b/paper-server/src/main/java/org/bukkit/craftbukkit/profile/CraftGameProfile.java deleted file mode 100644 index 51ee9102a4..0000000000 --- a/paper-server/src/main/java/org/bukkit/craftbukkit/profile/CraftGameProfile.java +++ /dev/null @@ -1,63 +0,0 @@ -package org.bukkit.craftbukkit.profile; - -import com.mojang.authlib.GameProfile; -import java.util.UUID; -import net.minecraft.SystemUtils; -import org.apache.commons.lang3.builder.ToStringBuilder; - -public final class CraftGameProfile extends GameProfile { - - private final boolean nullId; - private final boolean nullName; - - public CraftGameProfile(UUID id, String name) { - super((id == null) ? SystemUtils.NIL_UUID : id, (name == null) ? "" : name); - - this.nullId = (id == null); - this.nullName = (name == null); - } - - @Override - public UUID getId() { - return (nullId) ? null : super.getId(); - } - - @Override - public String getName() { - return (nullName) ? null : super.getName(); - } - - @Override - public boolean equals(Object o) { - if (this == o) { - return true; - } - if (o == null || getClass() != o.getClass()) { - return false; - } - GameProfile that = (GameProfile) o; - if ((this.getId() != null) ? !this.getId().equals(that.getId()) : (that.getId() != null)) { - return false; - } - if ((this.getName() != null) ? !this.getName().equals(that.getName()) : (that.getName() != null)) { - return false; - } - return true; - } - - @Override - public int hashCode() { - int result = (this.getId() != null) ? this.getId().hashCode() : 0; - result = 31 * result + ((this.getName() != null) ? this.getName().hashCode() : 0); - return result; - } - - @Override - public String toString() { - return new ToStringBuilder(this) - .append("id", this.getId()) - .append("name", this.getName()) - .append("properties", this.getProperties()) - .toString(); - } -} diff --git a/paper-server/src/main/java/org/bukkit/craftbukkit/profile/CraftPlayerProfile.java b/paper-server/src/main/java/org/bukkit/craftbukkit/profile/CraftPlayerProfile.java index bb3af1e0ad..d574a0bcd5 100644 --- a/paper-server/src/main/java/org/bukkit/craftbukkit/profile/CraftPlayerProfile.java +++ b/paper-server/src/main/java/org/bukkit/craftbukkit/profile/CraftPlayerProfile.java @@ -56,8 +56,8 @@ public final class CraftPlayerProfile implements PlayerProfile { public CraftPlayerProfile(UUID uniqueId, String name) { Preconditions.checkArgument((uniqueId != null) || !StringUtils.isBlank(name), "uniqueId is null or name is blank"); - this.uniqueId = uniqueId; - this.name = name; + this.uniqueId = (uniqueId == null) ? SystemUtils.NIL_UUID : uniqueId; + this.name = (name == null) ? "" : name; } // The Map of properties of the given GameProfile is not immutable. This captures a snapshot of the properties of @@ -75,12 +75,12 @@ public final class CraftPlayerProfile implements PlayerProfile { @Override public UUID getUniqueId() { - return uniqueId; + return (uniqueId.equals(SystemUtils.NIL_UUID)) ? null : uniqueId; } @Override public String getName() { - return name; + return (name.isEmpty()) ? null : name; } @Nullable @@ -120,7 +120,7 @@ public final class CraftPlayerProfile implements PlayerProfile { @Override public boolean isComplete() { - return (uniqueId != null) && (name != null) && !textures.isEmpty(); + return (getUniqueId() != null) && (getName() != null) && !textures.isEmpty(); } @Override @@ -133,12 +133,12 @@ public final class CraftPlayerProfile implements PlayerProfile { GameProfile profile = this.buildGameProfile(); // If missing, look up the uuid by name: - if (profile.getId() == null) { + if (profile.getId().equals(SystemUtils.NIL_UUID)) { profile = server.getProfileCache().get(profile.getName()).orElse(profile); } // Look up properties such as the textures: - if (profile.getId() != null) { + if (!profile.getId().equals(SystemUtils.NIL_UUID)) { GameProfile newProfile; try { newProfile = TileEntitySkull.fillProfileTextures(profile).get().orElse(null); // TODO: replace with CompletableFuture @@ -158,7 +158,7 @@ public final class CraftPlayerProfile implements PlayerProfile { @Nonnull public GameProfile buildGameProfile() { rebuildDirtyProperties(); - GameProfile profile = new CraftGameProfile(uniqueId, name); + GameProfile profile = new GameProfile(uniqueId, name); profile.getProperties().putAll(properties); return profile; } @@ -246,11 +246,11 @@ public final class CraftPlayerProfile implements PlayerProfile { @Override public Map serialize() { Map map = new LinkedHashMap<>(); - if (uniqueId != null) { - map.put("uniqueId", uniqueId.toString()); + if (getUniqueId() != null) { + map.put("uniqueId", getUniqueId().toString()); } - if (name != null) { - map.put("name", name); + if (getName() != null) { + map.put("name", getName()); } rebuildDirtyProperties(); if (!properties.isEmpty()) { diff --git a/paper-server/src/test/java/org/bukkit/craftbukkit/profile/PlayerProfileTest.java b/paper-server/src/test/java/org/bukkit/craftbukkit/profile/PlayerProfileTest.java index d399962d19..45a19d36d9 100644 --- a/paper-server/src/test/java/org/bukkit/craftbukkit/profile/PlayerProfileTest.java +++ b/paper-server/src/test/java/org/bukkit/craftbukkit/profile/PlayerProfileTest.java @@ -5,6 +5,7 @@ import com.mojang.authlib.properties.Property; import java.net.MalformedURLException; import java.net.URL; import java.util.UUID; +import net.minecraft.SystemUtils; import org.bukkit.configuration.InvalidConfigurationException; import org.bukkit.configuration.file.YamlConfiguration; import org.bukkit.configuration.serialization.ConfigurationSerialization; @@ -95,11 +96,11 @@ public class PlayerProfileTest { Assert.assertEquals("Unique id is not the same", UNIQUE_ID, profile1.getUniqueId()); Assert.assertEquals("Name is not the same", NAME, profile1.getName()); - CraftPlayerProfile profile2 = new CraftPlayerProfile(new CraftGameProfile(UNIQUE_ID, null)); + CraftPlayerProfile profile2 = new CraftPlayerProfile(new GameProfile(UNIQUE_ID, "")); Assert.assertEquals("Unique id is not the same", UNIQUE_ID, profile2.getUniqueId()); Assert.assertEquals("Name is not null", null, profile2.getName()); - CraftPlayerProfile profile3 = new CraftPlayerProfile(new CraftGameProfile(null, NAME)); + CraftPlayerProfile profile3 = new CraftPlayerProfile(new GameProfile(SystemUtils.NIL_UUID, NAME)); Assert.assertEquals("Unique id is not null", null, profile3.getUniqueId()); Assert.assertEquals("Name is not the same", NAME, profile3.getName()); }