Jump to content
View in the app

A better way to browse. Learn more.

Forge Forums

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

Featured Replies

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

 

 

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.

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

  • Author

Thank you for the replies.  I will probably create a wrapper class that holds the item, stack size(Maybe), and metadata and override hashcode.

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...

Important Information

By using this site, you agree to our Terms of Use.

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.