Skip to content

Commit

Permalink
proxy (mostly) begone, methodhandles go brrr instead
Browse files Browse the repository at this point in the history
  • Loading branch information
rhysdh540 committed Oct 7, 2024
1 parent a1034a8 commit 815293e
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,11 @@

import net.minecraftforge.fml.config.ModConfig;

import java.lang.invoke.MethodHandle;
import java.lang.invoke.MethodHandleProxies;
import java.lang.invoke.MethodHandles;
import java.lang.invoke.MethodType;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Proxy;
import java.util.function.Consumer;

import static dev.rdh.createunlimited.multiversion.SupportedMinecraftVersion.*;
Expand All @@ -26,7 +29,6 @@ public void onInitialize() {
CommandRegistrationCallback.EVENT.register(CUCommands::register);
}


/**
* @param <L> The type of the loading lambda
* @param <R> The type of the reloading lambda
Expand All @@ -53,18 +55,16 @@ private static <L, R> void registerConfigEvents() {
}
}

@SuppressWarnings({"unchecked", "SuspiciousInvocationHandlerImplementation"})
private static <T> T createHandlerProxy(Consumer<ModConfig> handler, Class<?> modConfigEventsClass, String handlerTypeName, String handlerMethodName) {
return (T) Proxy.newProxyInstance(
modConfigEventsClass.getClassLoader(),
new Class<?>[]{ findNestedClass(modConfigEventsClass, handlerTypeName) },
(proxy, method, args) -> {
if (method.getName().equals(handlerMethodName) && args.length == 1 && args[0] instanceof ModConfig config) {
handler.accept(config);
return null;
}
throw new UnsupportedOperationException("Unexpected method: " + method);
});
try {
@SuppressWarnings("unchecked")
Class<T> clazz = (Class<T>) findNestedClass(modConfigEventsClass, handlerTypeName);
MethodHandle handle = MethodHandles.publicLookup()
.findStatic(clazz, handlerMethodName, MethodType.methodType(void.class, ModConfig.class));
return MethodHandleProxies.asInterfaceInstance(clazz, handle.bindTo(handler));
} catch (Throwable t) {
throw unchecked(t);
}
}

private static Class<?> findNestedClass(Class<?> outerClass, String nestedClassName) {
Expand Down
2 changes: 1 addition & 1 deletion forge/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ unimined.minecraft {
}


runs.config("client") {
runs.all {
jvmArgs(
"-Dmixin.env.remapRefMap=true",
"-Dmixin.env.refMapRemappingFile=${(mcPatcher as ForgeLikeMinecraftTransformer).srgToMCPAsSRG}"
Expand Down
6 changes: 3 additions & 3 deletions settings.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ val message = """
OS: "${System.getProperty("os.name")}", arch "${System.getProperty("os.arch")}"
""".trimIndent()

message.lines().forEachIndexed { i, l ->
val padding = "${if (i == 0) '-' else ' '}".repeat((messageLen - l.length) / 2)
println("$padding$l$padding")
message.lines().forEachIndexed { n, it ->
val padding = "${if (n == 0) '-' else ' '}".repeat((messageLen - it.length) / 2)
println("$padding$it$padding")
}
println("-".repeat(messageLen))

0 comments on commit 815293e

Please sign in to comment.