Jump to content

[1.8] Craft a certain item with any sword/pickaxe/etc


SuperSaltyGamer

Recommended Posts

How to do that?

 

I have this code that i have written, but i don't know what to do now.

 

 

package com.supersalty.randomstuff;

 

import java.util.ArrayList;

import java.util.Iterator;

import java.util.List;

 

import com.google.common.collect.Lists;

import com.supersalty.randomstuff.init.ModItems;

 

import net.minecraft.inventory.InventoryCrafting;

import net.minecraft.item.ItemStack;

import net.minecraft.item.crafting.IRecipe;

import net.minecraft.world.World;

 

public class RecipeStrangify implements IRecipe

{

 

private ItemStack recipeOutput;

 

public List recipeItems;

 

    public RecipeStrangify()

    {

      recipeItems.add(ModItems.strangifier);

    }

 

@Override

public boolean matches(InventoryCrafting inv, World w)

{

 

ArrayList arraylist = Lists.newArrayList(this.recipeItems);

 

        for (int i = 0; i < inv.getHeight(); ++i)

        {

            for (int j = 0; j < inv.getWidth(); ++j)

            {

                ItemStack itemstack = inv.getStackInRowAndColumn(j, i);

 

                if (itemstack != null)

                {

                    boolean flag = false;

                    Iterator iterator = arraylist.iterator();

 

                    while (iterator.hasNext())

                    {

                        ItemStack itemstack1 = (ItemStack)iterator.next();

 

                        if (itemstack.getItem() == itemstack1.getItem() && (itemstack1.getMetadata() == 32767 || itemstack.getMetadata() == itemstack1.getMetadata()))

                        {

                            flag = true;

                            arraylist.remove(itemstack1);

                            break;

                        }

                    }

 

                    if (!flag)

                    {

                        return false;

                    }

                }

            }

        }

 

        return arraylist.isEmpty();

 

}

 

@Override

public ItemStack getCraftingResult(InventoryCrafting inv)

{

 

for (int i = 0; i < 8; i++)

{

 

if (inv.getStackInSlot(i).getUnlocalizedName().contains("sword"))

{

 

ItemStack stck = inv.getStackInSlot(i);

stck.getTagCompound().setInteger("strange", 0);

stck.setStackDisplayName("strange");

recipeOutput = stck;

return stck;

 

}

 

}

 

return null;

 

}

 

@Override

public int getRecipeSize()

{

 

return this.recipeItems.size();

 

}

 

@Override

public ItemStack getRecipeOutput()

{

 

return this.recipeOutput;

 

}

 

@Override

public ItemStack[] getRemainingItems(InventoryCrafting inv)

{

 

ItemStack[] aitemstack = new ItemStack[inv.getSizeInventory()];

 

        for (int i = 0; i < aitemstack.length; ++i)

        {

            ItemStack itemstack = inv.getStackInSlot(i);

            aitemstack = net.minecraftforge.common.ForgeHooks.getContainerItem(itemstack);

        }

 

        return aitemstack;

 

}

 

}

 

 

Link to comment
Share on other sites

Also for some reason this code returns a nullPointerException on game launch.

 

Can you post the stacktrace so we can see what line is causing the NullPointerException?

 

I'm going to assume however that this line is going to cause problems:

 

for (int i = 0; i < 8; i++) 
{
    if (inv.getStackInSlot(i).getUnlocalizedName().contains("sword"))
    {
        ...
    }
}

inv.getStackInSlot(i) may return null there, and then you're calling null.getUnlocalizedName(). This may be causing your exception. you should add another null check there.

Link to comment
Share on other sites

Also for some reason this code returns a nullPointerException on game launch.

 

Can you post the stacktrace so we can see what line is causing the NullPointerException?

 

I'm going to assume however that this line is going to cause problems:

 

for (int i = 0; i < 8; i++) 
{
    if (inv.getStackInSlot(i).getUnlocalizedName().contains("sword"))
    {
        ...
    }
}

inv.getStackInSlot(i) may return null there, and then you're calling null.getUnlocalizedName(). This may be causing your exception. you should add another null check there.

 

Lemme just add that this is NOT how you check for items.

 

for (ItemStack stack : stacks)
{
    if (stack != null)
    {
        Item item = stack.getItem()
        if (item instanceof ItemSword)
        {
        }
//OR
        if (item == Items.apple)
        {
        }
    }
}

1.7.10 is no longer supported by forge, you are on your own.

Link to comment
Share on other sites

Another thing to add is don't expect every modder to use the standards, even though you should extend ItemSword there are definitely modders out there that do not. You should use what Ernio posted above, but note that if you want your mod to be completely compatible with certain mods, you may need to add specific support for the mods that don't use standard practices.

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.