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.

[1.9] you can't replace item from the inside code methods

Featured Replies

Posted

good nights 

this time this post is not an accident

actually i think i found a bug

 

the trouble is i can't replace  the selected itemStack for another using the methods from the item class like

onItemUseFinish()  or onPlayerStoppedUsing() as i do in 1.8

 

this is the post with original question i made in the forums

http://www.minecraftforge.net/forum/index.php/topic,37521.msg197729.html#msg197729

 

lets say i have this ItemStackIn  is in the slot 1 from the hotbar if in the item code i do something like

 

@Override
public void onPlayerStoppedUsing(ItemStack stack, World worldIn, EntityLivingBase entityLiving, int timeLeft) {
if (!worldIn.isRemote){
if (entityLiving instanceof EntityPlayer) {
EntityPlayer playerIn = (EntityPlayer) entityLiving;
playerIn.inventory.setInventorySlotContents( 1, replaceItemStack);
}
}

 

the change is not happening and i remain with ItemStackIn in the slot 1

 

but if i set the item in any other slot

playerIn.inventory.setInventorySlotContents( 2, replaceItemStack);

i end having ItemStackIn in the slot 1  and replaceItemStack in slot 2

 

the only slot i couldn't replace is the one containing the item on use 

 

could you fix this

Thank for reading

 

 

 

 

[/code]

Are you in creative mode?

I do Forge for free, however the servers to run it arn't free, so anything is appreciated.
Consider supporting the team on Patreon

  • Author

Nup

 

but i test the creative surviver and mode 2 still fail in all

 

im using

	
// ############################################################################################3
@Override
public ItemStack onItemUseFinish(ItemStack stack, World worldIn, EntityLivingBase entityLiving) {

	if (!worldIn.isRemote) {

		if (entityLiving instanceof EntityPlayer) {

			ItemStack replaceItemStack = new ItemStack(MMMateriales.materialesSuspensionderedstoneActivada, 1, 0);
			Item comparationItem = MMMateriales.materialesSuspensionderedstone;

			EntityPlayer playerIn = (EntityPlayer) entityLiving;
			EnumHand ManoActiva = playerIn.getActiveHand();

			ItemStack leftHand = playerIn.getHeldItem(EnumHand.OFF_HAND);
			ItemStack rightHand = playerIn.getHeldItem(EnumHand.MAIN_HAND);

			int hand = playerIn.inventory.currentItem;
			ItemStack itemstack = null;

			itemstack = playerIn.inventory.getStackInSlot(hand);
			if (ManoActiva != null && ManoActiva == EnumHand.MAIN_HAND && itemstack.getItem() == comparationItem) {

				chat.chatm(playerIn, "replacing the Item in MAIN_HAND");
				playerIn.inventory.setInventorySlotContents(hand, replaceItemStack);
				//playerIn.inventory.markDirty();
			}

			itemstack = playerIn.inventory.getStackInSlot(40);
			if (ManoActiva != null && ManoActiva == EnumHand.OFF_HAND && itemstack.getItem() == comparationItem) {
				chat.chatm(playerIn, "replacing the Item in OFF_HAND");


				playerIn.inventory.setInventorySlotContents(40, replaceItemStack);
				//playerIn.inventory.decrStackSize(40, 1);
				//playerIn.inventory.markDirty();
				//playerIn.inventory.inventoryChanged = true;
			}

			int playerInvZise = playerIn.inventory.getSizeInventory();

			for (int i = 0; i < playerInvZise; ++i) {
				itemstack = playerIn.inventory.getStackInSlot(i);

				if (itemstack != null) {
					System.out.println("SLOT[" + i + "]=" + itemstack.getUnlocalizedName());
				}
			}

		}
	}

	return stack;
}

 

the funny part is that i only cant'not change the selected item but i can change hatever else i want so if i made

 

//MainHand
			itemstack = playerIn.inventory.getStackInSlot(hand);
			if (ManoActiva != null && ManoActiva == EnumHand.MAIN_HAND && itemstack.getItem() == comparationItem) {

				chat.chatm(playerIn, "replacing the Item in MAIN_HAND");
				playerIn.inventory.setInventorySlotContents(40, replaceItemStack);
				//playerIn.inventory.markDirty();
			}

 

being the selected item in the right hand i can change the item in the off hand

and also i can consume the item in the main hand even if its the selected using

playerIn.inventory.decrStackSize(hand, 1);

 

but is not allow to change the item in the main hand for any other if this is the selected  itemStack

 

 

 

 

 

 

 

 

 

 

  • Author

well is more like it changes to activates but inmediately comes back to normal

 

alsi if make the change here works 


@Override
public boolean onEntitySwing(EntityLivingBase entityLiving, ItemStack itemStackIn) {

	World worldIn = entityLiving.worldObj;

	if (entityLiving instanceof EntityPlayer) {
		EntityPlayer playerIn = (EntityPlayer) entityLiving;

	ItemStack replaceItemStack = new ItemStack(MMMateriales.materialesSuspensionderedstoneActivada, 1, 0);

	int handi = playerIn.inventory.currentItem;
	chat.chatm(playerIn, "replacing the Item in MAIN_HAND");
	playerIn.inventory.setInventorySlotContents(handi, replaceItemStack);

	}

	return true;
}

 

but if i made it here onItemRightClick like this


@Override
public ActionResult<ItemStack> onItemRightClick(ItemStack itemStackIn, World worldIn, EntityPlayer playerIn,
		EnumHand hand) {
	boolean sePudo = true;


	ItemStack replaceItemStack = new ItemStack(MMMateriales.materialesSuspensionderedstoneActivada, 1, 0);

	int handi = playerIn.inventory.currentItem;
	chat.chatm(playerIn, "replacing the Item in MAIN_HAND");
	playerIn.inventory.setInventorySlotContents(handi, replaceItemStack);



	return sePudo ? new ActionResult(EnumActionResult.PASS, itemStackIn) : new ActionResult(EnumActionResult.FAIL, itemStackIn);
}

is curious coze its visually changing for a brief moment but comes back to normal

 

 

 

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.