2021-09-27 20:55:48 +02:00
package io.gitlab.jfronny.libjf.unsafe ;
2021-07-21 15:22:25 +02:00
import net.fabricmc.loader.api.FabricLoader ;
2021-10-06 19:01:48 +02:00
import net.fabricmc.loader.api.ModContainer ;
import net.fabricmc.loader.impl.ModContainerImpl ;
import net.fabricmc.loader.impl.metadata.EntrypointMetadata ;
2021-07-21 15:22:25 +02:00
import java.lang.reflect.InvocationTargetException ;
2023-03-11 21:24:59 +01:00
import java.util.* ;
2021-07-21 15:22:25 +02:00
import java.util.function.Consumer ;
public class DynamicEntry {
/ * *
* Execute an entrypoint .
*
* @param name the name of the entrypoint to execute .
* @param entrypointType the entrypoint type .
* @param onExecute entrypoint execution callback with an instance of the entrypoint type .
* @param < T > the entrypoint type .
* /
public static < T > void execute ( final String name , final Class < T > entrypointType , final Consumer < ConsumableEntrypoint < T > > onExecute ) {
final List < ConsumableEntrypoint < T > > targets = new ArrayList < > ( ) ;
2021-12-29 23:25:28 +01:00
for ( final EntrypointContainer entrypoint : getEntrypointTargets ( name ) ) {
2021-07-21 15:22:25 +02:00
try {
targets . add ( new ConsumableEntrypoint < > (
entrypointType . cast ( Class . forName ( entrypoint . entrypoint ) . getConstructor ( ) . newInstance ( ) ) ,
entrypoint . mod
) ) ;
} catch ( final ClassNotFoundException exception ) {
throw new IllegalArgumentException ( String . format ( " class %s specified in the %s entrypoint of mod %s does not exist " , entrypoint . entrypoint , name , entrypoint . mod ) , exception ) ;
} catch ( final IllegalAccessException | InstantiationException | NoSuchMethodException exception ) {
throw new IllegalStateException ( String . format ( " class %s specified in the %s entrypoint of mod %s cannot be instantiated " , entrypoint . entrypoint , name , entrypoint . mod ) , exception ) ;
} catch ( final InvocationTargetException exception ) {
throw new RuntimeException ( String . format ( " an error was encountered during the instantiation of the %s entrypoint class %s " , name , entrypoint . entrypoint ) , exception ) ;
}
}
for ( ConsumableEntrypoint < T > target : targets ) {
onExecute . accept ( target ) ;
}
}
2021-12-29 23:25:28 +01:00
private static Collection < EntrypointContainer > getEntrypointTargets ( final String entrypoint ) {
final List < EntrypointContainer > entrypoints = new LinkedList < > ( ) ;
2021-10-06 19:01:48 +02:00
for ( final ModContainer mod : FabricLoader . getInstance ( ) . getAllMods ( ) ) {
2022-08-31 10:18:31 +02:00
final List < EntrypointMetadata > modEntrypoints = ( ( ModContainerImpl ) mod ) . getMetadata ( ) . getEntrypoints ( entrypoint ) ;
2021-07-21 15:22:25 +02:00
if ( modEntrypoints ! = null ) {
for ( final EntrypointMetadata metadata : modEntrypoints ) {
entrypoints . add ( new EntrypointContainer ( metadata . getValue ( ) , mod . getMetadata ( ) . getId ( ) ) ) ;
}
}
}
return entrypoints ;
}
record EntrypointContainer ( String entrypoint , String mod ) {
}
public record ConsumableEntrypoint < T > ( T instance , String modId ) {
}
}