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.

Featured Replies

Posted

So my last topic I posted in this section on PlayerInteractEvent was a bit messed up. I've rethought what's wrong and fixed it. But now there is a second problem. Here is the code:

@ForgeSubscribe
public void playerInteractEvent(PlayerInteractEvent event)
{
	EntityPlayer player = event.entityPlayer;
	if(player.getCurrentEquippedItem() != null && player.getCurrentEquippedItem().itemID == Base.tankEmpty.itemID && event.action == Action.RIGHT_CLICK_AIR)
	{	
		if (!player.capabilities.isCreativeMode)
		{
			--player.getCurrentEquippedItem().stackSize;
		}

		player.inventory.addItemStackToInventory(new ItemStack(Base.tankAir));

		if (!player.inventory.addItemStackToInventory(new ItemStack(Base.tankAir)))
		{
			player.dropPlayerItem(new ItemStack(Base.tankAir.itemID, 1, 0));
		}
	}
}

The code looks fine to me and it works. However, when I play the game and right click mid air with my "tankEmpty", it returns 2 "tankAir" instead of just 1. Does anyone know why this is and how I can fix this?

 

Thanks in advance.

if (player.worldObj.isRemote)

return;

 

if(player.getCurrentEquippedItem() != null && player.getCurrentEquippedItem().itemID == Base.tankEmpty.itemID && event.action == Action.RIGHT_CLICK_AIR)

{

If you guys dont get it.. then well ya.. try harder...

  • Author

if (player.worldObj.isRemote)

return;

 

if(player.getCurrentEquippedItem() != null && player.getCurrentEquippedItem().itemID == Base.tankEmpty.itemID && event.action == Action.RIGHT_CLICK_AIR)

{

All that's done is cause the stack to decrease in size but now NO items are returned at all.

 

player.inventory.addItemStackToInventory(new ItemStack(Base.tankAir));
if (!player.inventory.addItemStackToInventory(new ItemStack(Base.tankAir)))
...

You are giving it twice here. I'd recommend removing the first line.

  • Author

player.inventory.addItemStackToInventory(new ItemStack(Base.tankAir));
if (!player.inventory.addItemStackToInventory(new ItemStack(Base.tankAir)))
...

You are giving it twice here. I'd recommend removing the first line.

 

Hum are you sure? The addItemStackToInventory in the brackets of the if statement just checks if its been added. I wouldn't think it would cause it to return twice. I'll try out what you said and get back to you asap.

 

So I tried your recommendation. It didn't work. Now, with space in the inventory, nothing returns but the stack size of empty tanks still decrease and when my inventory is full, the air tank that should be returned is dropped (as it should do). Unfortunately though it did not work. Any other ideas anyone?

  • Author

Ok so this is my new code:

if(player.getCurrentEquippedItem() != null && player.getCurrentEquippedItem().itemID == Base.tankEmpty.itemID)
	{	
		if(event.action == Action.RIGHT_CLICK_AIR)
		{
			player.inventory.addItemStackToInventory(new ItemStack(Base.tankAir));

			if (!player.capabilities.isCreativeMode && player.inventory.addItemStackToInventory(new ItemStack(Base.tankAir)) == true)
			{
				--player.getCurrentEquippedItem().stackSize;
			}
		}
	}

I've changed it so that when there's no more space, the empty tank stack size does not decrease. This works fine.

However the issue of 2 items returning instead of 1 is back.

I can see how the bit

&& player.inventory.addItemStackToInventory(new ItemStack(Base.tankAir)) == true)

causes this.

 

Question: is there a better way to check if the itemstack has been added (one that does not cause 2 of the item to return)?

Hi

 

I might be missing something obvious, but is this what you want?

 

if(player.getCurrentEquippedItem() != null && player.getCurrentEquippedItem().itemID == Base.tankEmpty.itemID)
	{	
		if(event.action == Action.RIGHT_CLICK_AIR)
		{
                                                boolean itemWasAdded = player.inventory.addItemStackToInventory(new ItemStack(Base.tankAir));

			if (!player.capabilities.isCreativeMode && itemWasAdded)
			{
				--player.getCurrentEquippedItem().stackSize;
			}
		}
	}

 

-TGG

  • Author

Hi

 

I might be missing something obvious, but is this what you want?

 

if(player.getCurrentEquippedItem() != null && player.getCurrentEquippedItem().itemID == Base.tankEmpty.itemID)
	{	
		if(event.action == Action.RIGHT_CLICK_AIR)
		{
                                                boolean itemWasAdded = player.inventory.addItemStackToInventory(new ItemStack(Base.tankAir));

			if (!player.capabilities.isCreativeMode && itemWasAdded)
			{
				--player.getCurrentEquippedItem().stackSize;
			}
		}
	}

 

-TGG

Wow, that worked. Thank you so much. You sir are going on my "awesome list" :D

Hi

 

Wow, that worked. Thank you so much. You sir are going on my "awesome list"

 

ha ha, I've got a looong way to go before I'm worthy of that title.  Plenty of folks on this list are far more awesome than me!

 

Glad I could help.

 

-TGG

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.