Jump to content

[1.12] Stuck adding IRecipe


tomtomtom09

Recommended Posts

Tying to add a property which is stored inside a tile entity to a block when the block gets crafted. I can add the property with JSON files but don't want to make hundreds of files.

 

Hoping working on IRecipe for few days and hoping someone could point me in the right direction :)

 

_factory file

Spoiler

{
  "recipes":
  {
    "tent":"com.tomtomtom09.campcraft.crafting.RecipeTents$Factory"
  }
}

 

 
 
 

RecipeTents.java

Spoiler

package com.tomtomtom09.campcraft.crafting;


import com.google.gson.JsonObject;
import com.tomtomtom09.campcraft.block.BlockTent;
import com.tomtomtom09.campcraft.init.ModBlocks;
import com.tomtomtom09.campcraft.init.ModItems;
import net.minecraft.block.Block;
import net.minecraft.init.Items;
import net.minecraft.inventory.InventoryCrafting;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.item.crafting.IRecipe;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.nbt.NBTTagList;
import net.minecraft.potion.PotionUtils;
import net.minecraft.util.NonNullList;
import net.minecraft.world.World;
import net.minecraftforge.common.crafting.IRecipeFactory;
import net.minecraftforge.common.crafting.JsonContext;

public class RecipeTents extends net.minecraftforge.registries.IForgeRegistryEntry.Impl<IRecipe> implements IRecipe
{
    /**
     * Used to check if a recipe matches current crafting inventory
     */
    public boolean matches(InventoryCrafting inv, World worldIn)
    {
        if(inv.getWidth() == 3 && inv.getHeight() == 3)
        {
            for (int i = 0; i < inv.getWidth(); ++i)
            {
                for (int j = 0; j < inv.getHeight(); ++j)
                {
                    ItemStack itemStack = inv.getStackInRowAndColumn(i,j);

                    if(itemStack.isEmpty())
                    {
                        return false;
                    }

                    Item item = itemStack.getItem();

                    if(i == 1 && j == 1)
                    {
                        if(item != ModItems.TENTEQUIPMENT)
                        {
                            return false;
                        }
                    }
                }
            }
            return true;
        }
        else
        {
            return false;
        }
    }

    /**
     * Returns an Item that is the result of this recipe
     */
    public ItemStack getCraftingResult(InventoryCrafting inv)
    {
        ItemStack itemstack = inv.getStackInRowAndColumn(1, 1);
        int k = 0; //tent equipment

        if(itemstack.getItem() != ModItems.TENTEQUIPMENT)
        {
            return ItemStack.EMPTY;
        }
        else
        {
            ItemStack itemstack1 = new ItemStack(ModBlocks.TENT_BLOCK);

            return itemstack1;
        }
    }

    public ItemStack getRecipeOutput()
    {
        return ItemStack.EMPTY;
    }

    public NonNullList<ItemStack> getRemainingItems(InventoryCrafting inv)
    {
        return NonNullList.<ItemStack>withSize(inv.getSizeInventory(), ItemStack.EMPTY);
    }

    public boolean isDynamic()
    {
        return true;
    }

    public boolean canFit(int width, int height)
    {
        return width >= 2 && height >= 2;
    }

    public static class Factory implements IRecipeFactory
    {
        @Override
        public IRecipe parse(final JsonContext context, final JsonObject json)
        {
            return new RecipeTents();
        }
    }
}

 

 
 
 
Edited by tomtomtom09
Updated code

Developer of CampCraft

Link to comment
Share on other sites

21 hours ago, tomtomtom09 said:

Tying to add a property which is stored inside a tile entity to a block when the block gets crafted. I can add the property with JSON files but don't want to make hundreds of files.

 

Hoping working on IRecipe for few days and hoping someone could point me in the right direction :)

 

_factory file.

  Reveal hidden contents


{
  "recipes":
  {
    "tent":"com.tomtomtom09.campcraft.crafting.RecipeTents$Factory"
  }
}

 

 
 
  Reveal hidden contents


package com.tomtomtom09.campcraft.crafting;


