From 62277bd4ae8037989ff8ceb6d2ebdf61fe50b50c Mon Sep 17 00:00:00 2001 From: Sara Freimer Date: Wed, 17 Jul 2024 17:23:54 -0500 Subject: [PATCH] Merge display logic again for energy units, and support showing < 1 FE for if something is at 1 J --- .../client/gui/GuiWindGenerator.java | 2 +- .../java/mekanism/common/util/EnumUtils.java | 6 - .../mekanism/common/util/MekanismUtils.java | 13 +- .../common/util/UnitDisplayUtils.java | 146 +++++------------- 4 files changed, 41 insertions(+), 126 deletions(-) diff --git a/src/generators/java/mekanism/generators/client/gui/GuiWindGenerator.java b/src/generators/java/mekanism/generators/client/gui/GuiWindGenerator.java index 3515f502c9a..ce942eded73 100644 --- a/src/generators/java/mekanism/generators/client/gui/GuiWindGenerator.java +++ b/src/generators/java/mekanism/generators/client/gui/GuiWindGenerator.java @@ -36,7 +36,7 @@ protected void addGuiElements() { List list = new ArrayList<>(); list.add(EnergyDisplay.of(tile.getEnergyContainer()).getTextComponent()); long amount = tile.getCurrentGeneration(); - list.add(GeneratorsLang.POWER.translate(MekanismUtils.convertToDisplay(amount))); + list.add(GeneratorsLang.POWER.translate(MekanismUtils.getEnergyDisplayShort(amount))); list.add(GeneratorsLang.OUTPUT_RATE_SHORT.translate(EnergyDisplay.of(tile.getMaxOutput()))); if (!tile.getActive()) { ILangEntry reason = tile.isBlacklistDimension() ? GeneratorsLang.NO_WIND : GeneratorsLang.SKY_BLOCKED; diff --git a/src/main/java/mekanism/common/util/EnumUtils.java b/src/main/java/mekanism/common/util/EnumUtils.java index 06c868a2269..28fdcbc286b 100644 --- a/src/main/java/mekanism/common/util/EnumUtils.java +++ b/src/main/java/mekanism/common/util/EnumUtils.java @@ -25,7 +25,6 @@ import mekanism.common.tier.TubeTier; import mekanism.common.tile.qio.TileEntityQIODriveArray.DriveStatus; import mekanism.common.util.UnitDisplayUtils.MeasurementUnit; -import mekanism.common.util.UnitDisplayUtils.LongMeasurementUnit; import net.minecraft.core.Direction; import net.minecraft.world.entity.EquipmentSlot; import net.minecraft.world.item.ArmorItem; @@ -67,11 +66,6 @@ private EnumUtils() { */ public static final MeasurementUnit[] MEASUREMENT_UNITS = MeasurementUnit.values(); - /** - * Cached value of {@link LongMeasurementUnit#values()}. DO NOT MODIFY THIS LIST. - */ - public static final LongMeasurementUnit[] LONG_MEASUREMENT_UNITS = LongMeasurementUnit.values(); - /** * Cached value of {@link TransmissionType#values()}. DO NOT MODIFY THIS LIST. */ diff --git a/src/main/java/mekanism/common/util/MekanismUtils.java b/src/main/java/mekanism/common/util/MekanismUtils.java index 7acce334d61..f58accc6c5c 100644 --- a/src/main/java/mekanism/common/util/MekanismUtils.java +++ b/src/main/java/mekanism/common/util/MekanismUtils.java @@ -453,7 +453,7 @@ public static long calculateUsage(long capacity) { public static Component getEnergyDisplayShort(long energy) { EnergyUnit configured = EnergyUnit.getConfigured(); - return UnitDisplayUtils.getDisplayShort(configured.convertTo(energy), configured); + return UnitDisplayUtils.getDisplayShort(configured.convertToDouble(energy), configured); } /** @@ -467,17 +467,6 @@ public static long convertToJoules(long energy) { return EnergyUnit.getConfigured().convertFrom(energy); } - /** - * Convert from joules to the unit defined in the configuration. - * - * @param energy - energy to convert - * - * @return energy converted to configured unit - */ - public static long convertToDisplay(long energy) { - return EnergyUnit.getConfigured().convertTo(energy); - } - /** * Gets a rounded energy display of a defined amount of energy. * diff --git a/src/main/java/mekanism/common/util/UnitDisplayUtils.java b/src/main/java/mekanism/common/util/UnitDisplayUtils.java index 5108f5c1140..d225ce81f2a 100644 --- a/src/main/java/mekanism/common/util/UnitDisplayUtils.java +++ b/src/main/java/mekanism/common/util/UnitDisplayUtils.java @@ -31,40 +31,8 @@ public class UnitDisplayUtils { //TODO: Maybe at some point improve on the ITextComponents the two getDisplay methods build, and have them have better translation keys with formats // That would improve how well this handles en_ud as currently the order of the number and the unit is not reversed and the unit is not upside down - /** - * Displays the unit as text. - */ - public static Component getDisplay(long value, EnergyUnit unit, int decimalPlaces, boolean isShort) { - ILangEntry label = unit.pluralLangEntry; - if (isShort) { - label = unit.shortLangEntry; - } else if (value == 1) { - label = unit.singularLangEntry; - } - if (value == 0) { - return TextComponentUtil.build(value + " ", label); - } - boolean negative = value < 0; - if (negative) { - value = Math.abs(value); - } - for (int i = 0; i < EnumUtils.LONG_MEASUREMENT_UNITS.length; i++) { - LongMeasurementUnit lowerMeasure = EnumUtils.LONG_MEASUREMENT_UNITS[i]; - if ((i == 0 && lowerMeasure.below(value)) || - i + 1 >= EnumUtils.LONG_MEASUREMENT_UNITS.length || - (lowerMeasure.aboveEqual(value) && EnumUtils.LONG_MEASUREMENT_UNITS[i + 1].below(value))) { - //First element and it is below it (no more unit abbreviations before), - // or last element (no more unit abbreviations past), - // or we are within the bounds between this one and the next one - double rounded = roundDecimals(negative, lowerMeasure.process(value), decimalPlaces); - return TextComponentUtil.build(rounded + " " + lowerMeasure.getName(isShort), label); - } - } - return TextComponentUtil.build(value, label); - } - - public static Component getDisplayShort(long value, EnergyUnit unit) { - return getDisplay(value, unit, 2, true); + public static Component getDisplayShort(double value, EnergyUnit unit) { + return getDisplayBase(value, unit, 2, true, true); } public static Component getDisplay(double temp, TemperatureUnit unit, int decimalPlaces, boolean shift, boolean isShort, boolean spaceBetweenSymbol) { @@ -74,11 +42,14 @@ public static Component getDisplay(double temp, TemperatureUnit unit, int decima private static Component getDisplayBase(double value, Unit unit, int decimalPlaces, boolean isShort, boolean spaceBetweenSymbol) { if (value == 0) { if (isShort) { - String spaceStr = spaceBetweenSymbol ? " " : ""; - return TextComponentUtil.getString(value + spaceStr + unit.getSymbol()); + if (spaceBetweenSymbol) { + return TextComponentUtil.build(value + " ", unit.getSymbol(false)); + } + return TextComponentUtil.build(value, unit.getSymbol(false)); } - return TextComponentUtil.build(value, unit.getLabel()); + return TextComponentUtil.build(value, unit.getLabel(false)); } + boolean singular = Mth.equal(value, 1); boolean negative = value < 0; if (negative) { value = Math.abs(value); @@ -91,11 +62,11 @@ private static Component getDisplayBase(double value, Unit unit, int decimalPlac //First element and it is below it (no more unit abbreviations before), // or last element (no more unit abbreviations past), // or we are within the bounds between this one and the next one - return lowerMeasure.getDisplay(value, unit, decimalPlaces, isShort, spaceBetweenSymbol, negative); + return lowerMeasure.getDisplay(value, unit, decimalPlaces, isShort, spaceBetweenSymbol, negative, singular); } } //Fallback, should never be reached as should have been captured by the check in the loop - return EnumUtils.MEASUREMENT_UNITS[EnumUtils.MEASUREMENT_UNITS.length - 1].getDisplay(value, unit, decimalPlaces, isShort, spaceBetweenSymbol, negative); + return EnumUtils.MEASUREMENT_UNITS[EnumUtils.MEASUREMENT_UNITS.length - 1].getDisplay(value, unit, decimalPlaces, isShort, spaceBetweenSymbol, negative, singular); } public static Component getDisplayShort(double value, TemperatureUnit unit) { @@ -130,13 +101,13 @@ public static double roundDecimals(double d) { private interface Unit { - String getSymbol(); + Object getSymbol(boolean singular); - ILangEntry getLabel(); + ILangEntry getLabel(boolean singular); } @NothingNullByDefault - public enum EnergyUnit implements IDisableableEnum, IEnergyConversion { + public enum EnergyUnit implements IDisableableEnum, IEnergyConversion, Unit { JOULES(MekanismLang.ENERGY_JOULES, MekanismLang.ENERGY_JOULES_PLURAL, MekanismLang.ENERGY_JOULES_SHORT, "j", null, ConstantPredicates.ALWAYS_TRUE) { @Override protected double getConversion() { @@ -185,6 +156,16 @@ public long convertTo(long joules) { this.conversion = conversionRate; } + @Override + public ILangEntry getSymbol(boolean singular) { + return shortLangEntry; + } + + @Override + public ILangEntry getLabel(boolean singular) { + return singular ? singularLangEntry : pluralLangEntry; + } + protected double getConversion() { //Note: Use default value if called before configs are loaded. In general this should never happen, // but third party mods may just call it regardless @@ -198,11 +179,15 @@ public long convertFrom(long energy) { @Override public long convertTo(long joules) { + return MathUtils.clampToLong(convertToDouble(joules)); + } + + public double convertToDouble(long joules) { if (joules == 0) { //Short circuit if energy is zero to avoid floating point math - return 0L; + return 0; } - return MathUtils.clampToLong(joules / getConversion()); + return joules / getConversion(); } @Override @@ -273,12 +258,12 @@ public double convertToK(double temp, boolean shift) { } @Override - public String getSymbol() { + public String getSymbol(boolean singular) { return symbol; } @Override - public ILangEntry getLabel() { + public ILangEntry getLabel(boolean singular) { return langEntry; } @@ -308,12 +293,12 @@ public enum RadiationUnit implements Unit { } @Override - public String getSymbol() { + public String getSymbol(boolean singular) { return symbol; } @Override - public ILangEntry getLabel() { + public ILangEntry getLabel(boolean singular) { return MekanismLang.ERROR; } } @@ -377,69 +362,16 @@ public boolean below(double d) { return d < value; } - private Component getDisplay(double value, Unit unit, int decimalPlaces, boolean isShort, boolean spaceBetweenSymbol, boolean negative) { + private Component getDisplay(double value, Unit unit, int decimalPlaces, boolean isShort, boolean spaceBetweenSymbol, boolean negative, boolean singular) { double rounded = roundDecimals(negative, process(value), decimalPlaces); String name = getName(isShort); - if (isShort) { - if (spaceBetweenSymbol) { - name = " " + name; - } - return TextComponentUtil.getString(rounded + name + unit.getSymbol()); + if (spaceBetweenSymbol || !isShort) { + name = " " + name; } - return TextComponentUtil.build(rounded + " " + name, unit.getLabel()); - } - } - - /** - * Metric system of measurement. - */ - public enum LongMeasurementUnit {//TODO: Evaluate if we should just use MeasurementUnit instead? How much does the accuracy matter - BASE("", "", 1L), - KILO("Kilo", "k", 1_000L), - MEGA("Mega", "M", 1_000_000L), - GIGA("Giga", "G", 1_000_000_000L), - TERA("Tera", "T", 1_000_000_000_000L), - PETA("Peta", "P", 1_000_000_000_000_000L), - EXA("Exa", "E", 1_000_000_000_000_000_000L); - - /** - * long name for the unit - */ - private final String name; - - /** - * short unit version of the unit - */ - private final String symbol; - - /** - * Point by which a number is considered to be of this unit - */ - private final long value; - - LongMeasurementUnit(String name, String symbol, long value) { - this.name = name; - this.symbol = symbol; - this.value = value; - } - - public String getName(boolean getShort) { - if (getShort) { - return symbol; + if (isShort) { + return TextComponentUtil.build(rounded + name, unit.getSymbol(singular)); } - return name; - } - - public double process(long d) { - return ((double) d / value); - } - - public boolean aboveEqual(long d) { - return d >= value; - } - - public boolean below(long d) { - return d < value; + return TextComponentUtil.build(rounded + name, unit.getLabel(singular)); } } } \ No newline at end of file