Jump to content

Item damage in contructor


Edska

Recommended Posts

Hello,

 

I am trying to write my first mod. Everything goes fine, but I have one problem. How can I set item damage on item when it is found or created? Now I have this code:

 

public BrokenItem(int par1) {
	super(par1); 
	setMaxStackSize(1);
	setMaxDamage(1000);	
}

 

I would like to create broken items. For example with 500 durability when they are found.

Link to comment
Share on other sites

The item classes are, like the block classes, only instanciated once, meaning if you set a variable in the item class, it will be changed for all loaded "items" in the world and even across worlds.

What you want to achieve is saved into an ItemStack. It's instanciated for each "item", making it unique. There you can store the damage value and even NBT data. I suggest you look at it, for your specific problem you should use itemStackInstance.setItemDamage(damageValue)

Don't ask for support per PM! They'll get ignored! | If a post helped you, click the "Thank You" button at the top right corner of said post! |

mah twitter

This thread makes me sad because people just post copy-paste-ready code when it's obvious that the OP has little to no programming experience. This is not how learning works.

Link to comment
Share on other sites

But how can I call that ItemStack in my constructor or apply my created ItemStack to my item? I tried this in my constructor:

 

ItemStack itemstack = new ItemStack(par1, 1, 500);

 

and when I check this:

 

getItemDamageFromStack(itemstack)

 

I get that it is 500 damaged, but nothing changes on my items.

Link to comment
Share on other sites

When i understood it right you want it when you craft that item/tool that it is half broken?

 

When you mean that you could tell that the Itemdamage is at the recipe is like that you want:

 

Like this:

 

ModLoader.addRecipe(new ItemStack(yourItem, 1, damage), new Object[] {"X", 'X', Block.dirt});

 

Important for everyone who want to tell modloader sucks/isbad use gameregisty:

 

ModLoader is a PathProxy To gameRegisty and Gameregisty goes to the crafting manager so it does not matter if it is gameregistry or Modloader.

 

I personaly made my own PathProxy and does even not use Both of them xD

Link to comment
Share on other sites

When i understood it right you want it when you craft that item/tool that it is half broken?

 

When you mean that you could tell that the Itemdamage is at the recipe is like that you want:

 

Like this:

 

ModLoader.addRecipe(new ItemStack(yourItem, 1, damage), new Object[] {"X", 'X', Block.dirt});

 

Important for everyone who want to tell modloader sucks/isbad use gameregisty:

 

ModLoader is a PathProxy To gameRegisty and Gameregisty goes to the crafting manager so it does not matter if it is gameregistry or Modloader.

 

I personaly made my own PathProxy and does even not use Both of them xD

 

You know that this "PathProxy" is client-sided? Meaning it will likely not work on a dedicated server. Also what if the Forge devs drop the ModLoader compatibility? Or if ModLoader changes something about this method and Forge adapts those changes after a couple releases? You are better off with using the Forge methods, trust me.

Don't ask for support per PM! They'll get ignored! | If a post helped you, click the "Thank You" button at the top right corner of said post! |

mah twitter

This thread makes me sad because people just post copy-paste-ready code when it's obvious that the OP has little to no programming experience. This is not how learning works.

Link to comment
Share on other sites

My Pathproxy is on both sides! Trust me! When mine does not work then it will not work with every pathproxy (gameregistry, modloader)

 

Here my PathProxy:

 

package speiger.src.api.common.functions;

import java.util.Arrays;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;

import cpw.mods.fml.common.registry.GameRegistry;
import net.minecraft.block.Block;
import net.minecraft.client.settings.GameSettings;
import net.minecraft.item.Item;
import net.minecraft.item.ItemBlock;
import net.minecraft.item.ItemStack;
import net.minecraft.item.crafting.CraftingManager;
import net.minecraft.item.crafting.FurnaceRecipes;
import net.minecraft.item.crafting.IRecipe;
import net.minecraft.item.crafting.ShapedRecipes;
import net.minecraft.src.ModLoader;
import net.minecraft.tileentity.TileEntity;
import net.minecraftforge.common.ForgeDirection;

public class PathProxy 
{

    public static void registerTile(Class<? extends TileEntity> tileEntityClass, String id)
    {
        TileEntity.addMapping(tileEntityClass, id);
    }

    public static void addRecipe(ItemStack output, Object... params)
    {
        CraftingManager.getInstance().func_92051_a(output, params);
    }

    public static void addSRecipe(ItemStack output, Object... params)
    {
        CraftingManager.getInstance().addShapelessRecipe(output, params);
    }

    public static void addRecipe(IRecipe recipe)
    {
        CraftingManager.getInstance().getRecipeList().add(recipe);
    }

    public static void addSmelting(int input, ItemStack output, float xp)
    {
        FurnaceRecipes.smelting().addSmelting(input, output, xp);
    }


    public static void registItem(Item id, String name)
    {
    	GameRegistry.registerItem(id, name);
    }
    
    public static void registBlock(Block block, Class<? extends ItemBlock> itemclass, String name)
    {
    	GameRegistry.registerBlock(block, itemclass, name);
    }
    
    
    public static void addSmelting(int itemID, int metadata, ItemStack itemstack, float experience)
    {
    	FurnaceRecipes.smelting().addSmelting(itemID, metadata, itemstack, experience);
    }

public static int getFuel(ItemStack par1) 
{
	// TODO Auto-generated method stub
	return 0;
}

public static void removeRecipe(ItemStack resultItem) {
	List<IRecipe> recipes = CraftingManager.getInstance().getRecipeList();
	for (int i = 0; i < recipes.size(); i++)
	{
		IRecipe tmpRecipe = recipes.get(i);
		if (tmpRecipe instanceof ShapedRecipes) {
			ShapedRecipes recipe = (ShapedRecipes)tmpRecipe;
			ItemStack recipeResult = recipe.getRecipeOutput();

			if (ItemStack.areItemStacksEqual(resultItem, recipeResult)) {
				recipes.remove(i--);
			}
		}
	}
}

