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;
}
}
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";
}
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();
}
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);
}
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();
}
Aggregations