Jump to content
View in the app

A better way to browse. Learn more.

Forge Forums

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

[SOLVED] How to replace an item stack?

Featured Replies

Posted

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);
}

Probably the player don't get the changes,add this after changing itemstack.

((EntityPlayer)entity).inventoryContainer.detectAndSendChanges();

  • Author

sounds reasonable but there doesn't seem to be a method called "detectAndSendChanges()", or anything similar

  • Author

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

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...

Important Information

By using this site, you agree to our Terms of Use.

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.