diff --git a/src/main/java/net/kyori/adventure/platform/fabric/FabricAudiences.java b/src/main/java/net/kyori/adventure/platform/fabric/FabricAudiences.java index 9f68bd12..f07f23c5 100644 --- a/src/main/java/net/kyori/adventure/platform/fabric/FabricAudiences.java +++ b/src/main/java/net/kyori/adventure/platform/fabric/FabricAudiences.java @@ -160,7 +160,6 @@ static ResourceLocation toNative(final Key key) { * @param player the player to identify * @return an identified representation of the player * @since 4.0.0 - * */ static @NotNull Identified identified(final @NotNull Player player) { return (Identified) player; diff --git a/src/main/java/net/kyori/adventure/platform/fabric/impl/AdventurePrelaunch.java b/src/main/java/net/kyori/adventure/platform/fabric/impl/AdventurePrelaunch.java index 6e36d802..2193f077 100644 --- a/src/main/java/net/kyori/adventure/platform/fabric/impl/AdventurePrelaunch.java +++ b/src/main/java/net/kyori/adventure/platform/fabric/impl/AdventurePrelaunch.java @@ -23,20 +23,53 @@ */ package net.kyori.adventure.platform.fabric.impl; +import java.lang.reflect.InvocationTargetException; +import java.lang.reflect.Method; import java.net.URL; import net.fabricmc.loader.api.entrypoint.PreLaunchEntrypoint; -import net.fabricmc.loader.launch.common.FabricLauncherBase; +/** + * Via i509VCB, a trick to get Brig onto the Knot classpath in order to properly mix in. + * + *
YOU SHOULD ONLY USE THIS CLASS DURING "preLaunch" and ONLY TARGET A CLASS WHICH IS NOT ANY CLASS YOU MIXIN TO. + * This will likely not work on Gson because FabricLoader has some special logic related to Gson.
+ * + *Original on GitHub at i509VCB/Fabric-Junkkyard
+ */ public class AdventurePrelaunch implements PreLaunchEntrypoint { + private static final ClassLoader KNOT_CLASSLOADER = Thread.currentThread().getContextClassLoader(); + private static final Method ADD_URL_METHOD; + + static { + try { + ADD_URL_METHOD = KNOT_CLASSLOADER.getClass().getMethod("addURL", URL.class); + ADD_URL_METHOD.setAccessible(true); + } catch (final ReflectiveOperationException ex) { + throw new RuntimeException("Failed to load Classloader fields", ex); + } + } + @Override public void onPreLaunch() { - // TODO: Actually fix loader - final URL target; try { - target = Class.forName("com.mojang.authlib.UserAuthentication").getProtectionDomain().getCodeSource().getLocation(); - FabricLauncherBase.getLauncher().propose(target); - } catch (final ClassNotFoundException e) { + AdventurePrelaunch.hackilyLoadForMixin("com.mojang.authlib.UserAuthentication"); + } catch (final ClassNotFoundException | InvocationTargetException | IllegalAccessException e) { throw new RuntimeException("please fix fabric loader to enable transforming libraries normally", e); } } + + /** + * Hackily load the package which a mixin may exist within. + * + *YOU SHOULD NOT TARGET A CLASS WHICH YOU MIXIN TO. (this could be done to better avoid class loads)
+ * + * @param pathOfAClass The path of any class within the package. + * @throws ClassNotFoundException if an unknown class name is used + * @throws InvocationTargetException if an error occurs while injecting + * @throws IllegalAccessException if an error occurs while injecting + */ + static void hackilyLoadForMixin(final String pathOfAClass) throws ClassNotFoundException, InvocationTargetException, IllegalAccessException { + final URL url = Class.forName(pathOfAClass).getProtectionDomain().getCodeSource().getLocation(); + ADD_URL_METHOD.invoke(KNOT_CLASSLOADER, url); + } }