Jump to content

addItemStackToInventory


Enderlook

Recommended Posts

I'm designing a new food which is a soup, so after eating the soup, the player must get in its inventory an empty bowl.

The thing is that I've found two ways of adding the item in the inventory and I want to know which of them is "better":

@Override
protected void onFoodEaten(ItemStack stack, World worldIn, EntityPlayer player) {		
	super.onFoodEaten(stack, worldIn, player);
	if(!worldIn.isRemote) {
		// Both commands seems the same
		player.inventory.addItemStackToInventory(new ItemStack(Items.BOWL));
		player.addItemStackToInventory(new ItemStack(Items.BOWL));
	}
}

(I've used "!worldIn.isRemote" because I've read that is a good practice... is that true?)

 

Which method is better?:

  • player.inventory.addItemStackToInventory(ItemStack);
  • player.addItemStackToInventory(ItemStack);

 

Also, I've found "player.inventory.mainInventoryaddItemStackToInventory(ItemStack);" but that crash the game when the food is eaten.

 

This method is inside the class of my custom item ("ItemEggSoup") which inherits from generic class ("TItemFood"), which that inherits from vanilla class ("ItemFood").

 

Thanks.

 

PD: This is my first time using this forum, so tell me if I've broken any rule, not sure how questions are done here.

Link to comment
Share on other sites

57 minutes ago, Enderlook said:

(I've used "!worldIn.isRemote" because I've read that is a good practice... is that true?)

That's not why you use !worldIn.isRemote. You use !worldIn.isRemote when you want code that runs on both the client and the server to only run on the server.

Apparently I'm a complete and utter jerk and come to this forum just like to make fun of people, be confrontational, and make your personal life miserable.  If you think this is the case, JUST REPORT ME.  Otherwise you're just going to get reported when you reply to my posts and point it out, because odds are, I was trying to be nice.

 

Exception: If you do not understand Java, I WILL NOT HELP YOU and your thread will get locked.

 

DO NOT PM ME WITH PROBLEMS. No help will be given.

Link to comment
Share on other sites

1 hour ago, Enderlook said:

player.addItemStackToInventory(ItemStack)

Is just a wrapper around

1 hour ago, Enderlook said:

player.inventory.addItemStackToInventory(ItemStack)

So either works, one is just less writing.

VANILLA MINECRAFT CLASSES ARE THE BEST RESOURCES WHEN MODDING

I will be posting 1.15.2 modding tutorials on this channel. If you want to be notified of it do the normal YouTube stuff like subscribing, ect.

Forge and vanilla BlockState generator.

Link to comment
Share on other sites

13 hours ago, Animefan8888 said:

Is just a wrapper around

So either works, one is just less writing.

Ok, a bit stupid my question, but, which of both is more common to use?

 

Also, I'll say this in order to maintain a record in the topic for future read: a person has sent me a private message (not sure why he didn't post in the topic, but thanks anyway) saying I could also use:

ItemHandlerHelper.giveItemToPlayer(player, new ItemStack(Items.BOWL)); 

Which is better because if the inventory is full the item will be drop on the floor. So,  is that line better than the others?

Link to comment
Share on other sites

10 hours ago, Enderlook said:

Ok, a bit stupid my question, but, which of both is more common to use?

I would use player.addItemStackToInventory

10 hours ago, Enderlook said:

Which is better because if the inventory is full the item will be drop on the floor. So,  is that line better than the others?

It depends on what you mean by better ItemHandlerHelper.giveItemToPlayer will drop the item as an entity which is useful in most cases, but it also wraps the PlayerInventory in an IItemHandler and handles the input that way.

VANILLA MINECRAFT CLASSES ARE THE BEST RESOURCES WHEN MODDING

I will be posting 1.15.2 modding tutorials on this channel. If you want to be notified of it do the normal YouTube stuff like subscribing, ect.

Forge and vanilla BlockState generator.

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.