Skip to content

Instantly share code, notes, and snippets.

@Elix-x
Last active October 7, 2015 15:00
Show Gist options
  • Save Elix-x/61525e6fc172f0b7e6c5 to your computer and use it in GitHub Desktop.
Save Elix-x/61525e6fc172f0b7e6c5 to your computer and use it in GitHub Desktop.
Getting color from tool material
private static ItemStack recognizeRepairItem(ToolMaterial material) {
return material.getRepairItemStack();
}
private static String recognizeColor(ToolMaterial material) {
String color = "0:0:0";
if(recognizeRepairItem(material) != null){
ItemStack itemstack = recognizeRepairItem(material);
ResourceLocation texture = null;
if(Block.getBlockFromItem(itemstack.getItem()) != Blocks.air){
Block block = Block.getBlockFromItem(itemstack.getItem());
String textureName = ObfuscationReflectionHelper.getPrivateValue(Block.class, block, "textureName");
if(textureName != null){
if(textureName.split(":").length == 1){
textureName = "minecraft:" + textureName;
}
texture = new ResourceLocation(textureName.split(":")[0], "textures/blocks/" + textureName.split(":")[1] + ".png");
}
} else {
Item item = itemstack.getItem();
String textureName = ObfuscationReflectionHelper.getPrivateValue(Item.class, item, "iconString");
if(textureName != null){
if(textureName.split(":").length == 1){
textureName = "minecraft:" + textureName;
}
texture = new ResourceLocation(textureName.split(":")[0], "textures/items/" + textureName.split(":")[1] + ".png");
}
}
if(texture != null){
int[] colorBuff = new int[]{};
try {
colorBuff = TextureUtil.readImageData(Minecraft.getMinecraft().getResourceManager(), texture);
} catch (IOException e) {
logger.error("Caught exception while parsing texture to get color: ", e);
}
int[] red = new int[]{};
int[] green = new int[]{};
int[] blue = new int[]{};
for(int c : colorBuff){
Color col = new Color(c);
if(col.getAlpha() > 0){
red = ArrayUtils.add(red, col.getRed());
green = ArrayUtils.add(green, col.getGreen());
blue = ArrayUtils.add(blue, col.getBlue());
}
}
if(red.length > 0 && green.length > 0 && blue.length > 0){
int r = AdvancedMathUtils.average(red[0], ArrayUtils.subarray(red, 1, red.length));
int g = AdvancedMathUtils.average(green[0], ArrayUtils.subarray(green, 1, green.length));
int b = AdvancedMathUtils.average(blue[0], ArrayUtils.subarray(blue, 1, blue.length));
color = r + ":" + g + ":" + b;
}
}
}
return color;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment