aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/goodgenerator/blocks/tileEntity/GTMetaTileEntity/MTEYOTTAHatch.java
blob: 51dbc7f19993d27edee9ab677f44c55bed24e971 (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
package goodgenerator.blocks.tileEntity.GTMetaTileEntity;

import java.math.BigInteger;
import java.util.ArrayList;
import java.util.List;

import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.util.StatCollector;
import net.minecraftforge.common.util.ForgeDirection;
import net.minecraftforge.fluids.FluidStack;
import net.minecraftforge.fluids.FluidTankInfo;

import appeng.api.config.AccessRestriction;
import appeng.api.config.Actionable;
import appeng.api.networking.GridFlags;
import appeng.api.networking.IGrid;
import appeng.api.networking.IGridNode;
import appeng.api.networking.events.MENetworkCellArrayUpdate;
import appeng.api.networking.events.MENetworkStorageEvent;
import appeng.api.networking.security.BaseActionSource;
import appeng.api.networking.security.IActionHost;
import appeng.api.networking.storage.IStorageGrid;
import appeng.api.storage.ICellContainer;
import appeng.api.storage.IMEInventory;
import appeng.api.storage.IMEInventoryHandler;
import appeng.api.storage.StorageChannel;
import appeng.api.storage.data.IAEFluidStack;
import appeng.api.storage.data.IItemList;
import appeng.api.util.AECableType;
import appeng.api.util.DimensionalCoord;
import appeng.me.helpers.AENetworkProxy;
import appeng.me.helpers.IGridProxyable;
import goodgenerator.blocks.tileEntity.MTEYottaFluidTank;
import goodgenerator.loader.Loaders;
import goodgenerator.util.StackUtils;
import gregtech.api.enums.Textures;
import gregtech.api.interfaces.IIconContainer;
import gregtech.api.interfaces.ITexture;
import gregtech.api.interfaces.metatileentity.IMetaTileEntity;
import gregtech.api.interfaces.tileentity.IGregTechTileEntity;
import gregtech.api.metatileentity.implementations.MTEHatch;
import gregtech.api.render.TextureFactory;
import gregtech.api.util.GTUtility;

public class MTEYOTTAHatch extends MTEHatch implements IGridProxyable, IActionHost, ICellContainer,
    IMEInventory<IAEFluidStack>, IMEInventoryHandler<IAEFluidStack> {

    private static final IIconContainer textureFont = new Textures.BlockIcons.CustomIcon("icons/YOTTAHatch");

    private MTEYottaFluidTank host;
    private AENetworkProxy gridProxy = null;
    private int priority;
    private byte tickRate = 20;
    private FluidStack lastFluid = null;
    private BigInteger lastAmt = BigInteger.ZERO;
    private AccessRestriction readMode = AccessRestriction.READ_WRITE;
    private final AccessRestriction[] AEModes = new AccessRestriction[] { AccessRestriction.NO_ACCESS,
        AccessRestriction.READ, AccessRestriction.WRITE, AccessRestriction.READ_WRITE };

    private static final BigInteger MAX_LONG_BIGINT = BigInteger.valueOf(Long.MAX_VALUE);

    public MTEYOTTAHatch(int aID, String aName, String aNameRegional, int aTier) {
        super(
            aID,
            aName,
            aNameRegional,
            aTier,
            0,
            new String[] { "Special I/O port for AE2FC.", "Directly connected YOTTank with AE fluid storage system.",
                "Use screwdriver to set storage priority", "Use soldering iron to set read/write mode" });
    }

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

    public void setTank(MTEYottaFluidTank te) {
        this.host = te;
    }

    @Override
    public void saveNBTData(NBTTagCompound aNBT) {
        super.saveNBTData(aNBT);
        aNBT.setInteger("mAEPriority", this.priority);
        aNBT.setInteger("mAEMode", this.readMode.ordinal());
    }

    @Override
    public void loadNBTData(NBTTagCompound aNBT) {
        super.loadNBTData(aNBT);
        this.priority = aNBT.getInteger("mAEPriority");
        this.readMode = AEModes[aNBT.getInteger("mAEMode")];
    }

    @Override
    public boolean isFacingValid(ForgeDirection facing) {
        return true;
    }

    @Override
    public boolean isAccessAllowed(EntityPlayer aPlayer) {
        return true;
    }

    @Override
    public final void onScrewdriverRightClick(ForgeDirection side, EntityPlayer aPlayer, float aX, float aY, float aZ,
        ItemStack toolStack) {
        if (aPlayer.isSneaking()) this.priority -= 10;
        else this.priority += 10;
        GTUtility
            .sendChatToPlayer(aPlayer, String.format(StatCollector.translateToLocal("yothatch.chat.0"), this.priority));
    }

    @Override
    public boolean onSolderingToolRightClick(ForgeDirection side, ForgeDirection wrenchingSide, EntityPlayer aPlayer,
        float aX, float aY, float aZ, ItemStack toolStack) {
        this.readMode = AEModes[(readMode.ordinal() + 1) % 4];
        GTUtility
            .sendChatToPlayer(aPlayer, String.format(StatCollector.translateToLocal("yothatch.chat.1"), this.readMode));
        return true;
    }

    @Override
    public IGridNode getGridNode(ForgeDirection forgeDirection) {
        AENetworkProxy gp = getProxy();
        return gp != null ? gp.getNode() : null;
    }

    @Override
    public AECableType getCableConnectionType(ForgeDirection forgeDirection) {
        return AECableType.SMART;
    }

    @Override
    public void securityBreak() {}

    @Override
    public AENetworkProxy getProxy() {
        if (gridProxy == null) {
            gridProxy = new AENetworkProxy(this, "proxy", Loaders.YFH, true);
            gridProxy.onReady();
            gridProxy.setFlags(GridFlags.REQUIRE_CHANNEL);
        }
        return this.gridProxy;
    }

    @Override
    public DimensionalCoord getLocation() {
        IGregTechTileEntity gtm = this.getBaseMetaTileEntity();
        return new DimensionalCoord(gtm.getWorld(), gtm.getXCoord(), gtm.getYCoord(), gtm.getZCoord());
    }

    @Override
    public IItemList<IAEFluidStack> getAvailableItems(IItemList<IAEFluidStack> out, int iteration) {
        if (host == null || host.getBaseMetaTileEntity() == null
            || !host.getBaseMetaTileEntity()
                .isActive())
            return out;
        if (host.mFluid == null || host.mStorageCurrent.signum() <= 0) return out;
        long ready;
        if (host.mStorageCurrent.compareTo(MAX_LONG_BIGINT) >= 0) {
            ready = Long.MAX_VALUE;
        } else ready = host.mStorageCurrent.longValue();
        out.add(StackUtils.createAEFluidStack(host.mFluid.getFluid(), ready));
        return out;
    }

    @Override
    public IAEFluidStack injectItems(IAEFluidStack input, Actionable type, BaseActionSource src) {
        long amt = fill(null, input, type.equals(Actionable.MODULATE));
        if (amt == 0) {
            return input;
        }
        input = input.copy();
        input.decStackSize(amt);
        if (input.getStackSize() <= 0) return null;
        return input;
    }

    @Override
    public IAEFluidStack extractItems(IAEFluidStack request, Actionable mode, BaseActionSource src) {
        IAEFluidStack ready = drain(null, request, false);
        if (ready != null) {
            if (mode.equals(Actionable.MODULATE)) drain(null, ready, true);
            return ready;
        } else return null;
    }

    @Override
    public StorageChannel getChannel() {
        return StorageChannel.FLUIDS;
    }

    @Override
    public void onFirstTick(IGregTechTileEntity aBaseMetaTileEntity) {
        super.onFirstTick(aBaseMetaTileEntity);
        getProxy();
    }

    @Override
    public void onPostTick(IGregTechTileEntity aBaseMetaTileEntity, long aTick) {
        if (shouldTick(aTick)) {
            if (isChanged()) {
                IGridNode node = getGridNode(null);
                if (node != null) {
                    IGrid grid = node.getGrid();
                    if (grid != null) {
                        grid.postEvent(new MENetworkCellArrayUpdate());
                        IStorageGrid storageGrid = grid.getCache(IStorageGrid.class);
                        if (storageGrid == null) {
                            node.getGrid()
                                .postEvent(new MENetworkStorageEvent(null, StorageChannel.FLUIDS));
                        } else {
                            node.getGrid()
                                .postEvent(
                                    new MENetworkStorageEvent(storageGrid.getFluidInventory(), StorageChannel.FLUIDS));
                        }
                        node.getGrid()
                            .postEvent(new MENetworkCellArrayUpdate());
                    }
                }
                faster();
                update();
            } else {
                slower();
            }
        }
        super.onPostTick(aBaseMetaTileEntity, aTick);
    }

    @Override
    public int getCapacity() {
        if (host == null || host.getBaseMetaTileEntity() == null
            || !host.getBaseMetaTileEntity()
                .isActive())
            return 0;
        if (host.mStorage.compareTo(MTEYottaFluidTank.MAX_INT_BIGINT) >= 0) {
            return Integer.MAX_VALUE;
        } else return host.mStorage.intValue();
    }

    @Override
    public int fill(ForgeDirection from, FluidStack resource, boolean doFill) {
        if (host == null || host.getBaseMetaTileEntity() == null
            || !host.getBaseMetaTileEntity()
                .isActive())
            return 0;
        if (host.mLockedFluid != null && !host.mLockedFluid.isFluidEqual(resource)) return 0;
        if (host.mFluid == null || host.mFluid.isFluidEqual(resource)) {
            if (host.mFluid == null) {
                host.mFluid = resource.copy();
                host.mFluid.amount = 1;
            }

            if (host.addFluid(resource.amount, doFill)) {
                return resource.amount;
            } else {
                final int returned;
                if (host.getIsVoidExcessEnabled()) {
                    returned = resource.amount;
                } else {
                    final BigInteger delta = host.mStorage.subtract(host.mStorageCurrent);
                    returned = delta.intValueExact();
                }
                if (doFill) host.mStorageCurrent = host.mStorage;
                return returned;
            }
        }
        return 0;
    }

    public long fill(@SuppressWarnings("unused") ForgeDirection from, IAEFluidStack resource, boolean doFill) {
        if (host == null || host.getBaseMetaTileEntity() == null
            || !host.getBaseMetaTileEntity()
                .isActive())
            return 0;
        if (host.mLockedFluid != null && host.mLockedFluid.getFluid() != resource.getFluid()) return 0;
        if (host.mFluid == null || host.mFluid.getFluid() == resource.getFluid()) {
            if (host.mFluid == null) {
                host.mFluid = resource.getFluidStack(); // makes a copy internally
                host.mFluid.amount = 1;
            }

            if (host.addFluid(resource.getStackSize(), doFill)) {
                return resource.getStackSize();
            } else {
                final long returned;
                if (host.getIsVoidExcessEnabled()) {
                    returned = resource.getStackSize();
                } else {
                    final BigInteger delta = host.mStorage.subtract(host.mStorageCurrent);
                    returned = delta.longValueExact();
                }
                if (doFill) host.mStorageCurrent = host.mStorage;
                return returned;
            }
        }
        return 0;
    }

    @Override
    public FluidStack drain(ForgeDirection from, FluidStack resource, boolean doDrain) {
        if (host == null || host.getBaseMetaTileEntity() == null
            || !host.getBaseMetaTileEntity()
                .isActive())
            return null;
        if (host.mFluid == null || host.mFluid.getFluid() != resource.getFluid()) return null;
        int ready;
        if (host.mStorageCurrent.compareTo(MTEYottaFluidTank.MAX_INT_BIGINT) >= 0) {
            ready = Integer.MAX_VALUE;
        } else ready = host.mStorageCurrent.intValue();
        ready = Math.min(ready, resource.amount);
        if (doDrain) {
            host.reduceFluid(ready);
        }
        return new FluidStack(resource.getFluid(), ready);
    }

    public IAEFluidStack drain(@SuppressWarnings("unused") ForgeDirection from, IAEFluidStack resource,
        boolean doDrain) {
        if (host == null || host.getBaseMetaTileEntity() == null
            || !host.getBaseMetaTileEntity()
                .isActive())
            return null;
        if (host.mFluid == null || host.mFluid.getFluid() != resource.getFluid()) return null;
        long ready;
        if (host.mStorageCurrent.compareTo(MAX_LONG_BIGINT) > 0) {
            ready = Long.MAX_VALUE;
        } else ready = host.mStorageCurrent.longValue();
        ready = Math.min(ready, resource.getStackSize());
        if (doDrain) {
            host.reduceFluid(ready);
        }
        IAEFluidStack copy = resource.copy();
        copy.setStackSize(ready);
        return copy;
    }

    @Override
    public FluidStack drain(ForgeDirection from, int maxDrain, boolean doDrain) {
        if (host == null || host.getBaseMetaTileEntity() == null
            || !host.getBaseMetaTileEntity()
                .isActive())
            return null;
        if (host.mFluid == null) return null;
        final FluidStack drainStack = host.mFluid.copy();
        drainStack.amount = maxDrain;
        return this.drain(from, drainStack, doDrain);
    }

    private static final FluidTankInfo[] EMPTY_TANK_INFO = new FluidTankInfo[] { new FluidTankInfo(null, 0) };

    @Override
    public FluidTankInfo[] getTankInfo(ForgeDirection from) {
        if (host == null || host.getBaseMetaTileEntity() == null
            || !host.getBaseMetaTileEntity()
                .isActive())
            return EMPTY_TANK_INFO;

        return host.getTankInfo(from);
    }

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

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

    @Override
    public ITexture[] getTexturesActive(ITexture aBaseTexture) {
        return new ITexture[] { aBaseTexture, TextureFactory.of(textureFont), };
    }

    @Override
    public ITexture[] getTexturesInactive(ITexture aBaseTexture) {
        return new ITexture[] { aBaseTexture, TextureFactory.of(textureFont), };
    }

    @Override
    public IMetaTileEntity newMetaEntity(IGregTechTileEntity aTileEntity) {
        return new MTEYOTTAHatch(mName, mTier, mDescriptionArray, mTextures);
    }

    @Override
    public IGridNode getActionableNode() {
        AENetworkProxy gp = getProxy();
        return gp != null ? gp.getNode() : null;
    }

    @Override
    public AccessRestriction getAccess() {
        return this.readMode;
    }

    @Override
    public boolean isPrioritized(IAEFluidStack input) {
        return true;
    }

    @Override
    public boolean canAccept(IAEFluidStack input) {
        FluidStack rInput = input.getFluidStack();
        return fill(null, rInput, false) > 0;
    }

    @Override
    @SuppressWarnings("rawtypes")
    public List<IMEInventoryHandler> getCellArray(StorageChannel channel) {
        List<IMEInventoryHandler> list = new ArrayList<>();
        if (channel == StorageChannel.FLUIDS) {
            list.add(this);
        }
        return list;
    }

    @Override
    public int getPriority() {
        return this.priority;
    }

    @Override
    public int getSlot() {
        return 0;
    }

    @Override
    public boolean validForPass(int i) {
        return true;
    }

    @Override
    public void saveChanges(IMEInventory cellInventory) {
        // This is handled by host itself.
    }

    private boolean isChanged() {
        if (this.host == null) return false;
        return !this.lastAmt.equals(this.host.mStorageCurrent) || this.lastFluid != this.host.mFluid;
    }

    private void update() {
        if (this.host == null) return;
        this.lastAmt = this.host.mStorageCurrent;
        this.lastFluid = this.host.mFluid;
    }

    private void faster() {
        if (this.tickRate > 15) {
            this.tickRate -= 5;
        }
    }

    private void slower() {
        if (this.tickRate < 100) {
            this.tickRate += 5;
        }
    }

    private boolean shouldTick(long tick) {
        if (this.host == null) return false;
        return tick % this.tickRate == 0;
    }
}