Skip to content

Commit

Permalink
Merge display logic again for energy units, and support showing < 1 F…
Browse files Browse the repository at this point in the history
…E for if something is at 1 J
  • Loading branch information
pupnewfster committed Jul 17, 2024
1 parent a324541 commit 62277bd
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 126 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ protected void addGuiElements() {
List<Component> 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;
Expand Down
6 changes: 0 additions & 6 deletions src/main/java/mekanism/common/util/EnumUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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.
*/
Expand Down
13 changes: 1 addition & 12 deletions src/main/java/mekanism/common/util/MekanismUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

/**
Expand All @@ -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.
*
Expand Down
146 changes: 39 additions & 107 deletions src/main/java/mekanism/common/util/UnitDisplayUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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);
Expand All @@ -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) {
Expand Down Expand Up @@ -130,13 +101,13 @@ public static double roundDecimals(double d) {

private interface Unit {

String getSymbol();
Object getSymbol(boolean singular);

Check notice

Code scanning / CodeQL

Useless parameter Note

The parameter 'singular' is never used.

ILangEntry getLabel();
ILangEntry getLabel(boolean singular);
}

@NothingNullByDefault
public enum EnergyUnit implements IDisableableEnum<EnergyUnit>, IEnergyConversion {
public enum EnergyUnit implements IDisableableEnum<EnergyUnit>, IEnergyConversion, Unit {
JOULES(MekanismLang.ENERGY_JOULES, MekanismLang.ENERGY_JOULES_PLURAL, MekanismLang.ENERGY_JOULES_SHORT, "j", null, ConstantPredicates.ALWAYS_TRUE) {
@Override
protected double getConversion() {
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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;
}

Expand Down Expand Up @@ -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;
}
}
Expand Down Expand Up @@ -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));
}
}
}

0 comments on commit 62277bd

Please sign in to comment.