diff --git a/src/test/java/me/desht/modularrouters/test/RouterTestHelper.java b/src/test/java/me/desht/modularrouters/test/RouterTestHelper.java index 789af394..ef1f65aa 100644 --- a/src/test/java/me/desht/modularrouters/test/RouterTestHelper.java +++ b/src/test/java/me/desht/modularrouters/test/RouterTestHelper.java @@ -1,5 +1,6 @@ package me.desht.modularrouters.test; +import me.desht.modularrouters.block.ModularRouterBlock; import me.desht.modularrouters.block.tile.ModularRouterBlockEntity; import me.desht.modularrouters.container.handler.AugmentHandler; import me.desht.modularrouters.core.ModBlocks; @@ -23,6 +24,7 @@ import net.minecraft.world.level.block.entity.ChestBlockEntity; import net.minecraft.world.phys.Vec3; import net.neoforged.neoforge.items.IItemHandlerModifiable; +import net.neoforged.neoforge.items.ItemHandlerHelper; import net.neoforged.testframework.gametest.ExtendedGameTestHelper; import java.util.List; @@ -37,7 +39,11 @@ public RouterTestHelper(GameTestInfo info) { } public RouterWrapper placeRouter(int x, int y, int z) { - setBlock(x, y, z, ModBlocks.MODULAR_ROUTER.get()); + return placeRouter(x, y, z, Direction.NORTH); + } + + public RouterWrapper placeRouter(int x, int y, int z, Direction routerFacing) { + setBlock(x, y, z, ModBlocks.MODULAR_ROUTER.get().defaultBlockState().setValue(ModularRouterBlock.FACING, routerFacing)); var router = getBlockEntity(x, y, z, ModularRouterBlockEntity.class); addEndListener(success -> { if (success) { @@ -88,13 +94,7 @@ public RouterWrapper maxSpeed() { public ItemStack addDirectionalModule(Supplier module, RelativeDirection direction) { var stack = module.get().getDefaultInstance(); - stack.set(ModDataComponents.COMMON_MODULE_SETTINGS, new ModuleSettings( - ModuleFlags.DEFAULT, - direction, - ModuleTermination.NONE, - RedstoneBehaviour.ALWAYS, - 0 - )); + stack.set(ModDataComponents.COMMON_MODULE_SETTINGS, ModuleSettingsBuilder.create().facing(direction).build()); return addModule(stack); } @@ -107,13 +107,13 @@ public ItemStack addTargetedModule(Supplier module, int x, int y } public ItemStack addModule(ItemStack module) { - router.getModules().insertItem(0, module, false); + ItemHandlerHelper.insertItem(router.getModules(), module, false); return module; } - public ItemStack addUpgrade(ItemStack module) { - router.getUpgrades().insertItem(0, module, false); - return module; + public ItemStack addUpgrade(ItemStack upgrade) { + ItemHandlerHelper.insertItem(router.getUpgrades(), upgrade, false); + return upgrade; } public ItemStack insertBuffer(ItemStack stack) { @@ -173,7 +173,10 @@ public static class ModuleSettingsBuilder { private RedstoneBehaviour redstoneBehaviour; private int regulatorAmount; - public ModuleSettingsBuilder() {} + public static ModuleSettingsBuilder create() { + return new ModuleSettingsBuilder(ModuleSettings.DEFAULT); + } + public ModuleSettingsBuilder(ModuleSettings settings) { this.flags = settings.flags(); this.facing = settings.facing(); @@ -187,6 +190,26 @@ public ModuleSettingsBuilder facing(RelativeDirection direction) { return this; } + public ModuleSettingsBuilder flags(ModuleFlags moduleFlags) { + this.flags = moduleFlags; + return this; + } + + public ModuleSettingsBuilder termination(ModuleTermination termination) { + this.termination = termination; + return this; + } + + public ModuleSettingsBuilder redstone(RedstoneBehaviour redstoneBehaviour) { + this.redstoneBehaviour = redstoneBehaviour; + return this; + } + + public ModuleSettingsBuilder regulated(int amount) { + this.regulatorAmount = amount; + return this; + } + public ModuleSettings build() { return new ModuleSettings(flags, facing, termination, redstoneBehaviour, regulatorAmount); } diff --git a/src/test/java/me/desht/modularrouters/test/module/ActivatorModuleTest.java b/src/test/java/me/desht/modularrouters/test/module/ActivatorModuleTest.java new file mode 100644 index 00000000..75bafbdf --- /dev/null +++ b/src/test/java/me/desht/modularrouters/test/module/ActivatorModuleTest.java @@ -0,0 +1,89 @@ +package me.desht.modularrouters.test.module; + +import me.desht.modularrouters.core.ModDataComponents; +import me.desht.modularrouters.core.ModItems; +import me.desht.modularrouters.logic.compiled.CompiledActivatorModule; +import me.desht.modularrouters.logic.compiled.CompiledActivatorModule.ActionType; +import me.desht.modularrouters.logic.compiled.CompiledActivatorModule.ActivatorSettings; +import me.desht.modularrouters.logic.compiled.CompiledActivatorModule.EntityMode; +import me.desht.modularrouters.logic.compiled.CompiledActivatorModule.LookDirection; +import me.desht.modularrouters.logic.settings.RelativeDirection; +import me.desht.modularrouters.test.RouterTestHelper; +import me.desht.modularrouters.test.RouterTestHelper.ModuleSettingsBuilder; +import net.minecraft.core.BlockPos; +import net.minecraft.core.Direction; +import net.minecraft.gametest.framework.GameTest; +import net.minecraft.world.entity.EntityType; +import net.minecraft.world.item.ItemStack; +import net.minecraft.world.item.Items; +import net.minecraft.world.level.block.Blocks; +import net.minecraft.world.level.block.LeverBlock; +import net.neoforged.testframework.annotation.TestHolder; +import net.neoforged.testframework.gametest.EmptyTemplate; + +public class ActivatorModuleTest { + @GameTest + @TestHolder + @EmptyTemplate(floor = true) + static void activatorTestBlock(final RouterTestHelper helper) { + var router = helper.placeRouter(0, 2, 1, Direction.EAST); + helper.setBlock(2, 2, 1, Blocks.LEVER); + + ItemStack activator = ModItems.ACTIVATOR_MODULE.toStack(); + activator.set(ModDataComponents.COMMON_MODULE_SETTINGS, ModuleSettingsBuilder.create().facing(RelativeDirection.FRONT).build()); + router.addModule(activator); + + helper.startSequence() + .thenIdle(router.routerTicks(1)) + .thenExecute(() -> helper.assertBlockProperty(new BlockPos(2, 2, 1), LeverBlock.POWERED, true)) + .thenSucceed(); + } + + @GameTest + @TestHolder + @EmptyTemplate(floor = true) + static void activatorTestItem(final RouterTestHelper helper) { + var router = helper.placeRouter(1, 2, 1, Direction.EAST); + helper.setBlock(2, 2, 1, Blocks.AIR); + + ItemStack activator = ModItems.ACTIVATOR_MODULE.toStack(); + activator.set(ModDataComponents.COMMON_MODULE_SETTINGS, ModuleSettingsBuilder.create().facing(RelativeDirection.FRONT).build()); + activator.set(ModDataComponents.ACTIVATOR_SETTINGS, new ActivatorSettings( + ActionType.ITEM_OR_BLOCK, LookDirection.BELOW, EntityMode.NEAREST, false) + ); + router.addModule(activator); + router.insertBuffer(Items.FLINT_AND_STEEL.getDefaultInstance()); + + helper.startSequence() + .thenIdle(router.routerTicks(1)) + .thenExecute(() -> helper.assertBlockPresent(Blocks.FIRE, 2,2 , 1)) + .thenSucceed(); + } + + @GameTest + @TestHolder + @EmptyTemplate(floor = true) + static void activatorTestEntity(final RouterTestHelper helper) { + var router = helper.placeRouter(1, 2, 1, Direction.EAST); + helper.spawn(EntityType.COW, 2, 2, 1); + + ItemStack activator = ModItems.ACTIVATOR_MODULE.toStack(); + activator.set(ModDataComponents.COMMON_MODULE_SETTINGS, ModuleSettingsBuilder.create().facing(RelativeDirection.FRONT).build()); + activator.set(ModDataComponents.ACTIVATOR_SETTINGS, new ActivatorSettings( + ActionType.USE_ITEM_ON_ENTITY, LookDirection.LEVEL, EntityMode.NEAREST, false) + ); + router.addModule(activator); + router.insertBuffer(Items.BUCKET.getDefaultInstance()); + + helper.startSequence() + .thenIdle(router.routerTicks(1)) + .thenExecute(() -> router.assertBuffer(Items.MILK_BUCKET, 1)) + // 4 buckets in router; 3 empty buckets should stay, 1 milk bucket dropped + .thenExecute(() -> router.setBuffer(new ItemStack(Items.BUCKET, 4))) + .thenIdle(router.routerTicks(1)) + .thenExecute(() -> router.assertBuffer(Items.BUCKET, 3)) + .thenExecute(() -> helper.assertItemEntityCountIs(Items.MILK_BUCKET, new BlockPos(2, 2, 1), 1.0, 1)) + .thenSucceed(); + + } +} diff --git a/src/test/java/me/desht/modularrouters/test/module/DropperModuleTest.java b/src/test/java/me/desht/modularrouters/test/module/DropperModuleTest.java new file mode 100644 index 00000000..e77a7a65 --- /dev/null +++ b/src/test/java/me/desht/modularrouters/test/module/DropperModuleTest.java @@ -0,0 +1,46 @@ +package me.desht.modularrouters.test.module; + +import me.desht.modularrouters.core.ModItems; +import me.desht.modularrouters.logic.settings.RelativeDirection; +import me.desht.modularrouters.test.RouterTestHelper; +import net.minecraft.core.BlockPos; +import net.minecraft.gametest.framework.GameTest; +import net.minecraft.world.item.ItemStack; +import net.minecraft.world.item.Items; +import net.neoforged.testframework.annotation.TestHolder; +import net.neoforged.testframework.gametest.EmptyTemplate; + +public class DropperModuleTest { + @GameTest + @TestHolder + @EmptyTemplate(floor = true) + static void testDropperModule(final RouterTestHelper helper) { + var router = helper.placeRouter(1, 1, 1); + + router.addDirectionalModule(ModItems.DROPPER_MODULE, RelativeDirection.UP); + + helper.startSequence() + .thenExecute(() -> router.insertBuffer(new ItemStack(Items.DIAMOND))) + .thenIdle(router.routerTicks(1)) + .thenExecute(router::assertBufferEmpty) + .thenExecute(() -> helper.assertItemEntityPresent(Items.DIAMOND, new BlockPos(1, 2, 1), 0.0)) + .thenSucceed(); + } + + @GameTest + @TestHolder + @EmptyTemplate(floor = true) + static void testDropperModuleStacked(final RouterTestHelper helper) { + var router = helper.placeRouter(1, 1, 1); + + router.addDirectionalModule(ModItems.DROPPER_MODULE, RelativeDirection.FRONT); // effectively north + router.addUpgrade(ModItems.STACK_UPGRADE.toStack(5)); // 32 items at a time + + helper.startSequence() + .thenExecute(() -> router.insertBuffer(new ItemStack(Items.DIAMOND, 64))) + .thenIdle(router.routerTicks(1)) + .thenExecute(() -> router.assertBuffer(Items.DIAMOND, 32)) + .thenExecute(() -> helper.assertItemEntityCountIs(Items.DIAMOND, new BlockPos(1, 1, 0), 0.0, 32)) + .thenSucceed(); + } +} diff --git a/src/test/java/me/desht/modularrouters/test/module/FilterTest.java b/src/test/java/me/desht/modularrouters/test/module/FilterTest.java new file mode 100644 index 00000000..cf5b8a07 --- /dev/null +++ b/src/test/java/me/desht/modularrouters/test/module/FilterTest.java @@ -0,0 +1,51 @@ +package me.desht.modularrouters.test.module; + +import me.desht.modularrouters.core.ModDataComponents; +import me.desht.modularrouters.core.ModItems; +import me.desht.modularrouters.logic.settings.*; +import me.desht.modularrouters.test.RouterTestHelper; +import me.desht.modularrouters.test.RouterTestHelper.ModuleSettingsBuilder; +import net.minecraft.gametest.framework.GameTest; +import net.minecraft.world.item.ItemStack; +import net.minecraft.world.item.Items; +import net.minecraft.world.item.component.ItemContainerContents; +import net.neoforged.testframework.annotation.TestHolder; +import net.neoforged.testframework.gametest.EmptyTemplate; + +import java.util.List; + +public class FilterTest { + @GameTest + @TestHolder + @EmptyTemplate + static void simpleFilterTest(final RouterTestHelper helper) { + var router = helper.placeRouter(1, 1, 1); + + var topChest = helper.placeChest(1, 2, 1); // above router + var northChest = helper.placeChest(1, 1, 0); // north of router + + // first module blacklists cobblestone + ItemStack sender1 = ModItems.SENDER_MODULE_1.toStack(); + sender1.set(ModDataComponents.FILTER, ItemContainerContents.fromItems(List.of(Items.COBBLESTONE.getDefaultInstance()))); + sender1.set(ModDataComponents.COMMON_MODULE_SETTINGS, ModuleSettingsBuilder.create().facing(RelativeDirection.UP).build()); + router.router().getModules().insertItem(0, sender1, false); + + // second module has empty blacklist + ItemStack sender2 = ModItems.SENDER_MODULE_1.toStack(); + sender2.set(ModDataComponents.COMMON_MODULE_SETTINGS, ModuleSettingsBuilder.create().facing(RelativeDirection.FRONT).build()); + router.router().getModules().insertItem(1, sender2, false); + + router.insertBuffer(new ItemStack(Items.COBBLESTONE, 2)); + + helper.startSequence() + .thenIdle(router.routerTicks(2)) + .thenExecute(router::assertBufferEmpty) + .thenExecute(() -> helper.assertValueEqual(topChest.countItem(Items.COBBLESTONE), 0, "top chest cobblestone")) + .thenExecute(() -> helper.assertValueEqual(northChest.countItem(Items.COBBLESTONE), 2, "north chest cobblestone")) + .thenExecute(() -> router.insertBuffer(new ItemStack(Items.IRON_INGOT, 2))) + .thenIdle(router.routerTicks(2)) + .thenExecute(() -> helper.assertValueEqual(topChest.countItem(Items.IRON_INGOT), 1, "top chest iron")) + .thenExecute(() -> helper.assertValueEqual(topChest.countItem(Items.IRON_INGOT), 1, "north chest iron")) + .thenSucceed(); + } +}