11package com .gregtechceu .gtceu .integration .jade .provider ;
22
33import com .gregtechceu .gtceu .GTCEu ;
4+ import com .gregtechceu .gtceu .api .GTValues ;
5+ import com .gregtechceu .gtceu .api .capability .GTCapabilityHelper ;
6+ import com .gregtechceu .gtceu .api .capability .IElectricItem ;
7+ import com .gregtechceu .gtceu .api .capability .IEnergyContainer ;
48import com .gregtechceu .gtceu .api .machine .IMachineBlockEntity ;
59import com .gregtechceu .gtceu .api .transfer .item .CustomItemStackHandler ;
610import com .gregtechceu .gtceu .common .machine .electric .BatteryBufferMachine ;
711import com .gregtechceu .gtceu .common .machine .electric .ChargerMachine ;
812
13+ import net .minecraft .client .Minecraft ;
914import net .minecraft .nbt .CompoundTag ;
15+ import net .minecraft .network .chat .Component ;
1016import net .minecraft .resources .ResourceLocation ;
17+ import net .minecraft .world .item .ItemStack ;
1118
1219import snownee .jade .api .BlockAccessor ;
1320import snownee .jade .api .IBlockComponentProvider ;
1623import snownee .jade .api .config .IPluginConfig ;
1724import snownee .jade .api .ui .IElementHelper ;
1825
26+ import java .math .BigInteger ;
27+
28+ import static com .gregtechceu .gtceu .utils .FormattingUtil .DECIMAL_FORMAT_SIC_2F ;
29+
1930public class BatteryStorageInfoProvider implements IBlockComponentProvider , IServerDataProvider <BlockAccessor > {
2031
2132 @ Override
@@ -25,26 +36,81 @@ public void appendTooltip(ITooltip iTooltip, BlockAccessor blockAccessor, IPlugi
2536 blockEntity .getMetaMachine () instanceof BatteryBufferMachine ) {
2637 CompoundTag serverData = blockAccessor .getServerData ();
2738 if (serverData .contains ("batteries" )) {
28- CustomItemStackHandler handler = new CustomItemStackHandler ();
29- handler .deserializeNBT (serverData .getCompound ("batteries" ));
30- IElementHelper helper = iTooltip .getElementHelper ();
31- for (int i = 0 ; i < handler .getSlots (); i ++) {
32- if (handler .getStackInSlot (i ).getCount () != 0 ) {
33- iTooltip .add (helper .smallItem (handler .getStackInSlot (i )));
39+ CompoundTag tag = serverData .getCompound ("batteries" );
40+ long changed = tag .getLong ("changed" ), stored = tag .getLong ("stored" ),
41+ capacity = tag .getLong ("capacity" );
42+ iTooltip .add (Component .literal ("EU/sec: " + changed ));
43+ if (changed > 0 ) {
44+ iTooltip .add (Component
45+ .literal ("Until fully charged: " + getStringRemainTime ((capacity - stored ) / changed )));
46+ } else if (changed < 0 ) {
47+ iTooltip .add (Component .literal ("Until empty " + getStringRemainTime (stored / -changed )));
48+ }
49+ if (Minecraft .getInstance ().player .isShiftKeyDown ()) {
50+ CustomItemStackHandler handler = new CustomItemStackHandler ();
51+ handler .deserializeNBT (tag .getCompound ("storage" ));
52+ IElementHelper helper = iTooltip .getElementHelper ();
53+ for (int i = 0 ; i < handler .getSlots (); i ++) {
54+ if (handler .getStackInSlot (i ).getCount () != 0 ) {
55+ ItemStack stack = handler .getStackInSlot (i );
56+ iTooltip .add (helper .smallItem (stack ));
57+ IElectricItem item = GTCapabilityHelper .getElectricItem (stack );
58+ iTooltip .append (Component .literal (
59+ GTValues .VNF [item .getTier ()] + "§r " + formatEnergy (item .getCharge (), 100000 ) +
60+ " / " + formatEnergy (item .getMaxCharge (), 100000 ) + " EU" ));
61+ }
3462 }
3563 }
3664 }
3765 }
3866 }
3967 }
4068
69+ private String getStringRemainTime (long time ) {
70+ String s = time % 60 + " sec" ;
71+ time /= 60 ;
72+ if (time > 0 ) {
73+ s = time % 60 + " minutes " + s ;
74+ time /= 60 ;
75+ if (time > 0 ) {
76+ s = time % 60 + " hours " + s ;
77+ time /= 60 ;
78+ if (time > 0 ) {
79+ s = time % 24 + " days " + s ;
80+ time /= 24 ;
81+ if (time > 0 ) {
82+ s = formatEnergy (time , 10000 ) + " years " + s ;
83+ }
84+ }
85+ }
86+ }
87+ return s ;
88+ }
89+
90+ String formatEnergy (long energy , long trueshold ) {
91+ if (energy > trueshold ) return DECIMAL_FORMAT_SIC_2F .format (BigInteger .valueOf (energy ));
92+ else return "" + energy ;
93+ }
94+
4195 @ Override
4296 public void appendServerData (CompoundTag compoundTag , BlockAccessor blockAccessor ) {
4397 if (blockAccessor .getBlockEntity () instanceof IMachineBlockEntity blockEntity ) {
4498 if (blockEntity .getMetaMachine () instanceof ChargerMachine machine ) {
45- compoundTag .put ("batteries" , machine .getChargerInventory ().serializeNBT ());
99+ CompoundTag tag = new CompoundTag ();
100+ IEnergyContainer container = machine .energyContainer ;
101+ tag .putLong ("changed" , container .getInputPerSec () - container .getOutputPerSec ());
102+ tag .putLong ("capacity" , container .getEnergyCapacity ());
103+ tag .putLong ("stored" , container .getEnergyStored ());
104+ tag .put ("storage" , machine .getChargerInventory ().serializeNBT ());
105+ compoundTag .put ("batteries" , tag );
46106 } else if (blockEntity .getMetaMachine () instanceof BatteryBufferMachine machine ) {
47- compoundTag .put ("batteries" , machine .getBatteryInventory ().serializeNBT ());
107+ CompoundTag tag = new CompoundTag ();
108+ IEnergyContainer container = machine .energyContainer ;
109+ tag .putLong ("changed" , container .getInputPerSec () - container .getOutputPerSec ());
110+ tag .putLong ("capacity" , container .getEnergyCapacity ());
111+ tag .putLong ("stored" , container .getEnergyStored ());
112+ tag .put ("storage" , machine .getBatteryInventory ().serializeNBT ());
113+ compoundTag .put ("batteries" , tag );
48114 }
49115 }
50116 }
0 commit comments