FarmersDelight icon indicating copy to clipboard operation
FarmersDelight copied to clipboard

Subclassable Recipe Builders

Open Lordfirespeed opened this issue 2 years ago • 2 comments

When an addon mod for Farmer's Delight (Farmer's Respite, Nether's Delight, etc) want to register a CuttingBoardRecipe or CookingPotRecipe, they cannot subclass the existing recipe builders as their constructors are private. That means developers are limited to

  • ~~making an access transformer that modifies the recipe builders' constructor~~ (access transformers do not work on other mods)
  • copy + pasting the implementation of the recipe builder (present solution 😭)

This PR enables subclassing the recipe builders, for example

package umpaz.nethersdelight.data.builder;

import net.minecraft.MethodsReturnNonnullByDefault;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.world.level.ItemLike;

import javax.annotation.Nullable;
import javax.annotation.ParametersAreNonnullByDefault;

@ParametersAreNonnullByDefault
@MethodsReturnNonnullByDefault
class NethersDelightCookingPotRecipeBuilder extends CookingPotRecipeBuilder {
    protected NethersDelightCookingPotRecipeBuilder(ItemLike resultIn, int count, int cookingTime, float experience, @Nullable ItemLike container) {
        super(resultIn, count, cookingTime, experience, container);
    }

    public static NethersDelightCookingPotRecipeBuilder cookingPotRecipe(ItemLike mainResult, int count, int cookingTime, float experience) {
        return new NethersDelightCookingPotRecipeBuilder(mainResult, count, cookingTime, experience, null);
    }

    public static NethersDelightCookingPotRecipeBuilder cookingPotRecipe(ItemLike mainResult, int count, int cookingTime, float experience, ItemLike container) {
        return new NethersDelightCookingPotRecipeBuilder(mainResult, count, cookingTime, experience, container);
    }

    @Override
    protected String getDefaultRecipeName(ResourceLocation resultItemKey) {
        return "nethersdelight" + ":cooking/" + resultItemKey.getPath();
    }
}

This is achieved by making the constructors protected and providing an overridable method getDefaultRecipeName on each RecipeBuilder.

Lordfirespeed avatar Aug 29 '23 22:08 Lordfirespeed

Hello! Sorry for the long delay, @Lordfirespeed. ^^

I suppose I could un-private the constructors for the builders. When they were written, I didn't consider that add-ons would be using them internally.

That said, what is the use case here? In your example, it seems you're simply using the builder without changes to its format; you can use different overrides for the .save() method to define specific namespaces and path names. Is the goal to simplify this process, or to accomodate a custom cooking recipe, with different parameters than vanilla FD?

vectorwing avatar Aug 07 '24 02:08 vectorwing

Cor blimey, it's been a while on this - I remember I was working on nether's delight

I think the goal was to simplify the process - I'm not 100% sure about that

Lordfirespeed avatar Aug 07 '24 08:08 Lordfirespeed