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

I really don't think HashMap wants to work for me.

 

public static Map<Object, Integer[]> atomicMass = new HashMap<Object, Integer[]>();

public static void registerItem(Item item, int protons, int neutrons, int electrons)
{
	//registerItem(item, new AtomicMass(protons, neutrons, electrons));
	Integer[] i = new Integer[5];
	i[1] = protons;
	i[2] = neutrons;
	i[3] = electrons;
	i[4] = protons + neutrons;

	atomicMass.put(item, i);
}

This is the code I am currently using. I create a HashMap/Map to store the item Object and the proton/neutron/electron/mass Integer[] values. I use the method also there to register items into this HashMap at PreInit. I also use a similar method for Blocks and ItemStacks, the only difference being the Item changes.

 

@SubscribeEvent
public void tooltip(ItemTooltipEvent event)
{
	Integer[] mval = AtomicRegister.getStuffies(event.itemStack);

	if(mval != null)
	{
		event.toolTip.add("Atomic Mass: " + mval[4]);
		event.toolTip.add("Protons: " + mval[1]);
		event.toolTip.add("Neutrons: " + mval[2]);
		event.toolTip.add("Electrons: " + mval[3]);
	}
}

I then use an event to try to post these values to a tooltip. However, the NullPointerException occurs on the first tooltip adding line "event.toolTip.add("Atomic Mass: " + mval[4]);" I added the !=null check to stop this, but now the tooltip doesn't show up even on the Items/Blocks I had registered.

Hi

 

The root cause of your problem is that you defined your HashMap with Object, so the compiler didn't warn you that you are mixing up Items with ItemStacks

Map<Object, Integer[]>;

should be better be

Map<Item, Integer []>

 

Do you know about the difference between Items and ItemStacks?

 

If not, this might help

http://greyminecraftcoder.blogspot.com.au/2013/12/items.html

 

-TGG

  • Author

I made the changes to only use ItemStack but it still doesn't display the int[] values in game.

Hi

 

There are a couple of concepts I think you're missing that might help.

The first is that you're storing information about Items, not about ItemStacks. 

So for example there is only one Item for "Carbon", and it has six protons.

There might be many ItemStacks of LumpOfCarbon lying around, and they are all Carbon, but in different amounts, one lump might be 6 moles, the other lump 12 moles.  They are not the same.

 

So if you want a HashMap to tell you how many protons are in a Carbon, you need a HashMap for Carbon, not for LumpOfCarbon.  You could in principle set up a HashMap for LumpsOfCarbon which ignores the number of moles in the lump (using hashCode and equals, like DieSieben said) but that really doesn't make much sense in this case.

 

A second thing is that, if only your new items have atomic masses, why do you need the HashMap registry at all?  Why not store the information in the (eg) ItemElement class directly and override Item.addInformation(..) instead?

 

-TGG

 

 

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.