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 using a HashMap to write things to a json file. It works, but the problem is nothing is organized. I know it's a small thing, but it can have a huge impact on the person attempting to edit it. How would I organize my HashMap alphabetically?

 

Here is my writer (called in my postInit):

package com.happykiller.weightlimit.main.init.config;

import java.io.FileWriter;
import java.io.IOException;
import java.io.Writer;
import java.util.HashMap;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.happykiller.weightlimit.api.IWeightedItem;
import com.happykiller.weightlimit.main.ModMain;

import net.minecraft.item.Item;
import net.minecraftforge.fml.common.registry.FMLControlledNamespacedRegistry;
import net.minecraftforge.fml.common.registry.GameData;

public class WeightLimitJson {

private String name;
private float weightValue;


public static void createJSON() throws IOException {
	Gson json = new GsonBuilder().setPrettyPrinting().create();

	Writer jsonFileWriter = new FileWriter(ModMain.configDirectory + "/WeightLimit.json");

	FMLControlledNamespacedRegistry<Item> itemRegistry = GameData.getItemRegistry();

	HashMap<String, Float> items = new HashMap<String, Float>();

	for(Item item : itemRegistry.typeSafeIterable()) {
		String name = itemRegistry.getNameForObject(item).toString();
		float weight;

		if(item instanceof IWeightedItem) {
			weight = ((IWeightedItem)item).getItemWeight();
		}else {
			weight = 0.0F;
		}

		items.put(name, weight);

		//System.out.println(json.toJson(name).toString());
	}

	json.toJson(items, jsonFileWriter);	
	jsonFileWriter.close();
}
}

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 want a tree map if you want your map sorted by keys. Just note the String comparator is case sensitive.

I think its my java of the variables.

  • Author

I was looking online and I found that I'd need to do this:

Map<String, Person> people = new HashMap<String, Person>();

    Person jim = new Person("Jim", 25);
    Person scott = new Person("Scott", 28);
    Person anna = new Person("Anna", 23);

    people.put(jim.getName(), jim);
    people.put(scott.getName(), scott);
    people.put(anna.getName(), anna);

    // not yet sorted
    List<Person> peopleByAge = new ArrayList<Person>(people.values());

    Collections.sort(peopleByAge, new Comparator<Person>() {

        public int compare(Person o1, Person o2) {
            return o1.getAge() - o2.getAge();
        }
    });

 

I need to make a Map, add my string (mod id) to a List, and sort them with a Comparator?

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.

Why do you ask what you alredy know? Just use the damn Comparator. :)

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

  • Author

I found this, but I like to check with other people that have superior knowledge on the subject to see if there is anything more efficient to do. :P Thank you!

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.

  • Author

How I do it now, is with a LinkedHashmap, that I fill after I sort my ArrayList of item registry name's. :P I watched a few videos explaining your way of doing it, and most said it is not needed unless you are organizing frequently.

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.

  • Author

Well, I needed a mutable map that keeps the Objects in the order in which they were inserted. So, it all works now. :P I'll keep what you said in mind though, never realized how useful Maps are until now.

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.