aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/gtPlusPlus/xmod/gregtech/common/tileentities/machines/basic/MTEAtmosphericReconditioner.java
blob: 2dde10f18735a1fb19d04208468e899acd6de87f (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
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
package gtPlusPlus.xmod.gregtech.common.tileentities.machines.basic;

import static gregtech.api.enums.GTValues.V;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;

import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.world.World;
import net.minecraft.world.chunk.Chunk;
import net.minecraftforge.common.util.ForgeDirection;

import org.apache.commons.lang3.ArrayUtils;

import com.gtnewhorizons.modularui.api.screen.ModularWindow;
import com.gtnewhorizons.modularui.api.screen.UIBuildContext;
import com.gtnewhorizons.modularui.common.widget.DrawableWidget;
import com.gtnewhorizons.modularui.common.widget.FakeSyncWidget;
import com.gtnewhorizons.modularui.common.widget.SlotWidget;

import gregtech.api.enums.Materials;
import gregtech.api.enums.SoundResource;
import gregtech.api.enums.Textures;
import gregtech.api.gui.modularui.GTUITextures;
import gregtech.api.interfaces.ITexture;
import gregtech.api.interfaces.tileentity.IGregTechTileEntity;
import gregtech.api.items.MetaGeneratedTool;
import gregtech.api.metatileentity.MetaTileEntity;
import gregtech.api.metatileentity.implementations.MTEBasicMachine;
import gregtech.api.objects.GTItemStack;
import gregtech.api.objects.GTRenderedTexture;
import gregtech.api.util.GTModHandler;
import gregtech.api.util.GTUtility;
import gregtech.common.items.IDMetaTool01;
import gregtech.common.items.MetaGeneratedTool01;
import gregtech.common.pollution.Pollution;
import gtPlusPlus.api.objects.Logger;
import gtPlusPlus.core.item.general.ItemAirFilter;
import gtPlusPlus.core.item.general.ItemBasicScrubberTurbine;
import gtPlusPlus.core.recipe.common.CI;
import gtPlusPlus.core.util.math.MathUtils;
import gtPlusPlus.xmod.gregtech.api.gui.GTPPUITextures;
import gtPlusPlus.xmod.gregtech.common.blocks.textures.TexturesGtBlock;

public class MTEAtmosphericReconditioner extends MTEBasicMachine {

    public int mPollutionReduction = 0;
    protected int mBaseEff = 2500;
    protected int mOptimalAirFlow = 0;
    protected boolean mHasPollution = false;
    protected int SLOT_ROTOR = 5;
    protected int SLOT_FILTER = 6;

    protected boolean mSaveRotor = false;

    public MTEAtmosphericReconditioner(int aID, String aName, String aNameRegional, int aTier) {
        super(
            aID,
            aName,
            aNameRegional,
            aTier,
            2,
            "Making sure you don't live in Gwalior - Uses 2A",
            3,
            0,
            new GTRenderedTexture(Textures.BlockIcons.OVERLAY_SIDE_MASSFAB_ACTIVE),
            new GTRenderedTexture(Textures.BlockIcons.OVERLAY_SIDE_MASSFAB),
            new GTRenderedTexture(TexturesGtBlock.Overlay_MatterFab_Active),
            new GTRenderedTexture(TexturesGtBlock.Overlay_MatterFab),
            new GTRenderedTexture(TexturesGtBlock.Overlay_Machine_Vent_Fast),
            new GTRenderedTexture(TexturesGtBlock.Overlay_Machine_Vent),
            new GTRenderedTexture(Textures.BlockIcons.OVERLAY_BOTTOM_MASSFAB_ACTIVE),
            new GTRenderedTexture(Textures.BlockIcons.OVERLAY_BOTTOM_MASSFAB));
    }

    public MTEAtmosphericReconditioner(String aName, int aTier, String[] aDescription, ITexture[][][] aTextures) {
        super(aName, aTier, 2, aDescription, aTextures, 2, 0);
    }

    @Override
    public MetaTileEntity newMetaEntity(IGregTechTileEntity aTileEntity) {
        return new MTEAtmosphericReconditioner(this.mName, this.mTier, this.mDescriptionArray, this.mTextures);
    }

    @Override
    public String[] getDescription() {

        boolean highTier = this.mTier >= 7;

        String[] A = ArrayUtils.addAll(
            this.mDescriptionArray,
            highTier ? "Will attempt to remove 1/4 pollution from 8 surrounding chunks" : "",
            highTier ? "If these chunks are not loaded, they will be ignored" : "",
            "Requires a turbine rotor and an Air Filter [T1/T2] to run.",
            "The turbine rotor must be manually inserted/replaced",
            "Can be configured with a soldering iron to change modes",
            "Low Efficiency: Removes half pollution, Turbine takes 50% dmg",
            "High Efficiency: Removes full pollution, Turbine takes 100% dmg",
            "Turbine Rotor will not break in LE mode",
            "Insert an equal tier Conveyor Module to enable automation");
        return A;
    }

    @Override
    public void saveNBTData(NBTTagCompound aNBT) {
        super.saveNBTData(aNBT);
        aNBT.setInteger("mOptimalAirFlow", this.mOptimalAirFlow);
        aNBT.setBoolean("mSaveRotor", mSaveRotor);
    }

    @Override
    public void loadNBTData(NBTTagCompound aNBT) {
        super.loadNBTData(aNBT);
        this.mOptimalAirFlow = aNBT.getInteger("mOptimalAirFlow");
        this.mSaveRotor = aNBT.getBoolean("mSaveRotor");
    }

    @Override
    public long maxAmperesIn() {
        return 2;
    }

    @Override
    public long getMinimumStoredEU() {
        return V[mTier] * 2;
    }

    @Override
    public long maxEUStore() {
        return V[mTier] * 256;
    }

    @Override
    public long maxEUInput() {
        return V[mTier];
    }

    @Override
    public void onPostTick(IGregTechTileEntity aBaseMetaTileEntity, long aTick) {
        super.onPostTick(aBaseMetaTileEntity, aTick);
        if (aBaseMetaTileEntity.isServerSide()) {

            // Get Current Pollution Amount.
            int mCurrentPollution = getCurrentChunkPollution();
            boolean isIdle = true;

            // Get Inventory Item
            ItemStack stackRotor = this.mInventory[SLOT_ROTOR];
            ItemStack stackFilter = this.mInventory[SLOT_FILTER];

            // Power Drain
            long drainEU = maxEUInput() * maxAmperesIn();
            if (aBaseMetaTileEntity.isActive() && aBaseMetaTileEntity.getStoredEU() >= drainEU) {
                if (aBaseMetaTileEntity.decreaseStoredEnergyUnits(drainEU, false)) {
                    isIdle = false;
                } else {
                    aBaseMetaTileEntity.setActive(false);
                    this.sendSound((byte) -122);
                }
            } else if (!aBaseMetaTileEntity.isActive() && aBaseMetaTileEntity.getStoredEU() >= drainEU / 4) {
                if (aBaseMetaTileEntity.decreaseStoredEnergyUnits((drainEU / 4), false)) {
                    isIdle = false;
                } else {
                    aBaseMetaTileEntity.setActive(false);
                    this.sendSound((byte) -122);
                }
            } else {
                aBaseMetaTileEntity.setActive(false);
                this.sendSound((byte) -122);
            }

            // Only try once/sec.
            if (!isIdle && aTick % 20L == 0L) {

                for (int i = 0; i < this.mInventory.length; i++) {
                    ItemStack aSlotContent = this.mInventory[i];
                    if (aSlotContent != null) {
                        Logger.INFO("Found " + aSlotContent.getDisplayName() + " in slot " + i);
                    }
                }

                for (int i = 0; i < this.mInventory.length; i++) {
                    if (hasRotor(this.mInventory[i])) {
                        Logger.INFO("Found Rotor in slot " + i);
                        break;
                    }
                }
                for (int i = 0; i < this.mInventory.length; i++) {
                    if (hasAirFilter(this.mInventory[i])) {
                        Logger.INFO("Found Filter in slot " + i);
                        break;
                    }
                }

                // Check if machine can work.
                if ((aBaseMetaTileEntity.isAllowedToWork())) {
                    Logger.INFO("Can work.");

                    // Enable machine animation/graphic
                    if (hasRotor(stackRotor) && hasAirFilter(stackFilter) && this.mHasPollution) {
                        if (!this.getBaseMetaTileEntity()
                            .isActive()) {
                            Logger.INFO("Set Active.");
                            aBaseMetaTileEntity.setActive(true);
                        }
                    } else if (!this.mHasPollution || mCurrentPollution <= 0
                        || stackRotor == null
                        || stackFilter == null
                        || !hasRotor(stackRotor)
                        || !hasAirFilter(stackFilter)) {
                            if (!this.getBaseMetaTileEntity()
                                .isActive()) {
                                Logger.INFO("Set Inactive.");
                                aBaseMetaTileEntity.setActive(false);
                                this.sendSound((byte) -122);
                            }
                        }

                    // If Active.
                    if (aBaseMetaTileEntity.isActive()) {
                        Logger.INFO("Doing something.");

                        // Do nothing if there is no pollution.
                        if (this.mHasPollution && mCurrentPollution > 0) {
                            Logger
                                .INFO("Has Pollution? " + mHasPollution + ", Current Pollution: " + mCurrentPollution);

                            // Use a Turbine
                            if (hasRotor(stackRotor) && hasAirFilter(stackFilter)) {
                                Logger.INFO("Found Turbine.");

                                mBaseEff = getBaseEfficiency(stackRotor);
                                mOptimalAirFlow = getOptimalAirFlow(stackRotor);

                                // Make sure we have a valid Turbine and Eff/Airflow
                                if (this.mBaseEff > 0 && this.mOptimalAirFlow > 0) {
                                    // Utils.LOG_WARNING("Pollution Cleaner [5]");

                                    // Log Debug information.
                                    Logger.INFO("mBaseEff[1]:" + mBaseEff);
                                    Logger.INFO("mOptimalAirFlow[1]:" + mOptimalAirFlow);

                                    // Calculate The Voltage we are running
                                    byte tTier = (byte) Math.max(1, GTUtility.getTier(drainEU));

                                    // Check Sides for Air,
                                    // More air means more pollution processing.
                                    int mAirSides = getFreeSpaces();

                                    int reduction = 0;

                                    // If no sides are free, how will you process the atmosphere?
                                    if (mAirSides > 0) {
                                        reduction += (((Math.max((tTier - 2), 1) * 2) * 50) * mAirSides); // Was
                                                                                                          // originally
                                                                                                          // *100
                                        Logger.INFO("mPollutionReduction[1]:" + reduction);

                                        // I stole this code
                                        reduction = (MathUtils.safeInt((long) reduction * this.mBaseEff) / 100000)
                                            * mAirSides
                                            * Math.max((tTier - 2), 1);
                                        Logger.INFO("reduction[2]:" + reduction);
                                        reduction = MathUtils.safeInt(((long) reduction / 100) * this.mOptimalAirFlow);
                                        Logger.INFO("reduction[3]:" + reduction);

                                        mPollutionReduction = reduction;

                                        // Set a temp to remove variable to aleviate duplicate code.
                                        int toRemove = 0;

                                        Logger.INFO("mCurrentPollution[4]:" + mCurrentPollution);
                                        Logger.INFO("mCurrentPollution[5]:" + reduction);

                                        toRemove = Math.min(reduction, mCurrentPollution) / 2;
                                        Logger.INFO("mCurrentPollution[6]:" + toRemove);

                                        // We are good to clean
                                        if (toRemove > 0) {
                                            if (damageTurbineRotor() && damageAirFilter()) {
                                                Logger.INFO("Removing " + toRemove + " pollution");
                                                removePollution(mSaveRotor ? (toRemove / 2) : toRemove);
                                                Logger.INFO("mNewPollution[4]:" + getCurrentChunkPollution());
                                            } else {
                                                Logger.INFO("Could not damage turbine rotor or Air Filter.");
                                                aBaseMetaTileEntity.setActive(false);
                                            }
                                        } // End of pollution removal block.
                                    } // End of valid air sides block.
                                } // End of valid toolstats block.
                            } // End of correct inventory item block.
                            else {
                                // Utils.LOG_WARNING("Wrong Tool metaitem Found.");
                            }
                        }
                    } else if (!aBaseMetaTileEntity.isActive()) {
                        return;
                    }
                } // End of can work block.
                else { // Disable Machine.
                       // aBaseMetaTileEntity.setActive(false);
                }
            } // End of 1/sec action block.
            else {

                if (hasRotor(stackRotor) && hasAirFilter(stackFilter)
                    && this.mHasPollution
                    && !isIdle
                    && aBaseMetaTileEntity.isAllowedToWork()) {
                    aBaseMetaTileEntity.setActive(true);
                } else if (isIdle || !this.mHasPollution
                    || mCurrentPollution <= 0
                    || stackRotor == null
                    || stackFilter == null
                    || !hasRotor(stackRotor)
                    || !hasAirFilter(stackFilter)) {
                        aBaseMetaTileEntity.setActive(false);
                    }
            }
            if (this.getBaseMetaTileEntity()
                .isActive()) {
                if (MathUtils.randInt(0, 5) <= 2) {
                    this.sendSound((byte) -120);
                }
            }
        } // End of is serverside block.
    }

    public int getCurrentChunkPollution() {
        int mCurrentChunkPollution = 0;
        if (this.mTier < 7) {
            mCurrentChunkPollution = Pollution.getPollution(getBaseMetaTileEntity());
        } else {
            ArrayList<Chunk> aSurrounding = new ArrayList<>();
            World aWorld = this.getBaseMetaTileEntity()
                .getWorld();
            int xPos = this.getBaseMetaTileEntity()
                .getXCoord();
            int zPos = this.getBaseMetaTileEntity()
                .getZCoord();
            Chunk a1 = aWorld.getChunkFromBlockCoords(xPos - 32, zPos - 32);
            Chunk a2 = aWorld.getChunkFromBlockCoords(xPos - 32, zPos);
            Chunk a3 = aWorld.getChunkFromBlockCoords(xPos - 32, zPos + 32);
            Chunk b1 = aWorld.getChunkFromBlockCoords(xPos, zPos - 32);
            Chunk b2 = aWorld.getChunkFromBlockCoords(xPos, zPos);
            Chunk b3 = aWorld.getChunkFromBlockCoords(xPos, zPos + 32);
            Chunk c1 = aWorld.getChunkFromBlockCoords(xPos + 32, zPos - 32);
            Chunk c2 = aWorld.getChunkFromBlockCoords(xPos + 32, zPos);
            Chunk c3 = aWorld.getChunkFromBlockCoords(xPos + 32, zPos + 32);
            aSurrounding.add(a1);
            aSurrounding.add(a2);
            aSurrounding.add(a3);
            aSurrounding.add(b1);
            aSurrounding.add(b2);
            aSurrounding.add(b3);
            aSurrounding.add(c1);
            aSurrounding.add(c2);
            aSurrounding.add(c3);
            for (Chunk r : aSurrounding) {
                mCurrentChunkPollution += getPollutionInChunk(r);
            }
        }
        mHasPollution = mCurrentChunkPollution > 0;
        return mCurrentChunkPollution;
    }

    public int getPollutionInChunk(Chunk aChunk) {
        int mCurrentChunkPollution = Pollution.getPollution(aChunk);
        mHasPollution = mCurrentChunkPollution > 0;
        return mCurrentChunkPollution;
    }

    public boolean hasRotor(ItemStack rotorStack) {
        if (rotorStack != null) {
            if (rotorStack.getItem() instanceof ItemBasicScrubberTurbine) {
                // Logger.INFO("Found Basic Turbine Rotor.");
                return true;
            } else if (rotorStack.getItem() instanceof MetaGeneratedTool && rotorStack.getItemDamage() >= 170
                && rotorStack.getItemDamage() <= 179) {
                    // Logger.INFO("Found Turbine Rotor.");
                    return true;
                } else {
                    // Logger.INFO("Found: "+rotorStack.getDisplayName()+":"+rotorStack.getItemDamage());
                }
        }
        // Logger.INFO("Found No Turbine Rotor.");
        return false;
    }

    public boolean damageTurbineRotor() {
        try {

            boolean creativeRotor = false;
            ItemStack rotorStack = this.mInventory[SLOT_ROTOR];
            if (rotorStack == null) {
                return false;
            } else if (rotorStack.getItem() instanceof ItemBasicScrubberTurbine) {
                long currentUse = ItemBasicScrubberTurbine.getFilterDamage(rotorStack);
                // Remove broken Filter
                if (rotorStack.getItemDamage() == 0 && currentUse >= 2000 - 10) {
                    Logger.INFO("Depleting ItemBasicScrubberTurbine T1");
                    this.mInventory[this.SLOT_FILTER] = null;
                    return false;
                } else if (rotorStack.getItemDamage() == 1 && currentUse >= 4000 - 10) {
                    Logger.INFO("Depleting ItemBasicScrubberTurbine T2");
                    this.mInventory[this.SLOT_FILTER] = null;
                    return false;
                } else if (rotorStack.getItemDamage() == 2 && currentUse >= 6000 - 10) {
                    Logger.INFO("Depleting ItemBasicScrubberTurbine T3");
                    this.mInventory[this.SLOT_FILTER] = null;
                    return false;
                } else {
                    // Do Damage
                    Logger.INFO("Damaging ItemBasicScrubberTurbine");
                    ItemBasicScrubberTurbine.setFilterDamage(rotorStack, currentUse + 10);
                    Logger.INFO("Rotor Damage: " + currentUse);
                    return true;
                }
            } else if (rotorStack.getItem() instanceof MetaGeneratedTool01) {
                Materials t1 = MetaGeneratedTool.getPrimaryMaterial(rotorStack);
                Materials t2 = MetaGeneratedTool.getSecondaryMaterial(rotorStack);
                if (t1 == Materials._NULL && t2 == Materials._NULL) {
                    Logger.INFO("Found creative rotor.");
                    creativeRotor = true;
                }
            } else {
                Logger.INFO("Bad item in rotor slot.");
                return false;
            }

            if (mInventory[SLOT_ROTOR].getItem() instanceof MetaGeneratedTool01
                && ((MetaGeneratedTool) mInventory[SLOT_ROTOR].getItem()).getToolStats(mInventory[SLOT_ROTOR])
                    .getSpeedMultiplier() > 0
                && MetaGeneratedTool.getPrimaryMaterial(mInventory[SLOT_ROTOR]).mToolSpeed > 0) {

                long damageValue = (long) Math
                    .floor(Math.abs(MathUtils.randFloat(1, 2) - MathUtils.randFloat(1, 3)) * (1 + 3 - 1) + 1);
                double fDam = Math
                    .floor(Math.abs(MathUtils.randFloat(1f, 2f) - MathUtils.randFloat(1f, 2f)) * (1f + 2f - 1f) + 1f);
                damageValue -= fDam;

                // Logger.INFO("Trying to do "+damageValue+" damage to the rotor. ["+fDam+"]");
                /*
                 * Materials M1 = GT_MetaGenerated_Tool.getPrimaryMaterial(this.mInventory[this.SLOT_ROTOR]); Materials
                 * M2 = GT_MetaGenerated_Tool.getSecondaryMaterial(this.mInventory[this.SLOT_ROTOR]);
                 * Logger.INFO("Trying to do "+damageValue+" damage to the rotor. [2]");
                 */

                // Damage Rotor
                // int rotorDurability = this.mInventory[this.SLOT_ROTOR].getItemDamage();
                long rotorDamage = creativeRotor ? 0
                    : MetaGeneratedTool.getToolDamage(this.mInventory[this.SLOT_ROTOR]);
                long rotorDurabilityMax = creativeRotor ? Integer.MAX_VALUE
                    : MetaGeneratedTool.getToolMaxDamage(this.mInventory[this.SLOT_ROTOR]);
                long rotorDurability = (rotorDurabilityMax - rotorDamage);
                Logger.INFO(
                    "Rotor Damage: " + rotorDamage
                        + " | Max Durability: "
                        + rotorDurabilityMax
                        + " | "
                        + " Remaining Durability: "
                        + rotorDurability);
                if (rotorDurability >= damageValue) {

                    if (!mSaveRotor) {
                        Logger.INFO("Damaging Rotor.");

                        if (!creativeRotor) GTModHandler
                            .damageOrDechargeItem(this.mInventory[this.SLOT_ROTOR], (int) damageValue, 0, null);

                        long tempDur = MetaGeneratedTool.getToolDamage(this.mInventory[this.SLOT_ROTOR]);
                        if (tempDur < rotorDurabilityMax) {
                            return true;
                        } else {
                            rotorDurability = 0;
                        }
                    } else {
                        Logger.INFO("Damaging Rotor.");
                        if (rotorDurability > 1000) {
                            if (!creativeRotor) GTModHandler
                                .damageOrDechargeItem(this.mInventory[this.SLOT_ROTOR], (int) damageValue / 2, 0, null);
                            long tempDur = MetaGeneratedTool.getToolDamage(this.mInventory[this.SLOT_ROTOR]);
                            if (tempDur < rotorDurabilityMax) {
                                return true;
                            } else {
                                rotorDurability = 0;
                            }
                        }
                    }
                }

                if (rotorDurability <= 0 && !mSaveRotor && !creativeRotor) {
                    Logger.INFO("Destroying Rotor.");
                    this.mInventory[this.SLOT_ROTOR] = null;
                    return false;
                } else if (rotorDurability <= 0 && mSaveRotor) {
                    Logger.INFO("Saving Rotor.");
                    return false;
                }

            } else {
                Logger.INFO("Bad Rotor.");
                return false;
            }
        } catch (Throwable t) {
            t.printStackTrace();
        }
        return false;
    }

    public int getFreeSpaces() {
        int mAir = 0;
        IGregTechTileEntity aBaseMetaTileEntity = this.getBaseMetaTileEntity();
        if (aBaseMetaTileEntity.getAirOffset(1, 0, 0)) {
            mAir++;
        }
        if (aBaseMetaTileEntity.getAirOffset(-1, 0, 0)) {
            mAir++;
        }
        if (aBaseMetaTileEntity.getAirOffset(0, 0, 1)) {
            mAir++;
        }
        if (aBaseMetaTileEntity.getAirOffset(0, 0, -1)) {
            mAir++;
        }
        if (aBaseMetaTileEntity.getAirOffset(0, 1, 0)) {
            mAir++;
        }
        if (aBaseMetaTileEntity.getAirOffset(0, -1, 0)) {
            mAir++;
        }
        return mAir;
    }

    public boolean removePollution(int toRemove) {

        if (this.getBaseMetaTileEntity() == null || this.getBaseMetaTileEntity()
            .getWorld() == null) {
            return false;
        }

        if (this.mTier < 7) {
            int startPollution = getCurrentChunkPollution();
            Logger.INFO("Current Chunk Pollution: " + startPollution);
            Pollution.addPollution(this.getBaseMetaTileEntity(), -toRemove);
            int after = getCurrentChunkPollution();
            Logger.INFO("Current Chunk Pollution: " + after);
            return (after < startPollution);
        } else {
            int chunksWithRemoval = 0;
            int totalRemoved = 0;
            ArrayList<Chunk> aSurrounding = new ArrayList<>();
            Chunk aThisChunk = this.getBaseMetaTileEntity()
                .getWorld()
                .getChunkFromBlockCoords(
                    this.getBaseMetaTileEntity()
                        .getXCoord(),
                    this.getBaseMetaTileEntity()
                        .getZCoord());
            int mainChunkX = aThisChunk.xPosition;
            int mainChunkZ = aThisChunk.zPosition;

            World aWorld = this.getBaseMetaTileEntity()
                .getWorld();
            int xPos = this.getBaseMetaTileEntity()
                .getXCoord();
            int zPos = this.getBaseMetaTileEntity()
                .getZCoord();

            Chunk a1 = aWorld.getChunkFromBlockCoords(xPos - 32, zPos - 32);
            Chunk a2 = aWorld.getChunkFromBlockCoords(xPos - 32, zPos);
            Chunk a3 = aWorld.getChunkFromBlockCoords(xPos - 32, zPos + 32);
            Chunk b1 = aWorld.getChunkFromBlockCoords(xPos, zPos - 32);
            Chunk b2 = aWorld.getChunkFromBlockCoords(xPos, zPos);
            Chunk b3 = aWorld.getChunkFromBlockCoords(xPos, zPos + 32);
            Chunk c1 = aWorld.getChunkFromBlockCoords(xPos + 32, zPos - 32);
            Chunk c2 = aWorld.getChunkFromBlockCoords(xPos + 32, zPos);
            Chunk c3 = aWorld.getChunkFromBlockCoords(xPos + 32, zPos + 32);

            aSurrounding.add(a1);
            aSurrounding.add(a2);
            aSurrounding.add(a3);
            aSurrounding.add(b1);
            aSurrounding.add(b2);
            aSurrounding.add(b3);
            aSurrounding.add(c1);
            aSurrounding.add(c2);
            aSurrounding.add(c3);

            for (Chunk r : aSurrounding) {
                if (!r.isChunkLoaded) {
                    continue;
                }

                int startPollution = getPollutionInChunk(r);
                if (startPollution == 0) {
                    continue;
                }

                Logger.INFO(
                    "Trying to remove pollution from chunk " + r.xPosition
                        + ", "
                        + r.zPosition
                        + " | "
                        + startPollution);
                int after = 0;
                boolean isMainChunk = r.isAtLocation(mainChunkX, mainChunkZ);

                int removal = Math.max(0, !isMainChunk ? (toRemove / 4) : toRemove);
                if (removePollution(r, removal)) {
                    chunksWithRemoval++;
                    after = getPollutionInChunk(r);
                } else {
                    after = 0;
                }
                if (startPollution - after > 0) {
                    totalRemoved += (startPollution - after);
                }
                Logger.INFO(
                    "Removed " + (startPollution - after)
                        + " pollution from chunk "
                        + r.xPosition
                        + ", "
                        + r.zPosition
                        + " | "
                        + after);
            }
            return totalRemoved > 0 && chunksWithRemoval > 0;
        }
    }

    public boolean removePollution(Chunk aChunk, int toRemove) {
        int before = getCurrentChunkPollution();
        Pollution.addPollution(aChunk, -toRemove);
        int after = getCurrentChunkPollution();
        return (after < before);
    }

    public boolean hasAirFilter(ItemStack filter) {
        if (filter == null) {
            return false;
        }
        return filter.getItem() instanceof ItemAirFilter;
    }

    public boolean damageAirFilter() {
        ItemStack filter = this.mInventory[this.SLOT_FILTER];
        if (filter == null) {
            return false;
        }

        boolean creativeRotor = false;
        ItemStack rotorStack = this.mInventory[SLOT_ROTOR];
        if (rotorStack != null) {
            if (rotorStack.getItem() instanceof MetaGeneratedTool01) {
                Materials t1 = MetaGeneratedTool.getPrimaryMaterial(rotorStack);
                Materials t2 = MetaGeneratedTool.getSecondaryMaterial(rotorStack);
                if (t1 == Materials._NULL && t2 == Materials._NULL) {
                    creativeRotor = true;
                }
            }
        }

        if (creativeRotor) {
            return true;
        }

        if (filter.getItem() instanceof ItemAirFilter) {

            long currentUse = ItemAirFilter.getFilterDamage(filter);

            // Remove broken Filter
            if (filter.getItemDamage() == 0 && currentUse >= 50 - 1) {
                this.mInventory[this.SLOT_FILTER] = null;
                return false;
            } else if (filter.getItemDamage() == 1 && currentUse >= 2500 - 1) {
                this.mInventory[this.SLOT_FILTER] = null;
                return false;
            } else {
                // Do Damage
                ItemAirFilter.setFilterDamage(filter, currentUse + 1);
                Logger.INFO("Filter Damage: " + currentUse);
                return true;
            }
        }
        return false;
    }

    @Override
    public boolean canInsertItem(int aIndex, ItemStack aStack, int ordinalSide) {
        if (aIndex == SLOT_FILTER) {
            if (aStack.getItem() instanceof ItemAirFilter) {
                Logger.INFO("Inserting Air Filter into " + aIndex);
                return true;
            }
        }
        if (aIndex == SLOT_ROTOR) {
            if (this.mInventory[7] != null) {
                Logger.INFO("Found conveyor, can automate turbines. Inserting into " + aIndex);
                if (aStack.getItem() instanceof ItemBasicScrubberTurbine) {
                    return true;
                }
                return aStack.getItem() instanceof MetaGeneratedTool && aStack.getItemDamage() >= 170
                    && aStack.getItemDamage() <= 179;
            }
        }
        return false;
    }

    @Override
    public void onScrewdriverRightClick(ForgeDirection side, EntityPlayer aPlayer, float aX, float aY, float aZ) {
        super.onScrewdriverRightClick(side, aPlayer, aX, aY, aZ);
    }

    @Override
    public void doSound(byte aIndex, double aX, double aY, double aZ) {
        if (aIndex == -120) {
            GTUtility.doSoundAtClient(SoundResource.IC2_TOOLS_BATTERY_USE, MathUtils.randInt(5, 50), 0.05F, aX, aY, aZ);
        } else {
            super.doSound((byte) 0, aX, aY, aZ);
        }
    }

    @Override
    public String[] getInfoData() {
        ArrayList<String> aTooltipSuper = new ArrayList<>(Arrays.asList(super.getInfoData()));
        int mAirSides = getFreeSpaces();
        int reduction = 0;

        try {
            long tVoltage = maxEUInput();
            byte tTier = (byte) Math.max(1, GTUtility.getTier(tVoltage));
            reduction += (((Math.max((tTier - 2), 1) * 2) * 50) * mAirSides);
            reduction = (MathUtils.safeInt((long) reduction * this.mBaseEff) / 100000) * mAirSides
                * Math.max((tTier - 2), 1);
            reduction = MathUtils.safeInt(((long) reduction / 100) * this.mOptimalAirFlow);

            aTooltipSuper.add("Maximum pollution removed per second: " + reduction);
        } catch (Throwable t) {
            aTooltipSuper.add("Maximum pollution removed per second: " + mPollutionReduction);
        }
        aTooltipSuper.add("Air Sides: " + mAirSides);

        String[] mBuiltOutput = new String[aTooltipSuper.size()];
        int aIndex = 0;
        for (String i : aTooltipSuper) {
            mBuiltOutput[aIndex++] = i;
        }

        return mBuiltOutput;
    }

    @Override
    public boolean isGivingInformation() {
        return true;
    }

    @Override
    public boolean allowCoverOnSide(ForgeDirection side, GTItemStack aCoverID) {
        if (side.offsetY != 0) {
            return false;
        }
        return super.allowCoverOnSide(side, aCoverID);
    }

    @Override
    public ITexture[] getTopFacingInactive(byte aColor) {
        return super.getTopFacingInactive(aColor);
    }

    @Override
    public void setItemNBT(NBTTagCompound aNBT) {
        if (mOptimalAirFlow > 0) aNBT.setInteger("mOptimalAirFlow", this.mOptimalAirFlow);
        if (mSaveRotor) aNBT.setBoolean("mSaveRotor", true);
        super.setItemNBT(aNBT);
    }

    private static ItemStack[] sGregTurbines;

    public static ItemStack getTieredTurbine(int aTier) {
        if (sGregTurbines == null) {
            sGregTurbines = new ItemStack[3];
            sGregTurbines[0] = MetaGeneratedTool.sInstances.get("gt.metatool.01")
                .getToolWithStats(IDMetaTool01.TURBINE_SMALL.ID, 1, Materials.Iron, Materials.Iron, null);
            sGregTurbines[1] = MetaGeneratedTool.sInstances.get("gt.metatool.01")
                .getToolWithStats(IDMetaTool01.TURBINE_SMALL.ID, 1, Materials.Bronze, Materials.Bronze, null);
            sGregTurbines[2] = MetaGeneratedTool.sInstances.get("gt.metatool.01")
                .getToolWithStats(IDMetaTool01.TURBINE_SMALL.ID, 1, Materials.Steel, Materials.Steel, null);
        } else {
            return sGregTurbines[aTier];
        }

        return null;
    }

    public int getBaseEfficiency(ItemStack aStackRotor) {
        if (aStackRotor.getItem() instanceof ItemBasicScrubberTurbine) {
            return getBaseEfficiency(getTieredTurbine(aStackRotor.getItemDamage()));
        }
        return (int) ((50.0F + (10.0F * ((MetaGeneratedTool) aStackRotor.getItem()).getToolCombatDamage(aStackRotor)))
            * 100);
    }

    public int getOptimalAirFlow(ItemStack aStackRotor) {
        if (aStackRotor.getItem() instanceof ItemBasicScrubberTurbine) {
            return getOptimalAirFlow(getTieredTurbine(aStackRotor.getItemDamage()));
        }
        return (int) Math.max(
            Float.MIN_NORMAL,
            ((MetaGeneratedTool) aStackRotor.getItem()).getToolStats(aStackRotor)
                .getSpeedMultiplier() * MetaGeneratedTool.getPrimaryMaterial(aStackRotor).mToolSpeed * 50);
    }

    @Override
    public void addUIWidgets(ModularWindow.Builder builder, UIBuildContext buildContext) {
        builder.widget(new SlotWidget(inventoryHandler, SLOT_ROTOR).setFilter(stack -> {
            if (stack.getItem() instanceof ItemBasicScrubberTurbine) {
                return true;
            }
            return stack.getItem() instanceof MetaGeneratedTool && stack.getItemDamage() >= 170
                && stack.getItemDamage() <= 179;
        })
            .setBackground(getGUITextureSet().getItemSlot(), GTPPUITextures.OVERLAY_SLOT_TURBINE)
            .setPos(52, 24))
            .widget(
                new SlotWidget(inventoryHandler, SLOT_FILTER)
                    .setFilter(stack -> stack.getItem() instanceof ItemAirFilter)
                    .setBackground(getGUITextureSet().getItemSlot(), GTUITextures.OVERLAY_SLOT_RECYCLE)
                    .setPos(106, 24))
            .widget(
                new SlotWidget(inventoryHandler, 7)
                    .setFilter(stack -> GTUtility.areStacksEqual(stack, CI.getConveyor(mTier, 1), true))
                    .setPos(124, 62));
        builder.widget(
            new DrawableWidget().setDrawable(GTUITextures.PICTURE_INFORMATION)
                .dynamicTooltip(() -> Collections.singletonList("Reduction: " + mPollutionReduction + "/s"))
                .attachSyncer(
                    new FakeSyncWidget.IntegerSyncer(() -> mPollutionReduction, val -> mPollutionReduction = val),
                    builder,
                    (widget, val) -> widget.notifyTooltipChange())
                .setPos(163, 5)
                .setSize(7, 18));
    }
}