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

So, I am attempting to iterate through an NBTTagCompound in order to send a Map through a packet. Problem is, I can't seem to iterate correctly though it. Here is my code:

public IMessage onMessage(final PacketServerToClientWeightUpdate message, MessageContext ctx) {
		System.out.println("onMessage Ran!");

		IThreadListener mainThread = Minecraft.getMinecraft();

		final Map<String, Float> clientWeightValues = ModReference.weightMap;

		mainThread.addScheduledTask(new Runnable() {
			public void run() {
				Iterator it = message.mapData.getKeySet().iterator();

				while(it.hasNext()) {
					Map.Entry<String, Float> pair = (Map.Entry<String, Float>)it.next();

					System.out.println("Pair is: " + pair.getKey() + "/" + pair.getValue());

					clientWeightValues.put(pair.getKey(), pair.getValue());
					it.remove();
				}

				System.out.println("FINSHED SERVER PACKET");
			}
		});

		return null;
	}

 

When I attempt to send the packet, I get a "String cannot be cast to Map.Entry". When looking online, it says I need an entrySet to iterate; but I can't get one from NBTTagCompound. Any ideas on how to do this?

I am not a cat. I know my profile picture is sexy and amazing beyond anything you could imagine but my cat like features only persist in my fierce eyes. I might be a cat.

You're iterating through the key set, which is a

Set<String>

. If you want keys and values, iterate through the entry set instead; this is a

Set<Entry<String, Float>>

.

 

There's no need to use an

Iterator

and remove values here, just iterate through the entry set with a for-each/enhanced for loop and leave the message's map intact. It will simply be garbage collected with the message object itself when it's no longer referenced by anything.

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.

GODDAMIT MOON MOON!

 

YELL warning, I will yell at you because you will never learn.

 

NBTTagCompound works LIKE A MAP, but IS NOT A MAP.

 

1. Tell me - what are you expecting to get here?

Iterator it = message.mapData.getKeySet().iterator(); // A KEY SET of String KEYS
while(it.hasNext())
{
Map.Entry<String, Float> pair = (Map.Entry<String, Float>)it.next(); // Suddenly an entry in String KEY set becomes a PAIR? seriously...

 

2. Why do you even need iterator? Why are you using remove? What is the point?

 

3. Working code:

NBTTagCompound map = message.data.getCompoundTag("M");
for (Object o : map.getKeySet())
{
String key = (String) o;
Integer value = map.getInteger(key); // You need to KNOW what values you expect, in this case you want integer weights.
}

 

4. Please, I beg you - use IDE sometimes, if there is no method like "getEntrySet" (and you want to have keys and values), you just think about how to make it - above is like obvious thing to do. :P

 

5. Grumble, grumble... <angry>

1.7.10 is no longer supported by forge, you are on your own.

  • Author

I'm sorry! xD I've never used Iterator before, so I wasn't sure how to do it, and just went off some basic Java sites. :P Anyway, got that working, so no problems in the packet handling. My only problem now is, file overwriting. Every time I load up the server, or client, the original json file that may have been changed gets overwritten. I've tried using BufferedReader to read the line and check if it was null. And I've tried doing File#length; nothing seems to work. I'll be looking through some Java sites for a solution, but if any of you have one, feel free to post it!

I am not a cat. I know my profile picture is sexy and amazing beyond anything you could imagine but my cat like features only persist in my fierce eyes. I might be a cat.

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.