aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/gregtech/api/multitileentity/machine/MultiTileBasicMachine.java
blob: 9fcbaa235f56fc500fe94f3d50c4de87ff65d784 (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
package gregtech.api.multitileentity.machine;

import gregtech.api.enums.GT_Values;
import gregtech.api.enums.GT_Values.NBT;
import gregtech.api.enums.Textures;
import gregtech.api.fluid.FluidTankGT;
import gregtech.api.interfaces.IIconContainer;
import gregtech.api.interfaces.ITexture;
import gregtech.api.multitileentity.MultiTileEntityRegistry;
import gregtech.api.multitileentity.base.BaseTickableMultiTileEntity;
import gregtech.api.multitileentity.multiblock.base.MultiBlockPart;
import gregtech.api.render.TextureFactory;
import gregtech.api.util.GT_Util;
import gregtech.api.util.GT_Utility;
import net.minecraft.block.Block;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.nbt.NBTTagList;
import net.minecraft.tileentity.TileEntity;
import net.minecraftforge.fluids.FluidStack;
import net.minecraftforge.fluids.IFluidTank;

import static com.google.common.primitives.Ints.saturatedCast;
import static gregtech.api.enums.GT_Values.emptyIconContainerArray;

public class MultiTileBasicMachine extends BaseTickableMultiTileEntity  {
    private static final String TEXTURE_LOCATION = "multitileentity/machines/";
    public IIconContainer[] mTexturesInactive = emptyIconContainerArray;
    public IIconContainer[] mTexturesActive = emptyIconContainerArray;

    protected int mParallel = 1;
    protected boolean mActive = false;
    protected long mStoredEnergy = 0;
    protected FluidTankGT[] mTanksInput = GT_Values.emptyFluidTankGT, mTanksOutput = GT_Values.emptyFluidTankGT;
    protected ItemStack[] mOutputItems = GT_Values.emptyItemStackArray;
    protected FluidStack[] mOutputFluids = GT_Values.emptyFluidStack;

    protected ItemStack[] mInventory = GT_Values.emptyItemStackArray;

    @Override
    public String getTileEntityName() {
        return "gt.multitileentity.machine.basic";
    }


    @Override
    public void writeMultiTileNBT(NBTTagCompound aNBT) {
        super.writeMultiTileNBT(aNBT);
        if (mParallel > 0) aNBT.setInteger(NBT.PARALLEL, mParallel);
        if (mActive) aNBT.setBoolean(NBT.ACTIVE, mActive);

    }
    @Override
    public void readMultiTileNBT(NBTTagCompound aNBT) {
        super.readMultiTileNBT(aNBT);
        if (aNBT.hasKey(NBT.PARALLEL)) mParallel = Math.max(1, aNBT.getInteger(NBT.PARALLEL));
        if (aNBT.hasKey(NBT.ACTIVE)) mActive = aNBT.getBoolean(NBT.ACTIVE);

        mInventory = getDefaultInventory(aNBT);
        if(mInventory != null) {
            final NBTTagList tList = aNBT.getTagList(NBT.INV_LIST, 10);
            for (int i = 0; i < tList.tagCount(); i++) {
                final NBTTagCompound tNBT = tList.getCompoundTagAt(i);
                final int tSlot = tNBT.getShort("s");
                if (tSlot >= 0 && tSlot < mInventory.length) mInventory[tSlot] = GT_Utility.loadItem(tNBT);
            }
        }
        /* Tanks */
        long tCapacity = 1000;
        if (aNBT.hasKey(NBT.TANK_CAPACITY)) tCapacity = saturatedCast(aNBT.getLong(NBT.TANK_CAPACITY));

        mTanksInput = new FluidTankGT[getFluidInputCount()];
        mTanksOutput = new FluidTankGT[getFluidOutputCount()];
        mOutputFluids = new FluidStack[getFluidOutputCount()];
        mOutputItems = new ItemStack[getItemOutputCount()];

        // TODO: See if we need the adjustable map here `.setCapacity(mRecipes, mParallel * 2L)` in place of the `setCapacityMultiplier`
        for (int i = 0; i < mTanksInput.length; i++) mTanksInput[i] = new FluidTankGT(tCapacity).setCapacityMultiplier(mParallel * 2L).readFromNBT(aNBT, NBT.TANK_IN + i);
        for (int i = 0; i < mTanksOutput.length; i++) mTanksOutput[i] = new FluidTankGT().readFromNBT(aNBT, NBT.TANK_OUT + i);
        for (int i = 0; i < mOutputFluids.length; i++) mOutputFluids[i] = FluidStack.loadFluidStackFromNBT(aNBT.getCompoundTag(NBT.FLUID_OUT + "." + i));
        for (int i = 0; i < mOutputItems.length; i++) mOutputItems[i] = ItemStack.loadItemStackFromNBT(aNBT.getCompoundTag(NBT.INV_OUT + "." + i));

    }

    @Override
    public void loadTextureNBT(NBTTagCompound aNBT) {
        // Loading the registry
        final String textureName = aNBT.getString(NBT.TEXTURE);
        mTextures = new IIconContainer[]{
            new Textures.BlockIcons.CustomIcon(TEXTURE_LOCATION + textureName + "/bottom"),
            new Textures.BlockIcons.CustomIcon(TEXTURE_LOCATION + textureName + "/top"),
            new Textures.BlockIcons.CustomIcon(TEXTURE_LOCATION + textureName + "/left"),
            new Textures.BlockIcons.CustomIcon(TEXTURE_LOCATION + textureName + "/front"),
            new Textures.BlockIcons.CustomIcon(TEXTURE_LOCATION + textureName + "/right"),
            new Textures.BlockIcons.CustomIcon(TEXTURE_LOCATION + textureName + "/side")
        };
        mTexturesInactive = new IIconContainer[]{
            new Textures.BlockIcons.CustomIcon(TEXTURE_LOCATION + textureName + "/overlay/inactive/bottom"),
            new Textures.BlockIcons.CustomIcon(TEXTURE_LOCATION + textureName + "/overlay/inactive/top"),
            new Textures.BlockIcons.CustomIcon(TEXTURE_LOCATION + textureName + "/overlay/inactive/left"),
            new Textures.BlockIcons.CustomIcon(TEXTURE_LOCATION + textureName + "/overlay/inactive/front"),
            new Textures.BlockIcons.CustomIcon(TEXTURE_LOCATION + textureName + "/overlay/inactive/right"),
            new Textures.BlockIcons.CustomIcon(TEXTURE_LOCATION + textureName + "/overlay/inactive/back")
        };
        mTexturesActive = new IIconContainer[]{
            new Textures.BlockIcons.CustomIcon(TEXTURE_LOCATION + textureName + "/overlay/active/bottom"),
            new Textures.BlockIcons.CustomIcon(TEXTURE_LOCATION + textureName + "/overlay/active/top"),
            new Textures.BlockIcons.CustomIcon(TEXTURE_LOCATION + textureName + "/overlay/active/left"),
            new Textures.BlockIcons.CustomIcon(TEXTURE_LOCATION + textureName + "/overlay/active/front"),
            new Textures.BlockIcons.CustomIcon(TEXTURE_LOCATION + textureName + "/overlay/active/right"),
            new Textures.BlockIcons.CustomIcon(TEXTURE_LOCATION + textureName + "/overlay/active/back")
        };

    }

    @Override
    public void copyTextures() {
        // Loading an instance
        final TileEntity tCanonicalTileEntity = MultiTileEntityRegistry.getCanonicalTileEntity(getMultiTileEntityRegistryID(), getMultiTileEntityID());
        if(tCanonicalTileEntity instanceof MultiTileBasicMachine){
            mTextures = ((MultiTileBasicMachine)tCanonicalTileEntity).mTextures;
            mTexturesInactive = ((MultiTileBasicMachine)tCanonicalTileEntity).mTexturesInactive;
            mTexturesActive   = ((MultiTileBasicMachine)tCanonicalTileEntity).mTexturesActive;
        } else {
            mTextures = mTexturesInactive = mTexturesActive = emptyIconContainerArray;
        }
    }



    @Override
    public ITexture[] getTexture(Block aBlock, byte aSide, boolean isActive, int aRenderPass) {
        return new ITexture[]{
            TextureFactory.of(mTextures[GT_Values.FACING_ROTATIONS[mFacing][aSide]], GT_Util.getRGBaArray(mRGBa)),
            TextureFactory.of((mActive ? mTexturesActive : mTexturesInactive)[GT_Values.FACING_ROTATIONS[mFacing][aSide]])
        };
    }


    /*
     * Fluids
     */


    /**
     * The number of fluid (input) slots available for this machine
     */
    public int getFluidInputCount() {
        return 2;
    }

    /**
     * The number of fluid (output) slots available for this machine
     */
    public int getFluidOutputCount() {
        return 2;
    }

    public ItemStack[] getDefaultInventory(NBTTagCompound aNBT) {
        final int tSize = Math.max(0, aNBT.getShort(NBT.INV_SIZE));
        return tSize > 0 ? new ItemStack[tSize] : GT_Values.emptyItemStackArray;
    }


    @Override
    public void setLightValue(byte aLightValue) {

    }

    @Override
    public String getInventoryName() {
        final String name = getCustomName();
        if(name != null) return name;
        final MultiTileEntityRegistry tRegistry = MultiTileEntityRegistry.getRegistry(getMultiTileEntityRegistryID());
        return tRegistry == null ? getClass().getName() : tRegistry.getLocal(getMultiTileEntityID());
    }


    @Override
    public boolean isUseableByPlayer(EntityPlayer aPlayer) {
        return playerOwnsThis(aPlayer, false) && mTickTimer > 40 &&
            getTileEntityOffset(0, 0, 0) == this &&
            aPlayer.getDistanceSq(xCoord + 0.5, yCoord + 0.5, zCoord + 0.5) < 64 && allowInteraction(aPlayer);
    }


    @Override
    public boolean isLiquidInput(byte aSide) {
        return aSide != mFacing;
    }

    @Override
    public boolean isLiquidOutput(byte aSide) {
        return aSide != mFacing;
    }


    @Override
    protected IFluidTank[] getFluidTanks(byte aSide) {
        final boolean fluidInput = isLiquidInput(aSide);
        final boolean fluidOutput = isLiquidOutput(aSide);

        if(fluidInput && fluidOutput) {
            final IFluidTank[] rTanks = new IFluidTank[ mTanksInput.length + mTanksOutput.length];
            System.arraycopy(mTanksInput, 0, rTanks, 0, mTanksInput.length);
            System.arraycopy(mTanksOutput, 0, rTanks, mTanksInput.length, mTanksOutput.length);
            return rTanks;
        } else if(fluidInput) {
            return mTanksInput;
        } else if(fluidOutput) {
            return mTanksOutput;
        }
        return GT_Values.emptyFluidTank;
    }

    @Override
    public IFluidTank getFluidTankFillable(byte aSide, FluidStack aFluidToFill) {
        if(!isLiquidInput(aSide)) return null;
        for (FluidTankGT tankGT : mTanksInput) if (tankGT.contains(aFluidToFill)) return tankGT;
//        if (!mRecipes.containsInput(aFluidToFill, this, slot(mRecipes.mInputItemsCount + mRecipes.mOutputItemsCount))) return null;
        for (FluidTankGT fluidTankGT : mTanksInput) if (fluidTankGT.isEmpty()) return fluidTankGT;
        return null;
    }

    @Override
    protected IFluidTank getFluidTankDrainable(byte aSide, FluidStack aFluidToDrain) {
        if(!isLiquidOutput(aSide)) return null;
        for (FluidTankGT fluidTankGT : mTanksOutput)
            if (aFluidToDrain == null ? fluidTankGT.has() : fluidTankGT.contains(aFluidToDrain))
                return fluidTankGT;

        return null;
    }

    /*
     * Energy
     */

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

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

    @Override
    public boolean isUniversalEnergyStored(long aEnergyAmount) {
        return getUniversalEnergyStored() >= aEnergyAmount;
    }

    @Override
    public long getUniversalEnergyStored() {
        return mStoredEnergy;
    }

    @Override
    public long getUniversalEnergyCapacity() {
        return 0;
    }

    @Override
    public long getOutputAmperage() {
        return 1;
    }

    @Override
    public long getOutputVoltage() {
        return 1;
    }

    @Override
    public long getInputAmperage() {
        return 1;
    }

    @Override
    public long getInputVoltage() {
        return 1;
    }


    public boolean isEnergyInputSide(byte aSide) {
        return true;
    }

    public boolean isEnergyOutputSide(byte aSide) {
        return true;
    }

    @Override
    public boolean inputEnergyFrom(byte aSide) {
        if (aSide == GT_Values.SIDE_UNKNOWN) return true;
        if (aSide >= 0 && aSide < 6) {
            if(isInvalid()) return false;
            if (!getCoverBehaviorAtSideNew(aSide).letsEnergyIn(aSide, getCoverIDAtSide(aSide), getComplexCoverDataAtSide(aSide), this)) return false;
            if (isEnetInput()) return isEnergyInputSide(aSide);
        }
        return false;
    }

    @Override
    public boolean outputsEnergyTo(byte aSide) {
        if (aSide == GT_Values.SIDE_UNKNOWN) return true;
        if (aSide >= 0 && aSide < 6) {
            if (isInvalid()) return false;
            if (!getCoverBehaviorAtSideNew(aSide).letsEnergyOut(aSide, getCoverIDAtSide(aSide), getComplexCoverDataAtSide(aSide), this)) return false;
            if (isEnetOutput()) return isEnergyOutputSide(aSide);
        }
        return false;
    }

    /*
     * Inventory
     */

    @Override public boolean hasInventoryBeenModified() {
        return mInventoryChanged;
    }

    @Override
    public boolean isItemValidForSlot(int aSlot, ItemStack aStack) {
        return false;
    }

    @Override
    public int getInventoryStackLimit() {
        return 64;
    }

    /**
     * The number of item (input) slots available for this machine
     */
    public int getItemInputCount() {
        return 2;
    }

    /**
     * The number of item (output) slots available for this machine
     */
    public int getItemOutputCount() {
        return 2;
    }

}