aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/gregtech/common/tileentities/machines/multi/purification/MTEPurificationUnitBase.java
blob: 714d189fa29e31769a2232a64456912b4c12a795 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
package gregtech.common.tileentities.machines.multi.purification;

import static gregtech.api.metatileentity.BaseTileEntity.TOOLTIP_DELAY;
import static net.minecraft.util.StatCollector.translateToLocal;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.EnumSet;
import java.util.List;
import java.util.Set;
import java.util.concurrent.ThreadLocalRandom;
import java.util.stream.Stream;

import net.minecraft.block.Block;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.entity.player.EntityPlayerMP;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.util.ChatComponentText;
import net.minecraft.util.EnumChatFormatting;
import net.minecraft.world.World;
import net.minecraftforge.fluids.FluidStack;

import org.jetbrains.annotations.NotNull;

import com.gtnewhorizons.modularui.api.drawable.IDrawable;
import com.gtnewhorizons.modularui.api.drawable.UITexture;
import com.gtnewhorizons.modularui.api.math.Alignment;
import com.gtnewhorizons.modularui.api.math.Color;
import com.gtnewhorizons.modularui.api.math.Size;
import com.gtnewhorizons.modularui.api.screen.ModularWindow;
import com.gtnewhorizons.modularui.api.screen.UIBuildContext;
import com.gtnewhorizons.modularui.api.widget.Widget;
import com.gtnewhorizons.modularui.common.widget.ButtonWidget;
import com.gtnewhorizons.modularui.common.widget.FakeSyncWidget;
import com.gtnewhorizons.modularui.common.widget.MultiChildWidget;
import com.gtnewhorizons.modularui.common.widget.TextWidget;
import com.gtnewhorizons.modularui.common.widget.textfield.NumericWidget;

import gregtech.api.enums.ItemList;
import gregtech.api.enums.Materials;
import gregtech.api.enums.VoidingMode;
import gregtech.api.gui.modularui.GTUITextures;
import gregtech.api.interfaces.tileentity.IGregTechTileEntity;
import gregtech.api.metatileentity.implementations.MTEExtendedPowerMultiBlockBase;
import gregtech.api.metatileentity.implementations.MTEHatchInput;
import gregtech.api.recipe.RecipeMap;
import gregtech.api.recipe.check.CheckRecipeResult;
import gregtech.api.recipe.check.CheckRecipeResultRegistry;
import gregtech.api.recipe.metadata.PurificationPlantBaseChanceKey;
import gregtech.api.util.GTModHandler;
import gregtech.api.util.GTRecipe;
import gregtech.api.util.GTUtility;
import gregtech.common.blocks.BlockCasingsAbstract;
import mcp.mobius.waila.api.IWailaConfigHandler;
import mcp.mobius.waila.api.IWailaDataAccessor;

/**
 * Base class for purification units. This class handles all shared behaviour between units.
 * When inheriting from this, make sure to call super.loadNBTData() and super.saveNBTData()
 * if you override these methods, or linking will break.
 */
