Jump to content

[1.8] How to convert my ArrayList back into my HashMap


HappyKiller1O1

Recommended Posts

Alright, hey, I'm back again with another silly question. After taking my Strings, organizing them using Collection, and printing them out; all I need to do is put them back in my HashMap. I've tried clearing the keySet(), and re-adding it from my list, didn't work. I tried using #addAll(), sadly, didn't work. Could someone point me in the right direction?

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.

Link to comment
Share on other sites

Leave the hashmap alone. Only sort the keys in order to write the values for human reading. After that you shouldn't care.

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.

Link to comment
Share on other sites

@RANK I don't really understand what you mean, I sort them the best way I found:

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();

	Map<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());
	}

	List<String> itemsOrganized = new ArrayList<String>(items.keySet());

	Collections.sort(itemsOrganized, new Comparator<String>() {

		public int compare(String modid1, String modid2) {
			return modid1.compareTo(modid2);
		}
	});

	items.keySet().addAll(itemsOrganized);

	json.toJson(items, jsonFileWriter);

	System.out.println("Wrote Json");

	jsonFileWriter.close();
}

 

@Draco See, in the code posted above, I sort it and such; but it doesn't change my HashMap (But, I need to write the String(modid), and Float(weight) on the same line in the file. That's why I need to update the Map). I get why it doesn't, but I still can't organize my Map now considering the way I am doing it won't change it. :P

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.

Link to comment
Share on other sites

Jesus kid, you dont need to make a thread for every part of your project, especially considering what you're doing is a grand total of like 5 lines of code. Especially considering this has been answered MANY times on stack overflow.

Look into a TreeMap. This is what you want its simple and easy to implement/use.

A simple google of "Java sorted map" would of reviled this to you.

I do Forge for free, however the servers to run it arn't free, so anything is appreciated.
Consider supporting the team on Patreon

Link to comment
Share on other sites

@Draco See, in the code posted above, I sort it and such; but it doesn't change my HashMap (But, I need to write the String(modid), and Float(weight) on the same line in the file. That's why I need to update the Map). I get why it doesn't, but I still can't organize my Map now considering the way I am doing it won't change it. :P

 

No no no no. You're creating a map here. Good. You're spring the keys, good. You then save the story's keys to the map and json encode the map, bad: this fundamentally doesn't make sense.

 

What you should do is loop through the sorted array and retreive the values in order to write to the json file. It doesn't matter of the json is encoding a map with 4000 items in it or 4000 individually sorted key-value pairs one at a time.

 

Alternatively, use a data structure someone else already created that does what you need, as Lex suggests, rather than trying to make one that doesn't work that way work that way. The whole point of a HashTable is that the values are stored in a location that is easy to determine based on the key: "Item152964" is stored at index 15964, irregardless of if the preceding 15963 items exist or not. It is about fast lookup, not arrangement.

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.

Link to comment
Share on other sites

Alright, here is my new class. I have one problem, that I will explain. I hope I did remotely what you were saying I should, and I am sorry if I missed something. NOTE: I did not go with TreeMap yet as, I did not feel right abandoning this, and I sorta want to accomplish it this way so I have an understanding of how all this works.

 

New Class:

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

import java.io.FileWriter;
import java.io.IOException;
import java.io.Writer;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

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;
import net.minecraftforge.fml.common.registry.GameRegistry;

public class WeightLimitJson {

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();

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

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

		items.put(name, 0.0F);

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

	List<String> itemsOrganized = new ArrayList<String>(items.keySet());

	Collections.sort(itemsOrganized, new Comparator<String>() {

		public int compare(String modid1, String modid2) {
			return modid1.compareTo(modid2);
		}
	});

	items.clear();

	System.out.println("HASHMAP	[sHOULD BE CLEARED]: " + items);
	System.out.println("ARRAYLIST: " + itemsOrganized);

	for(String string : itemsOrganized) {
		float weight;

		String[] modidID = string.split(":");
		String modID = modidID[0];
		String id = modidID[1];

		Item item = GameRegistry.findItem(modID, id);

		if(item != null) {
			String name = itemRegistry.getNameForObject(item).toString();

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

			items.put(name, weight);
		}else {
			System.out.println("ERROR: One or more of the items in the ArrayList were NULL!");
		}
	}

