From 9367942b54f5d16dab0aca39aafdfc1550a13724 Mon Sep 17 00:00:00 2001 From: tr7zw Date: Wed, 24 Apr 2024 00:08:48 +0200 Subject: [PATCH] Re-implement gameprofile methods for 1.20.5+ --- .../tr7zw/changeme/nbtapi/NBTGameProfile.java | 8 ++ .../nbtapi/utils/GameprofileUtil.java | 76 +++++++++++++++++++ 2 files changed, 84 insertions(+) create mode 100644 item-nbt-api/src/main/java/de/tr7zw/changeme/nbtapi/utils/GameprofileUtil.java diff --git a/item-nbt-api/src/main/java/de/tr7zw/changeme/nbtapi/NBTGameProfile.java b/item-nbt-api/src/main/java/de/tr7zw/changeme/nbtapi/NBTGameProfile.java index 73b1feb1e..7eee1f86a 100644 --- a/item-nbt-api/src/main/java/de/tr7zw/changeme/nbtapi/NBTGameProfile.java +++ b/item-nbt-api/src/main/java/de/tr7zw/changeme/nbtapi/NBTGameProfile.java @@ -2,6 +2,8 @@ import com.mojang.authlib.GameProfile; +import de.tr7zw.changeme.nbtapi.utils.GameprofileUtil; +import de.tr7zw.changeme.nbtapi.utils.MinecraftVersion; import de.tr7zw.changeme.nbtapi.utils.nmsmappings.ObjectCreator; import de.tr7zw.changeme.nbtapi.utils.nmsmappings.ReflectionMethod; @@ -14,6 +16,9 @@ public class NBTGameProfile { * @return A NBTContainer with all the GameProfile data */ public static NBTCompound toNBT(GameProfile profile) { + if(MinecraftVersion.isAtLeastVersion(MinecraftVersion.MC1_20_R4)) { + return (NBTCompound) GameprofileUtil.writeGameProfile(NBT.createNBTObject(), profile); + } return new NBTContainer(ReflectionMethod.GAMEPROFILE_SERIALIZE.run(null, ObjectCreator.NMS_NBTTAGCOMPOUND.getInstance(), profile)); } @@ -25,6 +30,9 @@ public static NBTCompound toNBT(GameProfile profile) { * @return The reconstructed GameProfile */ public static GameProfile fromNBT(NBTCompound compound) { + if(MinecraftVersion.isAtLeastVersion(MinecraftVersion.MC1_20_R4)) { + return GameprofileUtil.readGameProfile(compound); + } return (GameProfile) ReflectionMethod.GAMEPROFILE_DESERIALIZE.run(null, NBTReflectionUtil.gettoCompount(compound.getCompound(), compound)); } diff --git a/item-nbt-api/src/main/java/de/tr7zw/changeme/nbtapi/utils/GameprofileUtil.java b/item-nbt-api/src/main/java/de/tr7zw/changeme/nbtapi/utils/GameprofileUtil.java new file mode 100644 index 000000000..cbbd936cb --- /dev/null +++ b/item-nbt-api/src/main/java/de/tr7zw/changeme/nbtapi/utils/GameprofileUtil.java @@ -0,0 +1,76 @@ +package de.tr7zw.changeme.nbtapi.utils; + +import java.util.UUID; + +import javax.annotation.Nullable; + +import com.mojang.authlib.GameProfile; + +import de.tr7zw.changeme.nbtapi.NBTType; +import de.tr7zw.changeme.nbtapi.iface.ReadWriteNBT; +import de.tr7zw.changeme.nbtapi.iface.ReadWriteNBTCompoundList; +import de.tr7zw.changeme.nbtapi.iface.ReadableNBT; +import de.tr7zw.changeme.nbtapi.iface.ReadableNBTList; + +public class GameprofileUtil { + + @Nullable + public static GameProfile readGameProfile(ReadableNBT arg) { + String string = null; + UUID uUID = null; + if (arg.hasTag("Name") && arg.getType("Name") == NBTType.NBTTagString) { + string = arg.getString("Name"); + } + if (arg.hasTag("Id") && arg.getType("Id") == NBTType.NBTTagIntArray && arg.getIntArray("Id").length == 4) { + uUID = arg.getUUID("Id"); + } + try { + GameProfile gameProfile = new GameProfile(uUID, string); + if (arg.hasTag("Properties") && arg.getType("Properties") == NBTType.NBTTagCompound) { + ReadableNBT compoundTag = arg.getCompound("Properties"); + for (String string2 : compoundTag.getKeys()) { + ReadableNBTList listTag = compoundTag.getCompoundList(string); + for (int i = 0; i < listTag.size(); ++i) { + ReadableNBT compoundTag2 = listTag.get(i); + String string3 = compoundTag2.getString("Value"); + if (compoundTag2.hasTag("Signature") + && compoundTag2.getType("Signature") == NBTType.NBTTagString) { + gameProfile.getProperties().put(string2, new com.mojang.authlib.properties.Property(string2, + string3, compoundTag2.getString("Signature"))); + } else { + gameProfile.getProperties().put(string2, + new com.mojang.authlib.properties.Property(string2, string3)); + } + } + } + } + return gameProfile; + } catch (Throwable var11) { + return null; + } + } + + public static ReadWriteNBT writeGameProfile(ReadWriteNBT arg, GameProfile gameProfile) { + if (!(gameProfile.getName() == null || gameProfile.getName().isEmpty())) { + arg.setString("Name", gameProfile.getName()); + } + if (gameProfile.getId() != null) { + arg.setUUID("Id", gameProfile.getId()); + } + if (!gameProfile.getProperties().isEmpty()) { + ReadWriteNBT compoundTag = arg.getOrCreateCompound("Properties"); + for (String string : gameProfile.getProperties().keySet()) { + ReadWriteNBTCompoundList list = compoundTag.getCompoundList(string); + for (com.mojang.authlib.properties.Property property : gameProfile.getProperties().get(string)) { + ReadWriteNBT tag = list.addCompound(); + tag.setString("Value", property.getValue()); + if (property.hasSignature()) { + tag.setString("Signature", property.getSignature()); + } + } + } + } + return arg; + } + +}