mirror of
https://github.com/MinecraftForge/MinecraftForge.git
synced 2026-11-22 13:09:59 +00:00
[1.18] Integrate the gametest framework with Forge (#8225)
Signed-off-by: SizableShrimp <[email protected]>
This commit is contained in:
1 parent
bb547a5748
commit
f0c2554b1c
28 files changed
+944
-8
No files matched your search
@@ -0,0 +1,73 @@
|
||||
/*
|
||||
* Minecraft Forge
|
||||
* Copyright (c) 2016-2021.
|
||||
*
|
||||
* This library is free software; you can redistribute it and/or
|
||||
* modify it under the terms of the GNU Lesser General Public
|
||||
* License as published by the Free Software Foundation version 2.1
|
||||
* of the License.
|
||||
*
|
||||
* This library is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
* Lesser General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Lesser General Public
|
||||
* License along with this library; if not, write to the Free Software
|
||||
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
||||
*/
|
||||
|
||||
package net.minecraftforge.event;
|
||||
|
||||
import net.minecraft.gametest.framework.GameTest;
|
||||
import net.minecraft.gametest.framework.GameTestGenerator;
|
||||
import net.minecraftforge.eventbus.api.Event;
|
||||
import net.minecraftforge.fml.event.IModBusEvent;
|
||||
import net.minecraftforge.gametest.ForgeGameTestHooks;
|
||||
|
||||
import java.lang.reflect.Method;
|
||||
import java.util.Arrays;
|
||||
import java.util.Set;
|
||||
|
||||
/**
|
||||
* Game tests are registered on client or server startup.
|
||||
* It is only run once for a given instance of the game if {@link ForgeGameTestHooks#isGametestEnabled} returns true.
|
||||
* This is the preferred way to register your game tests.
|
||||
* <p>
|
||||
* Fired on the Mod bus, see {@link IModBusEvent}.
|
||||
*/
|
||||
public class RegisterGameTestsEvent extends Event implements IModBusEvent
|
||||
{
|
||||
private final Set<Method> gameTestMethods;
|
||||
|
||||
public RegisterGameTestsEvent(Set<Method> gameTestMethods)
|
||||
{
|
||||
this.gameTestMethods = gameTestMethods;
|
||||
}
|
||||
|
||||
/**
|
||||
* Registers an entire class to the game test registry.
|
||||
* All methods annotated with {@link GameTest} or {@link GameTestGenerator} will be registered.
|
||||
* If the set of enabled namespaces is non-empty,
|
||||
* a method will only be registered if its {@link GameTest#templateNamespace() template namespace} is in an enabled namespace.
|
||||
*
|
||||
* @param testClass the test class to register to the game test registry
|
||||
*/
|
||||
public void register(Class<?> testClass)
|
||||
{
|
||||
Arrays.stream(testClass.getDeclaredMethods()).forEach(this::register);
|
||||
}
|
||||
|
||||
/**
|
||||
* Registers a single method to the game test registry.
|
||||
* The method will only be registered if it is annotated with {@link GameTest} or {@link GameTestGenerator}.
|
||||
* If the set of enabled namespaces is non-empty,
|
||||
* the method will only be registered if its {@link GameTest#templateNamespace() template namespace} is an enabled namespace.
|
||||
*
|
||||
* @param testMethod the test method to register to the game test registry
|
||||
*/
|
||||
public void register(Method testMethod)
|
||||
{
|
||||
this.gameTestMethods.add(testMethod);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,45 @@
|
||||
/*
|
||||
* Minecraft Forge
|
||||
* Copyright (c) 2016-2021.
|
||||
*
|
||||
* This library is free software; you can redistribute it and/or
|
||||
* modify it under the terms of the GNU Lesser General Public
|
||||
* License as published by the Free Software Foundation version 2.1
|
||||
* of the License.
|
||||
*
|
||||
* This library is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
* Lesser General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Lesser General Public
|
||||
* License along with this library; if not, write to the Free Software
|
||||
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
||||
*/
|
||||
|
||||
package net.minecraftforge.gametest;
|
||||
|
||||
import joptsimple.ValueConverter;
|
||||
import net.minecraft.core.BlockPos;
|
||||
|
||||
public class BlockPosValueConverter implements ValueConverter<BlockPos>
|
||||
{
|
||||
@Override
|
||||
public BlockPos convert(String value)
|
||||
{
|
||||
String[] split = value.split(",");
|
||||
return new BlockPos(Double.parseDouble(split[0]), Double.parseDouble(split[1]), Double.parseDouble(split[2]));
|
||||
}
|
||||
|
||||
@Override
|
||||
public Class<BlockPos> valueType()
|
||||
{
|
||||
return BlockPos.class;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String valuePattern()
|
||||
{
|
||||
return null;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,143 @@
|
||||
/*
|
||||
* Minecraft Forge
|
||||
* Copyright (c) 2016-2021.
|
||||
*
|
||||
* This library is free software; you can redistribute it and/or
|
||||
* modify it under the terms of the GNU Lesser General Public
|
||||
* License as published by the Free Software Foundation version 2.1
|
||||
* of the License.
|
||||
*
|
||||
* This library is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
* Lesser General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Lesser General Public
|
||||
* License along with this library; if not, write to the Free Software
|
||||
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
||||
*/
|
||||
|
||||
package net.minecraftforge.gametest;
|
||||
|
||||
import net.minecraft.SharedConstants;
|
||||
import net.minecraft.gametest.framework.GameTest;
|
||||
import net.minecraft.gametest.framework.GameTestRegistry;
|
||||
import net.minecraftforge.event.RegisterGameTestsEvent;
|
||||
import net.minecraftforge.fml.ModList;
|
||||
import net.minecraftforge.fml.ModLoader;
|
||||
import net.minecraftforge.fml.loading.FMLLoader;
|
||||
import net.minecraftforge.forgespi.language.ModFileScanData;
|
||||
import net.minecraftforge.forgespi.language.ModFileScanData.AnnotationData;
|
||||
import org.apache.logging.log4j.LogManager;
|
||||
import org.apache.logging.log4j.Logger;
|
||||
import org.objectweb.asm.Type;
|
||||
|
||||
import java.lang.reflect.Method;
|
||||
import java.util.Arrays;
|
||||
import java.util.Collection;
|
||||
import java.util.HashSet;
|
||||
import java.util.Set;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
public class ForgeGameTestHooks
|
||||
{
|
||||
private static boolean registeredGametests = false;
|
||||
private static final Logger LOGGER = LogManager.getLogger();
|
||||
private static final Type GAME_TEST_HOLDER = Type.getType(GameTestHolder.class);
|
||||
|
||||
public static boolean isGametestEnabled()
|
||||
{
|
||||
return !FMLLoader.isProduction() && (SharedConstants.IS_RUNNING_IN_IDE || isGametestServer() || Boolean.getBoolean("forge.enableGameTest"));
|
||||
}
|
||||
|
||||
public static boolean isGametestServer()
|
||||
{
|
||||
return !FMLLoader.isProduction() && Boolean.getBoolean("forge.gameTestServer");
|
||||
}
|
||||
|
||||
@SuppressWarnings("deprecation")
|
||||
public static void registerGametests()
|
||||
{
|
||||
if (!registeredGametests && isGametestEnabled())
|
||||
{
|
||||
Set<String> enabledNamespaces = getEnabledNamespaces();
|
||||
LOGGER.info("Enabled Gametest Namespaces: {}", enabledNamespaces);
|
||||
|
||||
Set<Method> gameTestMethods = new HashSet<>();
|
||||
RegisterGameTestsEvent event = new RegisterGameTestsEvent(gameTestMethods);
|
||||
|
||||
ModLoader.get().postEvent(event);
|
||||
|
||||
ModList.get().getAllScanData().stream()
|
||||
.map(ModFileScanData::getAnnotations)
|
||||
.flatMap(Collection::stream)
|
||||
.filter(a -> GAME_TEST_HOLDER.equals(a.annotationType()))
|
||||
.forEach(a -> addGameTestMethods(a, gameTestMethods));
|
||||
|
||||
for (Method gameTestMethod : gameTestMethods)
|
||||
{
|
||||
GameTestRegistry.register(gameTestMethod, enabledNamespaces);
|
||||
}
|
||||
|
||||
registeredGametests = true;
|
||||
}
|
||||
}
|
||||
|
||||
private static Set<String> getEnabledNamespaces()
|
||||
{
|
||||
String enabledNamespacesStr = System.getProperty("forge.enabledGameTestNamespaces");
|
||||
if (enabledNamespacesStr == null)
|
||||
{
|
||||
return Set.of();
|
||||
}
|
||||
|
||||
return Arrays.stream(enabledNamespacesStr.split(",")).filter(s -> !s.isBlank()).collect(Collectors.toUnmodifiableSet());
|
||||
}
|
||||
|
||||
private static void addGameTestMethods(AnnotationData annotationData, Set<Method> gameTestMethods)
|
||||
{
|
||||
try
|
||||
{
|
||||
Class<?> clazz = Class.forName(annotationData.clazz().getClassName(), true, ForgeGameTestHooks.class.getClassLoader());
|
||||
|
||||
gameTestMethods.addAll(Arrays.asList(clazz.getDeclaredMethods()));
|
||||
}
|
||||
catch (ClassNotFoundException e)
|
||||
{
|
||||
// Should not be possible
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
|
||||
public static String getTemplateNamespace(Method method)
|
||||
{
|
||||
GameTest gameTest = method.getAnnotation(GameTest.class);
|
||||
|
||||
if (gameTest != null && !gameTest.templateNamespace().isEmpty())
|
||||
{
|
||||
return gameTest.templateNamespace();
|
||||
}
|
||||
|
||||
GameTestHolder gameTestHolder = method.getDeclaringClass().getAnnotation(GameTestHolder.class);
|
||||
|
||||
if (gameTestHolder != null)
|
||||
{
|
||||
return gameTestHolder.value();
|
||||
}
|
||||
|
||||
return "minecraft";
|
||||
}
|
||||
|
||||
public static boolean prefixGameTestTemplate(Method method)
|
||||
{
|
||||
PrefixGameTestTemplate annotation = method.getAnnotation(PrefixGameTestTemplate.class);
|
||||
|
||||
if (annotation == null)
|
||||
{
|
||||
annotation = method.getDeclaringClass().getAnnotation(PrefixGameTestTemplate.class);
|
||||
}
|
||||
|
||||
// The vanilla game test system prefixes the classname by default, so we preserve that behavior here.
|
||||
return annotation == null || annotation.value();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,23 @@
|
||||
package net.minecraftforge.gametest;
|
||||
|
||||
import net.minecraft.gametest.framework.GameTest;
|
||||
import net.minecraft.gametest.framework.GameTestGenerator;
|
||||
|
||||
import java.lang.annotation.ElementType;
|
||||
import java.lang.annotation.Retention;
|
||||
import java.lang.annotation.RetentionPolicy;
|
||||
import java.lang.annotation.Target;
|
||||
|
||||
/**
|
||||
* Marks a class as containing game tests that should be registered automatically.
|
||||
* All methods annotated with {@link GameTest} or {@link GameTestGenerator} will be registered.
|
||||
*/
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
@Target(ElementType.TYPE)
|
||||
public @interface GameTestHolder
|
||||
{
|
||||
/**
|
||||
* Used as the default {@link GameTest#templateNamespace() template namespace} for any game tests in the class that do not specify one.
|
||||
*/
|
||||
String value() default "minecraft";
|
||||
}
|
||||
@@ -0,0 +1,32 @@
|
||||
/*
|
||||
* Minecraft Forge
|
||||
* Copyright (c) 2016-2021.
|
||||
*
|
||||
* This library is free software; you can redistribute it and/or
|
||||
* modify it under the terms of the GNU Lesser General Public
|
||||
* License as published by the Free Software Foundation version 2.1
|
||||
* of the License.
|
||||
*
|
||||
* This library is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
* Lesser General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Lesser General Public
|
||||
* License along with this library; if not, write to the Free Software
|
||||
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
||||
*/
|
||||
|
||||
package net.minecraftforge.gametest;
|
||||
|
||||
import net.minecraft.server.Main;
|
||||
|
||||
public class GameTestMain
|
||||
{
|
||||
public static void main(String[] args)
|
||||
{
|
||||
System.setProperty("forge.enableGameTest", "true");
|
||||
System.setProperty("forge.gameTestServer", "true");
|
||||
Main.main(args);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,33 @@
|
||||
package net.minecraftforge.gametest;
|
||||
|
||||
import net.minecraft.gametest.framework.GameTest;
|
||||
|
||||
import java.lang.annotation.ElementType;
|
||||
import java.lang.annotation.Retention;
|
||||
import java.lang.annotation.RetentionPolicy;
|
||||
import java.lang.annotation.Target;
|
||||
|
||||
/**
|
||||
* When used on a class, this sets the default state for whether
|
||||
* to prefix any contained game test templates with the {@link Class#getSimpleName() simple class name} or not.
|
||||
* <p>
|
||||
* When used on a method, this defines whether the specific method should be prefixed with the simple class name or not.
|
||||
* <p>
|
||||
* If this annotation cannot be found on a game test method or its containing class, the default behavior is to prefix the class name.
|
||||
* <p>
|
||||
* Method annotations override any class annotations.
|
||||
*
|
||||
* @see GameTestHolder
|
||||
*/
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
@Target({ElementType.TYPE, ElementType.METHOD})
|
||||
public @interface PrefixGameTestTemplate
|
||||
{
|
||||
/**
|
||||
* Whether to prefix the game test template with the containing class' {@link Class#getSimpleName() simple name}.
|
||||
* For example, true in a class named "MyTest" would result in "mytest.structure" while false would result in "structure".
|
||||
* <p>
|
||||
* Only applies to methods annotated with {@link GameTest}.
|
||||
*/
|
||||
boolean value() default true;
|
||||
}
|
||||
@@ -29,6 +29,7 @@ import java.util.concurrent.CountDownLatch;
|
||||
import java.util.concurrent.atomic.AtomicBoolean;
|
||||
import java.util.function.Consumer;
|
||||
|
||||
import net.minecraft.gametest.framework.GameTestServer;
|
||||
import net.minecraft.server.packs.repository.PackSource;
|
||||
import net.minecraft.server.packs.repository.RepositorySource;
|
||||
import net.minecraft.world.level.storage.LevelResource;
|
||||
@@ -97,7 +98,12 @@ public class ServerLifecycleHooks
|
||||
|
||||
public static boolean handleServerStarting(final MinecraftServer server)
|
||||
{
|
||||
DistExecutor.runWhenOn(Dist.DEDICATED_SERVER, ()->()->LanguageHook.loadLanguagesOnServer(server));
|
||||
DistExecutor.runWhenOn(Dist.DEDICATED_SERVER, ()->()->{
|
||||
LanguageHook.loadLanguagesOnServer(server);
|
||||
// GameTestServer requires the gametests to be registered earlier, so it is done in main and should not be done twice.
|
||||
if (!(server instanceof GameTestServer))
|
||||
net.minecraftforge.gametest.ForgeGameTestHooks.registerGametests();
|
||||
});
|
||||
PermissionAPI.initializePermissionAPI();
|
||||
return !MinecraftForge.EVENT_BUS.post(new ServerStartingEvent(server));
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user