Search in sources :

Example 1 with GameTest

use of net.minecraft.gametest.framework.GameTest in project MinecraftForge by MinecraftForge.

the class ForgeGameTestHooks method registerGametests.

@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;
    }
}
Also used : ModLoader(net.minecraftforge.fml.ModLoader) Arrays(java.util.Arrays) AnnotationData(net.minecraftforge.forgespi.language.ModFileScanData.AnnotationData) Collection(java.util.Collection) ModList(net.minecraftforge.fml.ModList) Set(java.util.Set) GameTestRegistry(net.minecraft.gametest.framework.GameTestRegistry) Type(org.objectweb.asm.Type) Collectors(java.util.stream.Collectors) HashSet(java.util.HashSet) FMLLoader(net.minecraftforge.fml.loading.FMLLoader) Logger(org.apache.logging.log4j.Logger) RegisterGameTestsEvent(net.minecraftforge.event.RegisterGameTestsEvent) SharedConstants(net.minecraft.SharedConstants) ModFileScanData(net.minecraftforge.forgespi.language.ModFileScanData) GameTest(net.minecraft.gametest.framework.GameTest) LogManager(org.apache.logging.log4j.LogManager) Method(java.lang.reflect.Method) RegisterGameTestsEvent(net.minecraftforge.event.RegisterGameTestsEvent) Collection(java.util.Collection) Method(java.lang.reflect.Method) HashSet(java.util.HashSet)

Example 2 with GameTest

use of net.minecraft.gametest.framework.GameTest in project MinecraftForge by MinecraftForge.

the class ForgeGameTestHooks method getTemplateNamespace.

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";
}
Also used : GameTest(net.minecraft.gametest.framework.GameTest)

Example 3 with GameTest

use of net.minecraft.gametest.framework.GameTest in project MinecraftForge by MinecraftForge.

the class GameTestTest method testWood.

/**
 * An example game test
 * <p><ul>
 * <li>Must take <b>one</b> parameter, the {@link GameTestHelper}</li>
 * <li>The return type is ignored, so it should be {@code void}</li>
 * <li>Can be {@code static} or non-static<p>
 * WARNING: If made non-static, then it will create an instance of the class every time it is run.</li>
 * </ul>
 * The default template name converts the containing class's name to all lowercase, and the method name to all lowercase.
 * In this example, the structure name would be "gametesttest.testwood" under the "gametest_test" namespace.
 */
@GameTest(templateNamespace = MODID)
public static void testWood(GameTestHelper helper) {
    // The woodPos is in the bottom center of the 3x3x3 structure
    BlockPos woodPos = new BlockPos(1, 1, 1);
    // assertBlockState will convert the relative woodPos into a real world block position and check it with the predicate.
    // Relative positions are made absolute by adding their value to the block position of the structure tile entity,
    // which is always the lowest northwest corner inside the structure.
    // If the predicate fails, the String supplier will be used to construct an exception message, which is thrown
    helper.assertBlockState(woodPos, b -> b.is(Blocks.OAK_LOG), () -> "Block was not an oak log");
    // If we got to this point, the assert succeeded, so the test has succeeded.
    // Game tests require explicitly declaring success, otherwise they fail from timeout.
    helper.succeed();
}
Also used : BlockPos(net.minecraft.core.BlockPos) GameTest(net.minecraft.gametest.framework.GameTest)

Example 4 with GameTest

use of net.minecraft.gametest.framework.GameTest in project MinecraftForge by MinecraftForge.

the class GameTestTest method testHopperPickup.

@PrefixGameTestTemplate(false)
@GameTest(templateNamespace = MODID, template = "empty3x3x3")
public static void testHopperPickup(GameTestHelper helper) {
    BlockPos hopperPos = new BlockPos(1, 1, 1);
    // Sets (1,1,1) to a hopper and spawns a golden apple one block above it
    helper.setBlock(hopperPos, Blocks.HOPPER);
    helper.spawnItem(Items.GOLDEN_APPLE, 1, 2, 1);
    // Waits 20 ticks before checking that the hopper contains the golden apple
    helper.assertAtTickTimeContainerContains(20, hopperPos, Items.GOLDEN_APPLE);
    // Succeeds at 21 ticks if the previous check didn't throw an exception
    helper.runAtTickTime(21, helper::succeed);
}
Also used : BlockPos(net.minecraft.core.BlockPos) GameTest(net.minecraft.gametest.framework.GameTest) PrefixGameTestTemplate(net.minecraftforge.gametest.PrefixGameTestTemplate)

Example 5 with GameTest

use of net.minecraft.gametest.framework.GameTest in project MinecraftForge by MinecraftForge.

the class GameTestTest method testEnergyStorage.

@PrefixGameTestTemplate(false)
@GameTest(templateNamespace = MODID, template = "empty3x3x3")
public static void testEnergyStorage(GameTestHelper helper) {
    BlockPos energyPos = new BlockPos(1, 1, 1);
    // Sets (1,1,1) to our custom energy block
    helper.setBlock(energyPos, ENERGY_BLOCK.get());
    // Queries the energy capability
    LazyOptional<IEnergyStorage> energyHolder = helper.getBlockEntity(energyPos).getCapability(CapabilityEnergy.ENERGY);
    // Adds 2000 FE, but our energy storage can only hold 1000 FE
    energyHolder.ifPresent(energyStorage -> energyStorage.receiveEnergy(2000, false));
    // Fails test if stored energy is not equal to 1000 FE
    int energy = energyHolder.map(IEnergyStorage::getEnergyStored).orElse(0);
    int target = 1000;
    if (energy != target) {
        helper.fail("Expected energy=" + target + " but it was energy=" + energy, energyPos);
    }
    helper.succeed();
}
Also used : IEnergyStorage(net.minecraftforge.energy.IEnergyStorage) BlockPos(net.minecraft.core.BlockPos) GameTest(net.minecraft.gametest.framework.GameTest) PrefixGameTestTemplate(net.minecraftforge.gametest.PrefixGameTestTemplate)

Aggregations

GameTest (net.minecraft.gametest.framework.GameTest)5 BlockPos (net.minecraft.core.BlockPos)3 PrefixGameTestTemplate (net.minecraftforge.gametest.PrefixGameTestTemplate)2 Method (java.lang.reflect.Method)1 Arrays (java.util.Arrays)1 Collection (java.util.Collection)1 HashSet (java.util.HashSet)1 Set (java.util.Set)1 Collectors (java.util.stream.Collectors)1 SharedConstants (net.minecraft.SharedConstants)1 GameTestRegistry (net.minecraft.gametest.framework.GameTestRegistry)1 IEnergyStorage (net.minecraftforge.energy.IEnergyStorage)1 RegisterGameTestsEvent (net.minecraftforge.event.RegisterGameTestsEvent)1 ModList (net.minecraftforge.fml.ModList)1 ModLoader (net.minecraftforge.fml.ModLoader)1 FMLLoader (net.minecraftforge.fml.loading.FMLLoader)1 ModFileScanData (net.minecraftforge.forgespi.language.ModFileScanData)1 AnnotationData (net.minecraftforge.forgespi.language.ModFileScanData.AnnotationData)1 LogManager (org.apache.logging.log4j.LogManager)1 Logger (org.apache.logging.log4j.Logger)1