	json.toJson(items, jsonFileWriter);

	System.out.println("Wrote Json");

	jsonFileWriter.close();
}
}

 

So, I made it where at the start, I only write every item to the json, with a 0.0F value. Next, I organize it; then I clear my HashMap, and Iterate through my ArrayList for items. Now, I decided to have it print my HashMap after clearing, and my ArrayList. Here is what I got:

 

NOTE: No need to read this, it shows my HashMap is clear, and my ArrayList ordered it correctly.

 

 

[23:30:07] [Client thread/INFO] [sTDOUT]: [com.happykiller.weightlimit.main.init.config.WeightLimitJson:createJSON:57]: HASHMAP	[sHOULD BE CLEARED]: {}
[23:30:07] [Client thread/INFO] [sTDOUT]: [com.happykiller.weightlimit.main.init.config.WeightLimitJson:createJSON:58]: ARRAYLIST: [minecraft:acacia_door, minecraft:acacia_fence, minecraft:acacia_fence_gate, minecraft:acacia_stairs, minecraft:activator_rail, minecraft:anvil, minecraft:apple, minecraft:armor_stand, minecraft:arrow, minecraft:baked_potato, minecraft:banner, minecraft:barrier, minecraft:beacon, minecraft:bed, minecraft:bedrock, minecraft:beef, minecraft:birch_door, minecraft:birch_fence, minecraft:birch_fence_gate, minecraft:birch_stairs, minecraft:blaze_powder, minecraft:blaze_rod, minecraft:boat, minecraft:bone, minecraft:book, minecraft:bookshelf, minecraft:bow, minecraft:bowl, minecraft:bread, minecraft:brewing_stand, minecraft:brick, minecraft:brick_block, minecraft:brick_stairs, minecraft:brown_mushroom, minecraft:brown_mushroom_block, minecraft:bucket, minecraft:cactus, minecraft:cake, minecraft:carpet, minecraft:carrot, minecraft:carrot_on_a_stick, minecraft:cauldron, minecraft:chainmail_boots, minecraft:chainmail_chestplate, minecraft:chainmail_helmet, minecraft:chainmail_leggings, minecraft:chest, minecraft:chest_minecart, minecraft:chicken, minecraft:clay, minecraft:clay_ball, minecraft:clock, minecraft:coal, minecraft:coal_block, minecraft:coal_ore, minecraft:cobblestone, minecraft:cobblestone_wall, minecraft:command_block, minecraft:command_block_minecart, minecraft:comparator, minecraft:compass, minecraft:cooked_beef, minecraft:cooked_chicken, minecraft:cooked_fish, minecraft:cooked_mutton, minecraft:cooked_porkchop, minecraft:cooked_rabbit, minecraft:cookie, minecraft:crafting_table, minecraft:dark_oak_door, minecraft:dark_oak_fence, minecraft:dark_oak_fence_gate, minecraft:dark_oak_stairs, minecraft:daylight_detector, minecraft:deadbush, minecraft:detector_rail, minecraft:diamond, minecraft:diamond_axe, minecraft:diamond_block, minecraft:diamond_boots, minecraft:diamond_chestplate, minecraft:diamond_helmet, minecraft:diamond_hoe, minecraft:diamond_horse_armor, minecraft:diamond_leggings, minecraft:diamond_ore, minecraft:diamond_pickaxe, minecraft:diamond_shovel, minecraft:diamond_sword, minecraft:dirt, minecraft:dispenser, minecraft:double_plant, minecraft:dragon_egg, minecraft:dropper, minecraft:dye, minecraft:egg, minecraft:emerald, minecraft:emerald_block, minecraft:emerald_ore, minecraft:enchanted_book, minecraft:enchanting_table, minecraft:end_portal_frame, minecraft:end_stone, minecraft:ender_chest, minecraft:ender_eye, minecraft:ender_pearl, minecraft:experience_bottle, minecraft:farmland, minecraft:feather, minecraft:fence, minecraft:fence_gate, minecraft:fermented_spider_eye, minecraft:filled_map, minecraft:fire_charge, minecraft:firework_charge, minecraft:fireworks, minecraft:fish, minecraft:fishing_rod, minecraft:flint, minecraft:flint_and_steel, minecraft:flower_pot, minecraft:furnace, minecraft:furnace_minecart, minecraft:ghast_tear, minecraft:glass, minecraft:glass_bottle, minecraft:glass_pane, minecraft:glowstone, minecraft:glowstone_dust, minecraft:gold_block, minecraft:gold_ingot, minecraft:gold_nugget, minecraft:gold_ore, minecraft:golden_apple, minecraft:golden_axe, minecraft:golden_boots, minecraft:golden_carrot, minecraft:golden_chestplate, minecraft:golden_helmet, minecraft:golden_hoe, minecraft:golden_horse_armor, minecraft:golden_leggings, minecraft:golden_pickaxe, minecraft:golden_rail, minecraft:golden_shovel, minecraft:golden_sword, minecraft:grass, minecraft:gravel, minecraft:gunpowder, minecraft:hardened_clay, minecraft:hay_block, minecraft:heavy_weighted_pressure_plate, minecraft:hopper, minecraft:hopper_minecart, minecraft:ice, minecraft:iron_axe, minecraft:iron_bars, minecraft:iron_block, minecraft:iron_boots, minecraft:iron_chestplate, minecraft:iron_door, minecraft:iron_helmet, minecraft:iron_hoe, minecraft:iron_horse_armor, minecraft:iron_ingot, minecraft:iron_leggings, minecraft:iron_ore, minecraft:iron_pickaxe, minecraft:iron_shovel, minecraft:iron_sword, minecraft:iron_trapdoor, minecraft:item_frame, minecraft:jukebox, minecraft:jungle_door, minecraft:jungle_fence, minecraft:jungle_fence_gate, minecraft:jungle_stairs, minecraft:ladder, minecraft:lapis_block, minecraft:lapis_ore, minecraft:lava_bucket, minecraft:lead, minecraft:leather, minecraft:leather_boots, minecraft:leather_chestplate, minecraft:leather_helmet, minecraft:leather_leggings, minecraft:leaves, minecraft:leaves2, minecraft:lever, minecraft:light_weighted_pressure_plate, minecraft:lit_furnace, minecraft:lit_pumpkin, minecraft:log, minecraft:log2, minecraft:magma_cream, minecraft:map, minecraft:melon, minecraft:melon_block, minecraft:melon_seeds, minecraft:milk_bucket, minecraft:minecart, minecraft:mob_spawner, minecraft:monster_egg, minecraft:mossy_cobblestone, minecraft:mushroom_stew, minecraft:mutton, minecraft:mycelium, minecraft:name_tag, minecraft:nether_brick, minecraft:nether_brick_fence, minecraft:nether_brick_stairs, minecraft:nether_star, minecraft:nether_wart, minecraft:netherbrick, minecraft:netherrack, minecraft:noteblock, minecraft:oak_stairs, minecraft:obsidian, minecraft:packed_ice, minecraft:painting, minecraft:paper, minecraft:piston, minecraft:planks, minecraft:poisonous_potato, minecraft:porkchop, minecraft:potato, minecraft:potion, minecraft:prismarine, minecraft:prismarine_crystals, minecraft:prismarine_shard, minecraft:pumpkin, minecraft:pumpkin_pie, minecraft:pumpkin_seeds, minecraft:quartz, minecraft:quartz_block, minecraft:quartz_ore, minecraft:quartz_stairs, minecraft:rabbit, minecraft:rabbit_foot, minecraft:rabbit_hide, minecraft:rabbit_stew, minecraft:rail, minecraft:record_11, minecraft:record_13, minecraft:record_blocks, minecraft:record_cat, minecraft:record_chirp, minecraft:record_far, minecraft:record_mall, minecraft:record_mellohi, minecraft:record_stal, minecraft:record_strad, minecraft:record_wait, minecraft:record_ward, minecraft:red_flower, minecraft:red_mushroom, minecraft:red_mushroom_block, minecraft:red_sandstone, minecraft:red_sandstone_stairs, minecraft:redstone, minecraft:redstone_block, minecraft:redstone_lamp, minecraft:redstone_ore, minecraft:redstone_torch, minecraft:reeds, minecraft:repeater, minecraft:rotten_flesh, minecraft:saddle, minecraft:sand, minecraft:sandstone, minecraft:sandstone_stairs, minecraft:sapling, minecraft:sea_lantern, minecraft:shears, minecraft:sign, minecraft:skull, minecraft:slime, minecraft:slime_ball, minecraft:snow, minecraft:snow_layer, minecraft:snowball, minecraft:soul_sand, minecraft:spawn_egg, minecraft:speckled_melon, minecraft:spider_eye, minecraft:sponge, minecraft:spruce_door, minecraft:spruce_fence, minecraft:spruce_fence_gate, minecraft:spruce_stairs, minecraft:stained_glass, minecraft:stained_glass_pane, minecraft:stained_hardened_clay, minecraft:stick, minecraft:sticky_piston, minecraft:stone, minecraft:stone_axe, minecraft:stone_brick_stairs, minecraft:stone_button, minecraft:stone_hoe, minecraft:stone_pickaxe, minecraft:stone_pressure_plate, minecraft:stone_shovel, minecraft:stone_slab, minecraft:stone_slab2, minecraft:stone_stairs, minecraft:stone_sword, minecraft:stonebrick, minecraft:string, minecraft:sugar, minecraft:tallgrass, minecraft:tnt, minecraft:tnt_minecart, minecraft:torch, minecraft:trapdoor, minecraft:trapped_chest, minecraft:tripwire_hook, minecraft:vine, minecraft:water_bucket, minecraft:waterlily, minecraft:web, minecraft:wheat, minecraft:wheat_seeds, minecraft:wooden_axe, minecraft:wooden_button, minecraft:wooden_door, minecraft:wooden_hoe, minecraft:wooden_pickaxe, minecraft:wooden_pressure_plate, minecraft:wooden_shovel, minecraft:wooden_slab, minecraft:wooden_sword, minecraft:wool, minecraft:writable_book, minecraft:written_book, minecraft:yellow_flower, wl:backpack_large, wl:backpack_medium, wl:backpack_small, wl:upgrade_station]
[23:30:07] [Client thread/INFO] [sTDOUT]: [com.happykiller.weightlimit.main.init.config.WeightLimitJson:createJSON:86]: Wrote Json

 

 

 

