3535
3636public class CoverDetectorEnergyAdvanced extends CoverDetectorEnergy implements CoverWithUI {
3737
38- private static final long DEFAULT_MIN_EU = 0 , DEFAULT_MAX_EU = 2048 ;
39- private static final int DEFAULT_MIN_PERCENT = 33 , DEFAULT_MAX_PERCENT = 66 ;
40-
41- public long minValue = DEFAULT_MIN_EU ;
42- public long maxValue = DEFAULT_MAX_EU ;
38+ private static final byte DEFAULT_MIN_PERCENT = 33 , DEFAULT_MAX_PERCENT = 66 ;
39+ private final byte [] percentValues = { DEFAULT_MIN_PERCENT , DEFAULT_MAX_PERCENT };
40+ private final long [] euValues ;
4341 private int outputAmount = 0 ;
4442 private boolean usePercent = false ;
4543
4644 public CoverDetectorEnergyAdvanced (@ NotNull CoverDefinition definition , @ NotNull CoverableView coverableView ,
4745 @ NotNull EnumFacing attachedSide ) {
4846 super (definition , coverableView , attachedSide );
47+ euValues = new long [] {
48+ (long ) (getCoverHolderCapacity () * (DEFAULT_MIN_PERCENT / 100f )),
49+ (long ) (getCoverHolderCapacity () * (DEFAULT_MAX_PERCENT / 100f ))
50+ };
4951 }
5052
5153 @ Override
@@ -73,14 +75,14 @@ public void update() {
7375 if (usePercent ) {
7476 if (energyCapacity > 0 ) {
7577 float ratio = (float ) storedEnergy / energyCapacity ;
76- this .outputAmount = RedstoneUtil .computeLatchedRedstoneBetweenValues (ratio * 100 , this .maxValue ,
77- this .minValue , isInverted (), this .outputAmount );
78+ this .outputAmount = RedstoneUtil .computeLatchedRedstoneBetweenValues (ratio * 100 , this .getMaxValue () ,
79+ this .getMinValue () , isInverted (), this .outputAmount );
7880 } else {
7981 this .outputAmount = isInverted () ? 0 : 15 ;
8082 }
8183 } else {
8284 this .outputAmount = RedstoneUtil .computeLatchedRedstoneBetweenValues (storedEnergy ,
83- this .maxValue , this .minValue , isInverted (), this .outputAmount );
85+ this .getMaxValue () , this .getMinValue () , isInverted (), this .outputAmount );
8486 }
8587 setRedstoneSignalOutput (outputAmount );
8688 }
@@ -138,21 +140,35 @@ private void updateWidget(GTTextFieldWidget w) {
138140 }
139141
140142 private long getMinValue () {
141- return ( minValue ) ;
143+ return isUsePercent () ? percentValues [ 0 ] : euValues [ 0 ] ;
142144 }
143145
144146 private long getMaxValue () {
145- return ( maxValue ) ;
147+ return isUsePercent () ? percentValues [ 1 ] : euValues [ 1 ] ;
146148 }
147149
148150 private void setMinValue (long val ) {
149- this .minValue = clamp (val ,
150- 0 , this .maxValue - 1 );
151+ if (isUsePercent ()) {
152+ long eu = (long ) (getCoverHolderCapacity () * (val / 100f ));
153+ euValues [0 ] = clamp (eu , 0 , euValues [1 ] - 1 );
154+ percentValues [0 ] = (byte ) clamp (val , 0 , percentValues [1 ] - 1 );
155+ } else {
156+ long p = (long ) ((float ) val / getCoverHolderCapacity () * 100 );
157+ percentValues [0 ] = (byte ) clamp (p , 0 , percentValues [1 ] - 1 );
158+ euValues [0 ] = clamp (val , 0 , euValues [1 ] - 1 );
159+ }
151160 }
152161
153162 private void setMaxValue (long val ) {
154- this .maxValue = clamp (val ,
155- this .minValue + 1 , usePercent ? 100 : Long .MAX_VALUE );
163+ if (isUsePercent ()) {
164+ long eu = (long ) (getCoverHolderCapacity () * (val / 100f ));
165+ euValues [1 ] = clamp (eu , euValues [0 ] + 1 , Integer .MAX_VALUE );
166+ percentValues [1 ] = (byte ) clamp (val , percentValues [0 ] + 1 , 100 );
167+ } else {
168+ long p = (long ) ((float ) val / getCoverHolderCapacity () * 100 );
169+ percentValues [1 ] = (byte ) clamp (p , percentValues [0 ] + 1 , 100 );
170+ euValues [1 ] = clamp (val , euValues [0 ] + 1 , Integer .MAX_VALUE );
171+ }
156172 }
157173
158174 private boolean isUsePercent () {
@@ -161,14 +177,6 @@ private boolean isUsePercent() {
161177
162178 private void setUsePercent (boolean b ) {
163179 this .usePercent = b ;
164-
165- if (this .usePercent ) { // using percent
166- this .minValue = DEFAULT_MIN_PERCENT ;
167- this .maxValue = DEFAULT_MAX_PERCENT ;
168- } else { // using discrete EU
169- this .minValue = DEFAULT_MIN_EU ;
170- this .maxValue = DEFAULT_MAX_EU ;
171- }
172180 }
173181
174182 private String getPostFix () {
@@ -182,19 +190,19 @@ private int getLength() {
182190 @ Override
183191 public void writeToNBT (@ NotNull NBTTagCompound tagCompound ) {
184192 super .writeToNBT (tagCompound );
185- tagCompound .setLong ("maxEU" , this .maxValue );
186- tagCompound .setLong ("minEU" , this .minValue );
187193 tagCompound .setInteger ("outputAmount" , this .outputAmount );
188194 tagCompound .setBoolean ("usePercent" , this .usePercent );
195+ tagCompound .setLong ("minEU" , this .getMinValue ());
196+ tagCompound .setLong ("maxEU" , this .getMaxValue ());
189197 }
190198
191199 @ Override
192200 public void readFromNBT (@ NotNull NBTTagCompound tagCompound ) {
193201 super .readFromNBT (tagCompound );
194- this .minValue = tagCompound .getLong ("minEU" );
195- this .maxValue = tagCompound .getLong ("maxEU" );
196202 this .outputAmount = tagCompound .getInteger ("outputAmount" );
197203 this .usePercent = tagCompound .getBoolean ("usePercent" );
204+ setMinValue (tagCompound .getLong ("minEU" ));
205+ setMaxValue (tagCompound .getLong ("maxEU" ));
198206
199207 readDeprecatedInvertedKeyFromNBT (tagCompound );
200208 }
@@ -210,18 +218,18 @@ private void readDeprecatedInvertedKeyFromNBT(@NotNull NBTTagCompound tagCompoun
210218 @ Override
211219 public void writeInitialSyncData (@ NotNull PacketBuffer packetBuffer ) {
212220 super .writeInitialSyncData (packetBuffer );
213- packetBuffer .writeLong (this .minValue );
214- packetBuffer .writeLong (this .maxValue );
215221 packetBuffer .writeInt (this .outputAmount );
216222 packetBuffer .writeBoolean (this .usePercent );
223+ packetBuffer .writeLong (this .getMinValue ());
224+ packetBuffer .writeLong (this .getMaxValue ());
217225 }
218226
219227 @ Override
220228 public void readInitialSyncData (@ NotNull PacketBuffer packetBuffer ) {
221229 super .readInitialSyncData (packetBuffer );
222- this .minValue = packetBuffer .readLong ();
223- this .maxValue = packetBuffer .readLong ();
224230 this .outputAmount = packetBuffer .readInt ();
225231 this .usePercent = packetBuffer .readBoolean ();
232+ setMinValue (packetBuffer .readLong ());
233+ setMaxValue (packetBuffer .readLong ());
226234 }
227235}
0 commit comments