Jump to content
  • Home
  • Files
  • Docs
Topics
  • All Content

  • This Topic
  • This Forum

  • Advanced Search
  • Existing user? Sign In  

    Sign In



    • Not recommended on shared computers


    • Forgot your password?

  • Sign Up
  • All Activity
  • Home
  • Mod Developer Central
  • Modder Support
  • [1.12.2] Check for free slot in player inventory
Currently Supported: 1.16.X (Latest) and 1.15.X (LTS)
Sign in to follow this  
Followers 1
xorinzor

[1.12.2] Check for free slot in player inventory

By xorinzor, January 14, 2018 in Modder Support

  • Reply to this topic
  • Start new topic

Recommended Posts

xorinzor    1

xorinzor

xorinzor    1

  • Tree Puncher
  • xorinzor
  • Members
  • 1
  • 15 posts
Posted January 14, 2018 (edited)

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, 2018 by xorinzor
edited code block to Java instead of HTML
  • Quote

Share this post


Link to post
Share on other sites

diesieben07    7696

diesieben07

diesieben07    7696

  • Reality Controller
  • diesieben07
  • Forum Team
  • 7696
  • 56376 posts
Posted January 14, 2018

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?

  • Quote

Share this post


Link to post
Share on other sites

xorinzor    1

xorinzor

xorinzor    1

  • Tree Puncher
  • xorinzor
  • Members
  • 1
  • 15 posts
Posted January 14, 2018
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.

  • Quote

Share this post


Link to post
Share on other sites

diesieben07    7696

diesieben07

diesieben07    7696

  • Reality Controller
  • diesieben07
  • Forum Team
  • 7696
  • 56376 posts
Posted January 14, 2018

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

  • Quote

Share this post


Link to post
Share on other sites

xorinzor    1

xorinzor

xorinzor    1

  • Tree Puncher
  • xorinzor
  • Members
  • 1
  • 15 posts
Posted January 14, 2018
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.
}

 

  • Quote

Share this post


Link to post
Share on other sites

diesieben07    7696

diesieben07

diesieben07    7696

  • Reality Controller
  • diesieben07
  • Forum Team
  • 7696
  • 56376 posts
Posted January 14, 2018

Don't use equals (or ==, they are equivalent here) for ItemStacks. To check if an ItemStack is empty you must use ItemStack::isEmpty.

  • Quote

Share this post


Link to post
Share on other sites

xorinzor    1

xorinzor

xorinzor    1

  • Tree Puncher
  • xorinzor
  • Members
  • 1
  • 15 posts
Posted January 14, 2018
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.

  • Quote

Share this post


Link to post
Share on other sites

Draco18s    2414

Draco18s

Draco18s    2414

  • Reality Controller
  • Draco18s
  • Members
  • 2414
  • 15996 posts
Posted January 14, 2018
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
  • Quote

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.

Share this post


Link to post
Share on other sites

KittenKoder    6

KittenKoder

KittenKoder    6

  • Stone Miner
  • KittenKoder
  • Members
  • 6
  • 77 posts
Posted January 15, 2018

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? 

  • Quote

Share this post


Link to post
Share on other sites

xorinzor    1

xorinzor

xorinzor    1

  • Tree Puncher
  • xorinzor
  • Members
  • 1
  • 15 posts
Posted January 15, 2018
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
  • Quote

Share this post


Link to post
Share on other sites

American2050    11

American2050

American2050    11

  • Dragon Slayer
  • American2050
  • Members
  • 11
  • 541 posts
Posted January 15, 2018

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
  • Quote

Share this post


Link to post
Share on other sites

xorinzor    1

xorinzor

xorinzor    1

  • Tree Puncher
  • xorinzor
  • Members
  • 1
  • 15 posts
Posted January 15, 2018
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.

  • Quote

Share this post


Link to post
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.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  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.

    • Insert image from URL
×
  • Desktop
  • Tablet
  • Phone
Sign in to follow this  
Followers 1
Go To Topic Listing



  • Recently Browsing

    No registered users viewing this page.

  • Posts

    • JayNeedsHelp
      Logger not working

      By JayNeedsHelp · Posted 11 minutes ago

      So I'm currently creating a forge mod and I'm having an issue where the console stops logging after some errors. It seems to be connected to the access transformers that I'm using as before I added at's my console was working fine.   Here is my at file:  public-f net.minecraft.client.Minecraft session public net.minecraft.client.Minecraft timer public net.minecraft.client.gui.GuiScreen buttonList public net.minecraft.util.Timer tickLength public net.minecraft.network.play.client.CPacketPlayer onGround public net.minecraft.network.play.server.SPacketEntityVelocity motionX public net.minecraft.network.play.server.SPacketEntityVelocity motionY public net.minecraft.network.play.server.SPacketEntityVelocity motionZ public net.minecraft.network.play.server.SPacketExplosion motionX public net.minecraft.network.play.server.SPacketExplosion motionY public net.minecraft.network.play.server.SPacketExplosion motionZ public net.minecraft.client.renderer.entity.RenderManager renderPosX public net.minecraft.client.renderer.entity.RenderManager renderPosY public net.minecraft.client.renderer.entity.RenderManager renderPosZ   Any help is greatly appreciated thank you!
    • cadbane86140
      Minecraft: Hunger Games Game #36- Shear FIGHT!

      By cadbane86140 · Posted 1 hour ago

      Hello There! Today we are back on Hunger Games after a little break but we are finally back! In this episode we are on the good ol' map Survival Games 4 and it ACTUALLY went well for once. Also we have so many great battles on rooftops, small rooms and just out in the open! We also use shears to fight at one point and that was pretty crazy! There are so many hilarious moments in this episode that I know you guys are gonna love! I hope you all enjoy this video and if you did don't forget to like and sub for more Hunger Games in the future!  
    • Sad Whale
      Game crashes whenever I try to increase the RAM

      By Sad Whale · Posted 1 hour ago

      latest.log
    • diesieben07
      Game crashes whenever I try to increase the RAM

      By diesieben07 · Posted 2 hours ago

      In the logs folder of your game directory.
    • Unusualty
      GUI'S and player editing

      By Unusualty · Posted 2 hours ago

      So I'm trying to make a mod that is inspired by Origin's because this mod isn't for forge I was wondering if anyone can help me do something like this where you get a GUI when you join the world where you can select a race that has abilities and down sides to them, I also want to have classes but first I want these race's done so if anyone can help this would be appreciated.
  • Topics

    • JayNeedsHelp
      0
      Logger not working

      By JayNeedsHelp
      Started 12 minutes ago

    • cadbane86140
      0
      Minecraft: Hunger Games Game #36- Shear FIGHT!

      By cadbane86140
      Started 1 hour ago

    • Sad Whale
      6
      Game crashes whenever I try to increase the RAM

      By Sad Whale
      Started 3 hours ago

    • Unusualty
      0
      GUI'S and player editing

      By Unusualty
      Started 2 hours ago

    • fluiX
      1
      server wont start

      By fluiX
      Started 3 hours ago

  • Who's Online (See full list)

    • _Traficantefav_
    • Lyon
    • JayNeedsHelp
    • PyRoTheLifeLess
    • Beethoven92
  • All Activity
  • Home
  • Mod Developer Central
  • Modder Support
  • [1.12.2] Check for free slot in player inventory
  • Theme

Copyright © 2019 ForgeDevelopment LLC · Ads by Longitude Ads LLC Powered by Invision Community