So, I decide to fill my HashMap from my ArrayList or organized names, and run through my process of checking for my Interface. Works just fine. But then, I go and check my json, and get this:

 

 

{
  "minecraft:iron_leggings": 0.5,
  "minecraft:name_tag": 0.5,
  "minecraft:redstone": 0.5,
  "minecraft:wooden_button": 0.5,
  "minecraft:stained_hardened_clay": 0.5,
  "minecraft:wheat_seeds": 0.5,
  "minecraft:stone_pressure_plate": 0.5,
  "minecraft:chainmail_helmet": 0.5,
  "minecraft:compass": 0.5,
  "minecraft:golden_horse_armor": 0.5,
  "minecraft:golden_rail": 0.5,
  "minecraft:shears": 0.5,
  "minecraft:experience_bottle": 0.5,
  "minecraft:acacia_fence_gate": 0.5,
  "minecraft:golden_hoe": 0.5,
  "minecraft:rabbit_hide": 0.5,
  "minecraft:comparator": 0.5,
  "minecraft:ender_eye": 0.5,
  "minecraft:stone_shovel": 0.5,
  "minecraft:grass": 0.5,
  "minecraft:oak_stairs": 0.5,
  "minecraft:chainmail_leggings": 0.5,
  "minecraft:saddle": 0.5,
  "minecraft:cake": 0.5,
  "minecraft:monster_egg": 0.5,
  "minecraft:netherbrick": 0.5,
  "minecraft:wooden_door": 0.5,
  "minecraft:birch_fence": 0.5,
  "minecraft:enchanting_table": 0.5,
  "minecraft:apple": 0.5,
  "minecraft:deadbush": 0.5,
  "minecraft:record_cat": 0.5,
  "minecraft:beef": 0.5,
  "minecraft:diamond_pickaxe": 0.5,
  "minecraft:cobblestone_wall": 0.5,
  "minecraft:record_13": 0.5,
  "minecraft:stone": 0.5,
  "minecraft:record_11": 0.5,
  "minecraft:sand": 0.5,
  "minecraft:mushroom_stew": 0.5,
  "minecraft:redstone_ore": 0.5,
  "minecraft:cooked_chicken": 0.5,
  "minecraft:melon_seeds": 0.5,
  "minecraft:nether_brick_stairs": 0.5,
  "minecraft:hay_block": 0.5,
  "minecraft:trapdoor": 0.5,
  "minecraft:birch_door": 0.5,
  "minecraft:carrot_on_a_stick": 0.5,
  "minecraft:stonebrick": 0.5,
  "minecraft:leather_leggings": 0.5,
  "minecraft:wheat": 0.5,
  "minecraft:dark_oak_fence": 0.5,
  "minecraft:dye": 0.5,
  "minecraft:iron_sword": 0.5,
  "minecraft:filled_map": 0.5,
  "minecraft:jungle_fence_gate": 0.5,
  "minecraft:golden_carrot": 0.5,
  "minecraft:jukebox": 0.5,
  "minecraft:beacon": 0.5,
  "minecraft:ice": 0.5,
  "minecraft:quartz": 0.5,
  "minecraft:waterlily": 0.5,
  "minecraft:written_book": 0.5,
  "minecraft:acacia_fence": 0.5,
  "minecraft:wooden_pickaxe": 0.5,
  "minecraft:golden_shovel": 0.5,
  "minecraft:lava_bucket": 0.5,
  "minecraft:bed": 0.5,
  "minecraft:birch_fence_gate": 0.5,
  "minecraft:gunpowder": 0.5,
  "minecraft:sea_lantern": 0.5,
  "minecraft:rabbit_stew": 0.5,
  "minecraft:wool": 0.5,
  "minecraft:ghast_tear": 0.5,
  "minecraft:pumpkin_seeds": 0.5,
  "minecraft:iron_boots": 0.5,
  "wl:backpack_medium": 1.0,
  "minecraft:bowl": 0.5,
  "minecraft:golden_apple": 0.5,
  "minecraft:cooked_mutton": 0.5,
  "minecraft:record_mall": 0.5,
  "minecraft:chainmail_chestplate": 0.5,
  "minecraft:crafting_table": 0.5,
  "minecraft:lever": 0.5,
  "minecraft:blaze_powder": 0.5,
  "minecraft:tnt": 0.5,
  "minecraft:golden_boots": 0.5,
  "minecraft:milk_bucket": 0.5,
  "minecraft:iron_horse_armor": 0.5,
  "minecraft:acacia_stairs": 0.5,
  "minecraft:brown_mushroom_block": 0.5,
  "minecraft:jungle_fence": 0.5,
  "minecraft:arrow": 0.5,
  "minecraft:chest_minecart": 0.5,
  "minecraft:red_sandstone": 0.5,
  "minecraft:feather": 0.5,
  "minecraft:glass_bottle": 0.5,
  "minecraft:log": 0.5,
  "minecraft:wooden_hoe": 0.5,
  "minecraft:ender_pearl": 0.5,
  "minecraft:brick_block": 0.5,
  "minecraft:pumpkin": 0.5,
  "minecraft:iron_axe": 0.5,
  "minecraft:light_weighted_pressure_plate": 0.5,
  "minecraft:bookshelf": 0.5,
  "minecraft:melon": 0.5,
  "minecraft:stone_sword": 0.5,
  "minecraft:end_portal_frame": 0.5,
  "minecraft:diamond_ore": 0.5,
  "minecraft:diamond_shovel": 0.5,
  "minecraft:leather_helmet": 0.5,
  "minecraft:magma_cream": 0.5,
  "minecraft:coal": 0.5,
  "minecraft:string": 0.5,
  "minecraft:iron_door": 0.5,
  "minecraft:reeds": 0.5,
  "minecraft:rabbit_foot": 0.5,
  "minecraft:leather_chestplate": 0.5,
  "minecraft:stained_glass": 0.5,
  "minecraft:mob_spawner": 0.5,
  "minecraft:record_wait": 0.5,
  "minecraft:noteblock": 0.5,
  "minecraft:dirt": 0.5,
  "minecraft:diamond": 0.5,
  "minecraft:gold_nugget": 0.5,
  "minecraft:diamond_sword": 0.5,
  "minecraft:stone_button": 0.5,
  "minecraft:ender_chest": 0.5,
  "minecraft:armor_stand": 0.5,
  "minecraft:diamond_axe": 0.5,
  "minecraft:glass": 0.5,
  "minecraft:iron_helmet": 0.5,
  "minecraft:double_plant": 0.5,
  "minecraft:firework_charge": 0.5,
  "minecraft:slime": 0.5,
  "minecraft:gold_block": 0.5,
  "minecraft:golden_leggings": 0.5,
  "minecraft:mutton": 0.5,
  "minecraft:stone_axe": 0.5,
  "minecraft:hardened_clay": 0.5,
  "minecraft:porkchop": 0.5,
  "minecraft:end_stone": 0.5,
  "minecraft:detector_rail": 0.5,
  "minecraft:record_far": 0.5,
  "minecraft:stick": 0.5,
  "minecraft:piston": 0.5,
  "minecraft:snow_layer": 0.5,
  "minecraft:flint": 0.5,
  "minecraft:speckled_melon": 0.5,
  "minecraft:lead": 0.5,
  "minecraft:red_sandstone_stairs": 0.5,
  "minecraft:chicken": 0.5,
  "minecraft:record_strad": 0.5,
  "minecraft:cactus": 0.5,
  "minecraft:leaves2": 0.5,
  "minecraft:iron_ore": 0.5,
  "minecraft:leaves": 0.5,
  "minecraft:sapling": 0.5,
  "minecraft:fence_gate": 0.5,
  "minecraft:iron_chestplate": 0.5,
  "minecraft:netherrack": 0.5,
  "minecraft:prismarine_crystals": 0.5,
  "minecraft:diamond_hoe": 0.5,
  "minecraft:redstone_torch": 0.5,
  "minecraft:fireworks": 0.5,
  "minecraft:wooden_pressure_plate": 0.5,
  "minecraft:sandstone_stairs": 0.5,
  "minecraft:mossy_cobblestone": 0.5,
  "minecraft:furnace": 0.5,
  "wl:upgrade_station": 0.5,
  "minecraft:hopper": 0.5,
  "minecraft:iron_trapdoor": 0.5,
  "minecraft:nether_brick_fence": 0.5,
  "minecraft:emerald": 0.5,
  "minecraft:boat": 0.5,
  "minecraft:bow": 0.5,
  "minecraft:nether_star": 0.5,
  "minecraft:log2": 0.5,
  "minecraft:pumpkin_pie": 0.5,
  "minecraft:redstone_lamp": 0.5,
  "minecraft:quartz_stairs": 0.5,
  "minecraft:brewing_stand": 0.5,
  "minecraft:golden_axe": 0.5,
  "minecraft:prismarine_shard": 0.5,
  "minecraft:barrier": 0.5,
  "minecraft:slime_ball": 0.5,
  "minecraft:spruce_fence": 0.5,
  "minecraft:dark_oak_door": 0.5,
  "minecraft:banner": 0.5,
  "minecraft:flint_and_steel": 0.5,
  "minecraft:dragon_egg": 0.5,
  "minecraft:cooked_beef": 0.5,
  "minecraft:glowstone_dust": 0.5,
  "minecraft:stone_stairs": 0.5,
  "minecraft:ladder": 0.5,
  "minecraft:sticky_piston": 0.5,
  "minecraft:melon_block": 0.5,
  "minecraft:snow": 0.5,
  "minecraft:skull": 0.5,
  "minecraft:item_frame": 0.5,
  "minecraft:fishing_rod": 0.5,
  "minecraft:glass_pane": 0.5,
  "minecraft:iron_shovel": 0.5,
  "minecraft:jungle_stairs": 0.5,
  "minecraft:minecart": 0.5,
  "minecraft:rail": 0.5,
  "minecraft:spruce_fence_gate": 0.5,
  "minecraft:clay_ball": 0.5,
  "minecraft:diamond_block": 0.5,
  "minecraft:sugar": 0.5,
  "minecraft:tallgrass": 0.5,
  "minecraft:lapis_block": 0.5,
  "minecraft:farmland": 0.5,
  "minecraft:prismarine": 0.5,
  "minecraft:stone_slab": 0.5,
  "minecraft:bedrock": 0.5,
  "minecraft:iron_block": 0.5,
  "minecraft:paper": 0.5,
  "minecraft:brick": 0.5,
  "minecraft:wooden_axe": 0.5,
  "minecraft:activator_rail": 0.5,
  "minecraft:spruce_stairs": 0.5,
  "minecraft:carpet": 0.5,
  "minecraft:gravel": 0.5,
  "minecraft:nether_brick": 0.5,
  "minecraft:nether_wart": 0.5,
  "minecraft:soul_sand": 0.5,
  "minecraft:vine": 0.5,
  "minecraft:red_mushroom_block": 0.5,
  "minecraft:glowstone": 0.5,
  "minecraft:wooden_sword": 0.5,
  "minecraft:potion": 0.5,
  "minecraft:golden_pickaxe": 0.5,
  "minecraft:cooked_fish": 0.5,
  "minecraft:map": 0.5,
  "minecraft:stone_slab2": 0.5,
  "minecraft:tripwire_hook": 0.5,
  "minecraft:sign": 0.5,
  "minecraft:web": 0.5,
  "minecraft:book": 0.5,
  "minecraft:wooden_shovel": 0.5,
  "minecraft:stone_brick_stairs": 0.5,
  "minecraft:hopper_minecart": 0.5,
  "minecraft:trapped_chest": 0.5,
  "minecraft:dropper": 0.5,
  "minecraft:fire_charge": 0.5,
  "minecraft:gold_ore": 0.5,
  "minecraft:chest": 0.5,
  "minecraft:birch_stairs": 0.5,
  "minecraft:brown_mushroom": 0.5,
  "minecraft:lit_furnace": 0.5,
  "minecraft:baked_potato": 0.5,
  "minecraft:cauldron": 0.5,
  "minecraft:stained_glass_pane": 0.5,
  "minecraft:rabbit": 0.5,
  "minecraft:yellow_flower": 0.5,
  "minecraft:daylight_detector": 0.5,
  "minecraft:painting": 0.5,
  "minecraft:cooked_porkchop": 0.5,
  "minecraft:clock": 0.5,
  "minecraft:dark_oak_fence_gate": 0.5,
  "minecraft:dark_oak_stairs": 0.5,
  "minecraft:quartz_block": 0.5,
  "minecraft:dispenser": 0.5,
  "minecraft:bone": 0.5,
  "minecraft:coal_ore": 0.5,
  "minecraft:fence": 0.5,
  "minecraft:jungle_door": 0.5,
  "minecraft:bucket": 0.5,
  "minecraft:stone_hoe": 0.5,
  "minecraft:bread": 0.5,
  "minecraft:iron_ingot": 0.5,
  "minecraft:planks": 0.5,
  "minecraft:tnt_minecart": 0.5,
  "minecraft:rotten_flesh": 0.5,
  "minecraft:iron_hoe": 0.5,
  "minecraft:carrot": 0.5,
  "minecraft:torch": 0.5,
  "minecraft:record_chirp": 0.5,
  "minecraft:command_block_minecart": 0.5,
  "minecraft:acacia_door": 0.5,
  "minecraft:diamond_boots": 0.5,
  "minecraft:flower_pot": 0.5,
  "minecraft:brick_stairs": 0.5,
  "minecraft:quartz_ore": 0.5,
  "minecraft:diamond_horse_armor": 0.5,
  "minecraft:record_blocks": 0.5,
  "minecraft:leather_boots": 0.5,
  "minecraft:spruce_door": 0.5,
  "wl:backpack_large": 1.0,
  "minecraft:blaze_rod": 0.5,
  "minecraft:diamond_chestplate": 0.5,
  "minecraft:furnace_minecart": 0.5,
  "minecraft:cobblestone": 0.5,
  "minecraft:heavy_weighted_pressure_plate": 0.5,
  "minecraft:spawn_egg": 0.5,
  "minecraft:coal_block": 0.5,
  "minecraft:redstone_block": 0.5,
  "minecraft:writable_book": 0.5,
  "minecraft:golden_helmet": 0.5,
  "minecraft:snowball": 0.5,
  "minecraft:wooden_slab": 0.5,
  "minecraft:command_block": 0.5,
  "minecraft:record_stal": 0.5,
  "minecraft:sandstone": 0.5,
  "minecraft:chainmail_boots": 0.5,
  "minecraft:lit_pumpkin": 0.5,
  "minecraft:packed_ice": 0.5,
  "minecraft:red_flower": 0.5,
  "wl:backpack_small": 1.0,
  "minecraft:emerald_block": 0.5,
  "minecraft:potato": 0.5,
  "minecraft:water_bucket": 0.5,
  "minecraft:iron_bars": 0.5,
  "minecraft:record_ward": 0.5,
  "minecraft:lapis_ore": 0.5,
  "minecraft:sponge": 0.5,
  "minecraft:mycelium": 0.5,
  "minecraft:golden_sword": 0.5,
  "minecraft:egg": 0.5,
  "minecraft:anvil": 0.5,
  "minecraft:fermented_spider_eye": 0.5,
  "minecraft:diamond_helmet": 0.5,
  "minecraft:obsidian": 0.5,
  "minecraft:stone_pickaxe": 0.5,
  "minecraft:poisonous_potato": 0.5,
  "minecraft:red_mushroom": 0.5,
  "minecraft:enchanted_book": 0.5,
  "minecraft:repeater": 0.5,
  "minecraft:cooked_rabbit": 0.5,
  "minecraft:spider_eye": 0.5,
  "minecraft:iron_pickaxe": 0.5,
  "minecraft:clay": 0.5,
  "minecraft:gold_ingot": 0.5,
  "minecraft:leather": 0.5,
  "minecraft:diamond_leggings": 0.5,
  "minecraft:golden_chestplate": 0.5,
  "minecraft:record_mellohi": 0.5,
  "minecraft:fish": 0.5,
  "minecraft:emerald_ore": 0.5,
  "minecraft:cookie": 0.5
}

 

 

 

