|
| 1 | +package dev.compactmods.crafting.tests.proxies; |
| 2 | + |
| 3 | +import javax.annotation.Nonnull; |
| 4 | +import com.alcatrazescapee.mcjunitlib.framework.IntegrationTest; |
| 5 | +import com.alcatrazescapee.mcjunitlib.framework.IntegrationTestClass; |
| 6 | +import com.alcatrazescapee.mcjunitlib.framework.IntegrationTestHelper; |
| 7 | +import dev.compactmods.crafting.Registration; |
| 8 | +import dev.compactmods.crafting.api.EnumCraftingState; |
| 9 | +import dev.compactmods.crafting.api.field.IFieldListener; |
| 10 | +import dev.compactmods.crafting.api.field.IMiniaturizationField; |
| 11 | +import dev.compactmods.crafting.api.recipe.IMiniaturizationRecipe; |
| 12 | +import dev.compactmods.crafting.field.capability.CapabilityMiniaturizationField; |
| 13 | +import dev.compactmods.crafting.proxies.data.RescanFieldProxyEntity; |
| 14 | +import dev.compactmods.crafting.server.ServerConfig; |
| 15 | +import dev.compactmods.crafting.tests.recipes.util.RecipeTestUtil; |
| 16 | +import net.minecraft.block.Blocks; |
| 17 | +import net.minecraft.entity.item.ItemEntity; |
| 18 | +import net.minecraft.item.ItemStack; |
| 19 | +import net.minecraft.item.Items; |
| 20 | +import net.minecraft.tileentity.TileEntity; |
| 21 | +import net.minecraft.util.ResourceLocation; |
| 22 | +import net.minecraft.util.math.BlockPos; |
| 23 | +import net.minecraftforge.common.util.LazyOptional; |
| 24 | +import org.junit.jupiter.api.Assertions; |
| 25 | +import org.junit.jupiter.api.Tag; |
| 26 | + |
| 27 | +@IntegrationTestClass("proxies") |
| 28 | +public class RescanProxyTests { |
| 29 | + |
| 30 | + @Tag("minecraft") |
| 31 | + @org.junit.jupiter.api.BeforeAll |
| 32 | + static void BeforeAllTests() { |
| 33 | + ServerConfig.RECIPE_REGISTRATION.set(true); |
| 34 | + ServerConfig.RECIPE_MATCHING.set(true); |
| 35 | + ServerConfig.FIELD_BLOCK_CHANGES.set(true); |
| 36 | + } |
| 37 | + |
| 38 | + @Nonnull |
| 39 | + private RescanFieldProxyEntity setupProxyForMediumField(IntegrationTestHelper test) { |
| 40 | + final BlockPos proxyLocation = new BlockPos(0, 1, 0); |
| 41 | + |
| 42 | + test.setBlockState(proxyLocation, Registration.RESCAN_FIELD_PROXY_BLOCK.get().defaultBlockState()); |
| 43 | + test.setBlockState(BlockPos.ZERO, Blocks.BLACK_CONCRETE.defaultBlockState()); |
| 44 | + |
| 45 | + final TileEntity tile = test.getTileEntity(proxyLocation); |
| 46 | + Assertions.assertTrue(tile instanceof RescanFieldProxyEntity); |
| 47 | + |
| 48 | + RescanFieldProxyEntity proxy = (RescanFieldProxyEntity) tile; |
| 49 | + |
| 50 | + BlockPos centerField = new BlockPos(6, 3, 6); |
| 51 | + proxy.updateField(test.relativePos(centerField).orElse(BlockPos.ZERO)); |
| 52 | + return proxy; |
| 53 | + } |
| 54 | + |
| 55 | + @IntegrationTest("medium_field") |
| 56 | + void RescanProxyDoesNotRescanActivelyCraftingField(IntegrationTestHelper test) { |
| 57 | + |
| 58 | + final IntegrationTestHelper.ScheduleHelper scheduler = test.scheduler(); |
| 59 | + |
| 60 | + RescanFieldProxyEntity proxy = setupProxyForMediumField(test); |
| 61 | + test.addNamedRunnable("postMatch", () -> { |
| 62 | + scheduler.thenRun(0, () -> { |
| 63 | + // spawn catalyst item to drop into field |
| 64 | + BlockPos aboveField = test.getOrigin().offset(new BlockPos(6, 7, 6)); |
| 65 | + ItemEntity catalyst = new ItemEntity(test.getWorld(), |
| 66 | + aboveField.getX(), aboveField.getY(), aboveField.getZ(), |
| 67 | + new ItemStack(Items.ENDER_PEARL)); |
| 68 | + |
| 69 | + test.getWorld().addFreshEntity(catalyst); |
| 70 | + }).thenRun(10, () -> { |
| 71 | + test.setBlockState(BlockPos.ZERO, Blocks.REDSTONE_BLOCK.defaultBlockState()); |
| 72 | + }).thenRun(1, () -> { |
| 73 | + // after the rescan is triggered, the field should still be matched and crafting |
| 74 | + proxy.getCapability(CapabilityMiniaturizationField.MINIATURIZATION_FIELD).ifPresent(field -> { |
| 75 | + test.assertTrue(() -> field.getCurrentRecipe().isPresent(), "Recipe was cleared"); |
| 76 | + test.assertTrue(() -> EnumCraftingState.CRAFTING.equals(field.getCraftingState()), |
| 77 | + "Field crafting state erroneously not 'CRAFTING'"); |
| 78 | + }); |
| 79 | + }); |
| 80 | + }); |
| 81 | + |
| 82 | + RecipeTestUtil.loadStructureIntoTestArea(test, |
| 83 | + new ResourceLocation("compactcrafting", "recipes/ender_crystal"), |
| 84 | + new BlockPos(4, 1, 4)); |
| 85 | + |
| 86 | + final LazyOptional<IMiniaturizationField> f = proxy.getCapability(CapabilityMiniaturizationField.MINIATURIZATION_FIELD); |
| 87 | + f.ifPresent(field -> { |
| 88 | + field.registerListener(LazyOptional.of(() -> new IFieldListener() { |
| 89 | + @Override |
| 90 | + public void onRecipeMatched(IMiniaturizationField field, IMiniaturizationRecipe recipe) { |
| 91 | + test.executeNamedRunnable("postMatch"); |
| 92 | + } |
| 93 | + })); |
| 94 | + |
| 95 | + field.fieldContentsChanged(); |
| 96 | + }); |
| 97 | + } |
| 98 | +} |
0 commit comments