import com.google.gson.JsonObject;
import com.tomtomtom09.campcraft.block.BlockTent;
import com.tomtomtom09.campcraft.init.ModBlocks;
import com.tomtomtom09.campcraft.init.ModItems;
import net.minecraft.block.Block;
import net.minecraft.init.Items;
import net.minecraft.inventory.InventoryCrafting;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.item.crafting.IRecipe;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.nbt.NBTTagList;
import net.minecraft.potion.PotionUtils;
import net.minecraft.util.NonNullList;
import net.minecraft.world.World;
import net.minecraftforge.common.crafting.IRecipeFactory;
import net.minecraftforge.common.crafting.JsonContext;

public class RecipeTents extends net.minecraftforge.registries.IForgeRegistryEntry.Impl<IRecipe> implements IRecipe
{
    /**
     * Used to check if a recipe matches current crafting inventory
     */
    public boolean matches(InventoryCrafting inv, World worldIn)
    {
        if(inv.getWidth() == 3 && inv.getHeight() == 3)
        {
            for (int i = 0; i < inv.getWidth(); ++i)
            {
                for (int j = 0; j < inv.getHeight(); ++j)
                {
                    ItemStack itemStack = inv.getStackInRowAndColumn(i,j);

                    if(itemStack.isEmpty())
                    {
                        return false;
                    }

                    Item item = itemStack.getItem();

                    if(i == 1 && j == 1)
                    {
                        if(item != ModItems.TENTEQUIPMENT)
                        {
                            return false;
                        }
                    }
                }
            }
            return true;
        }
        else
        {
            return false;
        }
    }

    /**
     * Returns an Item that is the result of this recipe
     */
    public ItemStack getCraftingResult(InventoryCrafting inv)
    {
        ItemStack itemstack = inv.getStackInRowAndColumn(1, 1);
        int k = 0; //tent equipment

        if(itemstack.getItem() != ModItems.TENTEQUIPMENT)
        {
            return ItemStack.EMPTY;
        }
        else
        {
            ItemStack itemstack1 = new ItemStack(ModBlocks.TENT_BLOCK);

            int size = ModItems.TENTEQUIPMENT.getMetadata(k);
            if (size != -1)
            {
                k = size;
            }

            NBTTagCompound nbtTagCompound = new NBTTagCompound();
            nbtTagCompound.setInteger("tentSize", k);
            return itemstack1;
        }
    }

    public ItemStack getRecipeOutput()
    {
        return ItemStack.EMPTY;
    }

    public NonNullList<ItemStack> getRemainingItems(InventoryCrafting inv)
    {
        return NonNullList.<ItemStack>withSize(inv.getSizeInventory(), ItemStack.EMPTY);
    }

    public boolean isDynamic()
    {
        return true;
    }

    public boolean canFit(int width, int height)
    {
        return width >= 2 && height >= 2;
    }

    public static class Factory implements IRecipeFactory
    {
        @Override
        public IRecipe parse(final JsonContext context, final JsonObject json)
        {
            return new RecipeTents();
        }
    }
}

 

 

 

If you can make them in JSON, do. This is so that they can be disabled by data packs in 1.13, and allow customisation of your mod. If you’ve got a lot of recipes, write some code to do it for you! Your a programmer, you have god powers!

About Me

Spoiler

My Discord - Cadiboo#8887

My WebsiteCadiboo.github.io

My ModsCadiboo.github.io/projects

My TutorialsCadiboo.github.io/tutorials

Versions below 1.14.4 are no longer supported on this forum. Use the latest version to receive support.

When asking support remember to include all relevant log files (logs are found in .minecraft/logs/), code if applicable and screenshots if possible.

Only download mods from trusted sites like CurseForge (minecraft.curseforge.com). A list of bad sites can be found here, with more information available at stopmodreposts.org

Edit your own signature at www.minecraftforge.net/forum/settings/signature/ (Make sure to check its compatibility with the Dark Theme)

Link to comment
Share on other sites

16 minutes ago, Cadiboo said:

If you can make them in JSON, do. This is so that they can be disabled by data packs in 1.13, and allow customisation of your mod. If you’ve got a lot of recipes, write some code to do it for you! Your a programmer, you have god powers!

