77
88import dan200 .computercraft .export .Exporter ;
99import dan200 .computercraft .gametest .api .GameTestHolder ;
10- import net .minecraft .gametest .framework .GameTest ;
11- import net .minecraft .gametest .framework .GameTestRegistry ;
12- import net .minecraft .gametest .framework .StructureUtils ;
13- import net .minecraft .gametest .framework .TestFunction ;
14- import net .minecraft .resources .ResourceLocation ;
10+ import net .minecraftforge .api .distmarker .Dist ;
1511import net .minecraftforge .client .event .RegisterClientCommandsEvent ;
12+ import net .minecraftforge .client .event .ScreenEvent ;
1613import net .minecraftforge .common .MinecraftForge ;
1714import net .minecraftforge .event .RegisterCommandsEvent ;
1815import net .minecraftforge .event .RegisterGameTestsEvent ;
16+ import net .minecraftforge .event .TickEvent ;
1917import net .minecraftforge .event .server .ServerStartedEvent ;
2018import net .minecraftforge .eventbus .api .EventPriority ;
19+ import net .minecraftforge .fml .DistExecutor ;
2120import net .minecraftforge .fml .ModList ;
2221import net .minecraftforge .fml .common .Mod ;
2322import net .minecraftforge .fml .javafmlmod .FMLJavaModLoadingContext ;
@@ -43,7 +42,7 @@ public TestMod() {
4342 var bus = MinecraftForge .EVENT_BUS ;
4443 bus .addListener (EventPriority .LOW , (ServerStartedEvent e ) -> TestHooks .onServerStarted (e .getServer ()));
4544 bus .addListener ((RegisterCommandsEvent e ) -> CCTestCommand .register (e .getDispatcher ()));
46- bus . addListener (( RegisterClientCommandsEvent e ) -> Exporter . register ( e . getDispatcher ()) );
45+ DistExecutor . unsafeRunWhenOn ( Dist . CLIENT , ( ) -> TestMod :: onInitializeClient );
4746
4847 var modBus = FMLJavaModLoadingContext .get ().getModEventBus ();
4948 modBus .addListener ((RegisterGameTestsEvent event ) -> {
@@ -56,6 +55,17 @@ public TestMod() {
5655 });
5756 }
5857
58+ private static void onInitializeClient () {
59+ var bus = MinecraftForge .EVENT_BUS ;
60+
61+ bus .addListener ((TickEvent .ServerTickEvent e ) -> {
62+ if (e .phase == TickEvent .Phase .START ) ClientTestHooks .onServerTick (e .getServer ());
63+ });
64+ bus .addListener ((ScreenEvent .Opening e ) -> {
65+ if (ClientTestHooks .onOpenScreen (e .getScreen ())) e .setCanceled (true );
66+ });
67+ bus .addListener ((RegisterClientCommandsEvent e ) -> Exporter .register (e .getDispatcher ()));
68+ }
5969
6070 private static Class <?> loadClass (String name ) {
6171 try {
@@ -68,60 +78,7 @@ private static Class<?> loadClass(String name) {
6878 private static void registerClass (String className , Consumer <Method > fallback ) {
6979 var klass = loadClass (className );
7080 for (var method : klass .getDeclaredMethods ()) {
71- var testInfo = method .getAnnotation (GameTest .class );
72- if (testInfo == null ) {
73- fallback .accept (method );
74- continue ;
75- }
76-
77- GameTestRegistry .getAllTestFunctions ().add (turnMethodIntoTestFunction (method , testInfo ));
78- GameTestRegistry .getAllTestClassNames ().add (className );
81+ TestHooks .registerTest (klass , method , fallback );
7982 }
8083 }
81-
82- /**
83- * Custom implementation of {@link GameTestRegistry#turnMethodIntoTestFunction(Method)} which makes
84- * {@link GameTest#template()} behave the same as Fabric, namely in that it points to a {@link ResourceLocation},
85- * rather than a test-class-specific structure.
86- * <p>
87- * This effectively acts as a global version of {@link PrefixGameTestTemplate}, just one which doesn't require Forge
88- * to be present.
89- *
90- * @param method The method to register.
91- * @param testInfo The test info.
92- * @return The constructed test function.
93- */
94- private static TestFunction turnMethodIntoTestFunction (Method method , GameTest testInfo ) {
95- var className = method .getDeclaringClass ().getSimpleName ().toLowerCase (Locale .ROOT );
96- var testName = className + "." + method .getName ().toLowerCase (Locale .ROOT );
97- return new TestFunction (
98- testInfo .batch (),
99- testName ,
100- testInfo .template ().isEmpty () ? testName : testInfo .template (),
101- StructureUtils .getRotationForRotationSteps (testInfo .rotationSteps ()), testInfo .timeoutTicks (), testInfo .setupTicks (),
102- testInfo .required (), testInfo .requiredSuccesses (), testInfo .attempts (),
103- turnMethodIntoConsumer (method )
104- );
105- }
106-
107- private static <T > Consumer <T > turnMethodIntoConsumer (Method method ) {
108- return value -> {
109- try {
110- Object instance = null ;
111- if (!Modifier .isStatic (method .getModifiers ())) {
112- instance = method .getDeclaringClass ().getConstructor ().newInstance ();
113- }
114-
115- method .invoke (instance , value );
116- } catch (InvocationTargetException e ) {
117- if (e .getCause () instanceof RuntimeException ) {
118- throw (RuntimeException ) e .getCause ();
119- } else {
120- throw new RuntimeException (e .getCause ());
121- }
122- } catch (ReflectiveOperationException e ) {
123- throw new RuntimeException (e );
124- }
125- };
126- }
12784}
0 commit comments