Jump to content

Adding a IRecipe Class to GameRegistry


El_Ludolf

Recommended Posts

Good Morning People,

 

maybe someone can help me with a Mod I'm triying to program right now. The mod also includes a grindstone which applies Sharpness every time used.

The class looks like this till now:

 

 

public class katanasharpening implements IRecipe {

    @Override
    public boolean matches(InventoryCrafting inv, World worldIn) {

        Boolean meitouexistend = false;
        Boolean grindstoneexistend = false;

        for(int slot = 0; slot < inv.getSizeInventory(); slot++)
        {
            if(inv.getStackInSlot(slot).getItem().getUnlocalizedName() == "meitoukokuhikari")
            {
                meitouexistend = true;
            }
            if(inv.getStackInSlot(slot).getItem().getUnlocalizedName() == "grindstone")
            {
                grindstoneexistend = true;
            }
        }
        if(grindstoneexistend && meitouexistend)
        {
            return true;
        }
        else
        {
            return false;
        }
    }

    @Override
    public ItemStack getCraftingResult(InventoryCrafting inv) {

        int meitouposition = 0;
        int currentlevel = 0;

        for(int slot = 0; slot < inv.getSizeInventory(); slot++)
        {
                if(inv.getStackInSlot(slot).getItem().getUnlocalizedName() == "meitoukokuhikari")
                {
                    meitouposition = slot;
                }
        }

        NBTTagList tags = inv.getStackInSlot(meitouposition).getEnchantmentTagList();

        if(tags != null)
        {
            for (int i = 0; i < tags.tagCount(); ++i)
            {
                int j = tags.getCompoundTagAt(i).getShort("id");
                int k = tags.getCompoundTagAt(i).getShort("lvl");
                if(j == 16)
                {
                    currentlevel = k;
                }
            }
        }

        inv.removeStackFromSlot(meitouposition);
        ItemStack kokumeitou = new ItemStack(HikariTouMod.meitoukokuhikari);
        kokumeitou.addEnchantment(Enchantments.SHARPNESS, currentlevel +1);

        return kokumeitou;
    }

 

Questions:

 

Is inv.getStackInSlot(slot).getItem().getUnlocalizedName() == "something" a good way to check if the right items inside?

Through debugging it seems 16 is the id for sharpness?

 

How would I now register the recipe?

GameRegistry.addShaplessRecipe(katanasharpening);  ??

 

addShapelessRecipe has a optional ResourceLocation parameter, is that it?

 

Happy for anyone who can give me some hints.

 

Link to comment
Share on other sites

2 minutes ago, El_Ludolf said:

Is inv.getStackInSlot(slot).getItem().getUnlocalizedName() == "something" a good way to check if the right items inside?

 

No, unlocalised names should only be used for display/translation purposes. They're not unique and can change at any time.

 

Instead, compare the Item instances directly (e.g. stack.getItem() == MyModItems.FOO_BAR) or check if the Item is an instance of the appropriate class (e.g. stack.getItem() instanceof ItemFooBar). This also applies to other registry entries like Blocks and Enchantments.

 

 

5 minutes ago, El_Ludolf said:

Through debugging it seems 16 is the id for sharpness?

 

Don't use numeric IDs, they can be different in every save. Use EnchantmentHelper.getEnchantments to get the Enchantments on an ItemStack. The Vanilla Enchantment instances are stored in the Enchantments class.

 

 

12 minutes ago, El_Ludolf said:

How would I now register the recipe?

GameRegistry.addShaplessRecipe(katanasharpening);  ??

 

Recipes are managed by a Forge registry, register them in the appropriate registry event; just like you do for Blocks, Items, etc.

 

 

16 minutes ago, El_Ludolf said:

addShapelessRecipe has a optional ResourceLocation parameter, is that it?

 

The name argument of addShapelessRecipe is the registry name of the recipe and the group argument is the recipe's group name. Recipes with the same group name are displayed in the same button in the recipe book.

 

 

You should move your recipes to the JSON system rather than hardcoding them.

  • Like 1

Please don't PM me to ask for help. Asking your question in a public thread preserves it for people who are having the same problem in the future.

Link to comment
Share on other sites

Just now, El_Ludolf said:

Thanks a lot. I haven't found a way to meddle with the NBT values inside a JSON file. All the other recipes are JSONs.

 

You can use your own recipe classes with the JSON system by creating an IRecipeFactory and specifying it in your _factories.json file.

 

You can see some example IRecipe and IRecipeFactory implementations here and the corresponding _factories.json file here.

Please don't PM me to ask for help. Asking your question in a public thread preserves it for people who are having the same problem in the future.

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.