From 25b22e32fd5f63b4655661f0b5d06ff1d93f2a78 Mon Sep 17 00:00:00 2001 From: KonSola5 <125081901+KonSola5@users.noreply.github.com> Date: Sun, 17 Mar 2024 21:57:00 +0100 Subject: [PATCH] Format energy and working Force Field modifier --- .../datagen/HephPlusModifierProvider.java | 1 + .../modifiers/BatteryModifier.java | 8 ++- .../modifiers/dynamic/ForceFieldModifier.java | 3 +- .../hephaestusplus/util/HephPlusUtil.java | 52 +++++++++++++++++++ .../assets/hephaestusplus/lang/en_us.json | 12 ++--- 5 files changed, 67 insertions(+), 9 deletions(-) diff --git a/src/main/java/konsola5/hephaestusplus/datagen/HephPlusModifierProvider.java b/src/main/java/konsola5/hephaestusplus/datagen/HephPlusModifierProvider.java index 688408e..9e7f1d4 100644 --- a/src/main/java/konsola5/hephaestusplus/datagen/HephPlusModifierProvider.java +++ b/src/main/java/konsola5/hephaestusplus/datagen/HephPlusModifierProvider.java @@ -29,6 +29,7 @@ public String getName() { @Override protected void addModifiers() { + // TODO: Pull off the good old Extra Utils random chance of completely breaking the tool when using Mythril. addModifier(MoarModifierIds.magically_modifiable, ExtraModifier.builder(SlotType.UPGRADE).alwaysShow().build()); addModifier(MoarModifierIds.stellar_swiftness, StatBoostModifier.builder() .multiplyBase(ToolStats.ATTACK_SPEED, 0.15f) diff --git a/src/main/java/konsola5/hephaestusplus/modifiers/BatteryModifier.java b/src/main/java/konsola5/hephaestusplus/modifiers/BatteryModifier.java index 459a635..a22227b 100644 --- a/src/main/java/konsola5/hephaestusplus/modifiers/BatteryModifier.java +++ b/src/main/java/konsola5/hephaestusplus/modifiers/BatteryModifier.java @@ -1,6 +1,7 @@ package konsola5.hephaestusplus.modifiers; import konsola5.hephaestusplus.HephaestusPlus; +import konsola5.hephaestusplus.util.HephPlusUtil; import konsola5.hephaestusplus.util.ToolEnergyCapability; import konsola5.hephaestusplus.util.ToolEnergyCapability.EnergyModifierHook; import lombok.Getter; @@ -98,8 +99,11 @@ public void addInformation(IToolStackView tool, int level, @Nullable Player play if (isOwner(tool)) { long currentEnergy = getEnergy(tool); long currentTransferRate = getTransferRate(tool); - tooltip.add(Component.translatable(ENERGY_KEY, currentEnergy, I18n.get(UNIT_KEY), getCapacity(tool), I18n.get(UNIT_KEY))); - tooltip.add(Component.translatable(TRANSFER_RATE_KEY, currentTransferRate, I18n.get(UNIT_PER_TICK_KEY))); + tooltip.add(Component.translatable(ENERGY_KEY, + HephPlusUtil.getNumberWithMagnitude(currentEnergy, I18n.get(HephPlusUtil.ENERGY)), + HephPlusUtil.getNumberWithMagnitude(getCapacity(tool), I18n.get(HephPlusUtil.ENERGY)))); + tooltip.add(Component.translatable(TRANSFER_RATE_KEY, + HephPlusUtil.getNumberWithMagnitude(currentTransferRate, I18n.get(HephPlusUtil.ENERGY_PER_TICK)))); } } diff --git a/src/main/java/konsola5/hephaestusplus/modifiers/dynamic/ForceFieldModifier.java b/src/main/java/konsola5/hephaestusplus/modifiers/dynamic/ForceFieldModifier.java index 9319a9b..e27f5ce 100644 --- a/src/main/java/konsola5/hephaestusplus/modifiers/dynamic/ForceFieldModifier.java +++ b/src/main/java/konsola5/hephaestusplus/modifiers/dynamic/ForceFieldModifier.java @@ -41,7 +41,7 @@ public ForceFieldModifier(long energyPerTry, double denominator) { public int onDamageTool(@NotNull IToolStackView tool, int level, int amount, @Nullable LivingEntity holder) { if (holder instanceof Player) { // Fail if the modifier can't extract energy - if (getEnergyUsage(level) <= getTransferRate() && !tool.isUnbreakable()) { + if (getEnergyUsage(level) <= getTransferRate(tool) && !tool.isUnbreakable()) { // Always extract energy, regardless whether durability damage was avoided or not extract(tool, getEnergyUsage(level)); // Rest of the logic like in ReinforcedModifier @@ -75,6 +75,7 @@ private long getEnergyUsage(int level) { @Override public void addInformation(IToolStackView tool, int level, @Nullable Player player, List tooltip, TooltipKey tooltipKey, TooltipFlag tooltipFlag) { + super.addInformation(tool, level, player, tooltip, tooltipKey, tooltipFlag); addPercentTooltip(FORCE_SHIELDED, getChance(level), tooltip); } diff --git a/src/main/java/konsola5/hephaestusplus/util/HephPlusUtil.java b/src/main/java/konsola5/hephaestusplus/util/HephPlusUtil.java index a753dca..55e109d 100644 --- a/src/main/java/konsola5/hephaestusplus/util/HephPlusUtil.java +++ b/src/main/java/konsola5/hephaestusplus/util/HephPlusUtil.java @@ -7,8 +7,23 @@ import net.minecraft.world.item.ItemStack; import slimeknights.tconstruct.library.tools.nbt.ToolStack; +import java.text.DecimalFormat; +import java.text.DecimalFormatSymbols; +import java.text.NumberFormat; +import java.util.Locale; +import java.util.stream.IntStream; + public class HephPlusUtil { public static final String SECONDS = HephaestusPlus.MOD_ID + ".seconds"; + public static final String ENERGY = HephaestusPlus.MOD_ID + ".energy"; + public static final String ENERGY_PER_TICK = HephaestusPlus.MOD_ID + ".energy_per_tick"; + + private static final char[] magnitude = new char[] { 'k', 'M', 'G', 'T' }; + + private static final long[] POWERS_OF_ONE_THOUSAND = IntStream.range(1, 4) + .mapToLong(i -> (long) Math.pow(1000, i)).toArray(); + + private static final long[] MAGNITUDE_THRESHOLDS = new long[] {10_000L, 1_000_000L, 1_000_000_000L, 1_000_000_000_000L}; public static boolean checkPersistentFlag(ItemStack stack, ResourceLocation flag) { CompoundTag nbt = stack.getTag(); @@ -17,4 +32,41 @@ public static boolean checkPersistentFlag(ItemStack stack, ResourceLocation flag } return false; } + + public static String getNumberWithMagnitude(double value, String units) { + String toReturn = ""; + int chosenMagnitude = -1; // -1 = no magnitude + + for (long x : MAGNITUDE_THRESHOLDS) { + if (x <= value) { + chosenMagnitude++; + } else { + break; + } + } + + if (chosenMagnitude > POWERS_OF_ONE_THOUSAND.length) { + toReturn += "∞"; + } else { + if (chosenMagnitude != -1) { + value /= POWERS_OF_ONE_THOUSAND[chosenMagnitude]; // Get the value to be joint with the magnitude + } + DecimalFormatSymbols symbols = new DecimalFormatSymbols(Locale.ROOT); // Always use dot + NumberFormat formatter = new DecimalFormat("##.##", symbols); // Round to 2 decimal places + + toReturn += formatter.format(value); + + if (chosenMagnitude != -1) { + toReturn += magnitude[chosenMagnitude]; + } + } + + if (!units.isEmpty()) { + toReturn += " " + units; + } + + return toReturn; + } + + } diff --git a/src/main/resources/assets/hephaestusplus/lang/en_us.json b/src/main/resources/assets/hephaestusplus/lang/en_us.json index 4e76ee8..2888df5 100644 --- a/src/main/resources/assets/hephaestusplus/lang/en_us.json +++ b/src/main/resources/assets/hephaestusplus/lang/en_us.json @@ -149,12 +149,10 @@ "modifier.hephaestusplus.lapotron_crystal.description": "Tool now has a battery, allowing it to store Energy", "modifier.hephaestusplus.lapotronic_energy_orb.description": "Tool now has a battery, allowing it to store Energy", "modifier.hephaestusplus.battery.description": "Tool now has a battery, allowing it to store Energy", - "_energy_comment": "Format: {current_energy} {unit} {capacity} {unit}", - "modifier.hephaestusplus.battery.energy": "Energy: %d %s / %d %s", - "_transfer_rate_comment": "Format: {transfer_rate} {unit_per_tick}", - "modifier.hephaestusplus.battery.transfer_rate": "Transfer Rate: %d %s", - "modifier.hephaestusplus.battery.unit": "E", - "modifier.hephaestusplus.battery.unit_per_tick": "E/t", + "_energy_comment": "Format: {current_energy w/ energy} {capacity w/ energy}", + "modifier.hephaestusplus.battery.energy": "Energy: %s / %s", + "_transfer_rate_comment": "Format: {transfer_rate w/ energy_per_tick}", + "modifier.hephaestusplus.battery.transfer_rate": "Transfer Rate: %s", "_comment4": "HephaestusTools Patterns", @@ -365,6 +363,8 @@ "comment_11": "Other useful strings", "hephaestusplus.seconds": "s", + "hephaestusplus.energy": "E", + "hephaestusplus.energy_per_tick": "E/t", "jade.modName.hephaestusplus": "HephaestusPlus" } \ No newline at end of file