Make it get the colour properly for the dust

This commit is contained in:
Alex_hawks 2014-01-04 23:57:28 +08:00
parent cce4db4a9c
commit f078b765e6

View file

@ -4,6 +4,9 @@ import java.awt.Color;
import java.awt.image.BufferedImage; import java.awt.image.BufferedImage;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.HashMap; import java.util.HashMap;
import java.util.HashSet; import java.util.HashSet;
import java.util.LinkedList; import java.util.LinkedList;
@ -15,6 +18,7 @@ import javax.imageio.ImageIO;
import net.minecraft.client.Minecraft; import net.minecraft.client.Minecraft;
import net.minecraft.creativetab.CreativeTabs; import net.minecraft.creativetab.CreativeTabs;
import net.minecraft.entity.player.EntityPlayer; import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack; import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound; import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.util.ResourceLocation; import net.minecraft.util.ResourceLocation;
@ -24,6 +28,7 @@ import net.minecraftforge.oredict.OreDictionary.OreRegisterEvent;
import resonantinduction.ResonantInduction; import resonantinduction.ResonantInduction;
import resonantinduction.core.base.ItemBase; import resonantinduction.core.base.ItemBase;
import calclavia.lib.Calclavia; import calclavia.lib.Calclavia;
import cpw.mods.fml.relauncher.ReflectionHelper;
import cpw.mods.fml.relauncher.Side; import cpw.mods.fml.relauncher.Side;
import cpw.mods.fml.relauncher.SideOnly; import cpw.mods.fml.relauncher.SideOnly;
@ -81,53 +86,73 @@ public class ItemDust extends ItemBase
{ {
for (String ingotName : ingotNames) for (String ingotName : ingotNames)
{ {
// Compute color for (ItemStack ingotStack : OreDictionary.getOres("ingot" + ingotName.substring(0, 1).toUpperCase() + ingotName.substring(1)))
int totalR = 0;
int totalG = 0;
int totalB = 0;
ResourceLocation textureLocation = new ResourceLocation("");
InputStream inputstream;
try
{ {
inputstream = Minecraft.getMinecraft().getResourceManager().getResource(textureLocation).getInputStream(); // Compute color
int totalR = 0;
int totalG = 0;
int totalB = 0;
BufferedImage bufferedimage = ImageIO.read(inputstream); Item theIngot = ingotStack.getItem();
LinkedList<Integer> colorCodes = new LinkedList<Integer>(); Method o = ReflectionHelper.findMethod(Item.class, theIngot, new String[] {"getIconString", "func_" + "111208_A"});
String iconString;
int width = bufferedimage.getWidth(); try
int height = bufferedimage.getWidth();
for (int x = 0; x < width; x++)
{ {
for (int y = 0; y < height; y++) iconString = (String) o.invoke(theIngot);
{ }
colorCodes.add(bufferedimage.getRGB(x, y)); catch (ReflectiveOperationException e1)
} {
break;
} }
if (colorCodes.size() > 0) ResourceLocation textureLocation = new ResourceLocation(iconString);
InputStream inputstream;
try
{ {
for (int colorCode : colorCodes) inputstream = Minecraft.getMinecraft().getResourceManager().getResource(textureLocation).getInputStream();
BufferedImage bufferedimage = ImageIO.read(inputstream);
LinkedList<Integer> colorCodes = new LinkedList<Integer>();
int width = bufferedimage.getWidth();
int height = bufferedimage.getWidth();
for (int x = 0; x < width; x++)
{ {
Color color = new Color(colorCode); for (int y = 0; y < height; y++)
totalR += color.getRed(); {
totalG += color.getGreen(); colorCodes.add(bufferedimage.getRGB(x, y));
totalB += color.getBlue(); }
} }
totalR /= colorCodes.size(); if (colorCodes.size() > 0)
totalG /= colorCodes.size(); {
totalB /= colorCodes.size(); for (int colorCode : colorCodes)
{
Color color = new Color(colorCode);
totalR += color.getRed();
totalG += color.getGreen();
totalB += color.getBlue();
}
int resultantColor = new Color(totalR, totalG, totalB).getRGB(); totalR /= colorCodes.size();
ingotColors.put(ingotName, resultantColor); totalG /= colorCodes.size();
totalB /= colorCodes.size();
int resultantColor = new Color(totalR, totalG, totalB).getRGB();
ingotColors.put(ingotName, resultantColor);
}
}
catch (IOException e)
{
e.printStackTrace();
} }
} }
catch (IOException e) if (!ingotColors.containsKey(ingotName))
{ {
e.printStackTrace(); ingotColors.put(ingotName, 0xFFFFFF);
} }
} }
} }