SPIGOT-7486: Alternate approach to null profile names

By: md_5 <git@md-5.net>
This commit is contained in:
CraftBukkit/Spigot 2023-09-22 07:36:30 +10:00
parent fb7dc796d5
commit 3ff2a7bc12
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
if (profile.getName() != null) {
if (!profile.getName().isEmpty()) {
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.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 {

View File

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

View File

@ -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;

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) {
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<String, Object> serialize() {
Map<String, Object> 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()) {

View File

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