Save full skull profile instead of just the uuid if available

This commit is contained in:
Brokkonaut
2022-02-16 08:38:50 +01:00
parent e61f9b058d
commit c3394fa8c5
2 changed files with 37 additions and 8 deletions

View File

@@ -44,7 +44,7 @@
<dependency>
<groupId>org.spigotmc</groupId>
<artifactId>spigot-api</artifactId>
<version>1.18-R0.1-SNAPSHOT</version>
<version>1.18.1-R0.1-SNAPSHOT</version>
<scope>provided</scope>
</dependency>
<dependency>

View File

@@ -8,8 +8,20 @@ import org.bukkit.OfflinePlayer;
import org.bukkit.block.BlockState;
import org.bukkit.block.Skull;
import org.bukkit.configuration.file.YamlConfiguration;
import org.bukkit.profile.PlayerProfile;
public class BlockStateCodecSkull implements BlockStateCodec {
private static final boolean HAS_PROFILE_API;
static {
boolean hasProfileApi = false;
try {
Skull.class.getMethod("getOwnerProfile");
hasProfileApi = true;
} catch (NoSuchMethodException ignored) {
}
HAS_PROFILE_API = hasProfileApi;
}
@Override
public Material[] getApplicableMaterials() {
return new Material[] { Material.PLAYER_WALL_HEAD, Material.PLAYER_HEAD };
@@ -20,9 +32,14 @@ public class BlockStateCodecSkull implements BlockStateCodec {
if (state instanceof Skull) {
Skull skull = (Skull) state;
OfflinePlayer owner = skull.hasOwner() ? skull.getOwningPlayer() : null;
if (owner != null) {
PlayerProfile profile = HAS_PROFILE_API ? skull.getOwnerProfile() : null;
if (owner != null || profile != null) {
YamlConfiguration conf = new YamlConfiguration();
if (profile != null) {
conf.set("profile", profile);
} else if (owner != null) {
conf.set("owner", owner.getUniqueId().toString());
}
return conf;
}
}
@@ -33,6 +50,10 @@ public class BlockStateCodecSkull implements BlockStateCodec {
public void deserialize(BlockState state, YamlConfiguration conf) {
if (state instanceof Skull) {
Skull skull = (Skull) state;
PlayerProfile profile = conf == null || !HAS_PROFILE_API ? null : (PlayerProfile) conf.get("profile");
if (profile != null) {
skull.setOwnerProfile(profile);
} else {
UUID ownerId = conf == null ? null : UUID.fromString(conf.getString("owner"));
if (ownerId == null) {
skull.setOwningPlayer(null);
@@ -41,10 +62,18 @@ public class BlockStateCodecSkull implements BlockStateCodec {
}
}
}
}
@Override
public String toString(YamlConfiguration conf, YamlConfiguration oldState) {
UUID ownerId = conf == null ? null : UUID.fromString(conf.getString("owner"));
if (HAS_PROFILE_API && conf != null) {
PlayerProfile profile = (PlayerProfile) conf.get("profile");
if (profile != null) {
return "[" + (profile.getName() != null ? profile.getName() : (profile.getUniqueId() != null ? profile.getUniqueId().toString() : "~unknown~")) + "]";
}
}
String ownerIdString = conf == null ? null : conf.getString("owner");
UUID ownerId = ownerIdString == null ? null : UUID.fromString(ownerIdString);
if (ownerId != null) {
OfflinePlayer owner = Bukkit.getOfflinePlayer(ownerId);
return "[" + (owner.getName() != null ? owner.getName() : owner.getUniqueId().toString()) + "]";