From b05506ad4694031ae208884d303851a71eddde47 Mon Sep 17 00:00:00 2001 From: simibubi <31564874+simibubi@users.noreply.github.com> Date: Thu, 2 Sep 2021 23:58:38 +0200 Subject: [PATCH] Mechanicalacinahcem - Added the option to prevent a Mechanical Crafting recipe from matching a vertically flipped arrangement --- src/generated/resources/.cache/cache | 10 ++-- .../mechanical_crafting/crushing_wheel.json | 3 +- .../mechanical_crafting/extendo_grip.json | 3 +- .../recipes/mechanical_crafting/flywheel.json | 3 +- .../mechanical_crafting/furnace_engine.json | 3 +- .../mechanical_crafting/potato_cannon.json | 3 +- .../crafter/MechanicalCraftingRecipe.java | 59 +++++++++++++++++-- .../MechanicalCraftingRecipeBuilder.java | 18 +++++- .../recipe/MechanicalCraftingRecipeGen.java | 6 +- 9 files changed, 88 insertions(+), 20 deletions(-) diff --git a/src/generated/resources/.cache/cache b/src/generated/resources/.cache/cache index b0bc41e77..3af0320b7 100644 --- a/src/generated/resources/.cache/cache +++ b/src/generated/resources/.cache/cache @@ -3377,11 +3377,11 @@ bbf64f7eb3868e354756e57348493e2b1ae6b0d9 data/create/recipes/limestone_cobblesto 88fa2b1ab746d5e13a8afd6e7e7d80ad843e0016 data/create/recipes/limestone_cobblestone_wall_from_limestone_cobblestone_stonecutting.json 327bb8a6535b60bb65d0dda9d5205e988bc82526 data/create/recipes/limestone_pillar.json c2e15ac0c9109bad3face6d13efc32d7116b4c25 data/create/recipes/limestone_pillar_from_limestone_stonecutting.json -88173753ceaf121c5430bbf928a40e3c046dbfe0 data/create/recipes/mechanical_crafting/crushing_wheel.json -14e322d4de8fae35d952274376497740bb3d5962 data/create/recipes/mechanical_crafting/extendo_grip.json -de7fea84434753873dfa2b929d9b5f5f86ac6a5c data/create/recipes/mechanical_crafting/flywheel.json -e491fd8a8873308270f9dc2a57ac8f2c70431dcc data/create/recipes/mechanical_crafting/furnace_engine.json -8e5224d22b228f69473ca48ca0d874b34660b573 data/create/recipes/mechanical_crafting/potato_cannon.json +66674d07de63aada0991d2fdff07e22e00450135 data/create/recipes/mechanical_crafting/crushing_wheel.json +599f8b87c24c131350ba7ceb69a0c8b9829c62bc data/create/recipes/mechanical_crafting/extendo_grip.json +f26ed47c10cc63613759b0f8ae4ef349000de60d data/create/recipes/mechanical_crafting/flywheel.json +2dc00d6e4c159e06ab2a705e666e83e4238a7814 data/create/recipes/mechanical_crafting/furnace_engine.json +b77911c169b6205a09001a09ca074eb2234f0d29 data/create/recipes/mechanical_crafting/potato_cannon.json 98f877bf8f3f8a686fc6cf7479a0fba5744248ce data/create/recipes/milling/allium.json 8c7e1cbc87c7ca7df2bf949957e89422fef8ad94 data/create/recipes/milling/aluminum_ore.json bcff4d30ae09a0729bce8b2dbde4ddd6719a998b data/create/recipes/milling/andesite.json diff --git a/src/generated/resources/data/create/recipes/mechanical_crafting/crushing_wheel.json b/src/generated/resources/data/create/recipes/mechanical_crafting/crushing_wheel.json index a8c1170ec..34ad9ea72 100644 --- a/src/generated/resources/data/create/recipes/mechanical_crafting/crushing_wheel.json +++ b/src/generated/resources/data/create/recipes/mechanical_crafting/crushing_wheel.json @@ -21,5 +21,6 @@ "result": { "item": "create:crushing_wheel", "count": 2 - } + }, + "acceptMirrored": false } \ No newline at end of file diff --git a/src/generated/resources/data/create/recipes/mechanical_crafting/extendo_grip.json b/src/generated/resources/data/create/recipes/mechanical_crafting/extendo_grip.json index c7fc84b1c..2d69859ee 100644 --- a/src/generated/resources/data/create/recipes/mechanical_crafting/extendo_grip.json +++ b/src/generated/resources/data/create/recipes/mechanical_crafting/extendo_grip.json @@ -23,5 +23,6 @@ }, "result": { "item": "create:extendo_grip" - } + }, + "acceptMirrored": false } \ No newline at end of file diff --git a/src/generated/resources/data/create/recipes/mechanical_crafting/flywheel.json b/src/generated/resources/data/create/recipes/mechanical_crafting/flywheel.json index 010b2fbae..dee9cffd7 100644 --- a/src/generated/resources/data/create/recipes/mechanical_crafting/flywheel.json +++ b/src/generated/resources/data/create/recipes/mechanical_crafting/flywheel.json @@ -15,5 +15,6 @@ }, "result": { "item": "create:flywheel" - } + }, + "acceptMirrored": true } \ No newline at end of file diff --git a/src/generated/resources/data/create/recipes/mechanical_crafting/furnace_engine.json b/src/generated/resources/data/create/recipes/mechanical_crafting/furnace_engine.json index 45765aebe..3842209f0 100644 --- a/src/generated/resources/data/create/recipes/mechanical_crafting/furnace_engine.json +++ b/src/generated/resources/data/create/recipes/mechanical_crafting/furnace_engine.json @@ -26,5 +26,6 @@ }, "result": { "item": "create:furnace_engine" - } + }, + "acceptMirrored": true } \ No newline at end of file diff --git a/src/generated/resources/data/create/recipes/mechanical_crafting/potato_cannon.json b/src/generated/resources/data/create/recipes/mechanical_crafting/potato_cannon.json index fffb05d24..a21d9bd04 100644 --- a/src/generated/resources/data/create/recipes/mechanical_crafting/potato_cannon.json +++ b/src/generated/resources/data/create/recipes/mechanical_crafting/potato_cannon.json @@ -20,5 +20,6 @@ }, "result": { "item": "create:potato_cannon" - } + }, + "acceptMirrored": true } \ No newline at end of file diff --git a/src/main/java/com/simibubi/create/content/contraptions/components/crafter/MechanicalCraftingRecipe.java b/src/main/java/com/simibubi/create/content/contraptions/components/crafter/MechanicalCraftingRecipe.java index 2022cccf2..4eef6c570 100644 --- a/src/main/java/com/simibubi/create/content/contraptions/components/crafter/MechanicalCraftingRecipe.java +++ b/src/main/java/com/simibubi/create/content/contraptions/components/crafter/MechanicalCraftingRecipe.java @@ -10,25 +10,58 @@ import net.minecraft.item.crafting.IRecipeType; import net.minecraft.item.crafting.Ingredient; import net.minecraft.item.crafting.ShapedRecipe; import net.minecraft.network.PacketBuffer; +import net.minecraft.util.JSONUtils; import net.minecraft.util.NonNullList; import net.minecraft.util.ResourceLocation; import net.minecraft.world.World; public class MechanicalCraftingRecipe extends ShapedRecipe { + private boolean acceptMirrored; + public MechanicalCraftingRecipe(ResourceLocation idIn, String groupIn, int recipeWidthIn, int recipeHeightIn, - NonNullList recipeItemsIn, ItemStack recipeOutputIn) { + NonNullList recipeItemsIn, ItemStack recipeOutputIn, boolean acceptMirrored) { super(idIn, groupIn, recipeWidthIn, recipeHeightIn, recipeItemsIn, recipeOutputIn); + this.acceptMirrored = acceptMirrored; } - private static MechanicalCraftingRecipe fromShaped(ShapedRecipe recipe) { + private static MechanicalCraftingRecipe fromShaped(ShapedRecipe recipe, boolean acceptMirrored) { return new MechanicalCraftingRecipe(recipe.getId(), recipe.getGroup(), recipe.getWidth(), recipe.getHeight(), - recipe.getIngredients(), recipe.getResultItem()); + recipe.getIngredients(), recipe.getResultItem(), acceptMirrored); } @Override public boolean matches(CraftingInventory inv, World worldIn) { - return inv instanceof MechanicalCraftingInventory && super.matches(inv, worldIn); + if (!(inv instanceof MechanicalCraftingInventory)) + return false; + if (acceptsMirrored()) + return super.matches(inv, worldIn); + + // From ShapedRecipe except the symmetry + for (int i = 0; i <= inv.getWidth() - this.getWidth(); ++i) + for (int j = 0; j <= inv.getHeight() - this.getHeight(); ++j) + if (this.matchesSpecific(inv, i, j)) + return true; + return false; + } + + // From ShapedRecipe + private boolean matchesSpecific(CraftingInventory inv, int p_77573_2_, int p_77573_3_) { + NonNullList ingredients = getIngredients(); + int width = getWidth(); + int height = getHeight(); + for (int i = 0; i < inv.getWidth(); ++i) { + for (int j = 0; j < inv.getHeight(); ++j) { + int k = i - p_77573_2_; + int l = j - p_77573_3_; + Ingredient ingredient = Ingredient.EMPTY; + if (k >= 0 && l >= 0 && k < width && l < height) + ingredient = ingredients.get(k + l * width); + if (!ingredient.test(inv.getItem(i + j * inv.getWidth()))) + return false; + } + } + return true; } @Override @@ -46,16 +79,30 @@ public class MechanicalCraftingRecipe extends ShapedRecipe { return AllRecipeTypes.MECHANICAL_CRAFTING.getSerializer(); } + public boolean acceptsMirrored() { + return acceptMirrored; + } + public static class Serializer extends ShapedRecipe.Serializer { @Override public ShapedRecipe fromJson(ResourceLocation recipeId, JsonObject json) { - return fromShaped(super.fromJson(recipeId, json)); + return fromShaped(super.fromJson(recipeId, json), JSONUtils.getAsBoolean(json, "acceptMirrored", true)); } @Override public ShapedRecipe fromNetwork(ResourceLocation recipeId, PacketBuffer buffer) { - return fromShaped(super.fromNetwork(recipeId, buffer)); + return fromShaped(super.fromNetwork(recipeId, buffer), buffer.readBoolean() && buffer.readBoolean()); + } + + @Override + public void toNetwork(PacketBuffer p_199427_1_, ShapedRecipe p_199427_2_) { + super.toNetwork(p_199427_1_, p_199427_2_); + if (p_199427_2_ instanceof MechanicalCraftingRecipe) { + p_199427_1_.writeBoolean(true); + p_199427_1_.writeBoolean(((MechanicalCraftingRecipe) p_199427_2_).acceptsMirrored()); + } else + p_199427_1_.writeBoolean(false); } } diff --git a/src/main/java/com/simibubi/create/foundation/data/recipe/MechanicalCraftingRecipeBuilder.java b/src/main/java/com/simibubi/create/foundation/data/recipe/MechanicalCraftingRecipeBuilder.java index 3f806faf7..2b75c2f4a 100644 --- a/src/main/java/com/simibubi/create/foundation/data/recipe/MechanicalCraftingRecipeBuilder.java +++ b/src/main/java/com/simibubi/create/foundation/data/recipe/MechanicalCraftingRecipeBuilder.java @@ -30,10 +30,12 @@ public class MechanicalCraftingRecipeBuilder { private final int count; private final List pattern = Lists.newArrayList(); private final Map key = Maps.newLinkedHashMap(); + private boolean acceptMirrored; public MechanicalCraftingRecipeBuilder(IItemProvider p_i48261_1_, int p_i48261_2_) { result = p_i48261_1_.asItem(); count = p_i48261_2_; + acceptMirrored = true; } /** @@ -91,6 +93,14 @@ public class MechanicalCraftingRecipeBuilder { } } + /** + * Prevents the crafters from matching a vertically flipped version of the recipe + */ + public MechanicalCraftingRecipeBuilder disallowMirrored() { + acceptMirrored = false; + return this; + } + /** * Builds this recipe into an {@link IFinishedRecipe}. */ @@ -116,7 +126,8 @@ public class MechanicalCraftingRecipeBuilder { */ public void build(Consumer p_200467_1_, ResourceLocation p_200467_2_) { validate(p_200467_2_); - p_200467_1_.accept(new MechanicalCraftingRecipeBuilder.Result(p_200467_2_, result, count, pattern, key)); + p_200467_1_ + .accept(new MechanicalCraftingRecipeBuilder.Result(p_200467_2_, result, count, pattern, key, acceptMirrored)); } /** @@ -151,14 +162,16 @@ public class MechanicalCraftingRecipeBuilder { private final int count; private final List pattern; private final Map key; + private final boolean acceptMirrored; public Result(ResourceLocation p_i48271_2_, Item p_i48271_3_, int p_i48271_4_, List p_i48271_6_, - Map p_i48271_7_) { + Map p_i48271_7_, boolean asymmetrical) { this.id = p_i48271_2_; this.result = p_i48271_3_; this.count = p_i48271_4_; this.pattern = p_i48271_6_; this.key = p_i48271_7_; + this.acceptMirrored = asymmetrical; } public void serializeRecipeData(JsonObject p_218610_1_) { @@ -180,6 +193,7 @@ public class MechanicalCraftingRecipeBuilder { jsonobject1.addProperty("count", this.count); p_218610_1_.add("result", jsonobject1); + p_218610_1_.addProperty("acceptMirrored", acceptMirrored); } public IRecipeSerializer getType() { diff --git a/src/main/java/com/simibubi/create/foundation/data/recipe/MechanicalCraftingRecipeGen.java b/src/main/java/com/simibubi/create/foundation/data/recipe/MechanicalCraftingRecipeGen.java index d4e5c5eb5..b15d1ac64 100644 --- a/src/main/java/com/simibubi/create/foundation/data/recipe/MechanicalCraftingRecipeGen.java +++ b/src/main/java/com/simibubi/create/foundation/data/recipe/MechanicalCraftingRecipeGen.java @@ -27,7 +27,8 @@ public class MechanicalCraftingRecipeGen extends CreateRecipeProvider { .patternLine("AAPAA") .patternLine("APSPA") .patternLine("AAPAA") - .patternLine(" AAA ")), + .patternLine(" AAA ") + .disallowMirrored()), EXTENDO_GRIP = create(AllItems.EXTENDO_GRIP::get).returns(1) .recipe(b -> b.key('L', Ingredient.of(I.brass())) @@ -38,7 +39,8 @@ public class MechanicalCraftingRecipeGen extends CreateRecipeProvider { .patternLine(" R ") .patternLine("SSS") .patternLine("SSS") - .patternLine(" H ")), + .patternLine(" H ") + .disallowMirrored()), POTATO_CANNON = create(AllItems.POTATO_CANNON::get).returns(1) .recipe(b -> b.key('L', I.andesite())