Jump to content

Recommended Posts

Posted

I created a block based on stonecutter and would like to add my own recipe type to it. How do I do that?

WoodcutterRecipe

public class WoodcutterRecipe extends SingleItemRecipe {
    public WoodcutterRecipe(ResourceLocation pId, String pGroup, Ingredient pIngredient, ItemStack pResult) {
        super(ModRecipeType.WOODCUTTER_RECIPE, RecipeSerializer.STONECUTTER, pId, pGroup, pIngredient, pResult);
    }

    @Override
    public boolean matches(Container pContainer, Level pLevel) {
        return this.ingredient.test(pContainer.getItem(0));
    }

    @Override
    public ItemStack getToastSymbol() {
        return new ItemStack(ModBlocks.WOODCUTTER.get());
    }
}

ModRecipeType

public class ModRecipeType {
    public static RecipeType<WoodcutterRecipe> WOODCUTTER_RECIPE = register("woodcutter");

    static <T extends Recipe<?>> RecipeType<T> register(final String pIdentifier) {
        return Registry.register(Registry.RECIPE_TYPE, new ResourceLocation(pIdentifier), new RecipeType<T>() {
            public String toString() {
                return pIdentifier;
            }
        });
    }
}

WoodcutterMenu

public class WoodcutterMenu extends StonecutterMenu {
    private final ContainerLevelAccess access;

    public WoodcutterMenu(int id, Inventory inventory) {
        super(id, inventory);
        this.access = ContainerLevelAccess.NULL;
    }

    public WoodcutterMenu(int id, Inventory inventory, ContainerLevelAccess containerLevelAccess) {
        super(id, inventory, containerLevelAccess);
        this.access = containerLevelAccess;
    }

    @Override
    public boolean stillValid(Player pPlayer) {
        return stillValid(this.access, pPlayer, ModBlocks.WOODCUTTER.get());
    }

    @Override
    public MenuType<?> getType() {
        return ModMenuType.WOOD_CUTTER_MENU.get();
    }
}

 

Posted

Okay, I made a custom serializer but I don't know how to register RecipeType since it can't be done like in vanilla
WoodcutterRecipe

public class WoodcutterRecipe implements Recipe<Container> {
    protected final Ingredient ingredient;
    protected final ItemStack result;
    protected final ResourceLocation id;
    protected final String group;

    public WoodcutterRecipe(Ingredient ingredient, ItemStack result, ResourceLocation id, String group) {
        this.ingredient = ingredient;
        this.result = result;
        this.id = id;
        this.group = group;
    }

    public RecipeType<?> getType() {
        return ModRecipeType.WOODCUTTER_RECIPE;
    }

    public RecipeSerializer<?> getSerializer() {
        return ModRecipeType.WOODCUTTER.get();
    }

    public ResourceLocation getId() {
        return this.id;
    }

    public String getGroup() {
        return this.group;
    }

    public ItemStack getResultItem() {
        return this.result;
    }

    public NonNullList<Ingredient> getIngredients() {
        NonNullList<Ingredient> nonnulllist = NonNullList.create();
        nonnulllist.add(this.ingredient);
        return nonnulllist;
    }

    public boolean canCraftInDimensions(int pWidth, int pHeight) {
        return true;
    }

    @Override
    public boolean matches(Container pContainer, Level pLevel) {
        return false;
    }

    public ItemStack assemble(Container pInv) {
        return this.result.copy();
    }

    public static class Serializer implements RecipeSerializer<WoodcutterRecipe> {

        public WoodcutterRecipe fromJson(ResourceLocation pRecipeId, JsonObject pJson) {
            String s = GsonHelper.getAsString(pJson, "group", "");
            Ingredient ingredient;
            if (GsonHelper.isArrayNode(pJson, "ingredient")) {
                ingredient = Ingredient.fromJson(GsonHelper.getAsJsonArray(pJson, "ingredient"));
            } else {
                ingredient = Ingredient.fromJson(GsonHelper.getAsJsonObject(pJson, "ingredient"));
            }

            String s1 = GsonHelper.getAsString(pJson, "result");
            int i = GsonHelper.getAsInt(pJson, "count");
            ItemStack itemstack = new ItemStack(Registry.ITEM.get(new ResourceLocation(s1)), i);
            return new WoodcutterRecipe(ingredient, itemstack, pRecipeId, s);
        }

        public WoodcutterRecipe fromNetwork(ResourceLocation pRecipeId, FriendlyByteBuf pBuffer) {
            String s = pBuffer.readUtf();
            Ingredient ingredient = Ingredient.fromNetwork(pBuffer);
            ItemStack itemstack = pBuffer.readItem();
            return new WoodcutterRecipe(ingredient, itemstack, pRecipeId, s);
        }

        public void toNetwork(FriendlyByteBuf pBuffer, WoodcutterRecipe pRecipe) {
            pBuffer.writeUtf(pRecipe.group);
            pRecipe.ingredient.toNetwork(pBuffer);
            pBuffer.writeItem(pRecipe.result);
        }

        @Override
        public RecipeSerializer<?> setRegistryName(ResourceLocation name) {
            return ModRecipeType.WOODCUTTER.get();
        }

        @Nullable
        @Override
        public ResourceLocation getRegistryName() {
            return new ResourceLocation(AnyMod.MOD_ID, "woodcutter");
        }

        @Override
        public Class<RecipeSerializer<?>> getRegistryType() {
            return Serializer.castClass(RecipeSerializer.class);
        }

        @SuppressWarnings("unchecked")
        private static <G> Class<G> castClass(Class<?> cls) {
            return (Class<G>)cls;
        }
    }
}

ModRecipeType

public class ModRecipeType {
    public static final DeferredRegister<RecipeSerializer<?>> SERIALIZER =
            DeferredRegister.create(ForgeRegistries.RECIPE_SERIALIZERS, AnyMod.MOD_ID);

    public static RegistryObject<RecipeSerializer<WoodcutterRecipe>> WOODCUTTER = SERIALIZER.register("woodcutter",
            WoodcutterRecipe.Serializer::new);

    public static RecipeType<WoodcutterRecipe> WOODCUTTER_RECIPE = register("woodcutter");

    static <T extends Recipe<?>> RecipeType<T> register(final String pIdentifier) {
        return Registry.register(Registry.RECIPE_TYPE, new ResourceLocation(pIdentifier), new RecipeType<T>() {
            public String toString() {
                return pIdentifier;
            }
        });
    }
}

 

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Announcements



×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use.