Jump to content

[1.12.2] Check for free slot in player inventory


xorinzor

Recommended Posts

So, maybe I'm just trying to cut corners here, but none of these IF-statements seem to be returning true, despite the inventory definitely having empty slots.
 

if(player.inventory.hasItemStack(ItemStack.EMPTY)) {
	//Do something
}

if(player.inventory.hasItemStack(new ItemStack(Items.AIR))) {
	//Do something
}

if(player.inventory.hasItemStack(new ItemStack(Blocks.AIR))) {
	//Do something
}


Am I really going to have to add the code to iterate over all slots in the inventory? or is there a much simpler way that I'm just not seeing here? (something along the lines of, less = more. Especially when it comes to readability).

EDIT: Probably worth noting, this is server-side code.

Edited by xorinzor
edited code block to Java instead of HTML
Link to comment
Share on other sites

1 minute ago, diesieben07 said:

You will need to write a for loop and check for empty stacks. But most likely you don't want to actually find an empty stack, you want to check if there is room for some other stack, right?

 

Yea, all it needs to do is just check for a empty spot. Was hoping a iterator wouldn't be necessary, but I guess not haha.

Link to comment
Share on other sites

5 minutes ago, diesieben07 said:

You don't need an iterator. There are several nice ways to do it using streams if you want to :D

haha oh boy, I'd rather not :P

This seems to work, so I'll just go with it.

 

public void someMethod() {

	checkFreeSlot: {
		for(int i=0; i < 36; i++) {
			if(player.inventory.getStackInSlot(i).equals(ItemStack.EMPTY)) {
				break checkFreeSlot;
			}
		}
		
		//Failed check	
		return;
	}

	//Check passed, do something.
}

 

Link to comment
Share on other sites

1 hour ago, xorinzor said:

It worked in testing, but I edited it ;) thanks.

A stack of size 0 (but still with some other item) won't == ItemStack.EMPTY but it will still resolve .isEmpty() as true.

  • Like 3

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

21 minutes ago, KittenKoder said:

One question that is related to this, should I make copies of ItemStack.EMPTY when using it to set an empty stack in methods that could potentially change it? 

No that's not required, you can use ItemStack.EMPTY safely.

  • Like 1
Link to comment
Share on other sites

There is this method you can use also. Just check what int it returns and if it's on the range of inventory slots you want (Because I don't know if it would also return armor slots as empty slots)

player.inventory.getFirstEmptyStack()

 

Depending on what number you get, you will know that the player has a free slot.

  • Like 1
Link to comment
Share on other sites

1 minute ago, American2050 said:

There is this method you can use also. Just check what int it returns and if it's on the range of inventory slots you want (Because I don't know if it would also return armor slots as empty slots)

player.inventory.getFirstEmptyStack()

 

Depending on what number you get, you will know that the player has a free slot.

Awesome. That's exactly the kind of method I was looking for.

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.