Jump to content

Recommended Posts

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.

Posted

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.

Posted

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.

Posted

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

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