2525import com .gregtechceu .gtceu .common .machine .multiblock .part .FluidHatchPartMachine ;
2626import com .gregtechceu .gtceu .config .ConfigHolder ;
2727import com .gregtechceu .gtceu .utils .FormattingUtil ;
28+ import com .gregtechceu .gtceu .utils .GTUtil ;
2829
2930import com .lowdragmc .lowdraglib .gui .modular .ModularUI ;
3031import com .lowdragmc .lowdraglib .gui .texture .IGuiTexture ;
@@ -95,7 +96,32 @@ public PTERBMachine(IMachineBlockEntity holder) {
9596 this .frequency = 0 ;
9697 }
9798
99+ public void explode () {
100+ long inputVoltage = 0 ;
101+ long outputVoltage = 0 ;
102+
103+ if (!localPowerInput .isEmpty ()) {
104+ EnergyContainerList localInputs = EnergyUtils .getEnergyListFromMultiParts (localPowerInput );
105+ inputVoltage = localInputs .getInputVoltage ();
106+ }
107+
108+ if (!localPowerOutput .isEmpty ()) {
109+ EnergyContainerList localOutputs = EnergyUtils .getEnergyListFromMultiParts (localPowerOutput );
110+ outputVoltage = localOutputs .getOutputVoltage ();
111+ }
112+
113+ long tier = Math .max (GTUtil .getFloorTierByVoltage (inputVoltage ), GTUtil .getFloorTierByVoltage (outputVoltage ));
114+
115+ removeWirelessEnergy ();
116+
117+ doExplosion (6f + tier );
118+ }
119+
98120 public void convertEnergyTick () {
121+ if (frequency == 0 ) {
122+ getRecipeLogic ().setStatus (RecipeLogic .Status .SUSPEND );
123+ return ;
124+ }
99125 if (isWorkingEnabled ()) {
100126 getRecipeLogic ()
101127 .setStatus (isSubscriptionActive () ? RecipeLogic .Status .WORKING : RecipeLogic .Status .SUSPEND );
@@ -113,7 +139,7 @@ public void convertEnergyTick() {
113139 false );
114140 } else {
115141 if (!ConfigHolder .INSTANCE .machines .harmlessActiveTransformers ) {
116- doExplosion ( 6.0f + getTier () );
142+ explode ( );
117143 } else {
118144 coolantTimer = 0 ;
119145 getRecipeLogic ().setStatus (RecipeLogic .Status .SUSPEND );
@@ -186,11 +212,6 @@ public boolean onWorking() {
186212 public void onStructureFormed () {
187213 super .onStructureFormed ();
188214
189- if (frequency == 0 ) {
190- getRecipeLogic ().setStatus (RecipeLogic .Status .SUSPEND );
191- return ;
192- }
193-
194215 // capture all energy containers
195216 List <IMultiPart > localPowerInput = new ArrayList <>();
196217 List <IMultiPart > localPowerOutput = new ArrayList <>();
@@ -229,11 +250,8 @@ public void onStructureFormed() {
229250
230251 this .coolantDrain = calculateCoolantDrain ();
231252
232- if (getLevel () instanceof ServerLevel serverLevel && frequency != 0 ) {
233- PTERBSavedData savedData = PTERBSavedData .getOrCreate (serverLevel );
234- savedData .addEnergyInputs (frequency , localPowerInput );
235- savedData .addEnergyOutputs (frequency , localPowerOutput );
236- savedData .saveDataToCache ();
253+ if (frequency != 0 ) {
254+ addWirelessEnergy ();
237255 }
238256
239257 converterSubscription .updateSubscription ();
@@ -269,20 +287,33 @@ public void onStructureInvalid() {
269287 coolantTimer = 0 ;
270288 if ((isWorkingEnabled () && recipeLogic .getStatus () == RecipeLogic .Status .WORKING ) &&
271289 !ConfigHolder .INSTANCE .machines .harmlessActiveTransformers ) {
272- doExplosion ( 6f + getTier () );
290+ explode ( );
273291 }
274292 super .onStructureInvalid ();
293+ removeWirelessEnergy ();
294+ this .localPowerOutput = new ArrayList <>();
295+ this .localPowerInput = new ArrayList <>();
296+ this .coolantHatchPos = null ;
297+ getRecipeLogic ().setStatus (RecipeLogic .Status .SUSPEND );
298+ converterSubscription .unsubscribe ();
299+ }
300+
301+ private void removeWirelessEnergy () {
275302 if (getLevel () instanceof ServerLevel serverLevel ) {
276303 PTERBSavedData savedData = PTERBSavedData .getOrCreate (serverLevel .getServer ().overworld ());
277304 savedData .removeEnergyInputs (frequency , localPowerInput );
278305 savedData .removeEnergyOutputs (frequency , localPowerOutput );
279306 savedData .saveDataToCache ();
280307 }
281- this .localPowerOutput = new ArrayList <>();
282- this .localPowerInput = new ArrayList <>();
283- this .coolantHatchPos = null ;
284- getRecipeLogic ().setStatus (RecipeLogic .Status .SUSPEND );
285- converterSubscription .unsubscribe ();
308+ }
309+
310+ private void addWirelessEnergy () {
311+ if (getLevel () instanceof ServerLevel serverLevel ) {
312+ PTERBSavedData savedData = PTERBSavedData .getOrCreate (serverLevel );
313+ savedData .addEnergyInputs (frequency , localPowerInput );
314+ savedData .addEnergyOutputs (frequency , localPowerOutput );
315+ savedData .saveDataToCache ();
316+ }
286317 }
287318
288319 public static TraceabilityPredicate getHatchPredicates () {
@@ -362,21 +393,13 @@ public void addDisplayText(@NotNull List<Component> textList) {
362393 }
363394
364395 public void setFrequencyFromString (String str ) {
365- if (getLevel () instanceof ServerLevel serverLevel ) {
366- PTERBSavedData savedData = PTERBSavedData .getOrCreate (serverLevel .getServer ().overworld ());
367- savedData .removeEnergyInputs (frequency , localPowerInput );
368- savedData .removeEnergyOutputs (frequency , localPowerOutput );
369- savedData .saveDataToCache ();
370- }
396+ removeWirelessEnergy ();
371397 frequency = Integer .parseInt (str );
372398 if (frequency == 0 ) {
373399 setWorkingEnabled (false );
374400 }
375- if (getLevel () instanceof ServerLevel serverLevel && frequency != 0 ) {
376- PTERBSavedData savedData = PTERBSavedData .getOrCreate (serverLevel .getServer ().overworld ());
377- savedData .addEnergyInputs (frequency , localPowerInput );
378- savedData .addEnergyOutputs (frequency , localPowerOutput );
379- savedData .saveDataToCache ();
401+ if (frequency != 0 ) {
402+ addWirelessEnergy ();
380403 }
381404 }
382405
@@ -391,16 +414,11 @@ public void setWorkingEnabled(boolean isWorkingAllowed) {
391414 return ;
392415 }
393416 super .setWorkingEnabled (isWorkingAllowed );
394- if (getLevel () instanceof ServerLevel serverLevel && frequency != 0 ) {
395- PTERBSavedData savedData = PTERBSavedData .getOrCreate (serverLevel .getServer ().overworld ());
417+ if (frequency != 0 ) {
396418 if (isWorkingAllowed ) {
397- savedData .addEnergyInputs (frequency , localPowerInput );
398- savedData .addEnergyOutputs (frequency , localPowerOutput );
399- savedData .saveDataToCache ();
419+ addWirelessEnergy ();
400420 } else {
401- savedData .removeEnergyInputs (frequency , localPowerInput );
402- savedData .removeEnergyOutputs (frequency , localPowerOutput );
403- savedData .saveDataToCache ();
421+ removeWirelessEnergy ();
404422 }
405423 }
406424 }
0 commit comments