Jump to content

Recommended Posts

Posted

I'm trying to make it so that my GUI slot can only accept certain items. I have created a new class, extended Slot and overwritten isItemValid() to try and do this, however I'm sorta stuck on how to actually set up a filter. My code (below) crashes the game on the line inside the first for loop with a nullPointerException. Any help would be appriciated. Thanks.

 

Code:

package roboguy99.foodTech.client.gui;

import net.minecraft.inventory.IInventory;
import net.minecraft.inventory.Slot;
import net.minecraft.item.ItemStack;

public class FilteredSlot extends Slot
{
private ItemStack[] allowedItems;
private boolean[] filterResponses;

public FilteredSlot(IInventory inventory, int id, int x, int y, ItemStack[] allowedItems)
{
	super(inventory, id, x, y);
	this.allowedItems = allowedItems;
}

@Override
public boolean isItemValid(ItemStack itemstack)
{
	this.filterResponses = new boolean[this.allowedItems.length];

	if(this.allowedItems != null)
	{
		for(int i = 0; i < this.allowedItems.length; i++)
		{
			this.filterResponses[i] = this.allowedItems[i] == itemstack ? true : false;
		}
	}

	if(this.filterResponses != null)
	{
		for(int i = 0; i < this.filterResponses.length; i++)
		{
			if (this.filterResponses[i] == true) return true;
		}
	}

	return false;
}
}

I have no idea what I'm doing.

Posted

That is not how you compare ItemStacks.  ItemStack is a complex data class and you need to check for equality in a different way.  The == operator tells you if the two variables reference the same object in memory.

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.

Posted

That is not how you compare ItemStacks.  ItemStack is a complex data class and you need to check for equality in a different way.  The == operator tells you if the two variables reference the same object in memory.

 

Would I use the getItem() function or use .equals() or something else please?

I have no idea what I'm doing.

Posted

I know that the smelting recipes uses getItem and getItemDamage (remember: metadata is important!).  You would have to look at equals so see what it does.  There's also a version of equation that checks NBT data.

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.

Posted

Hmm ok. I suppose it would make sense to have a look at the code for the furnace container and see how it does that then. I'll have a look and come back here if I need any more help.

I have no idea what I'm doing.

Posted

One thing to note with getItemDamage() is tools or other damageable items, in those cases I'd say you'd have to check getHasSubtypes() aswell to see if there are other types of blocks/items on the same id. (One alternative is to call isDamageable() on the item, they are almost the same)

--Remember to "Thank you" posts that actually helped you, and if a person seems nice, why not give them an applaud while you're at it--

Posted

Here's the code I borrowed from the smelting recipe registry when making my own machine.

 

private static boolean areSame(ItemStack input, ItemStack against) {
	if(input == null || against == null) return false;
        return against.getItem() == input.getItem() && (against.getItemDamage() == 32767 || against.getItemDamage() == input.getItemDamage());
    }

 

"against" is the stored itemstack that the slot item is compared against.

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.

Posted

You might want to replace the hardcoded wildcard value with

OreDicitionary.WILDCARD_VALUE

for future compatibility.

 

Good point.  Again, it was just what I'd yoinked from the recipe manager.  My mod, while I will likely release it, is primarily intended to scratch my own itches.

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.

Posted

Also, don't store these in your Slot class (you'd be duplicating your efforts, if the TE also stores the same info).  Make a static "RecipeManager" class that has a "getResult" method.  Your slot class just needs to then go "hey, does this input have a non-null result?" for it's boolean check.  Then your machine can use the same function to get its actual result.

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.

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.