diff --git a/src/main/scala/resonantinduction/core/ResonantInduction.java b/src/main/scala/resonantinduction/core/ResonantInduction.java index 0e15f3de8..4e7766d01 100644 --- a/src/main/scala/resonantinduction/core/ResonantInduction.java +++ b/src/main/scala/resonantinduction/core/ResonantInduction.java @@ -150,7 +150,7 @@ public class ResonantInduction Settings.CONFIGURATION.save(); // Generate Resources - if (Settings.RESOURCE_GENERATOR) + if (ResourceGenerator.ENABLED) ResourceGenerator.generateOreResources(); proxy.postInit(); } diff --git a/src/main/scala/resonantinduction/core/Settings.java b/src/main/scala/resonantinduction/core/Settings.java index cdfbdd4e7..73146c652 100644 --- a/src/main/scala/resonantinduction/core/Settings.java +++ b/src/main/scala/resonantinduction/core/Settings.java @@ -15,87 +15,85 @@ import cpw.mods.fml.common.ModMetadata; /** @author Calclavia */ public class Settings { - public static final Configuration CONFIGURATION = new Configuration(new File(Loader.instance().getConfigDir(), Reference.NAME + ".cfg")); + public static final Configuration CONFIGURATION = new Configuration(new File(Loader.instance().getConfigDir(), Reference.NAME + ".cfg")); - /** IDs suggested by Jyzarc and Horfius */ - public static final IDManager idManager; + /** IDs suggested by Jyzarc and Horfius */ + public static final IDManager idManager; - static - { - CONFIGURATION.load(); - idManager = new IDManager(CONFIGURATION.get(Configuration.CATEGORY_GENERAL, "BlockIDPrefix", 1200).getInt(1200), CONFIGURATION.get(Configuration.CATEGORY_GENERAL, "ItemIDPrefix", 20150).getInt(20150)); - CONFIGURATION.save(); - } + static + { + CONFIGURATION.load(); + idManager = new IDManager(CONFIGURATION.get(Configuration.CATEGORY_GENERAL, "BlockIDPrefix", 1200).getInt(1200), CONFIGURATION.get(Configuration.CATEGORY_GENERAL, "ItemIDPrefix", 20150).getInt(20150)); + CONFIGURATION.save(); + } - public static int getNextBlockID() - { - return idManager.getNextBlockID(); - } + public static int getNextBlockID() + { + return idManager.getNextBlockID(); + } - public static int getNextBlockID(String key) - { - int id = idManager.getNextBlockID(); - return Settings.CONFIGURATION.get(Configuration.CATEGORY_BLOCK, key, id).getInt(id); - } + public static int getNextBlockID(String key) + { + int id = idManager.getNextBlockID(); + return Settings.CONFIGURATION.get(Configuration.CATEGORY_BLOCK, key, id).getInt(id); + } - public static int getNextItemID() - { - return idManager.getNextItemID(); - } + public static int getNextItemID() + { + return idManager.getNextItemID(); + } - public static int getNextItemID(String key) - { - int id = idManager.getNextItemID(); - return Settings.CONFIGURATION.get(Configuration.CATEGORY_ITEM, key, id).getInt(id); - } + public static int getNextItemID(String key) + { + int id = idManager.getNextItemID(); + return Settings.CONFIGURATION.get(Configuration.CATEGORY_ITEM, key, id).getInt(id); + } - @Config(key = "Engineering Table Autocraft") - public static boolean ALLOW_ENGINEERING_AUTOCRAFT = true; - @Config(key = "Tesla Sound FXs") - public static boolean SOUND_FXS = true; - @Config(key = "Shiny silver Wires") - public static boolean SHINY_SILVER = true; - @Config(key = "Max EM Contractor Path") - public static int MAX_LEVITATOR_DISTANCE = 200; - @Config(category = Configuration.CATEGORY_GENERAL, key = "Levitator Max Reach") - public static int LEVITATOR_MAX_REACH = 40; - @Config(category = Configuration.CATEGORY_GENERAL, key = "Levitator Push Delay") - public static int LEVITATOR_PUSH_DELAY = 5; - @Config(category = Configuration.CATEGORY_GENERAL, key = "Levitator Max Speed") - public static double LEVITATOR_MAX_SPEED = .2; - @Config(category = Configuration.CATEGORY_GENERAL, key = "Levitator Acceleration") - public static double LEVITATOR_ACCELERATION = .02; - @Config(category = Configuration.CATEGORY_GENERAL, key = "Enabled Resource Generator") - public static boolean RESOURCE_GENERATOR = true; + @Config(key = "Engineering Table Autocraft") + public static boolean ALLOW_ENGINEERING_AUTOCRAFT = true; + @Config(key = "Tesla Sound FXs") + public static boolean SOUND_FXS = true; + @Config(key = "Shiny silver Wires") + public static boolean SHINY_SILVER = true; + @Config(key = "Max EM Contractor Path") + public static int MAX_LEVITATOR_DISTANCE = 200; + @Config(category = Configuration.CATEGORY_GENERAL, key = "Levitator Max Reach") + public static int LEVITATOR_MAX_REACH = 40; + @Config(category = Configuration.CATEGORY_GENERAL, key = "Levitator Push Delay") + public static int LEVITATOR_PUSH_DELAY = 5; + @Config(category = Configuration.CATEGORY_GENERAL, key = "Levitator Max Speed") + public static double LEVITATOR_MAX_SPEED = .2; + @Config(category = Configuration.CATEGORY_GENERAL, key = "Levitator Acceleration") + public static double LEVITATOR_ACCELERATION = .02; - public static void setModMetadata(ModMetadata metadata, String id, String name) - { - setModMetadata(metadata, id, name, ""); - } + public static void setModMetadata(ModMetadata metadata, String id, String name) + { + setModMetadata(metadata, id, name, ""); + } - public static void setModMetadata(ModMetadata metadata, String id, String name, String parent) - { - metadata.modId = id; - metadata.name = name; - metadata.description = LanguageUtility.getLocal("meta.resonantinduction.description"); - metadata.url = "http://calclavia.com/resonant-induction"; - metadata.logoFile = "ri_logo.png"; - metadata.version = Reference.VERSION + "." + Reference.BUILD_VERSION; - metadata.authorList = Arrays.asList(new String[] { "Calclavia", "DarkCow" }); - metadata.credits = LanguageUtility.getLocal("meta.resonantinduction.credits"); - metadata.parent = parent; - metadata.autogenerated = false; - } + public static void setModMetadata(ModMetadata metadata, String id, String name, String parent) + { + metadata.modId = id; + metadata.name = name; + metadata.description = LanguageUtility.getLocal("meta.resonantinduction.description"); + metadata.url = "http://calclavia.com/resonant-induction"; + metadata.logoFile = "ri_logo.png"; + metadata.version = Reference.VERSION + "." + Reference.BUILD_VERSION; + metadata.authorList = Arrays.asList(new String[] { "Calclavia", "DarkCow" }); + metadata.credits = LanguageUtility.getLocal("meta.resonantinduction.credits"); + metadata.parent = parent; + metadata.autogenerated = false; + } - public static boolean isOp(String username) - { - MinecraftServer theServer = FMLCommonHandler.instance().getMinecraftServerInstance(); + public static boolean isOp(String username) + { + MinecraftServer theServer = FMLCommonHandler.instance().getMinecraftServerInstance(); - if (theServer != null) - { - return theServer.getConfigurationManager().getOps().contains(username.trim().toLowerCase()); - } + if (theServer != null) + { + return theServer.getConfigurationManager().getOps().contains(username.trim().toLowerCase()); + } - return false; - } + return false; + } } diff --git a/src/main/scala/resonantinduction/core/resource/ResourceGenerator.java b/src/main/scala/resonantinduction/core/resource/ResourceGenerator.java index 3b30016c4..7ba2d2631 100644 --- a/src/main/scala/resonantinduction/core/resource/ResourceGenerator.java +++ b/src/main/scala/resonantinduction/core/resource/ResourceGenerator.java @@ -53,396 +53,405 @@ import cpw.mods.fml.relauncher.SideOnly; /** @author Calclavia */ public class ResourceGenerator implements IVirtualObject { - public static final ResourceGenerator INSTANCE = new ResourceGenerator(); - - @Config(comment = "Allow the Resource Generator to make ore dictionary compatible recipes?") - private static boolean allowOreDictCompatibility = true; - - /** - * A list of material names. They are all camelCase reference of ore dictionary names without - * the "ore" or "ingot" prefix. - * - * Name, ID - */ - static int maxID = 0; - public static final HashBiMap materials = HashBiMap.create(); - - static final HashMap materialColorCache = new HashMap(); - static final HashMap iconColorCache = new HashMap(); - - static - { - OreDetectionBlackList.addIngot("ingotRefinedIron"); - OreDetectionBlackList.addIngot("ingotUranium"); - SaveManager.registerClass("resourceGenerator", ResourceGenerator.class); - SaveManager.register(INSTANCE); - } - - // TODO: Generate teh resource here instead of elsewhere... - @ForgeSubscribe - public void oreRegisterEvent(OreRegisterEvent evt) - { - if (evt.Name.startsWith("ingot")) - { - String oreDictName = evt.Name.replace("ingot", ""); - String materialName = LanguageUtility.decapitalizeFirst(oreDictName); - - if (!materials.containsKey(materialName)) - { - Settings.CONFIGURATION.load(); - boolean allowMaterial = Settings.CONFIGURATION.get("Resource_Generator", "Enable " + oreDictName, true).getBoolean(true); - Settings.CONFIGURATION.save(); - - if (!allowMaterial || OreDetectionBlackList.isIngotBlackListed("ingot" + oreDictName) || OreDetectionBlackList.isOreBlackListed("ore" + oreDictName)) - return; - - materials.put(materialName, maxID++); - } - } - } - - public static void generate(String materialName) - { - // Caps version of the name - String nameCaps = LanguageUtility.capitalizeFirst(materialName); - String localizedName = materialName; - - List list = OreDictionary.getOres("ingot" + nameCaps); - - if (list.size() > 0) - { - ItemStack type = list.get(0); - localizedName = type.getDisplayName().trim(); - - if (LanguageUtility.getLocal(localizedName) != null && LanguageUtility.getLocal(localizedName) != "") - { - localizedName = LanguageUtility.getLocal(localizedName); - } - - localizedName.replace(LanguageUtility.getLocal("misc.resonantinduction.ingot"), "").replaceAll("^ ", "").replaceAll(" $", ""); - } - - /** Generate molten fluids */ - FluidColored fluidMolten = new FluidColored(materialNameToMolten(materialName)); - fluidMolten.setDensity(7); - fluidMolten.setViscosity(5000); - fluidMolten.setTemperature(273 + 1538); - FluidRegistry.registerFluid(fluidMolten); - LanguageRegistry.instance().addStringLocalization(fluidMolten.getUnlocalizedName(), LanguageUtility.getLocal("tooltip.molten") + " " + localizedName); - BlockFluidMaterial blockFluidMaterial = new BlockFluidMaterial(fluidMolten); - GameRegistry.registerBlock(blockFluidMaterial, "molten" + nameCaps); - ResonantInduction.blockMoltenFluid.put(getID(materialName), blockFluidMaterial); - FluidContainerRegistry.registerFluidContainer(fluidMolten, ResonantInduction.itemBucketMolten.getStackFromMaterial(materialName)); - - /** Generate dust mixture fluids */ - FluidColored fluidMixture = new FluidColored(materialNameToMixture(materialName)); - FluidRegistry.registerFluid(fluidMixture); - BlockFluidMixture blockFluidMixture = new BlockFluidMixture(fluidMixture); - LanguageRegistry.instance().addStringLocalization(fluidMixture.getUnlocalizedName(), localizedName + " " + LanguageUtility.getLocal("tooltip.mixture")); - GameRegistry.registerBlock(blockFluidMixture, "mixture" + nameCaps); - ResonantInduction.blockMixtureFluids.put(getID(materialName), blockFluidMixture); - FluidContainerRegistry.registerFluidContainer(fluidMixture, ResonantInduction.itemBucketMixture.getStackFromMaterial(materialName)); - - ItemStack dust = ResonantInduction.itemDust.getStackFromMaterial(materialName); - ItemStack rubble = ResonantInduction.itemRubble.getStackFromMaterial(materialName); - ItemStack refinedDust = ResonantInduction.itemRefinedDust.getStackFromMaterial(materialName); - - if (allowOreDictCompatibility) - { - OreDictionary.registerOre("dust" + nameCaps, ResonantInduction.itemDust.getStackFromMaterial(materialName)); - OreDictionary.registerOre("rubble" + nameCaps, ResonantInduction.itemRubble.getStackFromMaterial(materialName)); - OreDictionary.registerOre("dustRefined" + nameCaps, ResonantInduction.itemRefinedDust.getStackFromMaterial(materialName)); - - MachineRecipes.INSTANCE.addRecipe(RecipeType.GRINDER.name(), "rubble" + nameCaps, dust, dust); - MachineRecipes.INSTANCE.addRecipe(RecipeType.MIXER.name(), "dust" + nameCaps, refinedDust); - MachineRecipes.INSTANCE.addRecipe(RecipeType.SMELTER.name(), new FluidStack(fluidMolten, FluidContainerRegistry.BUCKET_VOLUME), "ingot" + nameCaps); - } - else - { - MachineRecipes.INSTANCE.addRecipe(RecipeType.GRINDER.name(), rubble, dust, dust); - MachineRecipes.INSTANCE.addRecipe(RecipeType.MIXER.name(), dust, refinedDust); - MachineRecipes.INSTANCE.addRecipe(RecipeType.SMELTER.name(), new FluidStack(fluidMolten, FluidContainerRegistry.BUCKET_VOLUME), "ingot" + nameCaps); - } - - FurnaceRecipes.smelting().addSmelting(dust.itemID, dust.getItemDamage(), OreDictionary.getOres("ingot" + nameCaps).get(0).copy(), 0.7f); - ItemStack smeltResult = OreDictionary.getOres("ingot" + nameCaps).get(0).copy(); - FurnaceRecipes.smelting().addSmelting(refinedDust.itemID, refinedDust.getItemDamage(), smeltResult, 0.7f); - - if (OreDictionary.getOres("ore" + nameCaps).size() > 0) - { - MachineRecipes.INSTANCE.addRecipe(RecipeType.CRUSHER.name(), "ore" + nameCaps, "rubble" + nameCaps); - } - } - - public static void generateOreResources() - { - OreDictionary.registerOre("ingotGold", Item.ingotGold); - OreDictionary.registerOre("ingotIron", Item.ingotIron); - - OreDictionary.registerOre("oreGold", Block.oreGold); - OreDictionary.registerOre("oreIron", Block.oreIron); - OreDictionary.registerOre("oreLapis", Block.oreLapis); - - // Vanilla fluid recipes - MachineRecipes.INSTANCE.addRecipe(RecipeType.SMELTER.name(), new FluidStack(FluidRegistry.LAVA, FluidContainerRegistry.BUCKET_VOLUME), new ItemStack(Block.stone)); - - // Vanilla crusher recipes - MachineRecipes.INSTANCE.addRecipe(RecipeType.CRUSHER.name(), Block.cobblestone, Block.gravel); - MachineRecipes.INSTANCE.addRecipe(RecipeType.CRUSHER.name(), Block.stone, Block.cobblestone); - MachineRecipes.INSTANCE.addRecipe(RecipeType.CRUSHER.name(), Block.chest, new ItemStack(Block.planks, 7, 0)); - - // Vanilla grinder recipes - MachineRecipes.INSTANCE.addRecipe(RecipeType.GRINDER.name(), Block.cobblestone, Block.sand); - MachineRecipes.INSTANCE.addRecipe(RecipeType.GRINDER.name(), Block.gravel, Block.sand); - MachineRecipes.INSTANCE.addRecipe(RecipeType.GRINDER.name(), Block.glass, Block.sand); - - Iterator it = materials.keySet().iterator(); - while (it.hasNext()) - { - String materialName = it.next(); - String nameCaps = LanguageUtility.capitalizeFirst(materialName); - - if (OreDictionary.getOres("ore" + nameCaps).size() > 0) - generate(materialName); - else - it.remove(); - } - } - - @ForgeSubscribe - @SideOnly(Side.CLIENT) - public void reloadTextures(TextureStitchEvent.Post e) - { - computeColors(); - } - - @SideOnly(Side.CLIENT) - public static void computeColors() - { - for (String material : materials.keySet()) - { - // Compute color - int totalR = 0; - int totalG = 0; - int totalB = 0; - - int colorCount = 0; - - for (ItemStack ingotStack : OreDictionary.getOres("ingot" + LanguageUtility.capitalizeFirst(material))) - { - Item theIngot = ingotStack.getItem(); - int color = getAverageColor(ingotStack); - materialColorCache.put(material, color); - } - - if (!materialColorCache.containsKey(material)) - { - materialColorCache.put(material, 0xFFFFFF); - } - } - } - - /** - * Gets the average color of this item. - * - * @param itemStack - * @return The RGB hexadecimal color code. - */ - @SideOnly(Side.CLIENT) - public static int getAverageColor(ItemStack itemStack) - { - int totalR = 0; - int totalG = 0; - int totalB = 0; - - int colorCount = 0; - Item item = itemStack.getItem(); - - try - { - Icon icon = item.getIconIndex(itemStack); - - if (iconColorCache.containsKey(icon)) - { - return iconColorCache.get(icon); - } - - String iconString = icon.getIconName(); - - if (iconString != null && !iconString.contains("MISSING_ICON_ITEM")) - { - iconString = (iconString.contains(":") ? iconString.replace(":", ":" + Reference.ITEM_TEXTURE_DIRECTORY) : Reference.ITEM_TEXTURE_DIRECTORY + iconString) + ".png"; - ResourceLocation textureLocation = new ResourceLocation(iconString); - - InputStream inputstream = Minecraft.getMinecraft().getResourceManager().getResource(textureLocation).getInputStream(); - BufferedImage bufferedimage = ImageIO.read(inputstream); - - int width = bufferedimage.getWidth(); - int height = bufferedimage.getWidth(); - - for (int x = 0; x < width; x++) - { - for (int y = 0; y < height; y++) - { - Color rgb = new Color(bufferedimage.getRGB(x, y)); - - /** Ignore things that are too dark. Standard luma calculation. */ - double luma = 0.2126 * rgb.getRed() + 0.7152 * rgb.getGreen() + 0.0722 * rgb.getBlue(); - - if (luma > 40) - { - totalR += rgb.getRed(); - totalG += rgb.getGreen(); - totalB += rgb.getBlue(); - colorCount++; - } - } - } - } - - if (colorCount > 0) - { - totalR /= colorCount; - totalG /= colorCount; - totalB /= colorCount; - int averageColor = new Color(totalR, totalG, totalB).brighter().getRGB(); - iconColorCache.put(icon, averageColor); - return averageColor; - } - } - catch (Exception e) - { - ResonantInduction.LOGGER.fine("Failed to compute colors for: " + item); - } - - return 0xFFFFFF; - } - - public static String moltenToMaterial(String fluidName) - { - return fluidNameToMaterial(fluidName, "molten"); - } - - public static String materialNameToMolten(String fluidName) - { - return materialNameToFluid(fluidName, "molten"); - } - - public static String mixtureToMaterial(String fluidName) - { - return fluidNameToMaterial(fluidName, "mixture"); - } - - public static String materialNameToMixture(String fluidName) - { - return materialNameToFluid(fluidName, "mixture"); - } - - public static String fluidNameToMaterial(String fluidName, String type) - { - return LanguageUtility.decapitalizeFirst(LanguageUtility.underscoreToCamel(fluidName).replace(type, "")); - } - - public static String materialNameToFluid(String materialName, String type) - { - return type + "_" + LanguageUtility.camelToLowerUnderscore(materialName); - } - - public static BlockFluidFinite getMixture(String name) - { - return ResonantInduction.blockMixtureFluids.get(getID(name)); - } - - public static BlockFluidFinite getMolten(String name) - { - return ResonantInduction.blockMoltenFluid.get(getID(name)); - } - - public static int getID(String name) - { - if (!materials.containsKey(name)) - { - ResonantInduction.LOGGER.severe("Trying to get invalid material name " + name); - return 0; - } - - return materials.get(name); - } - - public static String getName(int id) - { - return materials.inverse().get(id); - } - - public static String getName(ItemStack itemStack) - { - return LanguageUtility.decapitalizeFirst(OreDictionary.getOreName(OreDictionary.getOreID(itemStack)).replace("dust", "").replace("ore", "").replace("ingot", "")); - } - - public static int getColor(String name) - { - if (name != null && materialColorCache.containsKey(name)) - { - return materialColorCache.get(name); - } - - return 0xFFFFFF; - } - - @Deprecated - public static List getMaterials() - { - List returnMaterials = new ArrayList(); - - for (int i = 0; i < materials.size(); i++) - { - returnMaterials.add(getName(i)); - } - - return returnMaterials; - } - - @Override - public void save(NBTTagCompound nbt) - { - NBTTagList list = new NBTTagList(); - - for (Entry entry : materials.entrySet()) - { - NBTTagCompound node = new NBTTagCompound(); - node.setString("materialName", entry.getKey()); - node.setInteger("materialID", entry.getValue()); - list.appendTag(node); - } - - nbt.setTag("materialIDMap", list); - } - - @Override - public void load(NBTTagCompound nbt) - { - if (nbt.hasKey("materialIDMap")) - { - materials.clear(); - NBTTagList nbtList = nbt.getTagList("materialIDMap"); - - for (int i = 0; i < nbtList.tagCount(); ++i) - { - NBTTagCompound node = (NBTTagCompound) nbtList.tagAt(i); - materials.put(node.getString("materialName"), node.getInteger("materialID")); - } - } - } - - @Override - public File getSaveFile() - { - return new File(NBTUtility.getSaveDirectory(), "Resource_Generator.dat"); - } - - @Override - public void setSaveFile(File file) - { - - } + public static final ResourceGenerator INSTANCE = new ResourceGenerator(); + + @Config(comment = "Allow the Resource Generator to make ore dictionary compatible recipes?") + private static boolean allowOreDictCompatibility = true; + @Config(category = "Resource_Generator", key = "Enable_All") + public static boolean ENABLED = true; + @Config(category = "Resource_Generator", key = "Enabled_All_Fluids") + public static boolean ENABLE_FLUIDS = true; + + /** A list of material names. They are all camelCase reference of ore dictionary names without + * the "ore" or "ingot" prefix. + * + * Name, ID */ + static int maxID = 0; + public static final HashBiMap materials = HashBiMap.create(); + + static final HashMap materialColorCache = new HashMap(); + static final HashMap iconColorCache = new HashMap(); + + static + { + OreDetectionBlackList.addIngot("ingotRefinedIron"); + OreDetectionBlackList.addIngot("ingotUranium"); + SaveManager.registerClass("resourceGenerator", ResourceGenerator.class); + SaveManager.register(INSTANCE); + } + + // TODO: Generate teh resource here instead of elsewhere... + @ForgeSubscribe + public void oreRegisterEvent(OreRegisterEvent evt) + { + if (evt.Name.startsWith("ingot")) + { + String oreDictName = evt.Name.replace("ingot", ""); + String materialName = LanguageUtility.decapitalizeFirst(oreDictName); + + if (!materials.containsKey(materialName)) + { + Settings.CONFIGURATION.load(); + boolean allowMaterial = Settings.CONFIGURATION.get("Resource_Generator", "Enable " + oreDictName, true).getBoolean(true); + Settings.CONFIGURATION.save(); + + if (!allowMaterial || OreDetectionBlackList.isIngotBlackListed("ingot" + oreDictName) || OreDetectionBlackList.isOreBlackListed("ore" + oreDictName)) + return; + + materials.put(materialName, maxID++); + } + } + } + + public static void generate(String materialName) + { + // Caps version of the name + String nameCaps = LanguageUtility.capitalizeFirst(materialName); + String localizedName = materialName; + + List list = OreDictionary.getOres("ingot" + nameCaps); + + if (list.size() > 0) + { + ItemStack type = list.get(0); + localizedName = type.getDisplayName().trim(); + + if (LanguageUtility.getLocal(localizedName) != null && LanguageUtility.getLocal(localizedName) != "") + { + localizedName = LanguageUtility.getLocal(localizedName); + } + + localizedName.replace(LanguageUtility.getLocal("misc.resonantinduction.ingot"), "").replaceAll("^ ", "").replaceAll(" $", ""); + } + if (ENABLE_FLUIDS) + { + /** Generate molten fluids */ + FluidColored fluidMolten = new FluidColored(materialNameToMolten(materialName)); + fluidMolten.setDensity(7); + fluidMolten.setViscosity(5000); + fluidMolten.setTemperature(273 + 1538); + FluidRegistry.registerFluid(fluidMolten); + LanguageRegistry.instance().addStringLocalization(fluidMolten.getUnlocalizedName(), LanguageUtility.getLocal("tooltip.molten") + " " + localizedName); + BlockFluidMaterial blockFluidMaterial = new BlockFluidMaterial(fluidMolten); + GameRegistry.registerBlock(blockFluidMaterial, "molten" + nameCaps); + ResonantInduction.blockMoltenFluid.put(getID(materialName), blockFluidMaterial); + FluidContainerRegistry.registerFluidContainer(fluidMolten, ResonantInduction.itemBucketMolten.getStackFromMaterial(materialName)); + + /** Generate dust mixture fluids */ + FluidColored fluidMixture = new FluidColored(materialNameToMixture(materialName)); + FluidRegistry.registerFluid(fluidMixture); + BlockFluidMixture blockFluidMixture = new BlockFluidMixture(fluidMixture); + LanguageRegistry.instance().addStringLocalization(fluidMixture.getUnlocalizedName(), localizedName + " " + LanguageUtility.getLocal("tooltip.mixture")); + GameRegistry.registerBlock(blockFluidMixture, "mixture" + nameCaps); + ResonantInduction.blockMixtureFluids.put(getID(materialName), blockFluidMixture); + FluidContainerRegistry.registerFluidContainer(fluidMixture, ResonantInduction.itemBucketMixture.getStackFromMaterial(materialName)); + + if (allowOreDictCompatibility) + { + MachineRecipes.INSTANCE.addRecipe(RecipeType.SMELTER.name(), new FluidStack(fluidMolten, FluidContainerRegistry.BUCKET_VOLUME), "ingot" + nameCaps); + } + else + { + MachineRecipes.INSTANCE.addRecipe(RecipeType.SMELTER.name(), new FluidStack(fluidMolten, FluidContainerRegistry.BUCKET_VOLUME), "ingot" + nameCaps); + } + } + + ItemStack dust = ResonantInduction.itemDust.getStackFromMaterial(materialName); + ItemStack rubble = ResonantInduction.itemRubble.getStackFromMaterial(materialName); + ItemStack refinedDust = ResonantInduction.itemRefinedDust.getStackFromMaterial(materialName); + + if (allowOreDictCompatibility) + { + OreDictionary.registerOre("dust" + nameCaps, ResonantInduction.itemDust.getStackFromMaterial(materialName)); + OreDictionary.registerOre("rubble" + nameCaps, ResonantInduction.itemRubble.getStackFromMaterial(materialName)); + OreDictionary.registerOre("dustRefined" + nameCaps, ResonantInduction.itemRefinedDust.getStackFromMaterial(materialName)); + + MachineRecipes.INSTANCE.addRecipe(RecipeType.GRINDER.name(), "rubble" + nameCaps, dust, dust); + MachineRecipes.INSTANCE.addRecipe(RecipeType.MIXER.name(), "dust" + nameCaps, refinedDust); + } + else + { + MachineRecipes.INSTANCE.addRecipe(RecipeType.GRINDER.name(), rubble, dust, dust); + MachineRecipes.INSTANCE.addRecipe(RecipeType.MIXER.name(), dust, refinedDust); + } + + FurnaceRecipes.smelting().addSmelting(dust.itemID, dust.getItemDamage(), OreDictionary.getOres("ingot" + nameCaps).get(0).copy(), 0.7f); + ItemStack smeltResult = OreDictionary.getOres("ingot" + nameCaps).get(0).copy(); + FurnaceRecipes.smelting().addSmelting(refinedDust.itemID, refinedDust.getItemDamage(), smeltResult, 0.7f); + + if (OreDictionary.getOres("ore" + nameCaps).size() > 0) + { + MachineRecipes.INSTANCE.addRecipe(RecipeType.CRUSHER.name(), "ore" + nameCaps, "rubble" + nameCaps); + } + } + + public static void generateOreResources() + { + OreDictionary.registerOre("ingotGold", Item.ingotGold); + OreDictionary.registerOre("ingotIron", Item.ingotIron); + + OreDictionary.registerOre("oreGold", Block.oreGold); + OreDictionary.registerOre("oreIron", Block.oreIron); + OreDictionary.registerOre("oreLapis", Block.oreLapis); + + // Vanilla fluid recipes + MachineRecipes.INSTANCE.addRecipe(RecipeType.SMELTER.name(), new FluidStack(FluidRegistry.LAVA, FluidContainerRegistry.BUCKET_VOLUME), new ItemStack(Block.stone)); + + // Vanilla crusher recipes + MachineRecipes.INSTANCE.addRecipe(RecipeType.CRUSHER.name(), Block.cobblestone, Block.gravel); + MachineRecipes.INSTANCE.addRecipe(RecipeType.CRUSHER.name(), Block.stone, Block.cobblestone); + MachineRecipes.INSTANCE.addRecipe(RecipeType.CRUSHER.name(), Block.chest, new ItemStack(Block.planks, 7, 0)); + + // Vanilla grinder recipes + MachineRecipes.INSTANCE.addRecipe(RecipeType.GRINDER.name(), Block.cobblestone, Block.sand); + MachineRecipes.INSTANCE.addRecipe(RecipeType.GRINDER.name(), Block.gravel, Block.sand); + MachineRecipes.INSTANCE.addRecipe(RecipeType.GRINDER.name(), Block.glass, Block.sand); + + Iterator it = materials.keySet().iterator(); + while (it.hasNext()) + { + String materialName = it.next(); + String nameCaps = LanguageUtility.capitalizeFirst(materialName); + + if (OreDictionary.getOres("ore" + nameCaps).size() > 0) + generate(materialName); + else + it.remove(); + } + } + + @ForgeSubscribe + @SideOnly(Side.CLIENT) + public void reloadTextures(TextureStitchEvent.Post e) + { + computeColors(); + } + + @SideOnly(Side.CLIENT) + public static void computeColors() + { + for (String material : materials.keySet()) + { + // Compute color + int totalR = 0; + int totalG = 0; + int totalB = 0; + + int colorCount = 0; + + for (ItemStack ingotStack : OreDictionary.getOres("ingot" + LanguageUtility.capitalizeFirst(material))) + { + Item theIngot = ingotStack.getItem(); + int color = getAverageColor(ingotStack); + materialColorCache.put(material, color); + } + + if (!materialColorCache.containsKey(material)) + { + materialColorCache.put(material, 0xFFFFFF); + } + } + } + + /** Gets the average color of this item. + * + * @param itemStack + * @return The RGB hexadecimal color code. */ + @SideOnly(Side.CLIENT) + public static int getAverageColor(ItemStack itemStack) + { + int totalR = 0; + int totalG = 0; + int totalB = 0; + + int colorCount = 0; + Item item = itemStack.getItem(); + + try + { + Icon icon = item.getIconIndex(itemStack); + + if (iconColorCache.containsKey(icon)) + { + return iconColorCache.get(icon); + } + + String iconString = icon.getIconName(); + + if (iconString != null && !iconString.contains("MISSING_ICON_ITEM")) + { + iconString = (iconString.contains(":") ? iconString.replace(":", ":" + Reference.ITEM_TEXTURE_DIRECTORY) : Reference.ITEM_TEXTURE_DIRECTORY + iconString) + ".png"; + ResourceLocation textureLocation = new ResourceLocation(iconString); + + InputStream inputstream = Minecraft.getMinecraft().getResourceManager().getResource(textureLocation).getInputStream(); + BufferedImage bufferedimage = ImageIO.read(inputstream); + + int width = bufferedimage.getWidth(); + int height = bufferedimage.getWidth(); + + for (int x = 0; x < width; x++) + { + for (int y = 0; y < height; y++) + { + Color rgb = new Color(bufferedimage.getRGB(x, y)); + + /** Ignore things that are too dark. Standard luma calculation. */ + double luma = 0.2126 * rgb.getRed() + 0.7152 * rgb.getGreen() + 0.0722 * rgb.getBlue(); + + if (luma > 40) + { + totalR += rgb.getRed(); + totalG += rgb.getGreen(); + totalB += rgb.getBlue(); + colorCount++; + } + } + } + } + + if (colorCount > 0) + { + totalR /= colorCount; + totalG /= colorCount; + totalB /= colorCount; + int averageColor = new Color(totalR, totalG, totalB).brighter().getRGB(); + iconColorCache.put(icon, averageColor); + return averageColor; + } + } + catch (Exception e) + { + ResonantInduction.LOGGER.fine("Failed to compute colors for: " + item); + } + + return 0xFFFFFF; + } + + public static String moltenToMaterial(String fluidName) + { + return fluidNameToMaterial(fluidName, "molten"); + } + + public static String materialNameToMolten(String fluidName) + { + return materialNameToFluid(fluidName, "molten"); + } + + public static String mixtureToMaterial(String fluidName) + { + return fluidNameToMaterial(fluidName, "mixture"); + } + + public static String materialNameToMixture(String fluidName) + { + return materialNameToFluid(fluidName, "mixture"); + } + + public static String fluidNameToMaterial(String fluidName, String type) + { + return LanguageUtility.decapitalizeFirst(LanguageUtility.underscoreToCamel(fluidName).replace(type, "")); + } + + public static String materialNameToFluid(String materialName, String type) + { + return type + "_" + LanguageUtility.camelToLowerUnderscore(materialName); + } + + public static BlockFluidFinite getMixture(String name) + { + return ResonantInduction.blockMixtureFluids.get(getID(name)); + } + + public static BlockFluidFinite getMolten(String name) + { + return ResonantInduction.blockMoltenFluid.get(getID(name)); + } + + public static int getID(String name) + { + if (!materials.containsKey(name)) + { + ResonantInduction.LOGGER.severe("Trying to get invalid material name " + name); + return 0; + } + + return materials.get(name); + } + + public static String getName(int id) + { + return materials.inverse().get(id); + } + + public static String getName(ItemStack itemStack) + { + return LanguageUtility.decapitalizeFirst(OreDictionary.getOreName(OreDictionary.getOreID(itemStack)).replace("dust", "").replace("ore", "").replace("ingot", "")); + } + + public static int getColor(String name) + { + if (name != null && materialColorCache.containsKey(name)) + { + return materialColorCache.get(name); + } + + return 0xFFFFFF; + } + + @Deprecated + public static List getMaterials() + { + List returnMaterials = new ArrayList(); + + for (int i = 0; i < materials.size(); i++) + { + returnMaterials.add(getName(i)); + } + + return returnMaterials; + } + + @Override + public void save(NBTTagCompound nbt) + { + NBTTagList list = new NBTTagList(); + + for (Entry entry : materials.entrySet()) + { + NBTTagCompound node = new NBTTagCompound(); + node.setString("materialName", entry.getKey()); + node.setInteger("materialID", entry.getValue()); + list.appendTag(node); + } + + nbt.setTag("materialIDMap", list); + } + + @Override + public void load(NBTTagCompound nbt) + { + if (nbt.hasKey("materialIDMap")) + { + materials.clear(); + NBTTagList nbtList = nbt.getTagList("materialIDMap"); + + for (int i = 0; i < nbtList.tagCount(); ++i) + { + NBTTagCompound node = (NBTTagCompound) nbtList.tagAt(i); + materials.put(node.getString("materialName"), node.getInteger("materialID")); + } + } + } + + @Override + public File getSaveFile() + { + return new File(NBTUtility.getSaveDirectory(), "Resource_Generator.dat"); + } + + @Override + public void setSaveFile(File file) + { + + } }