I've got a lot of customisation in one block would be too many json files to even think about.

With this IRecipe file would make it easier to create the recipes, I just can't figure out how to get it to craft my block.

Do I need to register the RecipeTents file anywhere else?

Developer of CampCraft

Link to comment
Share on other sites

3 hours ago, tomtomtom09 said:

Do I need to register the RecipeTents file anywhere else?

The recommended way of adding recipes is with a JSON, why would it be too many? Create them with code if that is easier.

VANILLA MINECRAFT CLASSES ARE THE BEST RESOURCES WHEN MODDING

I will be posting 1.15.2 modding tutorials on this channel. If you want to be notified of it do the normal YouTube stuff like subscribing, ect.

Forge and vanilla BlockState generator.

Link to comment
Share on other sites

13 hours ago, tomtomtom09 said:

I've got a lot of customisation in one block would be too many json files to even think about.

With this IRecipe file would make it easier to create the recipes, I just can't figure out how to get it to craft my block.

Do I need to register the RecipeTents file anywhere else?

If it’s something like dyeing/repairing armor or making fireworks, use code. If it’s _anything_ else use json

About Me

Spoiler

My Discord - Cadiboo#8887

My WebsiteCadiboo.github.io

My ModsCadiboo.github.io/projects

My TutorialsCadiboo.github.io/tutorials

Versions below 1.14.4 are no longer supported on this forum. Use the latest version to receive support.

When asking support remember to include all relevant log files (logs are found in .minecraft/logs/), code if applicable and screenshots if possible.

Only download mods from trusted sites like CurseForge (minecraft.curseforge.com). A list of bad sites can be found here, with more information available at stopmodreposts.org

Edit your own signature at www.minecraftforge.net/forum/settings/signature/ (Make sure to check its compatibility with the Dark Theme)

Link to comment
Share on other sites

Just now, diesieben07 said:

No. Use JSON, period. Custom factories exist for a reason.

How would you implement repairing armor with json?

About Me

Spoiler

My Discord - Cadiboo#8887

My WebsiteCadiboo.github.io

My ModsCadiboo.github.io/projects

My TutorialsCadiboo.github.io/tutorials

Versions below 1.14.4 are no longer supported on this forum. Use the latest version to receive support.

When asking support remember to include all relevant log files (logs are found in .minecraft/logs/), code if applicable and screenshots if possible.

Only download mods from trusted sites like CurseForge (minecraft.curseforge.com). A list of bad sites can be found here, with more information available at stopmodreposts.org

Edit your own signature at www.minecraftforge.net/forum/settings/signature/ (Make sure to check its compatibility with the Dark Theme)

Link to comment
Share on other sites

19 hours ago, Animefan8888 said:

The recommended way of adding recipes is with a JSON, why would it be too many? Create them with code if that is easier.

Got a lot of stored types inside the block which creates a different tent depending on the crafting input.

Just an example of the planning.

Spoiler

JUNGLE TENT
	JUNGLE BED
		FENCE 1
			TORCH 1
				GLASS BLOCK 1
					SIZE SMALL.json file
						"BlockEntityTag"
						{
							"TentType":Jungle
							"BedType":Jungle
							"FenceType":1
							"TorchType":1
							"TentSize":Small
							"WindowBlock":White
						}
					SIZE MEDIUM
					SIZE LARGE
				GLASS BLOCK 2
				GLASS BLOCK 3 - 34
			TORCH 2
			TORCH 3
			TORCH 4
			TORCH 5
		FENCE 2
		FENCE 3
		FENCE 4
		FENCE 5
		FENCE 6
		FENCE 7
DESERT TENT
TUNDRA TENT
NETHER TENT
PINK TENT
YELLOW TENT
BROWN TENT
BLACK TENT
BLUE TENT
GREEN TENT

 

 
9 hours ago, diesieben07 said:

You implement IRecipeFactory to produce an IRecipe that does the repairing based on input from JSON. You could then specify the type of armor the recipe works with in JSON.

Of course you need actual code to handle the logic of the recipe, but the registration happens through JSON.

 

thank you, hopefully I can work it out :)

Developer of CampCraft

Link to comment
Share on other sites

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.