How to create a crafting recipe?

Hi,
i want to create a crafting recipe, but I have no Idea how to read any Java Docs. Could someone give me an example recipe (shaped or shapeless) so that I can compare it with the docs? Or maybe you do have a better idea.

Back when i used Bukkit. The way to create a crafting recipe was to simply hyjack the event were a player would place a item on the crafting bench and then compare the items in the slot. If it was the shape you wanted then set the output item.

The event is ChangeInventoryEvent and then simply check if the inventory is a crafting bench.

However it seems that you can build your own using Sponge without the event. Look at the builders for this class

Thanks. Is this correct and where I have to place it?

    Ingredient air = Ingredient.of(ItemTypes.AIR);
    Ingredient string = Ingredient.of(ItemTypes.STRING);
    Ingredient paper = Ingredient.of(ItemTypes.PAPER);
    
    ShapedCraftingRecipe.builder()
        .aisle("AAS", "APA", "PAA")
        .where('A', air)
        .where('S', string)
        .where('P', paper)
        .result(ItemStack.of(ItemTypes.NAME_TAG, 1))
        .build("name_tag", this);

The correct ingredient to use for ‘empty slot’ is Ingredient.NONE. However you don’t even need to explicitly use that because it defaults to space, so you can set your aisles to "  S", " P ", "P  " and not even bother with empty slots.

Could you elaborate more? What specifically do you have trouble with?

We must have been using different Bukkits.
https://jd.bukkit.org/?org/bukkit/inventory/ShapedRecipe.html

1 Like

It was back in 1.5 times that i did custom recipes. I do remeber checking for a recipe class of any kind but they didnt have one at the time

In fact 1.0 came out in November 2011 and the recipe API was added in April 2011. Recipes have been stable for longer than Minecraft has.

Okay, but after which event I have to place it?

You define recipes and register them in the GamePreInitializationEvent.

Make sure to assign your builder result to a recipe object, that you can register with the command below, or modify the command to put your builder in-line. I’d suggest object /register object for clarity purposes, esp if you want to register other custom recipes too.

ie:
Sponge.getRegistry().getCraftingRecipeRegistry().register(ShapedCraftingRecipe.builder().blah().blah().blah().build("name_tag",this) );
or ie:
Sponge.getRegistry().getCraftingRecipeRegistry().register(myrep_nametag);

Make sure to add the code to register your recipe:

Thank you all! :grinning: