Tech Reborn

Tech Reborn

30M Downloads

Question about the multiple output slots on some of TR's block entities

ScaredsmodsWeb opened this issue ยท 4 comments

commented

Hello Team Reborn,

I was developing a mod of mine with a custom block entity with 2 output slots. However Mojang seems to have a hardcoded limit of 1 ItemStack so I was wondering how you guys did it cause you have lots of those types of block entities.

I have looked at your code but because this is a fairly big mod, I did'nt really understand your code. I am looking for a simple answer.

So my question is: How do I create (or replicate, or something like that) a block entity, and recipe, with multiple output slots?

ScaredRabbit (scaredsmods version)

commented

Hi,

I dont think think we do anything special to allow 2 output slots, when creating your own machine I would take one step a time. First create a block entity with some slots for inputs and outputs, and then the screen handler.

My guess is you are struggling to see how how to create recipes with more than one output? If so you will need your own recipe type.

commented

It is indeed the second thing you said: Im struggling to create recipe with more than one output. See, I dont know where to start. If I know where to start, then I can get an idea of what I need to do but I need a more detailed explanation than this to be able to do something

commented

GenAI provides the following basic steps. They may vary depending on your mod structure, but should serve as a good starting point.

Creating a custom recipe type can be a bit complex, but it's definitely achievable. In this example, let's create a custom recipe type to craft a mod block entity in the game Minecraft using the Fabric mod development toolkit.

Here's a step-by-step guide:

  1. Firstly, define your custom recipe type. In your mod initializer:
public class MyMod implements ModInitializer {
    public static final Identifier MY_RECIPE = new Identifier("mymod", "my_recipe");

    @Override
    public void onInitialize() {
        //other init stuff ...
    }
}
  1. Create a new class extending Recipe<CraftingInventory>:
public class MyModRecipe implements Recipe<CraftingInventory> {
    //... class implementation. 
}
  1. You need to implement all methods from Recipe<CraftingInventory>.

  2. Define the Serializer for your Recipe in the same class, this is used to read your recipe from json:

public static class Serializer implements RecipeSerializer<MyModRecipe> {

        @Override
        public MyModRecipe read(Identifier id, JsonObject json) {
            //... code for read recipe from json.
        }

        @Override
        public MyModRecipe read(Identifier id, PacketByteBuf buf) {
            //... code for reading from PacketByteBuf.
        }

        @Override
        public void write(PacketByteBuf buf, MyModRecipe recipe) {
            //... code for writing to PacketByteBuf.
        }
    }
  1. Register your Recipe and its Serializer:
public class MyMod implements ModInitializer {
    public static final RecipeType<MyModRecipe> MY_RECIPE = registerRecipe("crafting_station");
    public static final RecipeSerializer<MyModRecipe> MY_RECIPE_SERIALIZER = new MyModRecipe.Serializer();

    private static RecipeType<MyModRecipe> registerRecipe(String id) {
        return Registry.register(Registry.RECIPE_TYPE, new Identifier(MyMod.MOD_ID, id), new RecipeType<MyModRecipe>() {
            public String toString() {
                return id;
            }
        });
    }

    @Override
    public void onInitialize() {
        Registry.register(Registry.RECIPE_SERIALIZER, new Identifier(MyMod.MOD_ID, "crafting_station"), MY_RECIPE_SERIALIZER);
    }
}
  1. Now, you should override getRecipeFor in your tile entity/block entity to use your custom recipe:
@Override
public Optional<MyModRecipe> getRecipeFor(ItemStack stack) {
    return this.world.getRecipeManager().getFirstMatch(MyMod.MY_RECIPE, this, this.world);
}
  1. Finally, you can define your custom recipe for the block in json under the data/<namespace>/recipes folder:
{
  "type": "mymod:my_recipe",
  "ingredient": {
    "item": "minecraft:gold_nugget"
  },
  "result": "mymod:my_mod_item"
}

Remember to replace mymod and my_recipe with your actual mod id and recipe id.

Please note, there are additional complexities and considerations depending on how complex your recipe logic needs to be. It's important to test these in-game and adjust the above code to suit your needs. This is a basic example and might not cover specific use cases, but it should help you get started with defining custom recipes using Fabric.

TR declares recipe types here: https://github.com/TechReborn/TechReborn/blob/1.20/src/main/java/techreborn/init/ModRecipes.java

You can follow remaining stuff, like serialization and recipe class implementation from there.

commented

Not a bug.