diff options
| author | OnyxDarkKnight <sor1n.iliutza16@gmail.com> | 2015-06-11 19:59:48 +0100 |
|---|---|---|
| committer | OnyxDarkKnight <sor1n.iliutza16@gmail.com> | 2015-06-11 19:59:48 +0100 |
| commit | 2f93a3de229a3351d91708cf60fbc9ab7a1be97f (patch) | |
| tree | e5c60900d517d428e00946a7816ab67a75c0e3aa /src/main/java/darkknight/jewelrycraft/item | |
| parent | e66ce0d4678e288d981f734fa7fdec8f9642545e (diff) | |
Improved color detection system by 10000000%! (not rly, but it's better
than b4)
Diffstat (limited to 'src/main/java/darkknight/jewelrycraft/item')
| -rw-r--r-- | src/main/java/darkknight/jewelrycraft/item/ItemMoltenMetal.java | 92 | ||||
| -rw-r--r-- | src/main/java/darkknight/jewelrycraft/item/ItemMoltenMetalBucket.java | 20 |
2 files changed, 12 insertions, 100 deletions
diff --git a/src/main/java/darkknight/jewelrycraft/item/ItemMoltenMetal.java b/src/main/java/darkknight/jewelrycraft/item/ItemMoltenMetal.java index 1ffdeb7..f25e09e 100644 --- a/src/main/java/darkknight/jewelrycraft/item/ItemMoltenMetal.java +++ b/src/main/java/darkknight/jewelrycraft/item/ItemMoltenMetal.java @@ -8,7 +8,9 @@ import java.util.HashMap; import java.util.LinkedList;
import java.util.List;
import java.util.Map;
+
import javax.imageio.ImageIO;
+
import net.minecraft.client.Minecraft;
import net.minecraft.client.renderer.texture.IIconRegister;
import net.minecraft.client.resources.IResourceManager;
@@ -18,6 +20,7 @@ import net.minecraft.util.ResourceLocation; import cpw.mods.fml.relauncher.Side;
import cpw.mods.fml.relauncher.SideOnly;
import darkknight.jewelrycraft.util.JewelryNBT;
+import darkknight.jewelrycraft.util.JewelrycraftUtil;
import darkknight.jewelrycraft.util.Variables;
public class ItemMoltenMetal extends Item
@@ -49,92 +52,7 @@ public class ItemMoltenMetal extends Item @SideOnly (Side.CLIENT)
public int getColorFromItemStack(ItemStack stack, int pass)
{
- try{
- return color(stack, pass);
- }
- catch(IOException e){
- e.printStackTrace();
- }
- return 16777215;
- }
-
- /**
- * @param stack
- * @param pass
- * @return
- * @throws IOException
- */
- @SideOnly(Side.CLIENT)
- public static int color(ItemStack stack, int pass) throws IOException
- {
- IResourceManager rm = Minecraft.getMinecraft().getResourceManager();
- ResourceLocation ingot;
- BufferedImage icon;
- if (stack != null && JewelryNBT.ingot(stack) != null && Item.getIdFromItem(JewelryNBT.ingot(stack).getItem()) > 0 && JewelryNBT.ingot(stack).getIconIndex() != null && JewelryNBT.ingotColor(stack) == 16777215){
- try{
- ingot = ItemBaseJewelry.getLocation(JewelryNBT.ingot(stack), stack, false);
- }
- catch(Exception e){
- ingot = new ResourceLocation("textures/items/apple.png");
- }
- icon = ImageIO.read(rm.getResource(ingot).getInputStream());
- int height = icon.getHeight();
- int width = icon.getWidth();
- Map m = new HashMap();
- for(int i = 0; i < width; i++)
- for(int j = 0; j < height; j++){
- int rgb = icon.getRGB(i, j);
- int red = rgb >> 16 & 0xff;
- int green = rgb >> 8 & 0xff;
- int blue = rgb & 0xff;
- int[] rgbArr = {red, green, blue};
- int Cmax = Math.max(red, Math.max(green, blue));
- int Cmin = Math.min(red, Math.min(green, blue));
- if (!isGray(rgbArr)) m.put(rgb, (Cmax + Cmin) / 2);
- }
- try{
- int color = getMostCommonColour(m);
- if (JewelryNBT.ingot(stack) != null && JewelryNBT.ingot(stack).getItem().getColorFromItemStack(JewelryNBT.ingot(stack), 1) != 16777215) JewelryNBT.addIngotColor(stack, JewelryNBT.ingot(stack).getItem().getColorFromItemStack(JewelryNBT.ingot(stack), 1));
- else JewelryNBT.addIngotColor(stack, color);
- }
- catch(Exception e){
- JewelryNBT.addIngotColor(stack, 16777215);
- }
- }
- if (JewelryNBT.ingot(stack) != null) return JewelryNBT.ingotColor(stack);
- return 0;
- }
-
- /**
- * @param map
- * @return
- */
- public static int getMostCommonColour(Map map)
- {
- List list = new LinkedList(map.entrySet());
- Collections.sort(list, new Comparator(){
- public int compare(Object o1, Object o2)
- {
- return ((Comparable)((Map.Entry)o1).getValue()).compareTo(((Map.Entry)o2).getValue());
- }
- });
- Map.Entry me = (Map.Entry)list.get(list.size() - 1);
- for(int i = 0; i < list.size(); i++){
- float alpha = Float.valueOf(list.get(i).toString().split("=")[1]);
- if (alpha < 180) me = (Map.Entry)list.get(i);
- }
- int rgb = (Integer)me.getKey();
- return rgb;
- }
-
- /**
- * @param rgbArr
- * @return
- */
- public static boolean isGray(int[] rgbArr)
- {
- int rgbSum = rgbArr[0] + rgbArr[1] + rgbArr[2];
- if (rgbSum > 0 && rgbSum < 256 * 3) return false;
- return true;
+ if(JewelryNBT.ingot(stack) != null) return JewelrycraftUtil.getColor(JewelryNBT.ingot(stack));
+ else return 0xFFFFFF;
}
}
diff --git a/src/main/java/darkknight/jewelrycraft/item/ItemMoltenMetalBucket.java b/src/main/java/darkknight/jewelrycraft/item/ItemMoltenMetalBucket.java index b9a83c4..0f68283 100644 --- a/src/main/java/darkknight/jewelrycraft/item/ItemMoltenMetalBucket.java +++ b/src/main/java/darkknight/jewelrycraft/item/ItemMoltenMetalBucket.java @@ -25,6 +25,7 @@ import darkknight.jewelrycraft.block.BlockList; import darkknight.jewelrycraft.network.PacketRequestLiquidData;
import darkknight.jewelrycraft.network.PacketSendLiquidData;
import darkknight.jewelrycraft.util.JewelryNBT;
+import darkknight.jewelrycraft.util.JewelrycraftUtil;
import darkknight.jewelrycraft.util.Variables;
public class ItemMoltenMetalBucket extends Item {
@@ -80,11 +81,7 @@ public class ItemMoltenMetalBucket extends Item { if (movingobjectposition.sideHit == 4) --i;
if (movingobjectposition.sideHit == 5) ++i;
if (!par3EntityPlayer.canPlayerEdit(i, j, k, movingobjectposition.sideHit, stack)) return stack;
- try {
- if (tryPlaceContainedLiquid(par2World, i, j, k, stack) && !par3EntityPlayer.capabilities.isCreativeMode) return new ItemStack(Items.bucket);
- } catch (IOException e) {
- e.printStackTrace();
- }
+ if (tryPlaceContainedLiquid(par2World, i, j, k, stack) && !par3EntityPlayer.capabilities.isCreativeMode) return new ItemStack(Items.bucket);
}
}
return stack;
@@ -117,7 +114,7 @@ public class ItemMoltenMetalBucket extends Item { * @return
* @throws IOException
*/
- public boolean tryPlaceContainedLiquid(World world, int x, int y, int z, ItemStack stack) throws IOException {
+ public boolean tryPlaceContainedLiquid(World world, int x, int y, int z, ItemStack stack) {
if (BlockList.moltenMetal == Blocks.air) return false;
else {
Material material = world.getBlock(x, y, z).getMaterial();
@@ -126,12 +123,8 @@ public class ItemMoltenMetalBucket extends Item { else if (stack != null && JewelryNBT.ingot(stack) != null) {
if (!world.isRemote && flag && !material.isLiquid()) world.func_147480_a(x, y, z, true);
if (world.isRemote) {
- int color = color(stack, 1);
- JewelrycraftMod.saveData.setString(x + " " + y + " " + z + " " + world.provider.dimensionId, Item.getIdFromItem(JewelryNBT.ingot(stack).getItem()) + ":" + JewelryNBT.ingot(stack).getItemDamage() + ":" + color);
- JewelrycraftMod.netWrapper.sendToAll(new PacketSendLiquidData(world.provider.dimensionId, x, y, z, Item.getIdFromItem(JewelryNBT.ingot(stack).getItem()), JewelryNBT.ingot(stack).getItemDamage(), color));
- } else {
- JewelrycraftMod.saveData.setString(x + " " + y + " " + z + " " + world.provider.dimensionId, Item.getIdFromItem(JewelryNBT.ingot(stack).getItem()) + ":" + JewelryNBT.ingot(stack).getItemDamage() + ":" + world.rand.nextInt(16777216));
- JewelrycraftMod.netWrapper.sendToAll(new PacketSendLiquidData(world.provider.dimensionId, x, y, z, Item.getIdFromItem(JewelryNBT.ingot(stack).getItem()), JewelryNBT.ingot(stack).getItemDamage(), world.rand.nextInt(16777216)));
+ JewelrycraftMod.saveData.setString(x + " " + y + " " + z + " " + world.provider.dimensionId, Item.getIdFromItem(JewelryNBT.ingot(stack).getItem()) + ":" + JewelryNBT.ingot(stack).getItemDamage());
+ JewelrycraftMod.netWrapper.sendToAll(new PacketSendLiquidData(world.provider.dimensionId, x, y, z, Item.getIdFromItem(JewelryNBT.ingot(stack).getItem()), JewelryNBT.ingot(stack).getItemDamage()));
}
world.setBlock(x, y, z, BlockList.moltenMetal, 0, 3);
return true;
@@ -192,7 +185,8 @@ public class ItemMoltenMetalBucket extends Item { */
@SideOnly(Side.CLIENT)
public static int color(ItemStack stack, int pass) throws IOException {
- if (pass == 1) return ItemMoltenMetal.color(stack, pass);
+ // System.out.println(JewelrycraftUtil.getColor(stack));
+ if (pass == 1 && JewelryNBT.ingot(stack) != null) return JewelrycraftUtil.getColor(JewelryNBT.ingot(stack));
return 16777215;
}
|
