From 9a55a4c615531a57df6d48c07837516484061434 Mon Sep 17 00:00:00 2001 From: ZekerZhayard Date: Tue, 10 Oct 2023 13:54:23 +0800 Subject: [PATCH] Adapting to Authlib changes (#269) --- .../loader/MojangAPILoader.java | 10 ++-- .../profile/DynamicSkullManager.java | 2 +- .../customskinloader/utils/TextureUtil.java | 48 +++++++++---------- Forge/Active/Forge.tsrg | 2 +- 4 files changed, 30 insertions(+), 32 deletions(-) diff --git a/Common/src/main/java/customskinloader/loader/MojangAPILoader.java b/Common/src/main/java/customskinloader/loader/MojangAPILoader.java index 43b49b8..47a2e48 100644 --- a/Common/src/main/java/customskinloader/loader/MojangAPILoader.java +++ b/Common/src/main/java/customskinloader/loader/MojangAPILoader.java @@ -163,8 +163,8 @@ public class MojangAPILoader implements ICustomSkinLoaderPlugin, ProfileLoader.I Gson gson = new GsonBuilder().registerTypeAdapter(UUID.class, new UUIDTypeAdapter()).registerTypeAdapter(PropertyMap.class, new PropertyMap.Serializer()).create(); MinecraftProfilePropertiesResponse propertiesResponce = gson.fromJson(responce.content, MinecraftProfilePropertiesResponse.class); - GameProfile newGameProfile = new GameProfile(propertiesResponce.getId(), propertiesResponce.getName()); - newGameProfile.getProperties().putAll(propertiesResponce.getProperties()); + GameProfile newGameProfile = new GameProfile(TextureUtil.AuthlibField.MINECRAFTPROFILEPROPERTIESRESPONSE_ID.get(propertiesResponce), TextureUtil.AuthlibField.MINECRAFTPROFILEPROPERTIESRESPONSE_NAME.get(propertiesResponce)); + newGameProfile.getProperties().putAll(TextureUtil.AuthlibField.MINECRAFTPROFILEPROPERTIESRESPONSE_PROPERTIES.get(propertiesResponce)); return newGameProfile; } @@ -177,7 +177,7 @@ public class MojangAPILoader implements ICustomSkinLoaderPlugin, ProfileLoader.I if (textureProperty == null) { return Maps.newHashMap(); } - String value = TextureUtil.getPropertyValue(textureProperty); + String value = TextureUtil.AuthlibField.PROPERTY_VALUE_FIELD.get(textureProperty); if (StringUtils.isBlank(value)) { return Maps.newHashMap(); } @@ -185,10 +185,10 @@ public class MojangAPILoader implements ICustomSkinLoaderPlugin, ProfileLoader.I Gson gson = new GsonBuilder().registerTypeAdapter(UUID.class, new UUIDTypeAdapter()).create(); MinecraftTexturesPayload result = gson.fromJson(json, MinecraftTexturesPayload.class); - if (result == null || TextureUtil.getMinecraftTexturesPayloadTextures(result) == null) { + if (result == null || TextureUtil.AuthlibField.MINECRAFTTEXTURESPAYLOAD_TEXTURES.get(result) == null) { return Maps.newHashMap(); } - return TextureUtil.getMinecraftTexturesPayloadTextures(result); + return TextureUtil.AuthlibField.MINECRAFTTEXTURESPAYLOAD_TEXTURES.get(result); } @Override diff --git a/Common/src/main/java/customskinloader/profile/DynamicSkullManager.java b/Common/src/main/java/customskinloader/profile/DynamicSkullManager.java index 28b601d..ae2e46e 100644 --- a/Common/src/main/java/customskinloader/profile/DynamicSkullManager.java +++ b/Common/src/main/java/customskinloader/profile/DynamicSkullManager.java @@ -49,7 +49,7 @@ public class DynamicSkullManager { staticTextures.put(profile, Maps.newHashMap()); return; } - String value = TextureUtil.getPropertyValue(textureProperty); + String value = TextureUtil.AuthlibField.PROPERTY_VALUE_FIELD.get(textureProperty); if (StringUtils.isBlank(value)) { staticTextures.put(profile, Maps.newHashMap()); return; diff --git a/Common/src/main/java/customskinloader/utils/TextureUtil.java b/Common/src/main/java/customskinloader/utils/TextureUtil.java index 0b49393..17b1d48 100644 --- a/Common/src/main/java/customskinloader/utils/TextureUtil.java +++ b/Common/src/main/java/customskinloader/utils/TextureUtil.java @@ -7,6 +7,7 @@ import java.util.UUID; import com.mojang.authlib.minecraft.MinecraftProfileTexture; import com.mojang.authlib.properties.Property; +import com.mojang.authlib.yggdrasil.response.MinecraftProfilePropertiesResponse; import com.mojang.authlib.yggdrasil.response.MinecraftTexturesPayload; import customskinloader.CustomSkinLoader; import org.apache.commons.codec.binary.Base64; @@ -40,34 +41,31 @@ public class TextureUtil { // Some of the classes in Authlib used after Minecraft 23w31a were changed to record classes, // resulting in changes to method names, so reflection is used here to be compatible with these changes. - private final static Field VALUE_FIELD; - private final static Field TEXTURES_FIELD; + public enum AuthlibField { + PROPERTY_VALUE_FIELD(Property.class, "value"), + MINECRAFTPROFILEPROPERTIESRESPONSE_ID(MinecraftProfilePropertiesResponse.class, "id"), + MINECRAFTPROFILEPROPERTIESRESPONSE_NAME(MinecraftProfilePropertiesResponse.class, "name"), + MINECRAFTPROFILEPROPERTIESRESPONSE_PROPERTIES(MinecraftProfilePropertiesResponse.class, "properties"), + MINECRAFTTEXTURESPAYLOAD_TEXTURES(MinecraftTexturesPayload.class, "textures"); - static { - try { - VALUE_FIELD = Property.class.getDeclaredField("value"); - VALUE_FIELD.setAccessible(true); - TEXTURES_FIELD = MinecraftTexturesPayload.class.getDeclaredField("textures"); - TEXTURES_FIELD.setAccessible(true); - } catch (Exception e) { - throw new RuntimeException(e); + private final Field field; + + AuthlibField(Class clazz, String name) { + try { + this.field = clazz.getDeclaredField(name); + this.field.setAccessible(true); + } catch (Throwable t) { + throw new RuntimeException(t); + } } - } - public static String getPropertyValue(Property property) { - try { - return (String) VALUE_FIELD.get(property); - } catch (Exception e) { - throw new RuntimeException(e); - } - } - - @SuppressWarnings("unchecked") - public static Map getMinecraftTexturesPayloadTextures(MinecraftTexturesPayload payload) { - try { - return (Map) TEXTURES_FIELD.get(payload); - } catch (Exception e) { - throw new RuntimeException(e); + @SuppressWarnings("unchecked") + public T get(Object o) { + try { + return (T) this.field.get(o); + } catch (Throwable t) { + throw new RuntimeException(t); + } } } diff --git a/Forge/Active/Forge.tsrg b/Forge/Active/Forge.tsrg index 3f05d23..0c67a85 100644 --- a/Forge/Active/Forge.tsrg +++ b/Forge/Active/Forge.tsrg @@ -51,7 +51,7 @@ net/minecraft/client/resources/SkinManager net/minecraft/client/resources/SkinMa loadSkin (Lcom/mojang/authlib/minecraft/MinecraftProfileTexture;Lcom/mojang/authlib/minecraft/MinecraftProfileTexture$Type;)Lnet/minecraft/util/ResourceLocation; m_118825_ net/minecraft/client/resources/SkinManager$1 net/minecraft/client/resources/SkinManager$1 net/minecraft/client/resources/SkinManager$CacheKey net/minecraft/client/resources/SkinManager$CacheKey - profile ()Lcom/mojang/authlib/GameProfile; profile + profile ()Lcom/mojang/authlib/GameProfile; f_290972_ net/minecraft/client/resources/SkinManager$SkinAvailableCallback net/minecraft/client/resources/SkinManager$SkinTextureCallback skinAvailable (Lcom/mojang/authlib/minecraft/MinecraftProfileTexture$Type;Lnet/minecraft/util/ResourceLocation;Lcom/mojang/authlib/minecraft/MinecraftProfileTexture;)V m_118856_ net/minecraft/client/resources/SkinManager$TextureCache net/minecraft/client/resources/SkinManager$TextureCache