SPIGOT-7486: Alternate approach to null profile names

This commit is contained in:
md_5 2023-09-22 07:36:30 +10:00
parent 069495671e
commit f0702775c3
No known key found for this signature in database
GPG Key ID: E8E901AC7C617C11
7 changed files with 28 additions and 91 deletions

View File

@ -60,7 +60,7 @@ public class CraftOfflinePlayer implements OfflinePlayer, ConfigurationSerializa
} }
// This might not match lastKnownName but if not it should be more correct // This might not match lastKnownName but if not it should be more correct
if (profile.getName() != null) { if (!profile.getName().isEmpty()) {
return profile.getName(); return profile.getName();
} }

View File

@ -189,7 +189,6 @@ import org.bukkit.craftbukkit.metadata.PlayerMetadataStore;
import org.bukkit.craftbukkit.metadata.WorldMetadataStore; import org.bukkit.craftbukkit.metadata.WorldMetadataStore;
import org.bukkit.craftbukkit.packs.CraftDataPackManager; import org.bukkit.craftbukkit.packs.CraftDataPackManager;
import org.bukkit.craftbukkit.potion.CraftPotionBrewer; import org.bukkit.craftbukkit.potion.CraftPotionBrewer;
import org.bukkit.craftbukkit.profile.CraftGameProfile;
import org.bukkit.craftbukkit.profile.CraftPlayerProfile; import org.bukkit.craftbukkit.profile.CraftPlayerProfile;
import org.bukkit.craftbukkit.scheduler.CraftScheduler; import org.bukkit.craftbukkit.scheduler.CraftScheduler;
import org.bukkit.craftbukkit.scoreboard.CraftCriteria; import org.bukkit.craftbukkit.scoreboard.CraftCriteria;
@ -1676,7 +1675,7 @@ public final class CraftServer implements Server {
if (result == null) { if (result == null) {
result = offlinePlayers.get(id); result = offlinePlayers.get(id);
if (result == null) { if (result == null) {
result = new CraftOfflinePlayer(this, new CraftGameProfile(id, null)); result = new CraftOfflinePlayer(this, new GameProfile(id, ""));
offlinePlayers.put(id, result); offlinePlayers.put(id, result);
} }
} else { } else {

View File

@ -2,6 +2,7 @@ package org.bukkit.craftbukkit.block;
import com.google.common.base.Preconditions; import com.google.common.base.Preconditions;
import com.mojang.authlib.GameProfile; import com.mojang.authlib.GameProfile;
import net.minecraft.SystemUtils;
import net.minecraft.resources.MinecraftKey; import net.minecraft.resources.MinecraftKey;
import net.minecraft.server.MinecraftServer; import net.minecraft.server.MinecraftServer;
import net.minecraft.world.level.block.entity.TileEntitySkull; 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.Directional;
import org.bukkit.block.data.Rotatable; import org.bukkit.block.data.Rotatable;
import org.bukkit.craftbukkit.entity.CraftPlayer; import org.bukkit.craftbukkit.entity.CraftPlayer;
import org.bukkit.craftbukkit.profile.CraftGameProfile;
import org.bukkit.craftbukkit.profile.CraftPlayerProfile; import org.bukkit.craftbukkit.profile.CraftPlayerProfile;
import org.bukkit.craftbukkit.util.CraftNamespacedKey; import org.bukkit.craftbukkit.util.CraftNamespacedKey;
import org.bukkit.profile.PlayerProfile; import org.bukkit.profile.PlayerProfile;
@ -84,11 +84,11 @@ public class CraftSkull extends CraftBlockEntityState<TileEntitySkull> implement
@Override @Override
public OfflinePlayer getOwningPlayer() { public OfflinePlayer getOwningPlayer() {
if (profile != null) { if (profile != null) {
if (profile.getId() != null) { if (!profile.getId().equals(SystemUtils.NIL_UUID)) {
return Bukkit.getOfflinePlayer(profile.getId()); return Bukkit.getOfflinePlayer(profile.getId());
} }
if (profile.getName() != null) { if (!profile.getName().isEmpty()) {
return Bukkit.getOfflinePlayer(profile.getName()); return Bukkit.getOfflinePlayer(profile.getName());
} }
} }
@ -103,7 +103,7 @@ public class CraftSkull extends CraftBlockEntityState<TileEntitySkull> implement
if (player instanceof CraftPlayer) { if (player instanceof CraftPlayer) {
this.profile = ((CraftPlayer) player).getProfile(); this.profile = ((CraftPlayer) player).getProfile();
} else { } else {
this.profile = new CraftGameProfile(player.getUniqueId(), player.getName()); this.profile = new GameProfile(player.getUniqueId(), player.getName());
} }
} }

View File

@ -7,6 +7,7 @@ import java.util.Map;
import java.util.Objects; import java.util.Objects;
import java.util.Set; import java.util.Set;
import java.util.UUID; import java.util.UUID;
import net.minecraft.SystemUtils;
import net.minecraft.nbt.GameProfileSerializer; import net.minecraft.nbt.GameProfileSerializer;
import net.minecraft.nbt.NBTTagCompound; import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.resources.MinecraftKey; import net.minecraft.resources.MinecraftKey;
@ -18,7 +19,6 @@ import org.bukkit.OfflinePlayer;
import org.bukkit.configuration.serialization.DelegateDeserialization; import org.bukkit.configuration.serialization.DelegateDeserialization;
import org.bukkit.craftbukkit.entity.CraftPlayer; import org.bukkit.craftbukkit.entity.CraftPlayer;
import org.bukkit.craftbukkit.inventory.CraftMetaItem.SerializableMeta; import org.bukkit.craftbukkit.inventory.CraftMetaItem.SerializableMeta;
import org.bukkit.craftbukkit.profile.CraftGameProfile;
import org.bukkit.craftbukkit.profile.CraftPlayerProfile; import org.bukkit.craftbukkit.profile.CraftPlayerProfile;
import org.bukkit.craftbukkit.util.CraftMagicNumbers; import org.bukkit.craftbukkit.util.CraftMagicNumbers;
import org.bukkit.craftbukkit.util.CraftNamespacedKey; 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)) { if (tag.contains(SKULL_OWNER.NBT, CraftMagicNumbers.NBT.TAG_COMPOUND)) {
this.setProfile(GameProfileSerializer.readGameProfile(tag.getCompound(SKULL_OWNER.NBT))); 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()) { } 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)) { if (tag.contains(BLOCK_ENTITY_TAG.NBT, CraftMagicNumbers.NBT.TAG_COMPOUND)) {
@ -179,7 +179,7 @@ class CraftMetaSkull extends CraftMetaItem implements SkullMeta {
@Override @Override
public boolean hasOwner() { public boolean hasOwner() {
return profile != null && profile.getName() != null; return profile != null && !profile.getName().isEmpty();
} }
@Override @Override
@ -190,11 +190,11 @@ class CraftMetaSkull extends CraftMetaItem implements SkullMeta {
@Override @Override
public OfflinePlayer getOwningPlayer() { public OfflinePlayer getOwningPlayer() {
if (hasOwner()) { if (hasOwner()) {
if (profile.getId() != null) { if (!profile.getId().equals(SystemUtils.NIL_UUID)) {
return Bukkit.getOfflinePlayer(profile.getId()); return Bukkit.getOfflinePlayer(profile.getId());
} }
if (profile.getName() != null) { if (!profile.getName().isEmpty()) {
return Bukkit.getOfflinePlayer(profile.getName()); return Bukkit.getOfflinePlayer(profile.getName());
} }
} }
@ -211,7 +211,7 @@ class CraftMetaSkull extends CraftMetaItem implements SkullMeta {
if (name == null) { if (name == null) {
setProfile(null); setProfile(null);
} else { } else {
setProfile(new CraftGameProfile(null, name)); setProfile(new GameProfile(SystemUtils.NIL_UUID, name));
} }
return true; return true;
@ -224,7 +224,7 @@ class CraftMetaSkull extends CraftMetaItem implements SkullMeta {
} else if (owner instanceof CraftPlayer) { } else if (owner instanceof CraftPlayer) {
setProfile(((CraftPlayer) owner).getProfile()); setProfile(((CraftPlayer) owner).getProfile());
} else { } else {
setProfile(new CraftGameProfile(owner.getUniqueId(), owner.getName())); setProfile(new GameProfile(owner.getUniqueId(), owner.getName()));
} }
return true; return true;

View File

@ -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();
}
}

View File

@ -56,8 +56,8 @@ public final class CraftPlayerProfile implements PlayerProfile {
public CraftPlayerProfile(UUID uniqueId, String name) { public CraftPlayerProfile(UUID uniqueId, String name) {
Preconditions.checkArgument((uniqueId != null) || !StringUtils.isBlank(name), "uniqueId is null or name is blank"); Preconditions.checkArgument((uniqueId != null) || !StringUtils.isBlank(name), "uniqueId is null or name is blank");
this.uniqueId = uniqueId; this.uniqueId = (uniqueId == null) ? SystemUtils.NIL_UUID : uniqueId;
this.name = name; this.name = (name == null) ? "" : name;
} }
// The Map of properties of the given GameProfile is not immutable. This captures a snapshot of the properties of // 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 @Override
public UUID getUniqueId() { public UUID getUniqueId() {
return uniqueId; return (uniqueId.equals(SystemUtils.NIL_UUID)) ? null : uniqueId;
} }
@Override @Override
public String getName() { public String getName() {
return name; return (name.isEmpty()) ? null : name;
} }
@Nullable @Nullable
@ -120,7 +120,7 @@ public final class CraftPlayerProfile implements PlayerProfile {
@Override @Override
public boolean isComplete() { public boolean isComplete() {
return (uniqueId != null) && (name != null) && !textures.isEmpty(); return (getUniqueId() != null) && (getName() != null) && !textures.isEmpty();
} }
@Override @Override
@ -133,12 +133,12 @@ public final class CraftPlayerProfile implements PlayerProfile {
GameProfile profile = this.buildGameProfile(); GameProfile profile = this.buildGameProfile();
// If missing, look up the uuid by name: // 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); profile = server.getProfileCache().get(profile.getName()).orElse(profile);
} }
// Look up properties such as the textures: // Look up properties such as the textures:
if (profile.getId() != null) { if (!profile.getId().equals(SystemUtils.NIL_UUID)) {
GameProfile newProfile; GameProfile newProfile;
try { try {
newProfile = TileEntitySkull.fillProfileTextures(profile).get().orElse(null); // TODO: replace with CompletableFuture newProfile = TileEntitySkull.fillProfileTextures(profile).get().orElse(null); // TODO: replace with CompletableFuture
@ -158,7 +158,7 @@ public final class CraftPlayerProfile implements PlayerProfile {
@Nonnull @Nonnull
public GameProfile buildGameProfile() { public GameProfile buildGameProfile() {
rebuildDirtyProperties(); rebuildDirtyProperties();
GameProfile profile = new CraftGameProfile(uniqueId, name); GameProfile profile = new GameProfile(uniqueId, name);
profile.getProperties().putAll(properties); profile.getProperties().putAll(properties);
return profile; return profile;
} }
@ -246,11 +246,11 @@ public final class CraftPlayerProfile implements PlayerProfile {
@Override @Override
public Map<String, Object> serialize() { public Map<String, Object> serialize() {
Map<String, Object> map = new LinkedHashMap<>(); Map<String, Object> map = new LinkedHashMap<>();
if (uniqueId != null) { if (getUniqueId() != null) {
map.put("uniqueId", uniqueId.toString()); map.put("uniqueId", getUniqueId().toString());
} }
if (name != null) { if (getName() != null) {
map.put("name", name); map.put("name", getName());
} }
rebuildDirtyProperties(); rebuildDirtyProperties();
if (!properties.isEmpty()) { if (!properties.isEmpty()) {

View File

@ -5,6 +5,7 @@ import com.mojang.authlib.properties.Property;
import java.net.MalformedURLException; import java.net.MalformedURLException;
import java.net.URL; import java.net.URL;
import java.util.UUID; import java.util.UUID;
import net.minecraft.SystemUtils;
import org.bukkit.configuration.InvalidConfigurationException; import org.bukkit.configuration.InvalidConfigurationException;
import org.bukkit.configuration.file.YamlConfiguration; import org.bukkit.configuration.file.YamlConfiguration;
import org.bukkit.configuration.serialization.ConfigurationSerialization; 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("Unique id is not the same", UNIQUE_ID, profile1.getUniqueId());
Assert.assertEquals("Name is not the same", NAME, profile1.getName()); 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("Unique id is not the same", UNIQUE_ID, profile2.getUniqueId());
Assert.assertEquals("Name is not null", null, profile2.getName()); 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("Unique id is not null", null, profile3.getUniqueId());
Assert.assertEquals("Name is not the same", NAME, profile3.getName()); Assert.assertEquals("Name is not the same", NAME, profile3.getName());
} }