Jump to content

Recommended Posts

Posted

How would I put and obtain the values within hashmap.  It always returns null?

 

class 1

public static HashMap<ItemStack, Integer> Levels = new HashMap(200)
...//code
ItemStack stack = new ItemStack(Blocks.stone);
Levels.put(stack, 5)

 

class2

ItemStack stack = new ItemStack(Blocks.stone);
int requiredLevel = Levels.get(stack)//Is Null

 

 

Posted

ItemStack

doesn't override the

hashCode

method, so two instances are treated as different keys even if they share the same contents.

 

To use an

ItemStack

as a key, you'll need a wrapper class that overrides

hashCode

to return the hash code of the

ItemStack

's contents;

com.google.common.base.Objects#hashCode

will combine the hash codes of multiple objects for you.

 

Exactly which parts of the

ItemStack

you use in the hash code depends on your requirements: Do you only care about the

Item

, stack size and metadata; or do you include the NBT as well?

NBTTagCompound

overrides

hashCode

to return the hash of its contents, so you don't need to calculate that yourself.

Please don't PM me to ask for help. Asking your question in a public thread preserves it for people who are having the same problem in the future.

Posted

also, if you'll only need to differentiate between item, stack size and metadata, and if you won't need to put or get things into the map too often then you could put/get ItemStack.toString. I think it would work, but it's probably slow.

WIP mods: easyautomation, easyenergy, easyelectronics, easymoney, easytrasportation, easysecurity, easymultiverse, easyfactions, easymagick, easyalchemy, easyseasons

Posted

If you don't want to use a wrapper class you can also use a TCustomHashMap from the trove library (Minecraft comes with it). This lets you specifiy how to calculate equals and hashCode instead of using the normal methods.

 

While I will probably just use wrapper objects, that's pretty neat.

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.