Skip to content

Commit

Permalink
Re-implement gameprofile methods for 1.20.5+
Browse files Browse the repository at this point in the history
  • Loading branch information
tr7zw committed Apr 23, 2024
1 parent 167487e commit 9367942
Show file tree
Hide file tree
Showing 2 changed files with 84 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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));
}
Expand All @@ -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));
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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<ReadWriteNBT> 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;
}

}

0 comments on commit 9367942

Please sign in to comment.