Skip to content

Commit

Permalink
make battery tooltips better
Browse files Browse the repository at this point in the history
  • Loading branch information
serenibyss committed Aug 21, 2021
1 parent 78e37f7 commit 835b5b9
Show file tree
Hide file tree
Showing 5 changed files with 69 additions and 9 deletions.
24 changes: 24 additions & 0 deletions src/main/java/gregtech/api/items/metaitem/ElectricStats.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package gregtech.api.items.metaitem;

import gregtech.api.GTValues;
import gregtech.api.capability.GregtechCapabilities;
import gregtech.api.capability.IElectricItem;
import gregtech.api.capability.impl.ElectricItem;
Expand All @@ -20,6 +21,8 @@
import net.minecraft.world.World;
import net.minecraftforge.common.capabilities.ICapabilityProvider;

import java.time.Duration;
import java.time.Instant;
import java.util.List;

public class ElectricStats implements IItemComponent, IItemCapabilityProvider, IItemMaxStackSizeProvider, IItemBehaviour {
Expand Down Expand Up @@ -114,10 +117,31 @@ private static void setInDischargeMode(ItemStack itemStack, boolean isDischargeM
public void addInformation(ItemStack itemStack, List<String> lines) {
IElectricItem electricItem = itemStack.getCapability(GregtechCapabilities.CAPABILITY_ELECTRIC_ITEM, null);
if (electricItem != null && electricItem.canProvideChargeExternally()) {
addTotalChargeTooltip(lines, electricItem.getMaxCharge(), electricItem.getTier());
lines.add(I18n.format("metaitem.electric.discharge_mode.tooltip"));
}
}

private static void addTotalChargeTooltip(List<String> tooltip, long maxCharge, int tier) {
Instant start = Instant.now();
Instant end = Instant.now().plusSeconds((long) ((maxCharge * 1.0) / GTValues.V[tier] / 20));
Duration duration = Duration.between(start, end);

long chargeTime;
String unit;
if (duration.getSeconds() <= 180) {
chargeTime = duration.getSeconds();
unit = "sec";
} else if (duration.toMinutes() <= 180) {
chargeTime = duration.toMinutes();
unit = "min";
} else {
chargeTime = duration.toHours();
unit = "hr";
}
tooltip.add(I18n.format("metaitem.battery.charge_time", chargeTime, unit, GTValues.VN[tier]));
}

private static boolean isInDischargeMode(ItemStack itemStack) {
NBTTagCompound tagCompound = itemStack.getTagCompound();
return tagCompound != null && tagCompound.getBoolean("DischargeMode");
Expand Down
41 changes: 37 additions & 4 deletions src/main/java/gregtech/api/items/metaitem/MetaItem.java
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,8 @@

import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import java.time.Duration;
import java.time.Instant;
import java.util.*;

/**
Expand Down Expand Up @@ -521,10 +523,14 @@ public void addInformation(@Nonnull ItemStack itemStack, @Nullable World worldIn

IElectricItem electricItem = itemStack.getCapability(GregtechCapabilities.CAPABILITY_ELECTRIC_ITEM, null);
if (electricItem != null) {
lines.add(I18n.format("metaitem.generic.electric_item.tooltip",
electricItem.getCharge(),
electricItem.getMaxCharge(),
GTValues.VN[electricItem.getTier()]));
if (electricItem.canProvideChargeExternally()) {
addDischargeItemTooltip(lines, electricItem.getMaxCharge(), electricItem.getCharge(), electricItem.getTier());
} else {
lines.add(I18n.format("metaitem.generic.electric_item.tooltip",
electricItem.getCharge(),
electricItem.getMaxCharge(),
GTValues.VN[electricItem.getTier()]));
}
}

IFluidHandlerItem fluidHandler = ItemHandlerHelper.copyStackWithSize(itemStack, 1)
Expand All @@ -545,6 +551,33 @@ public void addInformation(@Nonnull ItemStack itemStack, @Nullable World worldIn
}
}

private static void addDischargeItemTooltip(List<String> tooltip, long maxCharge, long currentCharge, int tier) {
if (currentCharge == 0) { // do not display when empty
tooltip.add(I18n.format("metaitem.generic.electric_item.tooltip", currentCharge, maxCharge, GTValues.VN[tier]));
return;
}
Instant start = Instant.now();
Instant end = Instant.now().plusSeconds((long)((currentCharge * 1.0) / GTValues.V[tier] / 20));
Duration duration = Duration.between(start, end);
double percentRemaining = currentCharge * 1.0 / maxCharge * 100; // used for color

long timeRemaining;
String unit;
if (duration.getSeconds() <= 180) {
timeRemaining = duration.getSeconds();
unit = "sec";
} else if (duration.toMinutes() <= 180) {
timeRemaining = duration.toMinutes();
unit = "min";
} else {
timeRemaining = duration.toHours();
unit = "hr";
}
tooltip.add(I18n.format("metaitem.battery.charge_detailed", currentCharge, maxCharge, GTValues.VN[tier],
percentRemaining < 30 ? 'c' : percentRemaining < 60 ? 'e' : 'a',
timeRemaining, unit));
}

@Override
public boolean hasContainerItem(@Nonnull ItemStack itemStack) {
T item = getItem(itemStack);
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/gregtech/common/items/MetaItem1.java
Original file line number Diff line number Diff line change
Expand Up @@ -542,8 +542,8 @@ public void registerSubItems() {
BATTERY_MV_CADMIUM = addItem(739, "battery.re.mv.cadmium").addComponents(ElectricStats.createRechargeableBattery(420000, GTValues.MV)).setUnificationData(OrePrefix.battery, Tier.Good).setModelAmount(8);
BATTERY_HV_CADMIUM = addItem(740, "battery.re.hv.cadmium").addComponents(ElectricStats.createRechargeableBattery(1800000, GTValues.HV)).setUnificationData(OrePrefix.battery, Tier.Advanced).setModelAmount(8);

ENERGY_CRYSTAL = addItem(741, "energy_crystal").addComponents(ElectricStats.createRechargeableBattery(4000000L, GTValues.HV)).setUnificationData(OrePrefix.battery, Tier.Advanced).setModelAmount(8).setMaxStackSize(1);
LAPOTRON_CRYSTAL = addItem(742, "lapotron_crystal").addComponents(ElectricStats.createRechargeableBattery(10000000L, GTValues.EV)).setUnificationData(OrePrefix.battery, Tier.Extreme).setModelAmount(8).setMaxStackSize(1);
ENERGY_CRYSTAL = addItem(741, "energy_crystal").addComponents(ElectricStats.createRechargeableBattery(6400000L, GTValues.HV)).setUnificationData(OrePrefix.battery, Tier.Advanced).setModelAmount(8).setMaxStackSize(1);
LAPOTRON_CRYSTAL = addItem(742, "lapotron_crystal").addComponents(ElectricStats.createRechargeableBattery(16000000L, GTValues.EV)).setUnificationData(OrePrefix.battery, Tier.Extreme).setModelAmount(8).setMaxStackSize(1);

BATTERY_EV_VANADIUM = addItem(743, "battery.ev.vanadium").addComponents(ElectricStats.createRechargeableBattery(10240000L, GTValues.EV)).setUnificationData(OrePrefix.battery, Tier.Extreme).setModelAmount(8);
BATTERY_IV_VANADIUM = addItem(744, "battery.iv.vanadium").addComponents(ElectricStats.createRechargeableBattery(40960000L, GTValues.IV)).setUnificationData(OrePrefix.battery, Tier.Elite).setModelAmount(8);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -277,9 +277,9 @@ private static void registerMixingCrystallizationRecipes() {
.buildAndRegister();

RecipeMaps.MIXER_RECIPES.recipeBuilder()
.duration(1200).EUt(16)
.input(OrePrefix.dust, Materials.Ruby, 9)
.input(OrePrefix.dust, Materials.Redstone, 9)
.duration(600).EUt(120)
.input(OrePrefix.dust, Materials.Ruby, 4)
.input(OrePrefix.dust, Materials.Redstone, 5)
.outputs(MetaItems.ENERGIUM_DUST.getStackForm(9))
.buildAndRegister();

Expand Down
3 changes: 3 additions & 0 deletions src/main/resources/assets/gregtech/lang/en_us.lang
Original file line number Diff line number Diff line change
Expand Up @@ -271,6 +271,9 @@ metaitem.battery.hull.zpm.tooltip=An empty ZPM Battery Hull
metaitem.battery.hull.uv.name=Large Naquadria Battery Hull
metaitem.battery.hull.uv.tooltip=An empty UV Battery Hull

metaitem.battery.charge_time=§aHolds %,d%s of Power
metaitem.battery.charge_detailed=%,d/%,d EU - Tier §e%s §r(§%c%,d%s remaining§r)

metaitem.battery.re.ulv.tantalum.name=Tantalum Capacitor
metaitem.battery.re.ulv.tantalum.tooltip=Reusable Battery

Expand Down

0 comments on commit 835b5b9

Please sign in to comment.