Fixup player profile getters and constructor to expected nullability (#9770)

This commit is contained in:
Nassim Jahnke
2023-10-05 15:31:24 +10:00
parent b0704c757b
commit 8ae1a1746e
2 changed files with 21 additions and 15 deletions

View File

@@ -59,7 +59,7 @@ index 0000000000000000000000000000000000000000..00000000000000000000000000000000
+ } + }
+ +
+ public CraftPlayerProfile(UUID id, String name) { + public CraftPlayerProfile(UUID id, String name) {
+ this.profile = new GameProfile(id, name); + this.profile = new GameProfile(id != null ? id : Util.NIL_UUID, name != null ? name : "");
+ } + }
+ +
+ public CraftPlayerProfile(GameProfile profile) { + public CraftPlayerProfile(GameProfile profile) {
@@ -103,16 +103,17 @@ index 0000000000000000000000000000000000000000..00000000000000000000000000000000
+ @Nullable + @Nullable
+ @Override + @Override
+ public UUID getId() { + public UUID getId() {
+ return profile.getId(); + return profile.getId().equals(Util.NIL_UUID) ? null : profile.getId();
+ } + }
+ +
+ @Override + @Override
+ @Deprecated(forRemoval = true) + @Deprecated(forRemoval = true)
+ public UUID setId(@Nullable UUID uuid) { + public UUID setId(@Nullable UUID uuid) {
+ GameProfile prev = this.profile; + final GameProfile previousProfile = this.profile;
+ this.profile = new GameProfile(uuid, prev.getName()); + final UUID previousId = this.getId();
+ copyProfileProperties(prev, this.profile); + this.profile = new GameProfile(previousProfile.getId(), previousProfile.getName());
+ return prev.getId(); + copyProfileProperties(previousProfile, this.profile);
+ return previousId;
+ } + }
+ +
+ @Override + @Override
@@ -130,7 +131,7 @@ index 0000000000000000000000000000000000000000..00000000000000000000000000000000
+ @Deprecated(forRemoval = true) + @Deprecated(forRemoval = true)
+ public String setName(@Nullable String name) { + public String setName(@Nullable String name) {
+ GameProfile prev = this.profile; + GameProfile prev = this.profile;
+ this.profile = new GameProfile(prev.getId(), name); + this.profile = new GameProfile(prev.getId(), name != null ? name : "");
+ copyProfileProperties(prev, this.profile); + copyProfileProperties(prev, this.profile);
+ return prev.getName(); + return prev.getName();
+ } + }
@@ -213,7 +214,7 @@ index 0000000000000000000000000000000000000000..00000000000000000000000000000000
+ MinecraftServer server = MinecraftServer.getServer(); + MinecraftServer server = MinecraftServer.getServer();
+ String name = profile.getName(); + String name = profile.getName();
+ GameProfileCache userCache = server.getProfileCache(); + GameProfileCache userCache = server.getProfileCache();
+ if (profile.getId() == null) { + if (this.getId() == null) {
+ final GameProfile profile; + final GameProfile profile;
+ if (onlineMode) { + if (onlineMode) {
+ profile = lookupUUID ? userCache.get(name).orElse(null) : userCache.getProfileIfCached(name); + profile = lookupUUID ? userCache.get(name).orElse(null) : userCache.getProfileIfCached(name);
@@ -228,11 +229,11 @@ index 0000000000000000000000000000000000000000..00000000000000000000000000000000
+ } + }
+ } + }
+ +
+ if ((profile.getName() == null || !hasTextures()) && profile.getId() != null) { + if ((profile.getName().isEmpty() || !hasTextures()) && this.getId() != null) {
+ Optional<GameProfile> optProfile = userCache.get(this.profile.getId()); + Optional<GameProfile> optProfile = userCache.get(this.profile.getId());
+ if (optProfile.isPresent()) { + if (optProfile.isPresent()) {
+ GameProfile profile = optProfile.get(); + GameProfile profile = optProfile.get();
+ if (this.profile.getName() == null) { + if (this.profile.getName().isEmpty()) {
+ // if old has it, assume its newer, so overwrite, else use cached if it was set and ours wasn't + // if old has it, assume its newer, so overwrite, else use cached if it was set and ours wasn't
+ copyProfileProperties(this.profile, profile); + copyProfileProperties(this.profile, profile);
+ this.profile = profile; + this.profile = profile;
@@ -314,7 +315,7 @@ index 0000000000000000000000000000000000000000..00000000000000000000000000000000
+ if (this.getId() != null) { + if (this.getId() != null) {
+ map.put("uniqueId", this.getId().toString()); + map.put("uniqueId", this.getId().toString());
+ } + }
+ if (this.getName() != null) { + if (!this.getName().isEmpty()) {
+ map.put("name", getName()); + map.put("name", getName());
+ } + }
+ if (!this.properties.isEmpty()) { + if (!this.properties.isEmpty()) {
@@ -661,14 +662,19 @@ index 0000000000000000000000000000000000000000..00000000000000000000000000000000
+ @Override + @Override
+ public com.destroystokyo.paper.profile.PlayerProfile createProfileExact(@Nullable UUID uuid, @Nullable String name) { + public com.destroystokyo.paper.profile.PlayerProfile createProfileExact(@Nullable UUID uuid, @Nullable String name) {
+ Player player = uuid != null ? Bukkit.getPlayer(uuid) : (name != null ? Bukkit.getPlayerExact(name) : null); + Player player = uuid != null ? Bukkit.getPlayer(uuid) : (name != null ? Bukkit.getPlayerExact(name) : null);
+ if (player == null) return new com.destroystokyo.paper.profile.CraftPlayerProfile(uuid, name); + if (player == null) {
+ return new com.destroystokyo.paper.profile.CraftPlayerProfile(uuid, name);
+ }
+ +
+ if (java.util.Objects.equals(uuid, player.getUniqueId()) && java.util.Objects.equals(name, player.getName())) { + if (java.util.Objects.equals(uuid, player.getUniqueId()) && java.util.Objects.equals(name, player.getName())) {
+ return new com.destroystokyo.paper.profile.CraftPlayerProfile((CraftPlayer) player); + return new com.destroystokyo.paper.profile.CraftPlayerProfile((CraftPlayer) player);
+ } + }
+ +
+ final com.mojang.authlib.GameProfile profile = new com.mojang.authlib.GameProfile(uuid, name); + final com.mojang.authlib.GameProfile profile = new com.mojang.authlib.GameProfile(
+ profile.getProperties().putAll(((CraftPlayer)player).getHandle().getGameProfile().getProperties()); + uuid != null ? uuid : net.minecraft.Util.NIL_UUID,
+ name != null ? name : ""
+ );
+ profile.getProperties().putAll(((CraftPlayer) player).getHandle().getGameProfile().getProperties());
+ return new com.destroystokyo.paper.profile.CraftPlayerProfile(profile); + return new com.destroystokyo.paper.profile.CraftPlayerProfile(profile);
+ } + }
// Paper end // Paper end

View File

@@ -9,7 +9,7 @@ index 0000000000000000000000000000000000000000..00000000000000000000000000000000
--- a/src/main/java/org/bukkit/craftbukkit/CraftServer.java --- a/src/main/java/org/bukkit/craftbukkit/CraftServer.java
+++ b/src/main/java/org/bukkit/craftbukkit/CraftServer.java +++ b/src/main/java/org/bukkit/craftbukkit/CraftServer.java
@@ -0,0 +0,0 @@ public final class CraftServer implements Server { @@ -0,0 +0,0 @@ public final class CraftServer implements Server {
profile.getProperties().putAll(((CraftPlayer)player).getHandle().getGameProfile().getProperties()); profile.getProperties().putAll(((CraftPlayer) player).getHandle().getGameProfile().getProperties());
return new com.destroystokyo.paper.profile.CraftPlayerProfile(profile); return new com.destroystokyo.paper.profile.CraftPlayerProfile(profile);
} }
+ +