Jump to content

[SOLVED] How to replace an item stack?


Yagoki

Recommended Posts

I'm trying to make an item which decays into another item over time. I've managed to get the decaying to work properly, which was a post i put on here a while ago and got no responses to, but now when the stack changes the Item it is replaced with is not of the same type as it should be (i.e. i tried changing it to stone for test purposes but the item it is replaced with will not stack with stone. Anyone know how to fix this?

 

Here's the relevant parts of the code:

	@Override
public void onCreated(ItemStack stack,World world,EntityPlayer player)
{	
	if(stack.itemID == ModResorces.decay.shiftedIndex){
		setWorldTimer(stack, world);
	}
}

@Override
 public void onUpdate(ItemStack stack, World world, Entity entity, int par4, boolean par5)
{
	if(stack.stackTagCompound == null && stack.itemID == ModResorces.decay.shiftedIndex){
		setWorldTimer(stack, world);
		return;
	}else if(stack.itemID != ModResorces.decay.shiftedIndex){
		return;
	}
	if(world.getTotalWorldTime() >= stack.stackTagCompound.getLong("EndTime")){
		stack.itemID = 1;
	}else{
		stack.setItemDamage(201 - (int)(stack.stackTagCompound.getLong("EndTime") - world.getTotalWorldTime()));
	}
}


public void setWorldTimer(ItemStack stack, World world)
{
	if(stack.itemID == ModResorces.decay.shiftedIndex && stack.stackTagCompound == null)
	{
		stack.setTagCompound(new NBTTagCompound());
	}
	stack.stackTagCompound.setLong("EndTime", world.getTotalWorldTime() + 200);
}

Link to comment
Share on other sites

FIXED IT  ;D

 

	 @Override
 public void onUpdate(ItemStack stack, World world, Entity entity, int par4, boolean par5)
{
	if(stack.stackTagCompound == null && stack.itemID == ModResorces.decay.shiftedIndex){
		setWorldTimer(stack, world);
		return;
	}else if(stack.itemID != ModResorces.decay.shiftedIndex){
		return;
	}
	if(world.getTotalWorldTime() >= stack.stackTagCompound.getLong("EndTime"))
	{	
		stack.itemID = ModItems.adamantiumBucket.shiftedIndex;
		for(int i = 0; i < 36; i++){
			if (((EntityPlayer)entity).inventory.mainInventory[i] != null){
				if(((EntityPlayer)entity).inventory.mainInventory[i] == stack){
					((EntityPlayer)entity).inventory.mainInventory[i] = new ItemStack(Block.stone);
				}
			}
		}

	}else{
		stack.setItemDamage(201 - (int)(stack.stackTagCompound.getLong("EndTime") - world.getTotalWorldTime()));
	}
}

 

Not sure if the null check is needed, but it doesn't hurt to put it there, Thanks for putting me onto the track of looking at what i could do with the inventory ;D

Thanks a tonne

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.