diff --git a/src/main/java/gregtech/api/items/metaitem/stats/IItemDurabilityManager.java b/src/main/java/gregtech/api/items/metaitem/stats/IItemDurabilityManager.java index ad45a50dfee..e4cb8fc3d5e 100644 --- a/src/main/java/gregtech/api/items/metaitem/stats/IItemDurabilityManager.java +++ b/src/main/java/gregtech/api/items/metaitem/stats/IItemDurabilityManager.java @@ -1,25 +1,26 @@ package gregtech.api.items.metaitem.stats; -import net.minecraft.item.ItemStack; +import gregtech.client.utils.ToolChargeBarRenderer; -import org.apache.commons.lang3.tuple.Pair; -import org.jetbrains.annotations.Nullable; +import net.minecraft.item.ItemStack; -import java.awt.*; +import org.jetbrains.annotations.NotNull; public interface IItemDurabilityManager extends IItemComponent { /** The durability remaining on this item (double from 0 to 1). */ - double getDurabilityForDisplay(ItemStack itemStack); + double getDurabilityForDisplay(@NotNull ItemStack itemStack); - /** The first and last colors of a gradient. Default to Green durability gradient (null Pair). */ - @Nullable - default Pair getDurabilityColorsForDisplay(ItemStack itemStack) { - return null; + /** + * The left and right gradient bounds as two ARGB encoded integers packed into a long.
+ * See {@link gregtech.api.util.ColorUtil#packTwoARGB(int, int)} on how to make the long in the proper format. + */ + default long getDurabilityColorsForDisplay(@NotNull ItemStack itemStack) { + return ToolChargeBarRenderer.defaultGradient; } /** Whether to show the durability as red when at the last 1/4th durability. Default true */ - default boolean doDamagedStateColors(ItemStack itemStack) { + default boolean doDamagedStateColors(@NotNull ItemStack itemStack) { return true; } @@ -27,7 +28,7 @@ default boolean doDamagedStateColors(ItemStack itemStack) { * Whether to show the durability bar when {@link IItemDurabilityManager#getDurabilityForDisplay(ItemStack)} is 0. * Default true */ - default boolean showEmptyBar(ItemStack itemStack) { + default boolean showEmptyBar(@NotNull ItemStack itemStack) { return true; } @@ -35,7 +36,7 @@ default boolean showEmptyBar(ItemStack itemStack) { * Whether to show the durability bar when {@link IItemDurabilityManager#getDurabilityForDisplay(ItemStack)} is 1. * Default true */ - default boolean showFullBar(ItemStack itemStack) { + default boolean showFullBar(@NotNull ItemStack itemStack) { return true; } } diff --git a/src/main/java/gregtech/api/util/ColorUtil.java b/src/main/java/gregtech/api/util/ColorUtil.java new file mode 100644 index 00000000000..019c7615019 --- /dev/null +++ b/src/main/java/gregtech/api/util/ColorUtil.java @@ -0,0 +1,162 @@ +package gregtech.api.util; + +import org.jetbrains.annotations.Range; + +public class ColorUtil { + + /** + * Combine three R, G, and B values into an (A)RGB encoded integer.
+ * Alpha channel will be {@code 0} + */ + public static int combineRGBNoAlpha(@Range(from = 0, to = 255) int r, @Range(from = 0, to = 255) int g, + @Range(from = 0, to = 255) int b) { + return (r << 16) | (g << 8) | b; + } + + /** + * Combine three R, G, and B values into an (A)RGB encoded integer.
+ * Alpha channel will be {@code 0xFF}/{@code 255} + */ + public static int combineRGBFullAlpha(@Range(from = 0, to = 255) int r, @Range(from = 0, to = 255) int g, + @Range(from = 0, to = 255) int b) { + return 0xFF000000 | (r << 16) | (g << 8) | b; + } + + /** + * Combine A, R, G, and B values into an ARGB encoded integer. + */ + public static int combineARGB(@Range(from = 0, to = 255) int a, @Range(from = 0, to = 255) int r, + @Range(from = 0, to = 255) int g, @Range(from = 0, to = 255) int b) { + return (a << 24) | (r << 16) | (g << 8) | b; + } + + /** + * Pack two ARGB encoded integers into a single {@code long} with the left integer taking the 32 most significant + * bits and the right the 32 least significant bits. + */ + public static long packTwoARGB(int left, int right) { + return ((long) left << 32) | right; + } + + /** + * Get the left ARGB encoded integer from a long produced from {@link #packTwoARGB(int, int)}. + */ + public static int getLeftARGB(long doublePackedARGB) { + return (int) (doublePackedARGB >>> 32); + } + + /** + * Get the right ARGB encoded integer from a long produced from {@link #packTwoARGB(int, int)}. + */ + public static int getRightARGB(long doublePackedARGB) { + return (int) doublePackedARGB; + } + + /** + * Get an array of the red, blue, and green channels as floats from 0 to 1.
+ * Index {@code 0} = {@code Red}
+ * Index {@code 1} = {@code Green}
+ * Index {@code 2} = {@code Blue}
+ * See {@link #getARGBFloats(int)} for getting the alpha channel. + */ + public static float[] getRGBFloats(int rgb) { + return new float[] { + ARGBHelper.RED.isolateAndShiftAsFloat(rgb), + ARGBHelper.GREEN.isolateAndShiftAsFloat(rgb), + ARGBHelper.BLUE.isolateAndShiftAsFloat(rgb) + }; + } + + /** + * Get an array of the alpha, red, blue, and green channels as floats from 0 to 1.
+ * Index {@code 0} = {@code Alpha}
+ * Index {@code 1} = {@code Red}
+ * Index {@code 2} = {@code Green}
+ * Index {@code 3} = {@code Blue}
+ * See {@link #getRGBFloats(int)} for ignoring the alpha channel. + */ + public static float[] getARGBFloats(int argb) { + return new float[] { + ARGBHelper.ALPHA.isolateAndShiftAsFloat(argb), + ARGBHelper.RED.isolateAndShiftAsFloat(argb), + ARGBHelper.GREEN.isolateAndShiftAsFloat(argb), + ARGBHelper.BLUE.isolateAndShiftAsFloat(argb) + }; + } + + /** + * A helper enum designed to help with changing or getting specific color channels from an ARGB encoded integer. + */ + public enum ARGBHelper { + + ALPHA(0xFF000000, 24), + RED(0xFF0000, 16), + GREEN(0xFF00, 8), + BLUE(0xFF, 0); + + public final int overlay; + public final int invertedOverlay; + public final int shift; + + ARGBHelper(int overlay, int shift) { + this.overlay = overlay; + this.invertedOverlay = ~overlay; + this.shift = shift; + } + + /** + * Isolate this channel as an integer from 0 to 255.
+ * Example: {@code GREEN.isolateAndShift(0xDEADBEEF)} will return {@code 0xBE} or {@code 190}. + */ + public final @Range(from = 0, to = 0xFF) int isolateAndShift(int value) { + return (value >> shift) & 0xFF; + } + + /** + * Isolate this channel as a float from 0 to 1.
+ * Example: {@code GREEN.isolateAndShift(0xDEADBEEF)} will return {@code 0xBE / 255} or {@code 0.74509805}. + */ + public final float isolateAndShiftAsFloat(int value) { + return ((value >> shift) & 0xFF) / 255.0F; + } + + /** + * Remove the other two colors from the integer encoded ARGB and set the alpha to 255.
+ * Will always return {@code 0xFF000000} if called on {@link #ALPHA}.
+ * Unlike {@link #isolateAndShift(int)}, this will not be between 0 and 255.
+ * Example: {@code GREEN.isolateWithFullAlpha(0xDEADBEEF)} will return {@code 0xFF00BE00} or {@code -16728576}. + */ + public final int isolateWithFullAlpha(int value) { + return (value & overlay) | ALPHA.overlay; + } + + /** + * Set the value of this channel in an integer encoded ARGB value. + */ + public final int replace(int originalARGB, @Range(from = 0, to = 0xFF) int value) { + return (originalARGB & invertedOverlay) | (value << shift); + } + + /** + * The same as {@link #replace(int, int)} but will behave as if {@code originalARGB} was {@code 0}. + */ + public final int get(@Range(from = 0, to = 0xFF) int value) { + return value << shift; + } + + /** + * Add a value to this channel's value. Can overflow in this channel, but will not affect the other channels. + */ + public final int add(int originalARGB, @Range(from = 0, to = 0xFF) int value) { + return replace(originalARGB, (isolateAndShift(originalARGB) + value) & 0xFF); + } + + /** + * Subtract a value from this channel's value. Can underflow in this channel, but will not affect the other + * channels. + */ + public final int subtract(int originalARGB, @Range(from = 0, to = 0xFF) int value) { + return replace(originalARGB, (isolateAndShift(originalARGB) - value) & 0xFF); + } + } +} diff --git a/src/main/java/gregtech/api/util/GradientUtil.java b/src/main/java/gregtech/api/util/GradientUtil.java index def37c80c42..030868b778c 100644 --- a/src/main/java/gregtech/api/util/GradientUtil.java +++ b/src/main/java/gregtech/api/util/GradientUtil.java @@ -1,53 +1,40 @@ package gregtech.api.util; -import org.apache.commons.lang3.tuple.Pair; - -import java.awt.*; - public class GradientUtil { - private GradientUtil() {} - - public static Pair getGradient(Color rgb, int luminanceDifference) { + public static long getGradient(int rgb, int luminanceDifference) { float[] hsl = RGBtoHSL(rgb); - float[] upshade = new float[3]; - float[] downshade = new float[3]; - System.arraycopy(hsl, 0, upshade, 0, 3); - System.arraycopy(hsl, 0, downshade, 0, 3); - upshade[2] = upshade[2] + luminanceDifference; - if (upshade[2] > 100.0F) upshade[2] = 100.0F; - downshade[2] = downshade[2] - luminanceDifference; - if (downshade[2] < 0.0F) downshade[2] = 0.0F; - Color upshadeRgb = toRGB(upshade); - Color downshadeRgb = toRGB(downshade); - return Pair.of(downshadeRgb, upshadeRgb); - } + float[] upShade = new float[3]; + float[] downShade = new float[3]; + + System.arraycopy(hsl, 0, upShade, 0, 3); + System.arraycopy(hsl, 0, downShade, 0, 3); + + upShade[2] = Math.min(upShade[2] + luminanceDifference, 100.0F); + downShade[2] = Math.max(downShade[2] - luminanceDifference, 0.0F); - public static Pair getGradient(int rgb, int luminanceDifference) { - return getGradient(new Color(rgb), luminanceDifference); + return ColorUtil.packTwoARGB(toRGB(downShade), toRGB(upShade)); } - public static float[] RGBtoHSL(Color rgbColor) { - // Get RGB values in the range 0 - 1 - float[] rgb = rgbColor.getRGBColorComponents(null); - float r = rgb[0]; - float g = rgb[1]; - float b = rgb[2]; + public static float[] RGBtoHSL(int rgb) { + float red = ColorUtil.ARGBHelper.RED.isolateAndShiftAsFloat(rgb); + float green = ColorUtil.ARGBHelper.GREEN.isolateAndShiftAsFloat(rgb); + float blue = ColorUtil.ARGBHelper.BLUE.isolateAndShiftAsFloat(rgb); // Minimum and Maximum RGB values are used in the HSL calculations - float min = Math.min(r, Math.min(g, b)); - float max = Math.max(r, Math.max(g, b)); + float min = Math.min(red, Math.min(green, blue)); + float max = Math.max(red, Math.max(green, blue)); // Calculate the Hue float h = 0; if (max == min) { h = 0; - } else if (max == r) { - h = ((60 * (g - b) / (max - min)) + 360) % 360; - } else if (max == g) { - h = (60 * (b - r) / (max - min)) + 120; - } else if (max == b) { - h = (60 * (r - g) / (max - min)) + 240; + } else if (max == red) { + h = ((60 * (green - blue) / (max - min)) + 360) % 360; + } else if (max == green) { + h = (60 * (blue - red) / (max - min)) + 120; + } else if (max == blue) { + h = (60 * (red - green) / (max - min)) + 240; } // Calculate the Luminance @@ -66,11 +53,11 @@ public static float[] RGBtoHSL(Color rgbColor) { return new float[] { h, s * 100, l * 100 }; } - public static Color toRGB(float[] hsv) { - return toRGB(hsv[0], hsv[1], hsv[2]); + public static int toRGB(float[] hsl) { + return toRGB(hsl[0], hsl[1], hsl[2]); } - public static Color toRGB(float h, float s, float l) { + public static int toRGB(float h, float s, float l) { // Formula needs all values between 0 - 1 h = h % 360.0F; h /= 360.0F; @@ -86,15 +73,15 @@ public static Color toRGB(float h, float s, float l) { float p = 2 * l - q; - float r = Math.max(0, hueToRGB(p, q, h + (1.0F / 3.0F))); - float g = Math.max(0, hueToRGB(p, q, h)); - float b = Math.max(0, hueToRGB(p, q, h - (1.0F / 3.0F))); + float red = Math.max(0, hueToRGB(p, q, h + (1.0F / 3.0F))); + float green = Math.max(0, hueToRGB(p, q, h)); + float blue = Math.max(0, hueToRGB(p, q, h - (1.0F / 3.0F))); - r = Math.min(r, 1.0F); - g = Math.min(g, 1.0F); - b = Math.min(b, 1.0F); + red = Math.min(red, 1.0F); + green = Math.min(green, 1.0F); + blue = Math.min(blue, 1.0F); - return new Color(r, g, b); + return ColorUtil.combineRGBFullAlpha((int) red * 255, (int) green * 255, (int) blue * 255); } private static float hueToRGB(float p, float q, float h) { diff --git a/src/main/java/gregtech/client/utils/ToolChargeBarRenderer.java b/src/main/java/gregtech/client/utils/ToolChargeBarRenderer.java index b6f3c8a4adc..1b0cedee00b 100644 --- a/src/main/java/gregtech/client/utils/ToolChargeBarRenderer.java +++ b/src/main/java/gregtech/client/utils/ToolChargeBarRenderer.java @@ -7,6 +7,7 @@ import gregtech.api.items.toolitem.IGTTool; import gregtech.api.items.toolitem.ItemGTToolbelt; import gregtech.api.items.toolitem.ToolHelper; +import gregtech.api.util.ColorUtil; import gregtech.api.util.GTUtility; import net.minecraft.client.renderer.BufferBuilder; @@ -18,31 +19,30 @@ import net.minecraftforge.fml.relauncher.Side; import net.minecraftforge.fml.relauncher.SideOnly; -import org.apache.commons.lang3.tuple.Pair; +import org.jetbrains.annotations.NotNull; import org.lwjgl.opengl.GL11; -import java.awt.*; - // Thanks to EnderIO, slightly modified @SideOnly(Side.CLIENT) public final class ToolChargeBarRenderer { private static final double BAR_W = 12d; - private static final Color colorShadow = new Color(0, 0, 0, 255); - private static final Color colorBG = new Color(0x0E, 0x01, 0x16, 255); + public static final int defaultGradientLeft = ColorUtil.combineRGBFullAlpha(20, 124, 0); + public static final int defaultGradientRight = ColorUtil.combineRGBFullAlpha(115, 255, 89); + public static final long defaultGradient = ColorUtil.packTwoARGB(defaultGradientLeft, defaultGradientRight); - private static final Color colorBarLeftEnergy = new Color(0, 101, 178, 255); - private static final Color colorBarRightEnergy = new Color(217, 238, 255, 255); + private static final int colorShadow = ColorUtil.combineRGBNoAlpha(0, 0, 0); + private static final int colorBackGround = ColorUtil.combineRGBNoAlpha(14, 1, 22); - private static final Color colorBarLeftDurability = new Color(20, 124, 0, 255); - private static final Color colorBarRightDurability = new Color(115, 255, 89, 255); + private static final int colorBarLeftEnergy = ColorUtil.combineRGBFullAlpha(0, 101, 178); + private static final int colorBarRightEnergy = ColorUtil.combineRGBFullAlpha(217, 238, 255); - private static final Color colorBarLeftDepleted = new Color(122, 0, 0, 255); - private static final Color colorBarRightDepleted = new Color(255, 27, 27, 255); + private static final int colorBarLeftDepleted = ColorUtil.combineRGBFullAlpha(122, 0, 0); + private static final int colorBarRightDepleted = ColorUtil.combineRGBFullAlpha(255, 27, 27); - public static void render(double level, int xPosition, int yPosition, int offset, boolean shadow, Color left, - Color right, boolean doDepletedColor) { + public static void render(double level, int xPosition, int yPosition, int offset, boolean shadow, int left, + int right, boolean doDepletedColor) { double width = level * BAR_W; if (doDepletedColor && level <= 0.25) { left = colorBarLeftDepleted; @@ -57,13 +57,13 @@ public static void render(double level, int xPosition, int yPosition, int offset GlStateManager.disableBlend(); GlStateManager.shadeModel(GL11.GL_SMOOTH); Tessellator tessellator = Tessellator.getInstance(); - BufferBuilder worldrenderer = tessellator.getBuffer(); - worldrenderer.begin(GL11.GL_QUADS, DefaultVertexFormats.POSITION_COLOR); - drawShadow(worldrenderer, xPosition + 2, yPosition + 13 - offset, 13, shadow ? 2 : 1); - drawGrad(worldrenderer, xPosition + 2, yPosition + 13 - offset, (BAR_W + width) / 2, left, right); - drawBG(worldrenderer, xPosition + 2 + (int) BAR_W, yPosition + 13 - offset, BAR_W - width); + BufferBuilder worldRenderer = tessellator.getBuffer(); + worldRenderer.begin(GL11.GL_QUADS, DefaultVertexFormats.POSITION_COLOR); + drawShadow(worldRenderer, xPosition + 2, yPosition + 13 - offset, BAR_W + 1.0D, shadow ? 2.0D : 1.0D); + drawGradient(worldRenderer, xPosition + 2, yPosition + 13 - offset, (BAR_W + width) / 2, left, right); + drawBackGround(worldRenderer, xPosition + 2 + (int) BAR_W, yPosition + 13 - offset, BAR_W - width); if (offset == 2) { - overpaintVanillaRenderBug(worldrenderer, xPosition, yPosition); + overpaintVanillaRenderBug(worldRenderer, xPosition, yPosition); } tessellator.draw(); GlStateManager.shadeModel(GL11.GL_FLAT); @@ -73,47 +73,77 @@ public static void render(double level, int xPosition, int yPosition, int offset GlStateManager.enableDepth(); } - private static void drawGrad(BufferBuilder renderer, int x, int y, double width, Color left, Color right) { - renderer.pos(x, y, 0.0D).color(left.getRed(), left.getGreen(), left.getBlue(), left.getAlpha()).endVertex(); - renderer.pos(x, y + (double) 1, 0.0D).color(left.getRed(), left.getGreen(), left.getBlue(), left.getAlpha()) + @SuppressWarnings("DuplicatedCode") + private static void drawGradient(@NotNull BufferBuilder renderer, int x, int y, double width, int left, int right) { + int leftAlpha = ColorUtil.ARGBHelper.ALPHA.isolateAndShift(left); + int leftRed = ColorUtil.ARGBHelper.RED.isolateAndShift(left); + int leftGreen = ColorUtil.ARGBHelper.GREEN.isolateAndShift(left); + int leftBlue = ColorUtil.ARGBHelper.BLUE.isolateAndShift(left); + + int rightAlpha = ColorUtil.ARGBHelper.ALPHA.isolateAndShift(right); + int rightRed = ColorUtil.ARGBHelper.RED.isolateAndShift(right); + int rightGreen = ColorUtil.ARGBHelper.GREEN.isolateAndShift(right); + int rightBlue = ColorUtil.ARGBHelper.BLUE.isolateAndShift(right); + + renderer.pos(x, y, 0.0D) + .color(leftRed, leftGreen, leftBlue, leftAlpha) + .endVertex(); + renderer.pos(x, y + 1d, 0.0D) + .color(leftRed, leftGreen, leftBlue, leftAlpha) .endVertex(); - renderer.pos(x + width, y + (double) 1, 0.0D) - .color(right.getRed(), right.getGreen(), right.getBlue(), right.getAlpha()).endVertex(); - renderer.pos(x + width, y, 0.0D).color(right.getRed(), right.getGreen(), right.getBlue(), right.getAlpha()) + renderer.pos(x + width, y + 1d, 0.0D) + .color(rightRed, rightGreen, rightBlue, rightAlpha) + .endVertex(); + renderer.pos(x + width, y, 0.0D) + .color(rightRed, rightGreen, rightBlue, rightAlpha) .endVertex(); } - private static void drawShadow(BufferBuilder renderer, int x, int y, double width, double height) { + @SuppressWarnings("DuplicatedCode") + private static void drawShadow(@NotNull BufferBuilder renderer, int x, int y, double width, double height) { + int red = ColorUtil.ARGBHelper.RED.isolateAndShift(colorShadow); + int green = ColorUtil.ARGBHelper.GREEN.isolateAndShift(colorShadow); + int blue = ColorUtil.ARGBHelper.BLUE.isolateAndShift(colorShadow); + renderer.pos(x, y, 0.0D) - .color(colorShadow.getRed(), colorShadow.getGreen(), colorShadow.getBlue(), colorShadow.getAlpha()) + .color(red, green, blue, 255) .endVertex(); renderer.pos(x, y + height, 0.0D) - .color(colorShadow.getRed(), colorShadow.getGreen(), colorShadow.getBlue(), colorShadow.getAlpha()) + .color(red, green, blue, 255) .endVertex(); renderer.pos(x + width, y + height, 0.0D) - .color(colorShadow.getRed(), colorShadow.getGreen(), colorShadow.getBlue(), colorShadow.getAlpha()) + .color(red, green, blue, 255) .endVertex(); renderer.pos(x + width, y, 0.0D) - .color(colorShadow.getRed(), colorShadow.getGreen(), colorShadow.getBlue(), colorShadow.getAlpha()) + .color(red, green, blue, 255) .endVertex(); } - private static void drawBG(BufferBuilder renderer, int x, int y, double width) { + @SuppressWarnings("DuplicatedCode") + private static void drawBackGround(@NotNull BufferBuilder renderer, int x, int y, double width) { + int red = ColorUtil.ARGBHelper.RED.isolateAndShift(colorBackGround); + int green = ColorUtil.ARGBHelper.GREEN.isolateAndShift(colorBackGround); + int blue = ColorUtil.ARGBHelper.BLUE.isolateAndShift(colorBackGround); + renderer.pos(x - width, y, 0.0D) - .color(colorBG.getRed(), colorBG.getGreen(), colorBG.getBlue(), colorBG.getAlpha()).endVertex(); - renderer.pos(x - width, y + (double) 1, 0.0D) - .color(colorBG.getRed(), colorBG.getGreen(), colorBG.getBlue(), colorBG.getAlpha()).endVertex(); - renderer.pos(x, y + (double) 1, 0.0D) - .color(colorBG.getRed(), colorBG.getGreen(), colorBG.getBlue(), colorBG.getAlpha()).endVertex(); - renderer.pos(x, y, 0.0D).color(colorBG.getRed(), colorBG.getGreen(), colorBG.getBlue(), colorBG.getAlpha()) + .color(red, green, blue, 255) + .endVertex(); + renderer.pos(x - width, y + 1.0D, 0.0D) + .color(red, green, blue, 255) + .endVertex(); + renderer.pos(x, y + 1.0D, 0.0D) + .color(red, green, blue, 255) + .endVertex(); + renderer.pos(x, y, 0.0D) + .color(red, green, blue, 255) .endVertex(); } - private static void overpaintVanillaRenderBug(BufferBuilder worldrenderer, int xPosition, int yPosition) { - drawShadow(worldrenderer, xPosition + 2 + 12, yPosition + 13, 1, 1); + private static void overpaintVanillaRenderBug(@NotNull BufferBuilder worldRenderer, int xPosition, int yPosition) { + drawShadow(worldRenderer, xPosition + 2 + 12, yPosition + 13, 1.0D, 1.0D); } - public static void renderBarsTool(IGTTool tool, ItemStack stack, int xPosition, int yPosition) { + public static void renderBarsTool(@NotNull IGTTool tool, @NotNull ItemStack stack, int x, int y) { if (tool instanceof ItemGTToolbelt toolbelt) { ItemStack selected = toolbelt.getSelectedTool(stack); if (!selected.isEmpty() && selected.getItem() instanceof IGTTool toool) { @@ -121,19 +151,20 @@ public static void renderBarsTool(IGTTool tool, ItemStack stack, int xPosition, stack = selected; } } + boolean renderedDurability = false; NBTTagCompound tag = GTUtility.getOrCreateNbtCompound(stack); if (!tag.getBoolean(ToolHelper.UNBREAKABLE_KEY)) { - renderedDurability = renderDurabilityBar(stack.getItem().getDurabilityForDisplay(stack), xPosition, - yPosition); + renderedDurability = renderDurabilityBar(stack.getItem().getDurabilityForDisplay(stack), x, y); } + if (tool.isElectric()) { - renderElectricBar(tool.getCharge(stack), tool.getMaxCharge(stack), xPosition, yPosition, - renderedDurability); + renderElectricBar(tool.getCharge(stack), tool.getMaxCharge(stack), x, y, renderedDurability); } } - public static void renderBarsItem(MetaItem metaItem, ItemStack stack, int xPosition, int yPosition) { + public static void renderBarsItem(@NotNull MetaItem metaItem, @NotNull ItemStack stack, int xPosition, + int yPosition) { boolean renderedDurability = false; MetaItem.MetaValueItem valueItem = metaItem.getItem(stack); if (valueItem != null && valueItem.getDurabilityManager() != null) { @@ -156,23 +187,19 @@ private static void renderElectricBar(long charge, long maxCharge, int xPosition } } - private static boolean renderDurabilityBar(ItemStack stack, IItemDurabilityManager manager, int xPosition, - int yPosition) { + private static boolean renderDurabilityBar(@NotNull ItemStack stack, @NotNull IItemDurabilityManager manager, + int xPosition, int yPosition) { double level = manager.getDurabilityForDisplay(stack); - if (level == 0.0 && !manager.showEmptyBar(stack)) return false; - if (level == 1.0 && !manager.showFullBar(stack)) return false; - Pair colors = manager.getDurabilityColorsForDisplay(stack); - boolean doDepletedColor = manager.doDamagedStateColors(stack); - Color left = colors != null ? colors.getLeft() : colorBarLeftDurability; - Color right = colors != null ? colors.getRight() : colorBarRightDurability; - render(1 - level, xPosition, yPosition, 0, true, left, right, doDepletedColor); + if (level == 0.0D && !manager.showEmptyBar(stack)) return false; + if (level == 1.0D && !manager.showFullBar(stack)) return false; + long colors = manager.getDurabilityColorsForDisplay(stack); + render(1 - level, xPosition, yPosition, 0, true, ColorUtil.getLeftARGB(colors), ColorUtil.getRightARGB(colors), + manager.doDamagedStateColors(stack)); return true; } private static boolean renderDurabilityBar(double level, int xPosition, int yPosition) { - render(1 - level, xPosition, yPosition, 0, true, colorBarLeftDurability, colorBarRightDurability, true); + render(1 - level, xPosition, yPosition, 0, true, defaultGradientLeft, defaultGradientRight, true); return true; } - - private ToolChargeBarRenderer() {} } diff --git a/src/main/java/gregtech/common/items/armor/PowerlessJetpack.java b/src/main/java/gregtech/common/items/armor/PowerlessJetpack.java index 17a87b8ca16..5ca02faf97f 100644 --- a/src/main/java/gregtech/common/items/armor/PowerlessJetpack.java +++ b/src/main/java/gregtech/common/items/armor/PowerlessJetpack.java @@ -34,9 +34,7 @@ import net.minecraftforge.fml.relauncher.Side; import net.minecraftforge.fml.relauncher.SideOnly; -import org.apache.commons.lang3.tuple.Pair; import org.jetbrains.annotations.NotNull; -import org.jetbrains.annotations.Nullable; import java.awt.*; import java.util.Collections; @@ -272,11 +270,10 @@ public int getPriority() { }; public final int maxCapacity; - private final Pair durabilityBarColors; + private static final long durabilityBarColors = GradientUtil.getGradient(0xB7AF08, 10); public Behaviour(int internalCapacity) { this.maxCapacity = internalCapacity; - this.durabilityBarColors = GradientUtil.getGradient(0xB7AF08, 10); } @Override @@ -289,9 +286,8 @@ public double getDurabilityForDisplay(@NotNull ItemStack itemStack) { return fluidStack == null ? 0 : (double) fluidStack.amount / (double) fluidTankProperties.getCapacity(); } - @Nullable @Override - public Pair getDurabilityColorsForDisplay(ItemStack itemStack) { + public long getDurabilityColorsForDisplay(@NotNull ItemStack itemStack) { return durabilityBarColors; } diff --git a/src/main/java/gregtech/common/items/behaviors/AbstractMaterialPartBehavior.java b/src/main/java/gregtech/common/items/behaviors/AbstractMaterialPartBehavior.java index a4abe287529..001defb8a65 100644 --- a/src/main/java/gregtech/common/items/behaviors/AbstractMaterialPartBehavior.java +++ b/src/main/java/gregtech/common/items/behaviors/AbstractMaterialPartBehavior.java @@ -88,7 +88,7 @@ public int getItemStackColor(ItemStack itemStack, int tintIndex) { } @Override - public double getDurabilityForDisplay(ItemStack itemStack) { + public double getDurabilityForDisplay(@NotNull ItemStack itemStack) { int maxDurability = getPartMaxDurability(itemStack); return (double) (maxDurability - getPartDamage(itemStack)) / (double) maxDurability; } diff --git a/src/main/java/gregtech/common/items/behaviors/ColorSprayBehaviour.java b/src/main/java/gregtech/common/items/behaviors/ColorSprayBehaviour.java index 72d164afd19..0fac80c9a5d 100644 --- a/src/main/java/gregtech/common/items/behaviors/ColorSprayBehaviour.java +++ b/src/main/java/gregtech/common/items/behaviors/ColorSprayBehaviour.java @@ -32,7 +32,7 @@ import appeng.api.util.AEColor; import appeng.tile.networking.TileCableBus; -import org.apache.commons.lang3.tuple.Pair; +import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; import java.awt.*; @@ -42,7 +42,7 @@ public class ColorSprayBehaviour extends AbstractUsableBehaviour implements IIte private final ItemStack empty; private final EnumDyeColor color; - private final Pair durabilityBarColors; + private final long durabilityBarColors; public ColorSprayBehaviour(ItemStack empty, int totalUses, int color) { super(totalUses); @@ -223,18 +223,17 @@ public void addInformation(ItemStack itemStack, List lines) { } @Override - public double getDurabilityForDisplay(ItemStack itemStack) { + public double getDurabilityForDisplay(@NotNull ItemStack itemStack) { return (double) getUsesLeft(itemStack) / totalUses; } - @Nullable @Override - public Pair getDurabilityColorsForDisplay(ItemStack itemStack) { + public long getDurabilityColorsForDisplay(@NotNull ItemStack itemStack) { return durabilityBarColors; } @Override - public boolean doDamagedStateColors(ItemStack itemStack) { + public boolean doDamagedStateColors(@NotNull ItemStack itemStack) { return false; } } diff --git a/src/main/java/gregtech/common/items/behaviors/FoamSprayerBehavior.java b/src/main/java/gregtech/common/items/behaviors/FoamSprayerBehavior.java index 7a059220e34..89e523a8c3c 100644 --- a/src/main/java/gregtech/common/items/behaviors/FoamSprayerBehavior.java +++ b/src/main/java/gregtech/common/items/behaviors/FoamSprayerBehavior.java @@ -27,8 +27,7 @@ import net.minecraftforge.fluids.capability.IFluidTankProperties; import it.unimi.dsi.fastutil.objects.ObjectOpenHashSet; -import org.apache.commons.lang3.tuple.Pair; -import org.jetbrains.annotations.Nullable; +import org.jetbrains.annotations.NotNull; import java.awt.*; import java.util.ArrayList; @@ -41,7 +40,7 @@ public class FoamSprayerBehavior implements IItemCapabilityProvider, IItemDurabi private static final int FLUID_PER_BLOCK = 100; - private final Pair durabilityBarColors; + private final long durabilityBarColors; public FoamSprayerBehavior() { this.durabilityBarColors = GradientUtil.getGradient(Materials.ConstructionFoam.getMaterialRGB(), 10); @@ -84,7 +83,7 @@ public ActionResult onItemUse(EntityPlayer player, World world, Block } @Override - public double getDurabilityForDisplay(ItemStack itemStack) { + public double getDurabilityForDisplay(@NotNull ItemStack itemStack) { IFluidHandlerItem fluidHandlerItem = itemStack .getCapability(CapabilityFluidHandler.FLUID_HANDLER_ITEM_CAPABILITY, null); if (fluidHandlerItem == null) return 0; @@ -93,9 +92,8 @@ public double getDurabilityForDisplay(ItemStack itemStack) { return fluidStack == null ? 0 : (double) fluidStack.amount / (double) fluidTankProperties.getCapacity(); } - @Nullable @Override - public Pair getDurabilityColorsForDisplay(ItemStack itemStack) { + public long getDurabilityColorsForDisplay(@NotNull ItemStack itemStack) { return durabilityBarColors; } diff --git a/src/main/java/gregtech/common/items/behaviors/LighterBehaviour.java b/src/main/java/gregtech/common/items/behaviors/LighterBehaviour.java index 66941091218..6f8bcb12e8e 100644 --- a/src/main/java/gregtech/common/items/behaviors/LighterBehaviour.java +++ b/src/main/java/gregtech/common/items/behaviors/LighterBehaviour.java @@ -39,7 +39,6 @@ import net.minecraftforge.fluids.capability.IFluidHandlerItem; import net.minecraftforge.fluids.capability.IFluidTankProperties; -import org.apache.commons.lang3.tuple.Pair; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; @@ -50,7 +49,7 @@ public class LighterBehaviour implements IItemBehaviour, IItemDurabilityManager, private static final String LIGHTER_OPEN = "lighterOpen"; private static final String USES_LEFT = "usesLeft"; - private static final Pair DURABILITY_BAR_COLORS = GradientUtil.getGradient(0xF07F1D, 10); + private static final long DURABILITY_BAR_COLORS = GradientUtil.getGradient(0xF07F1D, 10); private final ResourceLocation overrideLocation; private final boolean usesFluid; @@ -221,7 +220,7 @@ public void addPropertyOverride(@NotNull Item item) { } @Override - public double getDurabilityForDisplay(ItemStack itemStack) { + public double getDurabilityForDisplay(@NotNull ItemStack itemStack) { if (usesFluid) { // Lighters IFluidHandlerItem fluidHandlerItem = itemStack @@ -239,28 +238,27 @@ public double getDurabilityForDisplay(ItemStack itemStack) { return 0.0; } - @Nullable @Override - public Pair getDurabilityColorsForDisplay(ItemStack itemStack) { + public long getDurabilityColorsForDisplay(@NotNull ItemStack itemStack) { if (hasMultipleUses && usesFluid) { return DURABILITY_BAR_COLORS; } // use default colors for Matchbox - return null; + return IItemDurabilityManager.super.getDurabilityColorsForDisplay(itemStack); } @Override - public boolean doDamagedStateColors(ItemStack itemStack) { + public boolean doDamagedStateColors(@NotNull ItemStack itemStack) { return hasMultipleUses && !usesFluid; // don't show for Matchbox } @Override - public boolean showEmptyBar(ItemStack itemStack) { + public boolean showEmptyBar(@NotNull ItemStack itemStack) { return hasMultipleUses; // don't show for Match } @Override - public boolean showFullBar(ItemStack itemStack) { + public boolean showFullBar(@NotNull ItemStack itemStack) { return hasMultipleUses; // don't show for Match } diff --git a/src/main/java/gregtech/mixins/minecraft/RenderItemMixin.java b/src/main/java/gregtech/mixins/minecraft/RenderItemMixin.java index a5ef0e88d21..59ed2e1993c 100644 --- a/src/main/java/gregtech/mixins/minecraft/RenderItemMixin.java +++ b/src/main/java/gregtech/mixins/minecraft/RenderItemMixin.java @@ -43,10 +43,10 @@ public void showDurabilityBarMixin(FontRenderer fr, ItemStack stack, int xPositi @Unique private static void gregTechCEu$renderElectricBar(@NotNull ItemStack stack, int xPosition, int yPosition) { - if (stack.getItem() instanceof IGTTool) { - ToolChargeBarRenderer.renderBarsTool((IGTTool) stack.getItem(), stack, xPosition, yPosition); - } else if (stack.getItem() instanceof MetaItem) { - ToolChargeBarRenderer.renderBarsItem((MetaItem) stack.getItem(), stack, xPosition, yPosition); + if (stack.getItem() instanceof IGTTool iGTTool) { + ToolChargeBarRenderer.renderBarsTool(iGTTool, stack, xPosition, yPosition); + } else if (stack.getItem() instanceof MetaItemmetaItem) { + ToolChargeBarRenderer.renderBarsItem(metaItem, stack, xPosition, yPosition); } }