Posted January 14, 20187 yr 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 January 14, 20187 yr by xorinzor edited code block to Java instead of HTML
January 14, 20187 yr Author 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.
January 14, 20187 yr Author 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 haha oh boy, I'd rather not 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. }
January 14, 20187 yr Author 10 minutes ago, diesieben07 said: Don't use equals (or ==, they are equivalent here) for ItemStacks. To check if an ItemStack is empty you must use ItemStack::isEmpty. It worked in testing, but I edited it thanks.
January 14, 20187 yr 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. 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.
January 15, 20187 yr 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?
January 15, 20187 yr Author 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.
January 15, 20187 yr 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.
January 15, 20187 yr Author 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.
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.