aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/gtPlusPlus/core/tileentities/general/TileEntityVolumetricFlaskSetter.java
blob: 1c5a34e2caaa758d5454f58da2b9d37ff3c6f2a7 (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
package gtPlusPlus.core.tileentities.general;

import java.util.ArrayList;

import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.inventory.ISidedInventory;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.network.NetworkManager;
import net.minecraft.network.Packet;
import net.minecraft.network.play.server.S35PacketUpdateTileEntity;
import net.minecraft.tileentity.TileEntity;
import net.minecraftforge.fluids.FluidStack;

import gtPlusPlus.api.objects.Logger;
import gtPlusPlus.core.container.ContainerVolumetricFlaskSetter;
import gtPlusPlus.core.inventories.InventoryVolumetricFlaskSetter;
import gtPlusPlus.core.util.math.MathUtils;
import gtPlusPlus.core.util.minecraft.PlayerUtils;
import gtPlusPlus.xmod.gregtech.common.helpers.VolumetricFlaskHelper;

public class TileEntityVolumetricFlaskSetter extends TileEntity implements ISidedInventory {

    private int tickCount = 0;
    private final InventoryVolumetricFlaskSetter inventoryContents;
    private String customName;
    public int locationX;
    public int locationY;
    public int locationZ;
    private int aCurrentMode = 0;
    private int aCustomValue = 1000;

    public TileEntityVolumetricFlaskSetter() {
        this.inventoryContents = new InventoryVolumetricFlaskSetter();
        this.setTileLocation();
    }

    public int getCustomValue() {
        // Logger.INFO("Value: "+this.aCustomValue);
        return this.aCustomValue;
    }

    public void setCustomValue(int aVal) {
        log("Old Value: " + this.aCustomValue);
        this.aCustomValue = (short) MathUtils.balance(aVal, 0, Short.MAX_VALUE);
        log("New Value: " + this.aCustomValue);
        markDirty();
    }

    public boolean setTileLocation() {
        if (this.hasWorldObj()) {
            if (!this.getWorldObj().isRemote) {
                this.locationX = this.xCoord;
                this.locationY = this.yCoord;
                this.locationZ = this.zCoord;
                return true;
            }
        }
        return false;
    }

    // Rename to hasCircuitToConfigure
    public final boolean hasFlask() {
        for (int i = 0; i < this.getInventory()
            .getInventory().length - 1; i++) {
            if (i == ContainerVolumetricFlaskSetter.SLOT_OUTPUT) {
                continue;
            }
            if (this.getInventory()
                .getInventory()[i] != null) {
                return true;
            }
        }
        return false;
    }

    public InventoryVolumetricFlaskSetter getInventory() {
        return this.inventoryContents;
    }

    private int getFlaskType(ItemStack aStack) {
        if (VolumetricFlaskHelper.isNormalVolumetricFlask(aStack)) {
            return 1;
        } else if (VolumetricFlaskHelper.isLargeVolumetricFlask(aStack)) {
            return 2;
        } else if (VolumetricFlaskHelper.isGiganticVolumetricFlask(aStack)) {
            return 3;
        }
        return 0;
    }

    private int getCapacityForSlot(int aSlot) {
        return switch (aSlot) {
            case 0 -> // 16
                16;
            case 1 -> // 36
                36;
            case 2 -> // 144
                144;
            case 3 -> // 432
                432;
            case 4 -> // 576
                576;
            case 5 -> // 720
                720;
            case 6 -> // 864
                864;
            case 7 -> // Custom
                getCustomValue();
            default -> 1000;
        };
    }

    public boolean addOutput() {

        // Don't do anything unless we have items
        if (!hasFlask()) {
            Logger.INFO("No Flasks.");
            return false;
        }

        ItemStack[] aInputs = this.getInventory()
            .getInventory()
            .clone();

        // Check if there is output in slot.
        boolean hasOutput = false;
        if (aInputs[ContainerVolumetricFlaskSetter.SLOT_OUTPUT] != null) {
            hasOutput = true;
            if (aInputs[ContainerVolumetricFlaskSetter.SLOT_OUTPUT].stackSize >= 16) {
                return false;
            }
        }
        ArrayList<Integer> aValidSlots = new ArrayList<>();
        int aSlotCount = 0;
        for (ItemStack i : aInputs) {
            if (i != null) {
                aValidSlots.add(aSlotCount);
            }
            aSlotCount++;
        }
        for (int e : aValidSlots) {

            // Skip slot 7 (Custom) unless it has a value > 0
            if (e == 7 && getCustomValue() <= 0) {
                log("Skipping Custom slot as value <= 0");
                continue;
            }
            if (e == ContainerVolumetricFlaskSetter.SLOT_OUTPUT) {
                continue;
            }

            boolean doAdd = false;
            ItemStack g = this.getStackInSlot(e);
            FluidStack aInputFluidStack = VolumetricFlaskHelper.getFlaskFluid(g);
            int aSize = 0;
            ItemStack aInputStack = null;
            int aTypeInSlot = getFlaskType(g);
            if (aTypeInSlot > 0 && g != null) {
                // No Existing Output
                if (!hasOutput) {
                    aSize = g.stackSize;
                    doAdd = true;
                }
                // Existing Output
                else {
                    ItemStack f = aInputs[ContainerVolumetricFlaskSetter.SLOT_OUTPUT];
                    FluidStack aFluidInCheckedSlot = VolumetricFlaskHelper.getFlaskFluid(f);
                    int aTypeInCheckedSlot = getFlaskType(f);
                    // Check that the Circuit in the Output slot is not null and the same type as the circuit input.
                    if (aTypeInCheckedSlot > 0 && (aTypeInSlot == aTypeInCheckedSlot) && f != null) {
                        if (g.getItem() == f.getItem()
                            && VolumetricFlaskHelper.getFlaskCapacity(f) == getCapacityForSlot(e)
                            && ((aInputFluidStack == null && aFluidInCheckedSlot == null)
                                || aInputFluidStack.isFluidEqual(aFluidInCheckedSlot))) {
                            log(
                                "Input Slot Flask Contains: "
                                    + (aInputFluidStack != null ? aInputFluidStack.getLocalizedName() : "Empty"));
                            log(
                                "Output Slot Flask Contains: "
                                    + (aFluidInCheckedSlot != null ? aFluidInCheckedSlot.getLocalizedName() : "Empty"));
                            aSize = f.stackSize + g.stackSize;
                            if (aSize > 16) {
                                aInputStack = g.copy();
                                aInputStack.stackSize = (aSize - 16);
                            }
                            doAdd = true;
                        }
                    }
                }
                if (doAdd) {
                    // Check Circuit Type
                    ItemStack aOutput;
                    FluidStack aOutputFluid = null;
                    if (!VolumetricFlaskHelper.isFlaskEmpty(g)) {
                        aOutputFluid = aInputFluidStack.copy();
                    }
                    if (aTypeInSlot == 1) {
                        aOutput = VolumetricFlaskHelper.getVolumetricFlask(1);
                    } else if (aTypeInSlot == 2) {
                        aOutput = VolumetricFlaskHelper.getLargeVolumetricFlask(1);
                    } else if (aTypeInSlot == 3) {
                        aOutput = VolumetricFlaskHelper.getGiganticVolumetricFlask(1);
                    } else {
                        aOutput = null;
                    }
                    if (aOutput != null) {
                        aOutput.stackSize = aSize;
                        int aCapacity = getCapacityForSlot(e);
                        VolumetricFlaskHelper.setNewFlaskCapacity(aOutput, aCapacity);
                        if (aOutputFluid != null) {
                            if (aOutputFluid.amount > aCapacity) {
                                aOutputFluid.amount = aCapacity;
                            }
                            VolumetricFlaskHelper.setFluid(aOutput, aOutputFluid);
                        }
                        this.setInventorySlotContents(e, aInputStack);
                        this.setInventorySlotContents(ContainerVolumetricFlaskSetter.SLOT_OUTPUT, aOutput);
                        return true;
                    }
                }
            }
        }
        return false;
    }

    @Override
    public void updateEntity() {
        try {
            if (!this.worldObj.isRemote) {
                if (tickCount % 10 == 0) {
                    if (hasFlask()) {
                        this.addOutput();
                        this.markDirty();
                    }
                }
                this.tickCount++;
            }
        } catch (final Throwable t) {}
    }

    public boolean anyPlayerInRange() {
        return this.worldObj.getClosestPlayer(this.xCoord + 0.5D, this.yCoord + 0.5D, this.zCoord + 0.5D, 32) != null;
    }

    public NBTTagCompound getTag(final NBTTagCompound nbt, final String tag) {
        if (!nbt.hasKey(tag)) {
            nbt.setTag(tag, new NBTTagCompound());
        }
        return nbt.getCompoundTag(tag);
    }

    @Override
    public void writeToNBT(final NBTTagCompound nbt) {
        super.writeToNBT(nbt);
        // Utils.LOG_WARNING("Trying to write NBT data to TE.");
        final NBTTagCompound chestData = new NBTTagCompound();
        this.inventoryContents.writeToNBT(chestData);
        nbt.setTag("ContentsChest", chestData);
        nbt.setInteger("aCustomValue", aCustomValue);
        if (this.hasCustomInventoryName()) {
            nbt.setString("CustomName", this.getCustomName());
        }
        nbt.setInteger("aCurrentMode", aCurrentMode);
    }

    @Override
    public void readFromNBT(final NBTTagCompound nbt) {
        super.readFromNBT(nbt);
        // Utils.LOG_WARNING("Trying to read NBT data from TE.");
        this.inventoryContents.readFromNBT(nbt.getCompoundTag("ContentsChest"));
        this.aCustomValue = nbt.getInteger("aCustomValue");
        if (nbt.hasKey("CustomName", 8)) {
            this.setCustomName(nbt.getString("CustomName"));
        }
        aCurrentMode = nbt.getInteger("aCurrentMode");
    }

    @Override
    public int getSizeInventory() {
        return this.getInventory()
            .getSizeInventory();
    }

    @Override
    public ItemStack getStackInSlot(final int slot) {
        return this.getInventory()
            .getStackInSlot(slot);
    }

    @Override
    public ItemStack decrStackSize(final int slot, final int count) {
        return this.getInventory()
            .decrStackSize(slot, count);
    }

    @Override
    public ItemStack getStackInSlotOnClosing(final int slot) {
        return this.getInventory()
            .getStackInSlotOnClosing(slot);
    }

    @Override
    public void setInventorySlotContents(final int slot, final ItemStack stack) {
        this.getInventory()
            .setInventorySlotContents(slot, stack);
    }

    @Override
    public int getInventoryStackLimit() {
        return this.getInventory()
            .getInventoryStackLimit();
    }

    @Override
    public boolean isUseableByPlayer(final EntityPlayer entityplayer) {
        return this.getInventory()
            .isUseableByPlayer(entityplayer);
    }

    @Override
    public void openInventory() {
        this.worldObj.addBlockEvent(this.xCoord, this.yCoord, this.zCoord, this.getBlockType(), 1, 1);
        this.worldObj.notifyBlocksOfNeighborChange(this.xCoord, this.yCoord, this.zCoord, this.getBlockType());
        this.worldObj.notifyBlocksOfNeighborChange(this.xCoord, this.yCoord - 1, this.zCoord, this.getBlockType());
        this.getInventory()
            .openInventory();
    }

    @Override
    public void closeInventory() {
        this.worldObj.addBlockEvent(this.xCoord, this.yCoord, this.zCoord, this.getBlockType(), 1, 1);
        this.worldObj.notifyBlocksOfNeighborChange(this.xCoord, this.yCoord, this.zCoord, this.getBlockType());
        this.worldObj.notifyBlocksOfNeighborChange(this.xCoord, this.yCoord - 1, this.zCoord, this.getBlockType());
        this.getInventory()
            .closeInventory();
    }

    @Override
    public boolean isItemValidForSlot(final int slot, final ItemStack itemstack) {
        return this.getInventory()
            .isItemValidForSlot(slot, itemstack);
    }

    @Override
    public int[] getAccessibleSlotsFromSide(final int p_94128_1_) {
        final int[] accessibleSides = new int[this.getSizeInventory()];
        for (int r = 0; r < this.getInventory()
            .getSizeInventory(); r++) {
            accessibleSides[r] = r;
        }
        return accessibleSides;
    }

    @Override
    public boolean canInsertItem(final int aSlot, final ItemStack p_102007_2_, final int p_102007_3_) {
        return aSlot == aCurrentMode;
    }

    @Override
    public boolean canExtractItem(final int aSlot, final ItemStack p_102008_2_, final int p_102008_3_) {
        return aSlot == ContainerVolumetricFlaskSetter.SLOT_OUTPUT;
    }

    public String getCustomName() {
        return this.customName;
    }

    public void setCustomName(final String customName) {
        this.customName = customName;
    }

    @Override
    public String getInventoryName() {
        return this.hasCustomInventoryName() ? this.customName : "container.VolumetricFlaskSetter";
    }

    @Override
    public boolean hasCustomInventoryName() {
        return (this.customName != null) && !this.customName.isEmpty();
    }

    @Override
    public Packet getDescriptionPacket() {
        final NBTTagCompound tag = new NBTTagCompound();
        this.writeToNBT(tag);
        return new S35PacketUpdateTileEntity(this.xCoord, this.yCoord, this.zCoord, this.blockMetadata, tag);
    }

    @Override
    public void onDataPacket(final NetworkManager net, final S35PacketUpdateTileEntity pkt) {
        final NBTTagCompound tag = pkt.func_148857_g();
        this.readFromNBT(tag);
    }

    public boolean onScrewdriverRightClick(byte side, EntityPlayer player, int x, int y, int z) {

        if (player.isSneaking()) {
            PlayerUtils.messagePlayer(player, "Value: " + this.getCustomValue());
        }

        try {
            if (aCurrentMode == 7) {
                aCurrentMode = 0;
            } else {
                aCurrentMode++;
            }
            PlayerUtils.messagePlayer(player, "Slot " + aCurrentMode + " is now default.");
            return true;
        } catch (Throwable t) {
            return false;
        }
    }

    public void log(String aString) {
        Logger.INFO("[Flask-Tile] " + aString);
    }
}