Save full skull profile instead of just the uuid if available
This commit is contained in:
1 parent
e61f9b058d
commit
c3394fa8c5
2 files changed
+37
-8
No files matched your search
@@ -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();
|
||||
conf.set("owner", owner.getUniqueId().toString());
|
||||
if (profile != null) {
|
||||
conf.set("profile", profile);
|
||||
} else if (owner != null) {
|
||||
conf.set("owner", owner.getUniqueId().toString());
|
||||
}
|
||||
return conf;
|
||||
}
|
||||
}
|
||||
@@ -33,18 +50,30 @@ public class BlockStateCodecSkull implements BlockStateCodec {
|
||||
public void deserialize(BlockState state, YamlConfiguration conf) {
|
||||
if (state instanceof Skull) {
|
||||
Skull skull = (Skull) state;
|
||||
UUID ownerId = conf == null ? null : UUID.fromString(conf.getString("owner"));
|
||||
if (ownerId == null) {
|
||||
skull.setOwningPlayer(null);
|
||||
PlayerProfile profile = conf == null || !HAS_PROFILE_API ? null : (PlayerProfile) conf.get("profile");
|
||||
if (profile != null) {
|
||||
skull.setOwnerProfile(profile);
|
||||
} else {
|
||||
skull.setOwningPlayer(Bukkit.getOfflinePlayer(ownerId));
|
||||
UUID ownerId = conf == null ? null : UUID.fromString(conf.getString("owner"));
|
||||
if (ownerId == null) {
|
||||
skull.setOwningPlayer(null);
|
||||
} else {
|
||||
skull.setOwningPlayer(Bukkit.getOfflinePlayer(ownerId));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@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()) + "]";
|
||||
|
||||
Reference in new issue
Block a user