Skip to content

Commit

Permalink
Format energy and working Force Field modifier
Browse files Browse the repository at this point in the history
  • Loading branch information
KonSola5 committed Mar 17, 2024
1 parent e32dc12 commit 25b22e3
Show file tree
Hide file tree
Showing 5 changed files with 67 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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))));
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -75,6 +75,7 @@ private long getEnergyUsage(int level) {

@Override
public void addInformation(IToolStackView tool, int level, @Nullable Player player, List<Component> tooltip, TooltipKey tooltipKey, TooltipFlag tooltipFlag) {
super.addInformation(tool, level, player, tooltip, tooltipKey, tooltipFlag);
addPercentTooltip(FORCE_SHIELDED, getChance(level), tooltip);
}

Expand Down
52 changes: 52 additions & 0 deletions src/main/java/konsola5/hephaestusplus/util/HephPlusUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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;
}


}
12 changes: 6 additions & 6 deletions src/main/resources/assets/hephaestusplus/lang/en_us.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -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"
}

0 comments on commit 25b22e3

Please sign in to comment.