Jump to content
Search In
  • More options...
Find results that contain...
Find results in...

Checking for item placed in GUI slot?


Recommended Posts

Hi there,

I'm trying to make a machine which has 3 slots - 2 inputs and an output. One of the inputs is for anything, and the other is for cobblestone only. What I want is that when you place cobblestone into the bottom slot, it increases a variable and in turn increases a meter on the screen. How can I check for cobblestone in the slot?


I have tried

public void updateEntity()

		System.out.println("I am awesome!");

with no luck. When I attempt to load a world I get a null pointer exception.


P.S I know this is a bit off topic, but will help other issues I'm having with the GUI. How can I only let stone be placed in the slot, and how can I only allow items to be taken from the output slot and not placed?

I have no idea what I'm doing.

Link to comment
Share on other sites

If there is no stack in slot 2, you're going to get that NPE. Try testing for a 'null' before using .equals()

Link to comment
Share on other sites

Firstly, you should always check if the stack in the slot is null before doing anything with it, or you will get a null pointer exception.


Secondly, an ItemStack will never return true when comparing it to a Block with equals; instead, you need to get the Item and compare that to the ItemBlock:

ItemStack stack = getStackInSlot(2); // or whatever slot

if (stack != null && stack.getItem() == Item.getItemFromBlock(Blocks.cobblestone)) {
// now you know for sure you've got a stack with cobblestone


Finally, you should look at the Slot class for methods you can use to limit what goes into a slot; you will need to make your own class that extends Slot and add that instead of the generic Slot to your container.

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.

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.


  • Create New...

Important Information

By using this site, you agree to our Privacy Policy.