    public static int setNextFacing(int i)
    {
    	if(i == ForgeDirection.NORTH.ordinal())return ForgeDirection.EAST.ordinal();
    	else if(i == ForgeDirection.EAST.ordinal())return ForgeDirection.SOUTH.ordinal();
    	else if(i == ForgeDirection.SOUTH.ordinal())return ForgeDirection.WEST.ordinal();
    	else if(i == ForgeDirection.WEST.ordinal())return ForgeDirection.NORTH.ordinal();
    	else return 0;
    }
    
    public static void addBlockRecipe(ItemStack par1, ItemStack par2)
    {
    	CraftingManager.getInstance().func_92051_a(par1, new Object[]{"XXX", "XXX", "XXX", 'X', par2});
    }
}

Link to comment
Share on other sites

My Pathproxy is on both sides! Trust me! When mine does not work then it will not work with every pathproxy (gameregistry, modloader)

 

Here my PathProxy:

 

package speiger.src.api.common.functions;

import java.util.Arrays;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;

import cpw.mods.fml.common.registry.GameRegistry;
import net.minecraft.block.Block;
import net.minecraft.client.settings.GameSettings;
import net.minecraft.item.Item;
import net.minecraft.item.ItemBlock;
import net.minecraft.item.ItemStack;
import net.minecraft.item.crafting.CraftingManager;
import net.minecraft.item.crafting.FurnaceRecipes;
import net.minecraft.item.crafting.IRecipe;
import net.minecraft.item.crafting.ShapedRecipes;
import net.minecraft.src.ModLoader;
import net.minecraft.tileentity.TileEntity;
import net.minecraftforge.common.ForgeDirection;

public class PathProxy 
{

    public static void registerTile(Class<? extends TileEntity> tileEntityClass, String id)
    {
        TileEntity.addMapping(tileEntityClass, id);
    }

    public static void addRecipe(ItemStack output, Object... params)
    {
        CraftingManager.getInstance().func_92051_a(output, params);
    }

    public static void addSRecipe(ItemStack output, Object... params)
    {
        CraftingManager.getInstance().addShapelessRecipe(output, params);
    }

    public static void addRecipe(IRecipe recipe)
    {
        CraftingManager.getInstance().getRecipeList().add(recipe);
    }

    public static void addSmelting(int input, ItemStack output, float xp)
    {
        FurnaceRecipes.smelting().addSmelting(input, output, xp);
    }


    public static void registItem(Item id, String name)
    {
    	GameRegistry.registerItem(id, name);
    }
    
    public static void registBlock(Block block, Class<? extends ItemBlock> itemclass, String name)
    {
    	GameRegistry.registerBlock(block, itemclass, name);
    }
    
    
    public static void addSmelting(int itemID, int metadata, ItemStack itemstack, float experience)
    {
    	FurnaceRecipes.smelting().addSmelting(itemID, metadata, itemstack, experience);
    }

public static int getFuel(ItemStack par1) 
{
	// TODO Auto-generated method stub
	return 0;
}

public static void removeRecipe(ItemStack resultItem) {
	List<IRecipe> recipes = CraftingManager.getInstance().getRecipeList();
	for (int i = 0; i < recipes.size(); i++)
	{
		IRecipe tmpRecipe = recipes.get(i);
		if (tmpRecipe instanceof ShapedRecipes) {
			ShapedRecipes recipe = (ShapedRecipes)tmpRecipe;
			ItemStack recipeResult = recipe.getRecipeOutput();

			if (ItemStack.areItemStacksEqual(resultItem, recipeResult)) {
				recipes.remove(i--);
			}
		}
	}
}

    public static int setNextFacing(int i)
    {
    	if(i == ForgeDirection.NORTH.ordinal())return ForgeDirection.EAST.ordinal();
    	else if(i == ForgeDirection.EAST.ordinal())return ForgeDirection.SOUTH.ordinal();
    	else if(i == ForgeDirection.SOUTH.ordinal())return ForgeDirection.WEST.ordinal();
    	else if(i == ForgeDirection.WEST.ordinal())return ForgeDirection.NORTH.ordinal();
    	else return 0;
    }
    
    public static void addBlockRecipe(ItemStack par1, ItemStack par2)
    {
    	CraftingManager.getInstance().func_92051_a(par1, new Object[]{"XXX", "XXX", "XXX", 'X', par2});
    }
}

 

I'm not talking about your PathProxy, lol. I'm talking about the ModLoader class.

Don't ask for support per PM! They'll get ignored! | If a post helped you, click the "Thank You" button at the top right corner of said post! |

mah twitter

This thread makes me sad because people just post copy-paste-ready code when it's obvious that the OP has little to no programming experience. This is not how learning works.

Link to comment
Share on other sites

In the modloader class it looks like this:

 

public static void addRecipe(ItemStack par1, Object... params)

{

    GameRegistry.addRecipe(par1, params);

}

 

nothing else does modloader in forge do its like mine pathproxy but for modloader mods^^

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.