public abstract class MTEPurificationUnitBase<T extends MTEExtendedPowerMultiBlockBase<T>>
    extends MTEExtendedPowerMultiBlockBase<T> {

    /**
     * Ratio of output fluid that needs to be inserted back as input to trigger a "water boost".
     * Must be in [0, 1].
     */
    public static final float WATER_BOOST_NEEDED_FLUID = 0.1f;
    /**
     * Additive bonus to success chance when water boost is active.
     * Must be in [0, 1]
     */
    public static final float WATER_BOOST_BONUS_CHANCE = 0.15f;

    /**
     * Small internal enum to report back the various error cases when linking purification units to the
     * purification plant.
     */
    private enum LinkResult {
        /**
         * Link target was out of range of the main controller
         */
        TOO_FAR,
        /**
         * No valid GT_MetaTileEntity_PurificationPlant was found at the link target position.
         */
        NO_VALID_PLANT,
        /**
         * Link successful
         */
        SUCCESS,
    }

    /**
     * Coordinates of the main purification plant controller. These can be used to find the controller again
     * on world load.
     */
    private int controllerX, controllerY, controllerZ;

    /**
     * Whether a controller was previously set.
     */
    private boolean controllerSet = false;

    /**
     * Pointer to the main purification plant controller.
     */
    private MTEPurificationPlant controller = null;

    /**
     * The current recipe being run in the purification unit. Note that purification unit recipes are a bit special,
     * so input and output in the recipe might not exactly match the required inputs and produced outputs.
     * For more information, always look at the purification unit tooltip and implementation.
     */
    protected GTRecipe currentRecipe = null;

    /**
     * Current chance of the recipe succeeding, always in [0, 100]. A chance above 100 will be interpreted as 100.
     */
    protected float currentRecipeChance = 0.0f;

    /**
     * Configured parallel amount. Only water I/O and power scale.
     */
    protected int maxParallel = 1;

    protected int effectiveParallel = 1;

    protected ArrayList<FluidStack> storedFluids = null;

    protected MTEPurificationUnitBase(int aID, String aName, String aNameRegional) {
        super(aID, aName, aNameRegional);
    }

    protected MTEPurificationUnitBase(String aName) {
        super(aName);
    }

    @Override
    public int getMaxEfficiency(ItemStack aStack) {
        return 10000;
    }

    @Override
    public int getDamageToComponent(ItemStack aStack) {
        return 0;
    }

    @Override
    public boolean explodesOnComponentBreak(ItemStack aStack) {
        return false;
    }

    @Override
    public boolean doRandomMaintenanceDamage() {
        // The individual purification unit structures cannot have maintenance issues, so do nothing.
        return true;
    }

    @Override
    public boolean getDefaultHasMaintenanceChecks() {
        return false;
    }

    @Override
    protected void setHatchRecipeMap(MTEHatchInput hatch) {
        // Do nothing, we don't want to lock hatches to recipe maps since this can cause
        // them to reject our catalyst fluids
    }

    /**
     * Used to more easily grab a correct texture index from a block + meta.
     *
     * @param block Block to use as base. Must implement GT_Block_Casings_Abstract
     * @param meta  Metadata of the block to pick the actual block
     * @return The correct index into the global texture atlas.
     */
    protected static int getTextureIndex(Block block, int meta) {
        return ((BlockCasingsAbstract) block).getTextureIndex(meta);
    }

    @Override
    public void onPostTick(IGregTechTileEntity aBaseMetaTileEntity, long aTimer) {
        super.onPostTick(aBaseMetaTileEntity, aTimer);
        // Try to re-link to controller periodically, for example on game load.
        if (aTimer % 100 == 5 && controllerSet && getController() == null) {
            trySetControllerFromCoord(controllerX, controllerY, controllerZ);
        }
    }

    @Override
    protected void runMachine(IGregTechTileEntity aBaseMetaTileEntity, long aTick) {
        // Main controller updates progress time. We can do I/O logic here.
        // The logic for operating purification units is typically implemented by overriding this behaviour.
        if (mMaxProgresstime > 0) {
            this.markDirty();
            // Do not take maintenance into consideration, because purification units do not get
            // maintenance issues.
            // Technically, this entire efficiency stat is a bit useless for purification units, since
            // their power draw does not actually depend on it, but it's nice to keep around for consistency with other
            // multiblocks. This way, you still gradually see the efficiency go down when it powers down.
            mEfficiency = Math.max(0, Math.min(mEfficiency + mEfficiencyIncrease, getMaxEfficiency(mInventory[1])));
        }
    }

    protected CheckRecipeResult findRecipeForInputs(FluidStack[] fluidInputs, ItemStack... itemInputs) {
        RecipeMap<?> recipeMap = this.getRecipeMap();

        // Grab a stream of recipes and find the one with the highest success chance
        Stream<GTRecipe> recipes = recipeMap.findRecipeQuery()
            .fluids(fluidInputs)
            .items(itemInputs)
            .findAll();
        GTRecipe recipe = recipes
            .max(Comparator.comparing(r -> r.getMetadataOrDefault(PurificationPlantBaseChanceKey.INSTANCE, 0.0f)))
            .orElse(null);

        if (recipe == null) {
            return CheckRecipeResultRegistry.NO_RECIPE;
        }

        if (this.protectsExcessFluid() && !this.canOutputAll(recipe.mFluidOutputs)) {
            return CheckRecipeResultRegistry.FLUID_OUTPUT_FULL;
        }

        if (this.protectsExcessItem() && !this.canOutputAll(recipe.mOutputs)) {
            return CheckRecipeResultRegistry.ITEM_OUTPUT_FULL;
        }

        this.currentRecipe = recipe;
        return CheckRecipeResultRegistry.SUCCESSFUL;
    }

    /**
     * By default, only checks fluid input.
     *
     * @return
     */
    @NotNull
    @Override
    public CheckRecipeResult checkProcessing() {
        this.storedFluids = this.getStoredFluids();
        CheckRecipeResult result = overrideRecipeCheck();
        if (result == null) result = findRecipeForInputs(storedFluids.toArray(new FluidStack[] {}));

        // If we had a successful result, calculate effective parallel
        if (result.wasSuccessful()) {
            FluidStack waterInput = this.currentRecipe.mFluidInputs[0];
            // Count total available purified water input of the previous step
            long amountAvailable = 0;
            for (FluidStack fluid : this.storedFluids) {
                if (fluid.isFluidEqual(waterInput)) {
                    amountAvailable += fluid.amount;
                }
            }
            // Determine effective parallel
            effectiveParallel = (int) Math.min(maxParallel, Math.floorDiv(amountAvailable, waterInput.amount));
            // This should not happen, throw an error
            if (effectiveParallel == 0) return CheckRecipeResultRegistry.INTERNAL_ERROR;
        }

        return result;
    }

    public CheckRecipeResult overrideRecipeCheck() {
        return null;
    }

    /**
     * Equivalent to checkRecipe(), but public because the purification plant needs to access it and checkRecipe()
     * is protected.
     *
     * @return True if successfully found a recipe and/or started processing/
     */
    public boolean doPurificationRecipeCheck() {
        effectiveParallel = 1;
        return this.checkRecipe();
    }

    /**
     * Get the success chance of the recipe, from 0 to 100. Never call this while a recipe is running, because items
     * or modifiers used to boost might disappear by the time recipe check comes around,
     * which would invalidate this result.
     */
    public float calculateBoostedSuccessChance() {
        // If this.currentRecipe is null, there is a bug, so throwing a NPE is fine.
        float recipeChance = this.currentRecipe.getMetadataOrDefault(PurificationPlantBaseChanceKey.INSTANCE, 0.0f);
        // Apply water boost if available.
        if (isWaterBoosted(this.currentRecipe)) {
            recipeChance = Math.min(recipeChance + WATER_BOOST_BONUS_CHANCE * 100.0f, 100.0f);
        }
        return recipeChance;
    }

    /**
     * By default, the final recipe success chance is simply the success chance calculated on recipe check.
     * This applies water boosts when needed to the base chance. Purification units can override this to perform
     * more complex success chance calculations, that even take into account what happened during the runtime of the
     * recipe.
     *
     * @return The success chance of the recipe, at the point in time the outputs are to be produced.
     */
    public float calculateFinalSuccessChance() {
        return this.currentRecipeChance;
    }

    /**
     * Get the tier of water this unit makes. Starts at 1.
     */
    public abstract int getWaterTier();

    /**
     * Get the amount of water needed to execute a water boost, in mb.
     */
    public FluidStack getWaterBoostAmount(GTRecipe recipe) {
        // Recipes should always be constructed so that output water is always the first fluid output
        FluidStack outputWater = recipe.mFluidOutputs[0];
        int amount = Math.round(outputWater.amount * WATER_BOOST_NEEDED_FLUID * this.effectiveParallel);
        return new FluidStack(outputWater.getFluid(), amount);
    }

    /**
     * Returns true if this purification unit contains enough water to apply a water boost for the selected recipe.
     * This should only be called during recipe check! Never call this while a recipe is running, because water used to
     * boost might disappear by the time recipe check comes around, which would invalidate this result.
     *
     * @param recipe The recipe to check the water boost of
     */
    public boolean isWaterBoosted(GTRecipe recipe) {
        FluidStack inputWater = getWaterBoostAmount(recipe);
        // Simulate input drain to see if we can water boost
        return depleteInput(inputWater, true);
    }

    /**
     * Consumes all <b>fluid</b> inputs of the current recipe.
     * Should only scale the first fluid input with water
     */
    public void depleteRecipeInputs() {
        for (int i = 0; i < this.currentRecipe.mFluidInputs.length; ++i) {
            FluidStack input = this.currentRecipe.mFluidInputs[i];
            FluidStack copyWithParallel = input.copy();
            if (i == 0) {
                copyWithParallel.amount = input.amount * effectiveParallel;
            }
            this.depleteInput(copyWithParallel);
        }
    }

    /**
     * Called after a recipe is found and accepted.
     *
     * @param cycleTime    Time for a full cycle to complete
     * @param progressTime Current progress time
     */
    public void startCycle(int cycleTime, int progressTime) {
        ThreadLocalRandom random = ThreadLocalRandom.current();
        startRecipeProcessing();
        // Important to calculate this before depleting inputs, otherwise we may get issues with boost items
        // disappearing.
        this.currentRecipeChance = this.calculateBoostedSuccessChance();

        // Deplete inputs from water boost if enabled.
        if (isWaterBoosted(this.currentRecipe)) {
            FluidStack inputWater = this.getWaterBoostAmount(this.currentRecipe);
            this.depleteInput(inputWater);
        }

        // Consume inputs, only if debug mode is off
        if (!getController().debugModeOn()) {
            this.depleteRecipeInputs();
        }
        // Initialize recipe and progress information.
        this.mMaxProgresstime = cycleTime;
        this.mProgresstime = progressTime;
        this.mEfficiency = 10000;
        // These need to be set so the GUI code can display the produced outputs

        // Make sure to scale purified water output with parallel amount.
        // Make sure to make a full copy of the array, so we don't go modifying recipes
        FluidStack[] fluidOutputs = new FluidStack[this.currentRecipe.mFluidOutputs.length];
        for (int i = 0; i < this.currentRecipe.mFluidOutputs.length; ++i) {
            fluidOutputs[i] = this.currentRecipe.mFluidOutputs[i].copy();
            fluidOutputs[i].amount *= effectiveParallel;
        }

        ItemStack[] itemOutputs = new ItemStack[this.currentRecipe.mOutputs.length];

        // If this recipe has random item outputs, roll on it and add to outputs
        if (this.currentRecipe.mChances != null) {
            // Roll on each output individually
            for (int i = 0; i < this.currentRecipe.mOutputs.length; ++i) {
                // Recipes store probabilities as a value ranging from 1-10000
                int roll = random.nextInt(10000);
                if (roll <= this.currentRecipe.mChances[i]) {
                    itemOutputs[i] = this.currentRecipe.mOutputs[i].copy();
                }
            }
        } else {
            // Guaranteed item output
            for (int i = 0; i < this.currentRecipe.mOutputs.length; ++i) {
                itemOutputs[i] = this.currentRecipe.mOutputs[i].copy();
            }
        }

        this.mOutputFluids = fluidOutputs;
        this.mOutputItems = itemOutputs;
        // Set this value, so it can be displayed in Waila. Note that the logic for the units is
        // specifically overridden so setting this value does not actually drain power.
        // Instead, power is drained by the main purification plant controller.
        this.lEUt = -this.getActualPowerUsage();
        endRecipeProcessing();
    }

    public void addRecipeOutputs() {
        if (mOutputFluids != null) this.addFluidOutputs(mOutputFluids);
        if (mOutputItems != null) this.addItemOutputs(mOutputItems);
    }

    public void endCycle() {
        ThreadLocalRandom random = ThreadLocalRandom.current();

        // Only add output if debug mode was not on
        if (!getController().debugModeOn()) {
            // First see if the recipe succeeded. For some reason random.nextFloat does not compile, so we use this
            // hack instead.
            float successRoll = random.nextInt(0, 10000) / 100.0f;
            if (successRoll <= calculateFinalSuccessChance()) {
                addRecipeOutputs();
            } else {
                onRecipeFail();
            }
        }

        // Reset recipe values for next iteration
        checkRecipeResult = CheckRecipeResultRegistry.CYCLE_IDLE;
        this.mMaxProgresstime = 0;
        this.mProgresstime = 0;
        this.lEUt = 0;
        this.mEfficiency = 0;
        this.currentRecipe = null;
        this.currentRecipeChance = 0.0f;
        this.mOutputItems = null;
        this.mOutputFluids = null;
        this.effectiveParallel = 1;
    }

    /**
     * Outputs fluid when recipe fails.
     */
    private void onRecipeFail() {
        // Possibly output lower quality water.
        // Note that if there is no space for this, it will be voided regardless of fluid void setting!
        FluidStack outputWater = getDegradedOutputWater();
        this.addOutput(outputWater);
    }

    /**
     * On recipe fail, water quality may degrade to the same or lower tier. This function returns the water to output
     * in this case, or null if no water is produced at all.
     */
    private FluidStack getDegradedOutputWater() {
        ThreadLocalRandom random = ThreadLocalRandom.current();
        int roll = random.nextInt(0, 2);
        // 50% chance to not output anything at all
        if (roll == 0) return null;

        for (int waterTier = getWaterTier(); waterTier > 0; --waterTier) {
            // 50% chance every time of degrading into the previous tier
            roll = random.nextInt(0, 2);
            if (roll == 1) {
                // Rolled good, stop the loop and output water below current tier
                int amount = mOutputFluids[0].amount;
                // For tier 1, this is distilled water, so we cannot use the helper function!
                if (waterTier == 1) {
                    return GTModHandler.getDistilledWater(amount);
                }
                Materials water = PurifiedWaterHelpers.getPurifiedWaterTier(waterTier - 1);
                return water.getFluid(amount);
            }
            // Bad roll, keep looping and degrade quality even further
        }
        // Rolled bad on every iteration, no output for you
        return null;
    }

    /**
     * Get the EU/t usage of this unit while it is running.
     */
    public abstract long getBasePowerUsage();

    public long getActualPowerUsage() {
        return getBasePowerUsage() * effectiveParallel;
    }

    @Override
    public boolean checkMachine(IGregTechTileEntity aBaseMetaTileEntity, ItemStack aStack) {
        // The individual purification unit structures cannot have maintenance issues, so fix them all.
        this.mCrowbar = true;
        this.mWrench = true;
        this.mHardHammer = true;
        this.mSoftHammer = true;
        this.mSolderingTool = true;
        this.mScrewdriver = true;
        return true;
    }

    @Override
    public void loadNBTData(NBTTagCompound aNBT) {
        super.loadNBTData(aNBT);
        // If a linked controller was found, load its coordinates.
        // The unit will try to link to the real controller block periodically in onPostTick()
        // We cannot do this linking here yet because the controller block might not be loaded yet.
        // TODO: We could try though?
        if (aNBT.hasKey("controller")) {
            NBTTagCompound controllerNBT = aNBT.getCompoundTag("controller");
            controllerX = controllerNBT.getInteger("x");
            controllerY = controllerNBT.getInteger("y");
            controllerZ = controllerNBT.getInteger("z");
            controllerSet = true;
        }
        currentRecipeChance = aNBT.getFloat("currentRecipeChance");
        if (aNBT.hasKey("configuredParallel")) {
            maxParallel = aNBT.getInteger("configuredParallel");
        }
        if (aNBT.hasKey("effectiveParallel")) {
            effectiveParallel = aNBT.getInteger("effectiveParallel");
        }
    }

    public NBTTagCompound saveLinkDataToNBT() {
        NBTTagCompound controllerNBT = new NBTTagCompound();
        controllerNBT.setInteger("x", controllerX);
        controllerNBT.setInteger("y", controllerY);
        controllerNBT.setInteger("z", controllerZ);
        return controllerNBT;
    }

    @Override
    public void saveNBTData(NBTTagCompound aNBT) {
        super.saveNBTData(aNBT);
        if (controllerSet) {
            NBTTagCompound controllerNBT = saveLinkDataToNBT();
            aNBT.setTag("controller", controllerNBT);
        }
        aNBT.setFloat("currentRecipeChance", currentRecipeChance);
        aNBT.setInteger("configuredParallel", maxParallel);
        aNBT.setInteger("effectiveParallel", effectiveParallel);
    }

    private LinkResult trySetControllerFromCoord(int x, int y, int z) {
        IGregTechTileEntity ourBaseMetaTileEntity = this.getBaseMetaTileEntity();
        // First check whether the controller we try to link to is within range. The range is defined
        // as a max distance in each axis.
        if (Math.abs(ourBaseMetaTileEntity.getXCoord() - x) > MTEPurificationPlant.MAX_UNIT_DISTANCE)
            return LinkResult.TOO_FAR;
        if (Math.abs(ourBaseMetaTileEntity.getYCoord() - y) > MTEPurificationPlant.MAX_UNIT_DISTANCE)
            return LinkResult.TOO_FAR;
        if (Math.abs(ourBaseMetaTileEntity.getZCoord() - z) > MTEPurificationPlant.MAX_UNIT_DISTANCE)
            return LinkResult.TOO_FAR;

        // Find the block at the requested coordinated and check if it is a purification plant controller.
        var tileEntity = getBaseMetaTileEntity().getWorld()
            .getTileEntity(x, y, z);
        if (tileEntity == null) return LinkResult.NO_VALID_PLANT;
        if (!(tileEntity instanceof IGregTechTileEntity gtTileEntity)) return LinkResult.NO_VALID_PLANT;
        var metaTileEntity = gtTileEntity.getMetaTileEntity();
        if (!(metaTileEntity instanceof MTEPurificationPlant)) return LinkResult.NO_VALID_PLANT;

        // Before linking, unlink from current controller, so we don't end up with units linked to multiple
        // controllers.
        MTEPurificationPlant oldController = getController();
        if (oldController != null) {
            oldController.unregisterLinkedUnit(this);
            this.unlinkController();
        }

        // Now link to new controller
        controllerX = x;
        controllerY = y;
        controllerZ = z;
        controllerSet = true;
        controller = (MTEPurificationPlant) metaTileEntity;
        controller.registerLinkedUnit(this);
        return LinkResult.SUCCESS;
    }

    private boolean tryLinkDataStick(EntityPlayer aPlayer) {
        // Make sure the held item is a data stick
        ItemStack dataStick = aPlayer.inventory.getCurrentItem();
        if (!ItemList.Tool_DataStick.isStackEqual(dataStick, false, true)) {
            return false;
        }

        // Make sure this data stick is a proper purification plant link data stick.
        if (!dataStick.hasTagCompound() || !dataStick.stackTagCompound.getString("type")
            .equals("PurificationPlant")) {
            return false;
        }

        // Now read link coordinates from the data stick.
        NBTTagCompound nbt = dataStick.stackTagCompound;
        int x = nbt.getInteger("x");
        int y = nbt.getInteger("y");
        int z = nbt.getInteger("z");

        // Try to link, and report the result back to the player.
        LinkResult result = trySetControllerFromCoord(x, y, z);
        if (result == LinkResult.SUCCESS) {
            aPlayer.addChatMessage(new ChatComponentText("Link successful"));
        } else if (result == LinkResult.TOO_FAR) {
            aPlayer.addChatMessage(new ChatComponentText("Link failed: Out of range."));
        } else if (result == LinkResult.NO_VALID_PLANT) {
            aPlayer.addChatMessage(new ChatComponentText("Link failed: No Purification Plant found at link location"));
        }

        return true;
    }

    @Override
    public boolean onRightclick(IGregTechTileEntity aBaseMetaTileEntity, EntityPlayer aPlayer) {

        // Right-clicking could be a data stick linking action, so try this first.
        if (tryLinkDataStick(aPlayer)) {
            return true;
        }

        return super.onRightclick(aBaseMetaTileEntity, aPlayer);
    }

    public MTEPurificationPlant getController() {
        if (controller == null) return null;
        // Controller disappeared
        if (controller.getBaseMetaTileEntity() == null) return null;
        return controller;
    }

    // If the controller is broken this can be called to explicitly unlink the controller, so we don't have any
    // references lingering around
    public void unlinkController() {
        this.controllerSet = false;
        this.controller = null;
        this.controllerX = 0;
        this.controllerY = 0;
        this.controllerZ = 0;
    }

    @Override
    public void onBlockDestroyed() {
        // When this block is destroyed, explicitly unlink it from the controller if there is any.
        MTEPurificationPlant controller = getController();
        if (controller != null) {
            controller.unregisterLinkedUnit(this);
        }
        super.onBlockDestroyed();
    }

    @Override
    public String[] getInfoData() {
        var ret = new ArrayList<String>();
        // If this purification unit is linked to a controller, add this info to the scanner output.
        if (getController() != null) {
            ret.add(
                "This Purification Unit is linked to the Water Purification Plant at " + controllerX
                    + ", "
                    + controllerY
                    + ", "
                    + controllerZ
                    + ".");

            // If recipe is running, display success chance
            if (this.mMaxProgresstime != 0) {
                ret.add(
                    "Success chance: " + EnumChatFormatting.YELLOW
                        + GTUtility.formatNumbers(this.calculateFinalSuccessChance())
                        + "%"
                        + EnumChatFormatting.RESET);
            }

        } else ret.add("This Purification Unit is not linked to any Water Purification Plant.");
        ret.add("Current parallel: " + EnumChatFormatting.YELLOW + this.effectiveParallel);
        return ret.toArray(new String[0]);
    }

    @Override
    public void getWailaBody(ItemStack itemStack, List<String> currenttip, IWailaDataAccessor accessor,
        IWailaConfigHandler config) {
        NBTTagCompound tag = accessor.getNBTData();

        // Display linked controller in Waila.
        if (tag.getBoolean("linked")) {
            currenttip.add(
                EnumChatFormatting.AQUA + "Linked to Purification Plant at "
                    + EnumChatFormatting.WHITE
                    + tag.getInteger("controllerX")
                    + ", "
                    + tag.getInteger("controllerY")
                    + ", "
                    + tag.getInteger("controllerZ")
                    + EnumChatFormatting.RESET);
        } else {
            currenttip.add(EnumChatFormatting.AQUA + "Unlinked");
        }

        super.getWailaBody(itemStack, currenttip, accessor, config);
    }

    @Override
    public void getWailaNBTData(EntityPlayerMP player, TileEntity tile, NBTTagCompound tag, World world, int x, int y,
        int z) {

        tag.setBoolean("linked", getController() != null);
        if (getController() != null) {
            tag.setInteger("controllerX", controllerX);
            tag.setInteger("controllerY", controllerY);
            tag.setInteger("controllerZ", controllerZ);
        }

        super.getWailaNBTData(player, tile, tag, world, x, y, z);
    }

    public PurificationUnitStatus status() {
        if (!this.mMachine) {
            return PurificationUnitStatus.INCOMPLETE_STRUCTURE;
        } else if (!this.isAllowedToWork()) {
            return PurificationUnitStatus.DISABLED;
        } else {
            return PurificationUnitStatus.ONLINE;
        }
    }

    /**
     * Creates all widgets needed to sync this unit's status with the client
     */
    public Widget makeSyncerWidgets() {
        return new MultiChildWidget()
            .addChild(new FakeSyncWidget.BooleanSyncer(() -> this.mMachine, machine -> this.mMachine = machine))
            .addChild(new FakeSyncWidget.BooleanSyncer(this::isAllowedToWork, _work -> {}));
    }

    private static final int PARALLEL_WINDOW_ID = 10;

    @Override
    public void addUIWidgets(ModularWindow.Builder builder, UIBuildContext buildContext) {
        buildContext.addSyncedWindow(PARALLEL_WINDOW_ID, this::createParallelWindow);
        builder.widget(new ButtonWidget().setOnClick((clickData, widget) -> {
            if (!widget.isClient()) {
                widget.getContext()
                    .openSyncedWindow(PARALLEL_WINDOW_ID);
            }
        })
            .setPlayClickSound(true)
            .setBackground(() -> {
                List<UITexture> ret = new ArrayList<>();
                ret.add(GTUITextures.BUTTON_STANDARD);
                ret.add(GTUITextures.OVERLAY_BUTTON_BATCH_MODE_ON);
                return ret.toArray(new IDrawable[0]);
            })
            .addTooltip(translateToLocal("GT5U.tpm.parallelwindow"))
            .setTooltipShowUpDelay(TOOLTIP_DELAY)
            .setPos(174, 112)
            .setSize(16, 16));
        super.addUIWidgets(builder, buildContext);
    }

    protected ModularWindow createParallelWindow(final EntityPlayer player) {
        final int WIDTH = 158;
        final int HEIGHT = 52;
        final int PARENT_WIDTH = getGUIWidth();
        final int PARENT_HEIGHT = getGUIHeight();
        ModularWindow.Builder builder = ModularWindow.builder(WIDTH, HEIGHT);
        builder.setBackground(GTUITextures.BACKGROUND_SINGLEBLOCK_DEFAULT);
        builder.setGuiTint(getGUIColorization());
        builder.setDraggable(true);
        builder.setPos(
            (size, window) -> Alignment.Center.getAlignedPos(size, new Size(PARENT_WIDTH, PARENT_HEIGHT))
                .add(
                    Alignment.BottomRight.getAlignedPos(new Size(PARENT_WIDTH, PARENT_HEIGHT), new Size(WIDTH, HEIGHT))
                        .add(WIDTH - 3, 0)
                        .subtract(0, 10)));
        builder.widget(
            TextWidget.localised("GTPP.CC.parallel")
                .setPos(3, 4)
                .setSize(150, 20))
            .widget(
                new NumericWidget().setSetter(val -> maxParallel = (int) val)
                    .setGetter(() -> maxParallel)
                    .setBounds(1, Integer.MAX_VALUE)
                    .setDefaultValue(1)
                    .setScrollValues(1, 4, 64)
                    .setTextAlignment(Alignment.Center)
                    .setTextColor(Color.WHITE.normal)
                    .setSize(150, 18)
                    .setPos(4, 25)
                    .setBackground(GTUITextures.BACKGROUND_TEXT_FIELD)
                    .attachSyncer(
                        new FakeSyncWidget.IntegerSyncer(() -> maxParallel, (val) -> maxParallel = val),
                        builder));
        return builder.build();
    }

    @Override
    public boolean supportsVoidProtection() {
        return false;
    }

    @Override
    public Set<VoidingMode> getAllowedVoidingModes() {
        return EnumSet.of(VoidingMode.VOID_NONE);
    }

    @Override
    protected boolean supportsCraftingMEBuffer() {
        return false;
    }
}