Jump to content

Recommended Posts

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.

Posted

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

Posted

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.

 

Posted

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.

Posted

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?

Posted

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)?

Posted

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

Posted

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

Posted

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

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