As you can see, it's all unorganized again! Excuse me if I got this wrong, but clearing and replacing the HashMap, would replace the index number of every line, correct? Again, I'm terribly sorry if I misunderstood what you said Draco.

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.

Link to comment
Share on other sites

Alright, so I did a little more research (as I should of done before replying with my idiotic way of doing things, so I apologize), and I found that HashMaps will never save the Objects you put in them in a particular order (duh). But I found a neat little class called "LinkedHashMap", that will store the things you put in them, in the order that they enter. With this, I simply loop through the ItemRegistry, add the unsorted names to my List, then sort them. After that, I loop through my sorted list, grab the item from the name and get the weight value, then add it all to my LinkedHashMap (;)), and write it to json. Whoever is curious about this, here is my full code (NOTE: I create the file in my PreInit, and write to it in my post so I get every item):

 

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

import java.io.FileWriter;
import java.io.IOException;
import java.io.Writer;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

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 {

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

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

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

	Map<String, Float> items = new LinkedHashMap<String, Float>();
	List<String> itemsOrganized = new ArrayList<String>();

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

		itemsOrganized.add(name);
	}

	Collections.sort(itemsOrganized, new Comparator<String>() {

		public int compare(String modid1, String modid2) {
			return modid1.compareTo(modid2);
		}
	});

	for(String string : itemsOrganized) {
		float weight;

		Item item = itemRegistry.getObject(string);

		if(item != null) {
			String name = itemRegistry.getNameForObject(item).toString();

			System.out.println(name);

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

			items.put(name, weight);
		}else {
			System.out.println("ERROR: One or more of the items in the ArrayList were NULL!");
		}
	}

	json.toJson(items, jsonFileWriter);

	jsonFileWriter.close();
}
}

 

I did not use TreeMap, because I do not need to constantly re-organize my Objects. I will only organize them once, during Post-Initialization. Thus, a LinkedHashMap with a List for Collection organizing I deem a bit easier. :) Thank you for all your guy's help! Next time, I'll only make one thread @Lex :P

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.

Link to comment
Share on other sites

Alright, so I did a little more research (as I should of done before replying with my idiotic way of doing things, so I apologize), and I found that HashMaps will never save the Objects you put in them in a particular order (duh)

 

Pretty sure we mentioned that a couple times...

 

Ah, yes, here:

 

No no no no. You're creating a map here. Good. You're spring the keys, good. You then save the story's keys to the map and json encode the map, bad: this fundamentally doesn't make sense.

 

The whole point of a HashTable is that the values are stored in a location that is easy to determine based on the key: "Item152964" is stored at index 15964, irregardless of if the preceding 15963 items exist or not. It is about fast lookup, not arrangement.

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.

Link to comment
Share on other sites

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.