From f48f5fe1011af2f85f735f328cc6a158d68948fd Mon Sep 17 00:00:00 2001 From: simibubi <31564874+simibubi@users.noreply.github.com> Date: Sun, 19 Jan 2020 23:10:00 +0100 Subject: [PATCH] Supersized Assembly - Added option to add recipes for mechanical crafter exclusively, with a maximum grid of 9x9 - Cleaned up recipe registry foundation - Fixed recipe type namespace issue --- .../java/com/simibubi/create/AllRecipes.java | 62 ++++++++++++------- .../crafter/MechanicalCraftingInventory.java | 31 ++++++++++ .../crafter/MechanicalCraftingRecipe.java | 58 +++++++++++++++++ .../components/crafter/RecipeGridHandler.java | 30 +++------ .../CrushingWheelControllerTileEntity.java | 10 +-- .../press/MechanicalPressTileEntity.java | 2 +- .../components/saw/SawTileEntity.java | 2 +- .../modules/logistics/InWorldProcessing.java | 4 +- .../recipes/mechanical_crafting/circuit.json | 28 +++++++++ .../recipes/mechanical_crafting/motor.json | 37 +++++++++++ 10 files changed, 210 insertions(+), 54 deletions(-) create mode 100644 src/main/java/com/simibubi/create/modules/contraptions/components/crafter/MechanicalCraftingInventory.java create mode 100644 src/main/java/com/simibubi/create/modules/contraptions/components/crafter/MechanicalCraftingRecipe.java create mode 100644 src/main/resources/data/create/recipes/mechanical_crafting/circuit.json create mode 100644 src/main/resources/data/create/recipes/mechanical_crafting/motor.json diff --git a/src/main/java/com/simibubi/create/AllRecipes.java b/src/main/java/com/simibubi/create/AllRecipes.java index c02039519..ef8f96165 100644 --- a/src/main/java/com/simibubi/create/AllRecipes.java +++ b/src/main/java/com/simibubi/create/AllRecipes.java @@ -3,18 +3,22 @@ package com.simibubi.create; import java.util.function.Supplier; import com.simibubi.create.foundation.utility.Lang; +import com.simibubi.create.modules.contraptions.components.crafter.MechanicalCraftingRecipe; import com.simibubi.create.modules.contraptions.components.crusher.CrushingRecipe; import com.simibubi.create.modules.contraptions.components.fan.SplashingRecipe; import com.simibubi.create.modules.contraptions.components.mixer.MixingRecipe; import com.simibubi.create.modules.contraptions.components.press.PressingRecipe; import com.simibubi.create.modules.contraptions.components.saw.CuttingRecipe; +import com.simibubi.create.modules.contraptions.processing.ProcessingRecipe; import com.simibubi.create.modules.contraptions.processing.ProcessingRecipeSerializer; +import com.simibubi.create.modules.contraptions.processing.ProcessingRecipeSerializer.IRecipeFactory; import com.simibubi.create.modules.curiosities.placementHandgun.BuilderGunUpgradeRecipe; import net.minecraft.inventory.IInventory; import net.minecraft.item.crafting.IRecipe; import net.minecraft.item.crafting.IRecipeSerializer; import net.minecraft.item.crafting.IRecipeType; +import net.minecraft.item.crafting.ShapedRecipe; import net.minecraft.util.ResourceLocation; import net.minecraft.util.registry.Registry; import net.minecraftforge.event.RegistryEvent; @@ -22,46 +26,58 @@ import net.minecraftforge.event.RegistryEvent; public enum AllRecipes { BLOCKZAPPER_UPGRADE(BuilderGunUpgradeRecipe.Serializer::new, IRecipeType.CRAFTING), - CRUSHING(() -> new ProcessingRecipeSerializer<>(CrushingRecipe::new), Types.CRUSHING), - SPLASHING(() -> new ProcessingRecipeSerializer<>(SplashingRecipe::new), Types.SPLASHING), - PRESSING(() -> new ProcessingRecipeSerializer<>(PressingRecipe::new), Types.PRESSING), - CUTTING(() -> new ProcessingRecipeSerializer<>(CuttingRecipe::new), Types.CUTTING), - MIXING(() -> new ProcessingRecipeSerializer<>(MixingRecipe::new), Types.MIXING), + MECHANICAL_CRAFTING(MechanicalCraftingRecipe.Serializer::new), + CRUSHING(processingSerializer(CrushingRecipe::new)), + SPLASHING(processingSerializer(SplashingRecipe::new)), + PRESSING(processingSerializer(PressingRecipe::new)), + CUTTING(processingSerializer(CuttingRecipe::new)), + MIXING(processingSerializer(MixingRecipe::new)), ; - public static class Types { - public static IRecipeType CRUSHING = register("crushing"); - public static IRecipeType SPLASHING = register("splashing"); - public static IRecipeType PRESSING = register("pressing"); - public static IRecipeType CUTTING = register("cutting"); - public static IRecipeType MIXING = register("mixing"); - - static > IRecipeType register(final String key) { - return Registry.register(Registry.RECIPE_TYPE, new ResourceLocation(key), new IRecipeType() { - public String toString() { - return key; - } - }); - } - } - public IRecipeSerializer serializer; public Supplier> supplier; public IRecipeType> type; + @SuppressWarnings("unchecked") + public > T getType() { + return (T) type; + } + + private AllRecipes(Supplier> supplier) { + this(supplier, null); + } + private AllRecipes(Supplier> supplier, - IRecipeType> type) { + IRecipeType> existingType) { this.supplier = supplier; - this.type = type; + this.type = existingType; } public static void register(RegistryEvent.Register> event) { + ShapedRecipe.setCraftingSize(9, 9); + for (AllRecipes r : AllRecipes.values()) { + if (r.type == null) + r.type = customType(Lang.asId(r.name())); + r.serializer = r.supplier.get(); ResourceLocation location = new ResourceLocation(Create.ID, Lang.asId(r.name())); event.getRegistry().register(r.serializer.setRegistryName(location)); } } + + private static > IRecipeType customType(String id) { + return Registry.register(Registry.RECIPE_TYPE, new ResourceLocation(Create.ID, id), new IRecipeType() { + public String toString() { + return Create.ID + ":" + id; + } + }); + } + + private static Supplier> processingSerializer(IRecipeFactory> factory) { + return () -> new ProcessingRecipeSerializer<>(factory); + } + } diff --git a/src/main/java/com/simibubi/create/modules/contraptions/components/crafter/MechanicalCraftingInventory.java b/src/main/java/com/simibubi/create/modules/contraptions/components/crafter/MechanicalCraftingInventory.java new file mode 100644 index 000000000..6c2d13b28 --- /dev/null +++ b/src/main/java/com/simibubi/create/modules/contraptions/components/crafter/MechanicalCraftingInventory.java @@ -0,0 +1,31 @@ +package com.simibubi.create.modules.contraptions.components.crafter; + +import org.apache.commons.lang3.tuple.Pair; + +import com.simibubi.create.modules.contraptions.components.crafter.RecipeGridHandler.GroupedItems; + +import net.minecraft.entity.player.PlayerEntity; +import net.minecraft.inventory.CraftingInventory; +import net.minecraft.inventory.container.Container; +import net.minecraft.item.ItemStack; + +public class MechanicalCraftingInventory extends CraftingInventory { + + private static Container dummyContainer = new Container(null, -1) { + public boolean canInteractWith(PlayerEntity playerIn) { + return false; + } + }; + + public MechanicalCraftingInventory(GroupedItems items) { + super(dummyContainer, items.width, items.height); + for (int y = 0; y < items.height; y++) { + for (int x = 0; x < items.width; x++) { + ItemStack stack = items.grid.get(Pair.of(x + items.minX, y + items.minY)); + setInventorySlotContents(x + (items.height - y - 1) * items.width, + stack == null ? ItemStack.EMPTY : stack.copy()); + } + } + } + +} diff --git a/src/main/java/com/simibubi/create/modules/contraptions/components/crafter/MechanicalCraftingRecipe.java b/src/main/java/com/simibubi/create/modules/contraptions/components/crafter/MechanicalCraftingRecipe.java new file mode 100644 index 000000000..9048eed88 --- /dev/null +++ b/src/main/java/com/simibubi/create/modules/contraptions/components/crafter/MechanicalCraftingRecipe.java @@ -0,0 +1,58 @@ +package com.simibubi.create.modules.contraptions.components.crafter; + +import com.google.gson.JsonObject; +import com.simibubi.create.AllRecipes; + +import net.minecraft.inventory.CraftingInventory; +import net.minecraft.item.ItemStack; +import net.minecraft.item.crafting.IRecipeSerializer; +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.NonNullList; +import net.minecraft.util.ResourceLocation; +import net.minecraft.world.World; + +public class MechanicalCraftingRecipe extends ShapedRecipe { + + public MechanicalCraftingRecipe(ResourceLocation idIn, String groupIn, int recipeWidthIn, int recipeHeightIn, + NonNullList recipeItemsIn, ItemStack recipeOutputIn) { + super(idIn, groupIn, recipeWidthIn, recipeHeightIn, recipeItemsIn, recipeOutputIn); + } + + private static MechanicalCraftingRecipe fromShaped(ShapedRecipe recipe) { + return new MechanicalCraftingRecipe(recipe.getId(), recipe.getGroup(), recipe.getWidth(), recipe.getHeight(), + recipe.getIngredients(), recipe.getRecipeOutput()); + } + + @Override + public boolean matches(CraftingInventory inv, World worldIn) { + return inv instanceof MechanicalCraftingInventory && super.matches(inv, worldIn); + } + + @Override + public IRecipeType getType() { + return AllRecipes.MECHANICAL_CRAFTING.type; + } + + @Override + public IRecipeSerializer getSerializer() { + return AllRecipes.MECHANICAL_CRAFTING.serializer; + } + + public static class Serializer extends ShapedRecipe.Serializer { + + @Override + public ShapedRecipe read(ResourceLocation recipeId, JsonObject json) { + return fromShaped(super.read(recipeId, json)); + } + + @Override + public ShapedRecipe read(ResourceLocation recipeId, PacketBuffer buffer) { + return fromShaped(super.read(recipeId, buffer)); + } + + } + +} diff --git a/src/main/java/com/simibubi/create/modules/contraptions/components/crafter/RecipeGridHandler.java b/src/main/java/com/simibubi/create/modules/contraptions/components/crafter/RecipeGridHandler.java index 222ffea46..501339c6b 100644 --- a/src/main/java/com/simibubi/create/modules/contraptions/components/crafter/RecipeGridHandler.java +++ b/src/main/java/com/simibubi/create/modules/contraptions/components/crafter/RecipeGridHandler.java @@ -14,12 +14,11 @@ import org.apache.commons.lang3.tuple.Pair; import com.google.common.base.Predicates; import com.simibubi.create.AllBlocks; +import com.simibubi.create.AllRecipes; import com.simibubi.create.modules.contraptions.components.crafter.MechanicalCrafterBlock.Pointing; import net.minecraft.block.BlockState; -import net.minecraft.entity.player.PlayerEntity; import net.minecraft.inventory.CraftingInventory; -import net.minecraft.inventory.container.Container; import net.minecraft.item.ItemStack; import net.minecraft.item.crafting.IRecipeType; import net.minecraft.nbt.CompoundNBT; @@ -116,31 +115,18 @@ public class RecipeGridHandler { } public static ItemStack tryToApplyRecipe(World world, GroupedItems items) { - CraftingInventory craftinginventory = getCraftingInventory(items); + items.calcStats(); + CraftingInventory craftinginventory = new MechanicalCraftingInventory(items); ItemStack result = world.getRecipeManager().getRecipe(IRecipeType.CRAFTING, craftinginventory, world) .map(r -> r.getCraftingResult(craftinginventory)).orElse(null); + if (result == null) + result = world.getRecipeManager() + .getRecipe(AllRecipes.MECHANICAL_CRAFTING.getType(), craftinginventory, world) + .map(r -> r.getCraftingResult(craftinginventory)).orElse(null); + return result; } - private static CraftingInventory getCraftingInventory(GroupedItems items) { - items.calcStats(); - CraftingInventory craftinginventory = new CraftingInventory(new Container(null, -1) { - public boolean canInteractWith(PlayerEntity playerIn) { - return false; - } - }, items.width, items.height); - - for (int y = 0; y < items.height; y++) { - for (int x = 0; x < items.width; x++) { - ItemStack stack = items.grid.get(Pair.of(x + items.minX, y + items.minY)); - craftinginventory.setInventorySlotContents(x + (items.height - y - 1) * items.width, - stack == null ? ItemStack.EMPTY : stack.copy()); - } - } - - return craftinginventory; - } - public static class GroupedItems { Map, ItemStack> grid = new HashMap<>(); int minX, minY, maxX, maxY, width, height; diff --git a/src/main/java/com/simibubi/create/modules/contraptions/components/crusher/CrushingWheelControllerTileEntity.java b/src/main/java/com/simibubi/create/modules/contraptions/components/crusher/CrushingWheelControllerTileEntity.java index 981b9a4c5..8ce813989 100644 --- a/src/main/java/com/simibubi/create/modules/contraptions/components/crusher/CrushingWheelControllerTileEntity.java +++ b/src/main/java/com/simibubi/create/modules/contraptions/components/crusher/CrushingWheelControllerTileEntity.java @@ -56,12 +56,12 @@ public class CrushingWheelControllerTileEntity extends SyncedTileEntity implemen else processingEntity = search.get(0); } - + if (!isOccupied()) return; if (crushingspeed == 0) return; - + float speed = crushingspeed / 2.5f; if (!hasEntity()) { @@ -141,7 +141,7 @@ public class CrushingWheelControllerTileEntity extends SyncedTileEntity implemen } private void applyRecipe() { - Optional recipe = world.getRecipeManager().getRecipe(AllRecipes.Types.CRUSHING, inventory, + Optional recipe = world.getRecipeManager().getRecipe(AllRecipes.CRUSHING.getType(), inventory, world); if (recipe.isPresent()) { @@ -200,7 +200,7 @@ public class CrushingWheelControllerTileEntity extends SyncedTileEntity implemen private void insertItem(ItemEntity entity) { inventory.clear(); inventory.setInventorySlotContents(0, entity.getItem()); - Optional recipe = world.getRecipeManager().getRecipe(AllRecipes.Types.CRUSHING, inventory, + Optional recipe = world.getRecipeManager().getRecipe(AllRecipes.CRUSHING.getType(), inventory, world); inventory.remainingTime = recipe.isPresent() ? recipe.get().getProcessingDuration() : 100; @@ -219,7 +219,7 @@ public class CrushingWheelControllerTileEntity extends SyncedTileEntity implemen public boolean hasEntity() { return processingEntity != null; } - + public static boolean isFrozen() { return CreateConfig.parameters.freezeCrushing.get(); } diff --git a/src/main/java/com/simibubi/create/modules/contraptions/components/press/MechanicalPressTileEntity.java b/src/main/java/com/simibubi/create/modules/contraptions/components/press/MechanicalPressTileEntity.java index 204cfe759..0e4242788 100644 --- a/src/main/java/com/simibubi/create/modules/contraptions/components/press/MechanicalPressTileEntity.java +++ b/src/main/java/com/simibubi/create/modules/contraptions/components/press/MechanicalPressTileEntity.java @@ -257,7 +257,7 @@ public class MechanicalPressTileEntity extends BasinOperatingTileEntity { public Optional getRecipe(ItemStack item) { pressingInv.setInventorySlotContents(0, item); - Optional recipe = world.getRecipeManager().getRecipe(AllRecipes.Types.PRESSING, pressingInv, + Optional recipe = world.getRecipeManager().getRecipe(AllRecipes.PRESSING.getType(), pressingInv, world); return recipe; } diff --git a/src/main/java/com/simibubi/create/modules/contraptions/components/saw/SawTileEntity.java b/src/main/java/com/simibubi/create/modules/contraptions/components/saw/SawTileEntity.java index f6da3a6f9..69f90f7dc 100644 --- a/src/main/java/com/simibubi/create/modules/contraptions/components/saw/SawTileEntity.java +++ b/src/main/java/com/simibubi/create/modules/contraptions/components/saw/SawTileEntity.java @@ -292,7 +292,7 @@ public class SawTileEntity extends BlockBreakingKineticTileEntity { private List> getRecipes() { List> startedSearch = RecipeFinder.get(cuttingRecipesKey, world, - RecipeConditions.isOfType(IRecipeType.STONECUTTING, AllRecipes.Types.CUTTING)); + RecipeConditions.isOfType(IRecipeType.STONECUTTING, AllRecipes.CUTTING.getType())); return startedSearch.stream().filter(RecipeConditions.outputMatchesFilter(filtering)) .filter(RecipeConditions.firstIngredientMatches(inventory.getStackInSlot(0))) .collect(Collectors.toList()); diff --git a/src/main/java/com/simibubi/create/modules/logistics/InWorldProcessing.java b/src/main/java/com/simibubi/create/modules/logistics/InWorldProcessing.java index 044482ea4..35ac58c43 100644 --- a/src/main/java/com/simibubi/create/modules/logistics/InWorldProcessing.java +++ b/src/main/java/com/simibubi/create/modules/logistics/InWorldProcessing.java @@ -105,7 +105,7 @@ public class InWorldProcessing { if (type == Type.SPLASHING) { splashingInv.setInventorySlotContents(0, stack); - Optional recipe = world.getRecipeManager().getRecipe(AllRecipes.Types.SPLASHING, + Optional recipe = world.getRecipeManager().getRecipe(AllRecipes.SPLASHING.getType(), splashingInv, world); return recipe.isPresent(); } @@ -158,7 +158,7 @@ public class InWorldProcessing { private static List process(ItemStack stack, Type type, World world) { if (type == Type.SPLASHING) { splashingInv.setInventorySlotContents(0, stack); - Optional recipe = world.getRecipeManager().getRecipe(AllRecipes.Types.SPLASHING, + Optional recipe = world.getRecipeManager().getRecipe(AllRecipes.SPLASHING.getType(), splashingInv, world); if (recipe.isPresent()) return applyRecipeOn(stack, recipe.get()); diff --git a/src/main/resources/data/create/recipes/mechanical_crafting/circuit.json b/src/main/resources/data/create/recipes/mechanical_crafting/circuit.json new file mode 100644 index 000000000..065f1122c --- /dev/null +++ b/src/main/resources/data/create/recipes/mechanical_crafting/circuit.json @@ -0,0 +1,28 @@ +{ + "type": "create:mechanical_crafting", + "pattern": [ + "GI ", + "NNN" + ], + "key": { + "G": { + "tag": "forge:dyes/green" + }, + "I": { + "item": "create:iron_sheet" + }, + "N": { + "item": "create:copper_nugget" + } + }, + "result": { + "item": "create:circuit", + "count": 1 + }, + "conditions": [ + { + "type": "create:module", + "module": "contraptions" + } + ] +} \ No newline at end of file diff --git a/src/main/resources/data/create/recipes/mechanical_crafting/motor.json b/src/main/resources/data/create/recipes/mechanical_crafting/motor.json new file mode 100644 index 000000000..47a8e2570 --- /dev/null +++ b/src/main/resources/data/create/recipes/mechanical_crafting/motor.json @@ -0,0 +1,37 @@ +{ + "type": "create:mechanical_crafting", + "pattern": [ + "SAAA ", + "AABBI", + "ABBBI", + "ABBII", + " IIIO" + ], + "key": { + "B": { + "item": "create:chorus_chrome_cube" + }, + "A": { + "item": "create:iron_sheet" + }, + "I": { + "item": "minecraft:iron_block" + }, + "S": { + "item": "create:shaft" + }, + "O": { + "item": "minecraft:obsidian" + } + }, + "result": { + "item": "create:motor", + "count": 1 + }, + "conditions": [ + { + "type": "create:module", + "module": "contraptions" + } + ] +} \ No newline at end of file