mirror of
https://github.com/MinecraftForge/MinecraftForge.git
synced 2026-11-25 14:34:28 +00:00
Add DeferredRegistryData to help with generating data driven registry entries.
Revive GameTest system and make all but one Forge Test mod pass.
This commit is contained in:
102 files changed
+1229
-469
No files matched your search
@@ -6,46 +6,31 @@
|
||||
package net.minecraftforge.gametest;
|
||||
|
||||
import net.minecraft.SharedConstants;
|
||||
import net.minecraft.core.registries.BuiltInRegistries;
|
||||
import net.minecraft.core.registries.Registries;
|
||||
import net.minecraft.gametest.framework.GameTestEnvironments;
|
||||
import net.minecraft.gametest.framework.GameTestHelper;
|
||||
import net.minecraft.gametest.framework.GameTestInstance;
|
||||
import net.minecraft.gametest.framework.TestData;
|
||||
import net.minecraft.resources.ResourceLocation;
|
||||
import net.minecraftforge.fml.ModList;
|
||||
import net.minecraftforge.fml.ModLoader;
|
||||
import net.minecraftforge.fml.common.Mod;
|
||||
import net.minecraftforge.fml.loading.FMLLoader;
|
||||
import net.minecraftforge.fml.util.ObfuscationReflectionHelper;
|
||||
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.jetbrains.annotations.ApiStatus;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
import org.objectweb.asm.Type;
|
||||
|
||||
import java.lang.invoke.MethodHandle;
|
||||
import java.lang.invoke.MethodHandles;
|
||||
import java.lang.reflect.Method;
|
||||
import java.lang.reflect.Modifier;
|
||||
import java.util.Arrays;
|
||||
import java.util.Collection;
|
||||
import java.util.HashMap;
|
||||
import java.util.HashSet;
|
||||
import java.util.Locale;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
import java.util.stream.Collectors;
|
||||
import java.util.function.Consumer;
|
||||
|
||||
/**
|
||||
* Internal class used to glue mods into the game test framework.
|
||||
* Modders should use the supplied annotations and DeferredRegister
|
||||
*/
|
||||
@SuppressWarnings("unused")
|
||||
@ApiStatus.Internal
|
||||
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);
|
||||
private static MethodHandles.Lookup LOOKUP = MethodHandles.lookup();
|
||||
|
||||
public static boolean isGametestEnabled() {
|
||||
return !FMLLoader.isProduction() && (SharedConstants.IS_RUNNING_IN_IDE || isGametestServer() || Boolean.getBoolean("forge.enableGameTest"));
|
||||
@@ -55,78 +40,45 @@ public class ForgeGameTestHooks {
|
||||
return !FMLLoader.isProduction() && Boolean.getBoolean("forge.gameTestServer");
|
||||
}
|
||||
|
||||
/*
|
||||
@SuppressWarnings("deprecation")
|
||||
public static void registerGametests() {
|
||||
if (!registeredGametests && isGametestEnabled() && ModLoader.isLoadingStateValid()) {
|
||||
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 getTestName(Method method, GameTest meta) {
|
||||
var name = method.getName().toLowerCase(Locale.ENGLISH);
|
||||
return getPrefixed(method, name);
|
||||
}
|
||||
|
||||
private static String getPrefixed(Method method, String name) {
|
||||
var prefix = getPrefix(method);
|
||||
return prefix == null ? name : prefix + '.' + name;
|
||||
}
|
||||
|
||||
@Nullable
|
||||
private static String getPrefix(Method method) {
|
||||
private static boolean shouldPrefix(Method method) {
|
||||
var cls = method.getDeclaringClass();
|
||||
var shouldPrefix = !method.isAnnotationPresent(GameTestDontPrefix.class) &&
|
||||
!cls.isAnnotationPresent(GameTestDontPrefix.class);
|
||||
if (!shouldPrefix)
|
||||
return null;
|
||||
return !method.isAnnotationPresent(GameTestDontPrefix.class) &&
|
||||
!cls.isAnnotationPresent(GameTestDontPrefix.class);
|
||||
}
|
||||
|
||||
return getPrefix(cls);
|
||||
private static String snake(String input) {
|
||||
var result = new StringBuilder();
|
||||
for (char c : input.toCharArray()) {
|
||||
if (Character.isUpperCase(c)) {
|
||||
if (!result.isEmpty())
|
||||
result.append('_');
|
||||
result.append(Character.toLowerCase(c));
|
||||
} else
|
||||
result.append(c);
|
||||
}
|
||||
return result.toString();
|
||||
}
|
||||
|
||||
private static String getPrefix(Class<?> cls) {
|
||||
var prefix = cls.getAnnotation(GameTestPrefix.class);
|
||||
if (prefix != null)
|
||||
return prefix.value();
|
||||
return prefix.value() + '/';
|
||||
|
||||
var holder = cls.getAnnotation(GameTestHolder.class);
|
||||
var mod = cls.getAnnotation(Mod.class);
|
||||
if (mod != null) {
|
||||
var holder = cls.getAnnotation(GameTestNamespace.class);
|
||||
if (holder != null && !holder.value().isEmpty())
|
||||
return mod.value() + '/';
|
||||
}
|
||||
|
||||
|
||||
return cls.getSimpleName().toLowerCase(Locale.ENGLISH);
|
||||
}
|
||||
|
||||
private static String getNamespace(Class<?> cls) {
|
||||
var holder = cls.getAnnotation(GameTestNamespace.class);
|
||||
if (holder != null && !holder.value().isEmpty())
|
||||
return holder.value();
|
||||
|
||||
@@ -134,21 +86,32 @@ public class ForgeGameTestHooks {
|
||||
if (mod != null)
|
||||
return mod.value();
|
||||
|
||||
return cls.getSimpleName().toLowerCase(Locale.ENGLISH);
|
||||
throw new IllegalArgumentException("Could not find modid for " + cls.getName());
|
||||
}
|
||||
|
||||
private static ResourceLocation key(String namespace, String path) {
|
||||
return path.indexOf(':') == -1 ? ResourceLocation.parse(path) : ResourceLocation.fromNamespaceAndPath(namespace, path);
|
||||
private static ResourceLocation key(String namespace, String prefix, String path) {
|
||||
if (path.indexOf(':') != -1) {
|
||||
var rl = ResourceLocation.parse(path);
|
||||
if (!prefix.isEmpty())
|
||||
return rl.withPrefix(prefix);
|
||||
return rl;
|
||||
}
|
||||
|
||||
return ResourceLocation.fromNamespaceAndPath(namespace, prefix + path);
|
||||
}
|
||||
|
||||
public static Map<ResourceLocation, GameTestInstance> gatherTests(Class<?> root) {
|
||||
String prefix = getPrefix(root);
|
||||
var seen = new HashSet<ResourceLocation>();
|
||||
public record TestReference(Consumer<GameTestHelper> consumer, TestData<ResourceLocation> data) {}
|
||||
|
||||
public static Map<ResourceLocation, TestReference> gatherTests(Class<?> root, Object instance) {
|
||||
var ret = new HashMap<ResourceLocation, TestReference>();
|
||||
var seen = new HashSet<String>();
|
||||
String class_prefix = getPrefix(root);
|
||||
String namespace = getNamespace(root);
|
||||
Class<?> cls = root;
|
||||
|
||||
while (cls != Object.class) {
|
||||
for (var method : cls.getDeclaredMethods()) {
|
||||
if (cls != root && Modifier.isStatic(method.getModifiers()))
|
||||
if (!Modifier.isPublic(method.getModifiers()) || (cls != root && Modifier.isStatic(method.getModifiers())))
|
||||
continue;
|
||||
|
||||
var gametest = method.getAnnotation(GameTest.class);
|
||||
@@ -160,40 +123,73 @@ public class ForgeGameTestHooks {
|
||||
if (method.getParameterCount() != 1 || method.getParameterTypes()[0] != GameTestHelper.class)
|
||||
throw new IllegalStateException("Invalid @GameTest function " + owner + " incorrect arguments");
|
||||
|
||||
var testName = key(prefix, gametest.name().isEmpty() ? method.getName() : gametest.name());
|
||||
if (!seen.add(testName)) {
|
||||
if (!Modifier.isStatic(method.getModifiers()))
|
||||
continue;
|
||||
throw new IllegalStateException("Failed to register test, already seen " + testName + ", for " + owner);
|
||||
}
|
||||
// We don't want parent methods
|
||||
if (!seen.add(method.getName()))
|
||||
continue;
|
||||
|
||||
if (gametest.type() == GameTest.Type.FUNCTION) {
|
||||
var funcName = key(prefix, gametest.function());
|
||||
var func = BuiltInRegistries.TEST_FUNCTION.get(funcName).orElse(null);
|
||||
var prefix = shouldPrefix(method) ? class_prefix : "";
|
||||
var name = key(namespace, prefix, gametest.name().isEmpty() ? snake(method.getName()) : gametest.name());
|
||||
|
||||
if (func == null)
|
||||
throw new IllegalStateException("Could not find referenced function `" + funcName + "` for " + owner);
|
||||
var structure = GameTest.DEFAULT_STRUCTURE.equals(gametest.structure())
|
||||
? ResourceLocation.parse(gametest.structure())
|
||||
: key(namespace, "", gametest.structure());
|
||||
|
||||
var data = getTestData(prefix, owner, gametest.data());
|
||||
var env = GameTestEnvironments.DEFAULT.equals(gametest.environment())
|
||||
? ResourceLocation.withDefaultNamespace(gametest.environment())
|
||||
: key(namespace, "", gametest.environment());
|
||||
|
||||
var data = new TestData<ResourceLocation>(
|
||||
env,
|
||||
structure,
|
||||
gametest.maxTicks(),
|
||||
gametest.setupTicks(),
|
||||
gametest.required(),
|
||||
gametest.rotation(),
|
||||
gametest.manualOnly(),
|
||||
gametest.maxAttempts(),
|
||||
gametest.requiredSuccesses(),
|
||||
gametest.skyAccess()
|
||||
);
|
||||
|
||||
} else if (gametest.type() == GameTest.Type.BLOCK) {
|
||||
MethodHandle handle = unreflect(method, owner);
|
||||
|
||||
Consumer<GameTestHelper> func;
|
||||
if (Modifier.isStatic(method.getModifiers())) {
|
||||
func = ctx -> {
|
||||
try {
|
||||
handle.invoke(ctx);
|
||||
} catch (Throwable e) {
|
||||
sneakyThrow(e);
|
||||
}
|
||||
};
|
||||
} else {
|
||||
throw new IllegalStateException("Could not determine GameTest type for " + root.getName() + " -> " + gametest.type());
|
||||
func = ctx -> {
|
||||
try {
|
||||
handle.invoke(instance, ctx);
|
||||
} catch (Throwable e) {
|
||||
sneakyThrow(e);
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
ret.put(name, new TestReference(func, data));
|
||||
}
|
||||
|
||||
cls = cls.getSuperclass();
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
private static MethodHandle unreflect(Method method, String owner) {
|
||||
try {
|
||||
return LOOKUP.unreflect(method);
|
||||
} catch (IllegalAccessException e) {
|
||||
throw new IllegalArgumentException("Failed to unreflect " + owner, e);
|
||||
}
|
||||
}
|
||||
|
||||
private static TestData<?> getTestData(String prefix, String owner, GameTest.Data data) {
|
||||
var envName = key(prefix, data.environment());
|
||||
var env = BuiltInRegistries.TEST_ENVIRONMENT
|
||||
@SuppressWarnings("unchecked")
|
||||
private static <E extends Throwable> void sneakyThrow(Throwable e) throws E {
|
||||
throw (E)e;
|
||||
}
|
||||
*/
|
||||
}
|
||||
@@ -6,39 +6,23 @@ import static java.lang.annotation.RetentionPolicy.RUNTIME;
|
||||
import java.lang.annotation.Retention;
|
||||
import java.lang.annotation.Target;
|
||||
|
||||
import net.minecraft.gametest.framework.GameTestEnvironments;
|
||||
import net.minecraft.world.level.block.Rotation;
|
||||
|
||||
@Retention(RUNTIME)
|
||||
@Target(METHOD)
|
||||
public @interface GameTest {
|
||||
String name() default "";
|
||||
String function() default "";
|
||||
Type type() default Type.BLOCK;
|
||||
Data data() default @Data;
|
||||
|
||||
|
||||
|
||||
int rotationSteps() default 0;
|
||||
|
||||
|
||||
public enum Type {
|
||||
BLOCK,
|
||||
FUNCTION;
|
||||
}
|
||||
|
||||
/**
|
||||
* Annotation definitions for {@link net.minecraft.gametest.framework.TestData}
|
||||
*/
|
||||
public @interface Data {
|
||||
String environment() default "";
|
||||
String structure() default "forge:empty3x3";
|
||||
int maxTicks() default Integer.MAX_VALUE;
|
||||
int setupTicks() default 0;
|
||||
boolean required() default false;
|
||||
Rotation rotation() default Rotation.NONE;
|
||||
boolean manualOnly() default false;
|
||||
int maxAttempts() default 1;
|
||||
int requiredSuccesses() default 1;
|
||||
boolean skyAccess() default false;
|
||||
}
|
||||
static final String DEFAULT_STRUCTURE = "forge:empty3x3x3";
|
||||
String environment() default GameTestEnvironments.DEFAULT;
|
||||
String structure() default DEFAULT_STRUCTURE;
|
||||
int maxTicks() default 100;
|
||||
int setupTicks() default 0;
|
||||
boolean required() default true;
|
||||
Rotation rotation() default Rotation.NONE;
|
||||
boolean manualOnly() default false;
|
||||
int maxAttempts() default 1;
|
||||
int requiredSuccesses() default 1;
|
||||
boolean skyAccess() default false;
|
||||
}
|
||||
@@ -1,46 +0,0 @@
|
||||
/*
|
||||
* Copyright (c) Forge Development LLC and contributors
|
||||
* SPDX-License-Identifier: LGPL-2.1-only
|
||||
*/
|
||||
|
||||
package net.minecraftforge.gametest;
|
||||
|
||||
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 {
|
||||
/**
|
||||
* The default batch for all tests in this class that do not manually specify one.
|
||||
* It it recommended that this includes your mod id.
|
||||
* <br/>
|
||||
* This is also used when filtering out which batches to run during automation.
|
||||
* the filters will be tested against the prefix of the batch name. So it is
|
||||
* recommended to be structured in a logical fashion.
|
||||
* <pre>
|
||||
* Examples:
|
||||
* forge.tags.items
|
||||
* forge.tags.blocks
|
||||
* forge.global_loot_modifiers.block_drops
|
||||
* forge.global_loot_modifiers.tool_filter
|
||||
* </pre>
|
||||
*
|
||||
* The filter 'forge.global_loot_modifiers' would execute both the block_drops and tool_filter batches.
|
||||
* <p>
|
||||
* This will default to your modid if it is on the same class that is annotated with {@link net.minecraftforge.fml.common.Mod @Mod}
|
||||
*/
|
||||
String value() default "";
|
||||
|
||||
/**
|
||||
* Used as the default namespace to use for {@link GameTest#template() templates} for any game tests in the class.
|
||||
* Defaults to the modid if in a class with @Mod or else defaults to "minecraft"
|
||||
*/
|
||||
String namespace() default "";
|
||||
}
|
||||
@@ -0,0 +1,28 @@
|
||||
/*
|
||||
* Copyright (c) Forge Development LLC and contributors
|
||||
* SPDX-License-Identifier: LGPL-2.1-only
|
||||
*/
|
||||
|
||||
package net.minecraftforge.gametest;
|
||||
|
||||
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 GameTestNamespace {
|
||||
/**
|
||||
* The default namespace for all tests in this class that do not manually specify one.
|
||||
* It it recommended that this is your modid, but if you have multiple test mods that
|
||||
* you want to group together you can use this to do so.
|
||||
* <p>
|
||||
* This will default to your modid if it is on the same class that is annotated with {@link net.minecraftforge.fml.common.Mod @Mod}
|
||||
*/
|
||||
String value() default "";
|
||||
}
|
||||
Reference in new issue
Block a user