aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/gregtech/common/blocks/GT_Item_Machines.java
blob: 6965ffda2f0ee513f8ecffba6d3f4cba76aa68ce (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
package gregtech.common.blocks;

import static gregtech.GT_Mod.GT_FML_LOGGER;

import java.util.List;

import javax.annotation.Nullable;

import net.minecraft.block.Block;
import net.minecraft.entity.Entity;
import net.minecraft.entity.EntityLivingBase;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.item.ItemBlock;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.potion.Potion;
import net.minecraft.potion.PotionEffect;
import net.minecraft.util.EnumChatFormatting;
import net.minecraft.world.World;
import net.minecraftforge.fluids.FluidStack;
import net.minecraftforge.fluids.IFluidContainerItem;

import cpw.mods.fml.relauncher.Side;
import cpw.mods.fml.relauncher.SideOnly;
import gregtech.api.GregTech_API;
import gregtech.api.enums.Materials;
import gregtech.api.interfaces.ISecondaryDescribable;
import gregtech.api.interfaces.metatileentity.IConnectable;
import gregtech.api.interfaces.metatileentity.IMetaTileEntity;
import gregtech.api.interfaces.tileentity.IGregTechTileEntity;
import gregtech.api.metatileentity.CoverableTileEntity;
import gregtech.api.metatileentity.implementations.GT_MetaPipeEntity_Cable;
import gregtech.api.metatileentity.implementations.GT_MetaPipeEntity_Fluid;
import gregtech.api.metatileentity.implementations.GT_MetaPipeEntity_Frame;
import gregtech.api.metatileentity.implementations.GT_MetaPipeEntity_Item;
import gregtech.api.util.GT_ItsNotMyFaultException;
import gregtech.api.util.GT_LanguageManager;
import gregtech.api.util.GT_Utility;
import gregtech.common.tileentities.storage.*;

public class GT_Item_Machines extends ItemBlock implements IFluidContainerItem {

    public GT_Item_Machines(Block block) {
        super(block);
        setMaxDamage(0);
        setHasSubtypes(true);
        setCreativeTab(GregTech_API.TAB_GREGTECH);
    }

    public static IMetaTileEntity getMetaTileEntity(ItemStack aStack) {
        if (GT_Utility.isStackInvalid(aStack)) return null;
        if (!(aStack.getItem() instanceof GT_Item_Machines)) return null;
        if (aStack.getItemDamage() < 0 || aStack.getItemDamage() > GregTech_API.METATILEENTITIES.length) return null;
        return GregTech_API.METATILEENTITIES[aStack.getItemDamage()];
    }

    @Override
    @SuppressWarnings("unchecked")
    public void addInformation(ItemStack aStack, EntityPlayer aPlayer, List aList, boolean aF3_H) {
        try {
            final int tDamage = getDamage(aStack);
            if ((tDamage <= 0) || (tDamage >= GregTech_API.METATILEENTITIES.length)) {
                return;
            }

            if (GregTech_API.METATILEENTITIES[tDamage] != null) {
                final IGregTechTileEntity tTileEntity = GregTech_API.METATILEENTITIES[tDamage].getBaseMetaTileEntity();
                if (!GregTech_API.sPostloadFinished
                        && tTileEntity.getMetaTileEntity() instanceof ISecondaryDescribable) {
                    final String[] tSecondaryDescription = ((ISecondaryDescribable) tTileEntity.getMetaTileEntity()).getSecondaryDescription();
                    addDescription(null, tSecondaryDescription, tDamage, "_Secondary", true);
                }
                {
                    final IMetaTileEntity tMetaTileEntity = tTileEntity.getMetaTileEntity();
                    final String tSuffix = (tMetaTileEntity instanceof ISecondaryDescribable
                            && ((ISecondaryDescribable) tMetaTileEntity).isDisplaySecondaryDescription()) ? "_Secondary"
                                    : "";
                    addDescription(
                            aList,
                            tTileEntity.getDescription(),
                            tDamage,
                            tSuffix,
                            !GregTech_API.sPostloadFinished);
                }
                if (tTileEntity.getEUCapacity() > 0L) {
                    if (tTileEntity.getInputVoltage() > 0L) {
                        final byte inputTier = GT_Utility.getTier(tTileEntity.getInputVoltage());
                        aList.add(
                                GT_LanguageManager.addStringLocalization(
                                        "TileEntity_EUp_IN",
                                        "Voltage IN: ",
                                        !GregTech_API.sPostloadFinished) + EnumChatFormatting.GREEN
                                        + GT_Utility.formatNumbers(tTileEntity.getInputVoltage())
                                        + " ("
                                        + GT_Utility.getColoredTierNameFromTier(inputTier)
                                        + EnumChatFormatting.GREEN
                                        + ")"
                                        + EnumChatFormatting.GRAY);
                    }
                    if (tTileEntity.getOutputVoltage() > 0L) {
                        final byte outputTier = GT_Utility.getTier(tTileEntity.getOutputVoltage());
                        aList.add(
                                GT_LanguageManager.addStringLocalization(
                                        "TileEntity_EUp_OUT",
                                        "Voltage OUT: ",
                                        !GregTech_API.sPostloadFinished) + EnumChatFormatting.GREEN
                                        + GT_Utility.formatNumbers(tTileEntity.getOutputVoltage())
                                        + " ("
                                        + GT_Utility.getColoredTierNameFromTier(outputTier)
                                        + EnumChatFormatting.GREEN
                                        + ")"
                                        + EnumChatFormatting.GRAY);
                    }
                    if (tTileEntity.getOutputAmperage() > 1L) {
                        aList.add(
                                GT_LanguageManager.addStringLocalization(
                                        "TileEntity_EUp_AMOUNT",
                                        "Amperage: ",
                                        !GregTech_API.sPostloadFinished) + EnumChatFormatting.YELLOW
                                        + GT_Utility.formatNumbers(tTileEntity.getOutputAmperage())
                                        + EnumChatFormatting.GRAY);
                    }
                    aList.add(
                            GT_LanguageManager.addStringLocalization(
                                    "TileEntity_EUp_STORE",
                                    "Capacity: ",
                                    !GregTech_API.sPostloadFinished) + EnumChatFormatting.BLUE
                                    + GT_Utility.formatNumbers(tTileEntity.getEUCapacity())
                                    + EnumChatFormatting.GRAY
                                    + " EU");
                }
                if (GregTech_API.METATILEENTITIES[tDamage] instanceof GT_MetaTileEntity_QuantumTank
                        || GregTech_API.METATILEENTITIES[tDamage] instanceof GT_MetaTileEntity_SuperTank) {
                    if (aStack.hasTagCompound() && aStack.stackTagCompound.hasKey("mFluid")) {
                        final FluidStack tContents = FluidStack.loadFluidStackFromNBT(
                                aStack.stackTagCompound.getCompoundTag("mFluid"));
                        if (tContents != null && tContents.amount > 0) {
                            aList.add(
                                    GT_LanguageManager.addStringLocalization(
                                            "TileEntity_TANK_INFO",
                                            "Contains Fluid: ",
                                            !GregTech_API.sPostloadFinished) + EnumChatFormatting.YELLOW
                                            + tContents.getLocalizedName()
                                            + EnumChatFormatting.GRAY);
                            aList.add(
                                    GT_LanguageManager.addStringLocalization(
                                            "TileEntity_TANK_AMOUNT",
                                            "Fluid Amount: ",
                                            !GregTech_API.sPostloadFinished) + EnumChatFormatting.GREEN
                                            + GT_Utility.formatNumbers(tContents.amount)
                                            + " L"
                                            + EnumChatFormatting.GRAY);
                        }
                    }
                }
                if (GregTech_API.METATILEENTITIES[tDamage] instanceof GT_MetaTileEntity_DigitalChestBase) {
                    if (aStack.hasTagCompound() && aStack.stackTagCompound.hasKey("mItemStack")) {
                        final ItemStack tContents = ItemStack.loadItemStackFromNBT(
                                aStack.stackTagCompound.getCompoundTag("mItemStack"));
                        final int tSize = aStack.stackTagCompound.getInteger("mItemCount");
                        if (tContents != null && tSize > 0) {
                            aList.add(
                                    GT_LanguageManager.addStringLocalization(
                                            "TileEntity_CHEST_INFO",
                                            "Contains Item: ",
                                            !GregTech_API.sPostloadFinished) + EnumChatFormatting.YELLOW
                                            + tContents.getDisplayName()
                                            + EnumChatFormatting.GRAY);
                            aList.add(
                                    GT_LanguageManager.addStringLocalization(
                                            "TileEntity_CHEST_AMOUNT",
                                            "Item Amount: ",
                                            !GregTech_API.sPostloadFinished) + EnumChatFormatting.GREEN
                                            + GT_Utility.formatNumbers(tSize)
                                            + EnumChatFormatting.GRAY);
                        }
                    }
                }
            }
            final NBTTagCompound aNBT = aStack.getTagCompound();
            if (aNBT != null) {
                if (aNBT.getBoolean("mMuffler")) {
                    aList.add(
                            GT_LanguageManager.addStringLocalization(
                                    "GT_TileEntity_MUFFLER",
                                    "has Muffler Upgrade",
                                    !GregTech_API.sPostloadFinished));
                }
                if (aNBT.getBoolean("mSteamConverter")) {
                    aList.add(
                            GT_LanguageManager.addStringLocalization(
                                    "GT_TileEntity_STEAMCONVERTER",
                                    "has Steam Upgrade",
                                    !GregTech_API.sPostloadFinished));
                }
                int tAmount = 0;
                if ((tAmount = aNBT.getByte("mSteamTanks")) > 0) {
                    aList.add(
                            tAmount + " "
                                    + GT_LanguageManager.addStringLocalization(
                                            "GT_TileEntity_STEAMTANKS",
                                            "Steam Tank Upgrades",
                                            !GregTech_API.sPostloadFinished));
                }

                CoverableTileEntity.addInstalledCoversInformation(aNBT, aList);
            }
        } catch (Throwable e) {
            GT_FML_LOGGER.error("addInformation", e);
        }
    }

    private void addDescription(@Nullable List<String> aList, @Nullable String[] aDescription, int aDamage,
            String aSuffix, boolean aWriteIntoLangFile) {
        if (aDescription == null) return;
        for (int i = 0, tLength = aDescription.length; i < tLength; i++) {
            String tDescLine = aDescription[i];
            if (!GT_Utility.isStringValid(tDescLine)) continue;

            String tKey = String.format("TileEntity_DESCRIPTION_%05d%s_Index_%02d", aDamage, aSuffix, i);
            if (tDescLine.contains("%%%")) {
                final String[] tSplitStrings = tDescLine.split("%%%");
                final StringBuilder tBuffer = new StringBuilder();
                final String[] tRep = new String[tSplitStrings.length / 2];
                for (int j = 0; j < tSplitStrings.length; j++) if (j % 2 == 0) tBuffer.append(tSplitStrings[j]);
                else {
                    tBuffer.append("%s");
                    tRep[j / 2] = tSplitStrings[j];
                }
                final String tTranslated = String.format(
                        GT_LanguageManager.addStringLocalization(tKey, tBuffer.toString(), aWriteIntoLangFile),
                        (Object[]) tRep);
                if (aList != null) aList.add(tTranslated);
            } else {
                String tTranslated = GT_LanguageManager.addStringLocalization(tKey, tDescLine, aWriteIntoLangFile);
                if (aList != null) aList.add(tTranslated.equals("") ? tDescLine : tTranslated);
            }
        }
    }

    @SideOnly(Side.CLIENT)
    public void registerDescription(int aDamage) {
        if (aDamage >= GregTech_API.METATILEENTITIES.length) return;
        if (GregTech_API.METATILEENTITIES[aDamage] != null) {
            final IMetaTileEntity tMetaTileEntity = GregTech_API.METATILEENTITIES[aDamage].getBaseMetaTileEntity()
                                                                                          .getMetaTileEntity();
            if (tMetaTileEntity instanceof ISecondaryDescribable) {
                final String[] tSecondaryDescription = ((ISecondaryDescribable) tMetaTileEntity).getSecondaryDescription();
                addDescription(null, tSecondaryDescription, aDamage, "_Secondary", true);
            }
            addDescription(null, tMetaTileEntity.getDescription(), aDamage, "", true);
        }
    }

    @Override
    public boolean onItemUseFirst(ItemStack stack, EntityPlayer player, World world, int x, int y, int z, int side,
            float hitX, float hitY, float hitZ) {
        return false;
    }

    @Override
    public String getUnlocalizedName(ItemStack aStack) {
        final short tDamage = (short) getDamage(aStack);
        if ((tDamage < 0) || (tDamage >= GregTech_API.METATILEENTITIES.length)) {
            return "";
        }
        if (GregTech_API.METATILEENTITIES[tDamage] != null) {
            return getUnlocalizedName() + "." + GregTech_API.METATILEENTITIES[tDamage].getMetaName();
        }
        return "";
    }

    @Override
    public String getItemStackDisplayName(ItemStack aStack) {
        String aName = super.getItemStackDisplayName(aStack);
        final short aDamage = (short) getDamage(aStack);
        if (aDamage >= 0 && aDamage < GregTech_API.METATILEENTITIES.length
                && GregTech_API.METATILEENTITIES[aDamage] != null) {
            Materials aMaterial = null;
            if (GregTech_API.METATILEENTITIES[aDamage] instanceof GT_MetaPipeEntity_Item) {
                aMaterial = ((GT_MetaPipeEntity_Item) GregTech_API.METATILEENTITIES[aDamage]).mMaterial;
            } else if (GregTech_API.METATILEENTITIES[aDamage] instanceof GT_MetaPipeEntity_Fluid) {
                aMaterial = ((GT_MetaPipeEntity_Fluid) GregTech_API.METATILEENTITIES[aDamage]).mMaterial;
            } else if (GregTech_API.METATILEENTITIES[aDamage] instanceof GT_MetaPipeEntity_Cable) {
                aMaterial = ((GT_MetaPipeEntity_Cable) GregTech_API.METATILEENTITIES[aDamage]).mMaterial;
            } else if (GregTech_API.METATILEENTITIES[aDamage] instanceof GT_MetaPipeEntity_Frame) {
                aMaterial = ((GT_MetaPipeEntity_Frame) GregTech_API.METATILEENTITIES[aDamage]).mMaterial;
            }
            if (aMaterial != null) {
                aName = aMaterial.getLocalizedNameForItem(aName);
            }
        }
        return aName;
    }

    @Override
    public void onCreated(ItemStack aStack, World aWorld, EntityPlayer aPlayer) {
        super.onCreated(aStack, aWorld, aPlayer);
        final short tDamage = (short) getDamage(aStack);
        if ((tDamage < 0) || ((tDamage >= GregTech_API.METATILEENTITIES.length)
                && (GregTech_API.METATILEENTITIES[tDamage] != null))) {
            GregTech_API.METATILEENTITIES[tDamage].onCreated(aStack, aWorld, aPlayer);
        }
    }

    @Override
    public boolean placeBlockAt(ItemStack aStack, EntityPlayer aPlayer, World aWorld, int aX, int aY, int aZ, int side,
            float hitX, float hitY, float hitZ, int aMeta) {
        final short tDamage = (short) getDamage(aStack);
        if (tDamage > 0) {
            if (GregTech_API.METATILEENTITIES[tDamage] == null) {
                return false;
            }
            final int tMetaData = GregTech_API.METATILEENTITIES[tDamage].getTileEntityBaseType();
            if (!aWorld.setBlock(aX, aY, aZ, this.field_150939_a, tMetaData, 3)) {
                return false;
            }
            if (aWorld.getBlock(aX, aY, aZ) != this.field_150939_a) {
                throw new GT_ItsNotMyFaultException(
                        "Failed to place Block even though World.setBlock returned true. It COULD be MCPC/Bukkit causing that. In case you really have that installed, don't report this Bug to me, I don't know how to fix it.");
            }
            if (aWorld.getBlockMetadata(aX, aY, aZ) != tMetaData) {
                throw new GT_ItsNotMyFaultException(
                        "Failed to set the MetaValue of the Block even though World.setBlock returned true. It COULD be MCPC/Bukkit causing that. In case you really have that installed, don't report this Bug to me, I don't know how to fix it.");
            }
            final IGregTechTileEntity tTileEntity = (IGregTechTileEntity) aWorld.getTileEntity(aX, aY, aZ);
            if (tTileEntity != null) {
                tTileEntity.setInitialValuesAsNBT(tTileEntity.isServerSide() ? aStack.getTagCompound() : null, tDamage);
                if (aPlayer != null) {
                    tTileEntity.setOwnerName(aPlayer.getDisplayName());
                    tTileEntity.setOwnerUuid(aPlayer.getUniqueID());
                }
                tTileEntity.getMetaTileEntity()
                           .initDefaultModes(aStack.getTagCompound());
                final byte aSide = GT_Utility.getOppositeSide(side);
                if (tTileEntity.getMetaTileEntity() instanceof IConnectable) {
                    // If we're connectable, try connecting to whatever we're up against
                    ((IConnectable) tTileEntity.getMetaTileEntity()).connect(aSide);
                } else if (aPlayer != null && aPlayer.isSneaking()) {
                    // If we're being placed against something that is connectable, try telling it to connect to us
                    final IGregTechTileEntity aTileEntity = tTileEntity.getIGregTechTileEntityAtSide(aSide);
                    if (aTileEntity != null && aTileEntity.getMetaTileEntity() instanceof IConnectable) {
                        ((IConnectable) aTileEntity.getMetaTileEntity()).connect((byte) side);
                    }
                }
            }
        } else if (!aWorld.setBlock(aX, aY, aZ, this.field_150939_a, tDamage, 3)) {
            return false;
        }
        if (aWorld.getBlock(aX, aY, aZ) == this.field_150939_a) {
            this.field_150939_a.onBlockPlacedBy(aWorld, aX, aY, aZ, aPlayer, aStack);
            this.field_150939_a.onPostBlockPlaced(aWorld, aX, aY, aZ, tDamage);
        }
        return true;
    }

    @Override
    public void onUpdate(ItemStack aStack, World aWorld, Entity aPlayer, int aTimer, boolean aIsInHand) {
        super.onUpdate(aStack, aWorld, aPlayer, aTimer, aIsInHand);
        final short tDamage = (short) getDamage(aStack);
        final EntityLivingBase tPlayer = (EntityPlayer) aPlayer;
        if (GregTech_API.METATILEENTITIES[tDamage] instanceof GT_MetaTileEntity_QuantumChest
                || GregTech_API.METATILEENTITIES[tDamage] instanceof GT_MetaTileEntity_SuperTank
                || GregTech_API.METATILEENTITIES[tDamage] instanceof GT_MetaTileEntity_QuantumTank) {
            final NBTTagCompound tNBT = aStack.stackTagCompound;
            if (tNBT == null) return;
            if (tNBT.hasNoTags()) {
                aStack.setTagCompound(null);
                return;
            }
            if ((tNBT.hasKey("mItemCount") && tNBT.getInteger("mItemCount") > 0) || (tNBT.hasKey("mFluid")
                    && FluidStack.loadFluidStackFromNBT(tNBT.getCompoundTag("mFluid")).amount > 64000)) {
                final FluidStack tFluid = FluidStack.loadFluidStackFromNBT(tNBT.getCompoundTag("mFluid"));
                int tEffectDuration = 1200;
                if (tFluid != null) {
                    final double tFluidAmount = tFluid.amount;
                    final double tMiddlePoint = 4096000;
                    final double tSmoothingCoefficient = 2000000;
                    final int tMaxDuration = 12000;
                    final double tmp = (tFluidAmount - tMiddlePoint) / tSmoothingCoefficient;
                    tEffectDuration = (int) (Math.exp(tmp) / (Math.exp(tmp) + Math.exp(-tmp)) * tMaxDuration);
                }
                tPlayer.addPotionEffect(new PotionEffect(Potion.hunger.id, tEffectDuration, 1));
                tPlayer.addPotionEffect(new PotionEffect(Potion.moveSlowdown.id, tEffectDuration, 1));
                tPlayer.addPotionEffect(new PotionEffect(Potion.digSlowdown.id, tEffectDuration, 1));
                tPlayer.addPotionEffect(new PotionEffect(Potion.weakness.id, tEffectDuration, 1));
            }
        }
    }

    @Override
    public FluidStack getFluid(ItemStack container) {
        if (container != null) {
            final NBTTagCompound tNBT = container.stackTagCompound;
            if (tNBT != null && tNBT.hasKey("mFluid", 10)) {
                return FluidStack.loadFluidStackFromNBT(tNBT.getCompoundTag("mFluid"));
            }
        }
        return null;
    }

    @Override
    public int getCapacity(ItemStack container) {
        if (container != null) {
            final int tDamage = container.getItemDamage();
            final IMetaTileEntity tMetaTile = GregTech_API.METATILEENTITIES[tDamage];
            if (tMetaTile != null) return tMetaTile.getCapacity();
        }
        return 0;
    }

    @Override
    public int fill(ItemStack container, FluidStack resource, boolean doFill) {
        if (container != null && resource != null) {
            final int tDamage = container.getItemDamage();
            final IMetaTileEntity tMetaTile = GregTech_API.METATILEENTITIES[tDamage];
            if (!(tMetaTile instanceof GT_MetaTileEntity_QuantumTank
                    || tMetaTile instanceof GT_MetaTileEntity_SuperTank)) {
                return 0;
            }
            if (container.stackTagCompound == null) container.stackTagCompound = new NBTTagCompound();
            final FluidStack tStoredFluid = getFluid(container);
            final int tCapacity = getCapacity(container);
            if (tCapacity <= 0) return 0;
            if (tStoredFluid != null && tStoredFluid.isFluidEqual(resource)) {
                final int tAmount = Math.min(tCapacity - tStoredFluid.amount, resource.amount);
                if (doFill) {
                    final FluidStack tNewFluid = new FluidStack(tStoredFluid, tAmount + tStoredFluid.amount);
                    container.stackTagCompound.setTag("mFluid", tNewFluid.writeToNBT(new NBTTagCompound()));
                }
                return tAmount;
            }
            if (tStoredFluid == null) {
                final int tAmount = Math.min(tCapacity, resource.amount);
                if (doFill) {
                    final FluidStack tNewFluid = new FluidStack(resource, tAmount);
                    container.stackTagCompound.setTag("mFluid", tNewFluid.writeToNBT(new NBTTagCompound()));
                }
                return tAmount;
            }
        }
        return 0;
    }

    @Override
    public FluidStack drain(ItemStack container, int maxDrain, boolean doDrain) {
        if (container != null && container.hasTagCompound()) {
            final int tDamage = container.getItemDamage();
            final IMetaTileEntity tMetaTile = GregTech_API.METATILEENTITIES[tDamage];
            if (!(tMetaTile instanceof GT_MetaTileEntity_QuantumTank
                    || tMetaTile instanceof GT_MetaTileEntity_SuperTank)) {
                return null;
            }
            final FluidStack tStoredFluid = getFluid(container);
            if (tStoredFluid != null) {
                final int tAmount = Math.min(maxDrain, tStoredFluid.amount);
                final FluidStack tNewFluid = new FluidStack(tStoredFluid, tStoredFluid.amount - tAmount);
                final FluidStack tOutputFluid = new FluidStack(tStoredFluid, tAmount);
                if (doDrain) {
                    if (tNewFluid.amount <= 0) {
                        container.stackTagCompound.removeTag("mFluid");
                        if (container.stackTagCompound.hasNoTags()) container.setTagCompound(null);
                    } else {
                        container.stackTagCompound.setTag("mFluid", tNewFluid.writeToNBT(new NBTTagCompound()));
                    }
                }
                return tOutputFluid;
            }
        }
        return null;
    }
}