summaryrefslogtreecommitdiff
path: root/src/main/java/jp/plusplus/fbs/tileentity/TileEntityFillingTable.java
blob: ebd96281cae8aeebca9422307037565b7799092d (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
package jp.plusplus.fbs.tileentity;

import cpw.mods.fml.relauncher.Side;
import cpw.mods.fml.relauncher.SideOnly;
import jp.plusplus.fbs.Registry;
import jp.plusplus.fbs.block.BlockCore;
import jp.plusplus.fbs.item.ItemBookSorcery;
import jp.plusplus.fbs.item.ItemCore;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.inventory.ISidedInventory;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.nbt.NBTTagList;
import net.minecraft.network.NetworkManager;
import net.minecraft.network.Packet;
import net.minecraft.network.play.server.S35PacketUpdateTileEntity;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.util.IIcon;
import net.minecraftforge.common.util.ForgeDirection;
import net.minecraftforge.fluids.*;

/**
 * Createdby pluslus_Fon 2015/06/14.
 */
public class TileEntityFillingTable extends TileEntity implements ISidedInventory,IFluidHandler {
    public static final int TANK_CAPACITY=4000;
    public TankFBS tank=new TankFBS(TANK_CAPACITY);

    private static final int[] slotsBook=new int[]{0}, slotsFluid=new int[]{1,2};
    public ItemStack[] itemStacks=new ItemStack[3];

    public static final short MAX_PROGRESS=20*10;
    public short progress;

    @Override
    public void readFromNBT(NBTTagCompound par1NBTTagCompound){
        super.readFromNBT(par1NBTTagCompound);

        progress= par1NBTTagCompound.getShort("Progress");

        NBTTagList nbttaglist = (NBTTagList)par1NBTTagCompound.getTag("Items");
        itemStacks = new ItemStack[getSizeInventory()];
        for (int i=0;i<nbttaglist.tagCount();i++){
            NBTTagCompound nbt = nbttaglist.getCompoundTagAt(i);
            byte b0 = nbt.getByte("Slot");

            if (b0>=0 && b0<itemStacks.length){
                itemStacks[b0] = ItemStack.loadItemStackFromNBT(nbt);
            }
        }

        tank = new TankFBS(TANK_CAPACITY);
        if (par1NBTTagCompound.hasKey("Tank")) {
            tank.readFromNBT(par1NBTTagCompound.getCompoundTag("Tank"));
        }
    }
    @Override
    public void writeToNBT(NBTTagCompound par1NBTTagCompound){
        super.writeToNBT(par1NBTTagCompound);

        par1NBTTagCompound.setShort("Progress", progress);

        NBTTagList nbttaglist = new NBTTagList();
        for (int i=0;i<itemStacks.length;i++){
            if (itemStacks[i] != null){
                NBTTagCompound nbt = new NBTTagCompound();
                nbt.setByte("Slot", (byte) i);
                itemStacks[i].writeToNBT(nbt);
                nbttaglist.appendTag(nbt);
            }
        }
        par1NBTTagCompound.setTag("Items", nbttaglist);

        NBTTagCompound nbt = new NBTTagCompound();
        tank.writeToNBT(nbt);
        par1NBTTagCompound.setTag("Tank", nbt);
    }

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

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

    @Override
    public void updateEntity() {
        if(worldObj.isRemote) return;

        //------------ filling --------------
        if(itemStacks[0]!=null && itemStacks[0].getItem()==ItemCore.bookSorcery){
            Registry.BookData bd=Registry.GetBookDataFromItemStack(itemStacks[0]);
            if(bd!=null && bd.isMagic){
                int amount=40*bd.lv;
                if(tank.getFluidAmount()>=amount){
                    if (progress >= MAX_PROGRESS) {
                        tank.drain(amount, true);

                        //一定確率で失敗する
                        float pr;
                        if(bd.lv<=5) pr=1.0f;
                        else if(bd.lv<40) pr=1.0f-0.02f*(bd.lv-5);
                        else pr=0.3f;
                        if(worldObj.rand.nextFloat()<pr){
                            ItemBookSorcery.setMagicMaxUse(itemStacks[0], ItemBookSorcery.getMagicMaxUse(itemStacks[0])+1);
                        }
                        else{
                            setInventorySlotContents(0, new ItemStack(ItemCore.bookBroken, 1, itemStacks[0].getItemDamage()));
                        }

                        markDirty();
                        progress=0;
                    }

                    progress++;
                }
            }
        }
        else{
            progress=0;
        }

        //------------ bucket ---------------

        //checking inventory
        if (itemStacks[1] == null) return;
        if (itemStacks[1].stackSize <= 0) {
            itemStacks[1] = null;
            return;
        }

        FluidStack fluid2 = FluidContainerRegistry.getFluidForFilledItem(itemStacks[1]);
        if(fluid2 != null && fluid2.getFluid()!=null && fluid2.getFluid()==BlockCore.mana) {
            int put = fill(ForgeDirection.UNKNOWN, fluid2, false);

            if (put == fluid2.amount) {
                ItemStack emptyContainer = FluidContainerRegistry.drainFluidContainer(itemStacks[1]);
                if (emptyContainer != null) {
                    if (itemStacks[2] != null) {
                        if (!itemStacks[2].isItemEqual(emptyContainer)) return;
                        if (itemStacks[2].stackSize + emptyContainer.stackSize > itemStacks[2].getMaxStackSize()) return;
                        itemStacks[2].stackSize+=emptyContainer.stackSize;
                    }
                    else {
                        setInventorySlotContents(2, emptyContainer);
                    }
                }

                itemStacks[1].stackSize--;
                if(itemStacks[1].stackSize<=1){
                    setInventorySlotContents(1, null);
                }

                fill(ForgeDirection.UNKNOWN, fluid2, true);
                markDirty();
            }
        }
        else{
            FluidStack fluid = tank.getFluid();
            if (fluid != null && fluid.getFluid() != null) {

                ItemStack get = FluidContainerRegistry.fillFluidContainer(fluid.copy(), itemStacks[1]);
                if (get != null) {
                    int cap = FluidContainerRegistry.getContainerCapacity(get);
                    if (fluid.amount < cap) return;

                    if (itemStacks[2] != null) {
                        if (!itemStacks[2].isItemEqual(get)) return;
                        if (itemStacks[2].stackSize + get.stackSize > itemStacks[1].getMaxStackSize()) return;
                    }

                    if (itemStacks[2] == null || itemStacks[2].stackSize <= 0) {
                        setInventorySlotContents(2, get);
                    } else {
                        itemStacks[2].stackSize += get.stackSize;
                    }

                    itemStacks[1].stackSize--;
                    if (itemStacks[1].stackSize <= 1) {
                        setInventorySlotContents(1, null);
                    }

                    drain(ForgeDirection.UNKNOWN, cap, true);
                    markDirty();
                }
            }
        }
    }

    //-------------------------------------------------------------------------------------

    @Override
    public int fill(ForgeDirection from, FluidStack resource, boolean doFill) {
        if (resource == null || resource.getFluid() == null){
            return 0;
        }

        FluidStack current = tank.getFluid();
        FluidStack resourceCopy = resource.copy();
        if (current != null && current.amount > 0 && !current.isFluidEqual(resourceCopy)){
            return 0;
        }

        int i = 0;
        int used = tank.fill(resourceCopy, doFill);
        resourceCopy.amount -= used;
        i += used;

        return i;
    }

    @Override
    public FluidStack drain(ForgeDirection forgeDirection,  FluidStack resource, boolean doDrain) {
        if (resource == null) {
            return null;
        }
        if (tank.getFluidType() == resource.getFluid()) {
            return tank.drain(resource.amount, doDrain);
        }
        return null;
    }
    @Override
    public FluidStack drain(ForgeDirection forgeDirection, int max, boolean b) {
        return tank.drain(max, b);
    }

    @Override
    public boolean canFill(ForgeDirection from, Fluid fluid) {
        return fluid == BlockCore.mana;
    }

    @Override
    public boolean canDrain(ForgeDirection from, Fluid fluid) {
        return !tank.isEmpty();
    }

    @Override
    public FluidTankInfo[] getTankInfo(ForgeDirection from) {
        return new FluidTankInfo[0];
    }

    public int getProgressScaled(int par1){
        return par1*this.progress/MAX_PROGRESS;
    }
    @SideOnly(Side.CLIENT)
    public IIcon getFluidIcon(){
        Fluid fluid = tank.getFluidType();
        return fluid != null ? fluid.getIcon() : null;
    }

    //------------------------------------------------------------------
    @Override
    public int getSizeInventory() {
        return itemStacks.length;
    }
    @Override
    public ItemStack getStackInSlot(int i) {
        return itemStacks[i];
    }
    @Override
    public ItemStack decrStackSize(int i, int j) {
        if (itemStacks[i] != null){
            ItemStack itemstack;

            if (itemStacks[i].stackSize <= j){
                itemstack = itemStacks[i];
                itemStacks[i] = null;
                markDirty();
                return itemstack;
            }
            else{
                itemstack = itemStacks[i].splitStack(j);

                if (itemStacks[i].stackSize == 0){
                    itemStacks[i] = null;
                }
                markDirty();
                return itemstack;
            }
        }
        else{
            return null;
        }
    }
    @Override
    public ItemStack getStackInSlotOnClosing(int i) {
        if (itemStacks[i] != null){
            ItemStack itemstack = itemStacks[i];
            itemStacks[i] = null;
            markDirty();
            return itemstack;
        }
        else{
            return null;
        }
    }
    @Override
    public void setInventorySlotContents(int i, ItemStack itemStack) {
        itemStacks[i] = itemStack;
        if (itemStack != null && itemStack.stackSize > this.getInventoryStackLimit()){
            itemStack.stackSize = getInventoryStackLimit();
        }
        markDirty();
    }
    @Override
    public String getInventoryName() {
        return BlockCore.fillingTable.getLocalizedName();
    }
    @Override
    public boolean hasCustomInventoryName() {
        return false;
    }
    @Override
    public int getInventoryStackLimit() {
        return 64;
    }
    @Override
    public boolean isUseableByPlayer(EntityPlayer entityPlayer) {
        return worldObj.getTileEntity(xCoord, yCoord, zCoord) != this ? false : entityPlayer.getDistanceSq((double)xCoord+0.5D, (double)yCoord+0.5D, (double)zCoord+0.5D) <= 64.0D;
    }
    @Override
    public void openInventory() {
    }
    @Override
    public void closeInventory() {
    }
    @Override
    public boolean isItemValidForSlot(int i, ItemStack itemstack) {
        if(i==0) {
            return itemstack.getItem()==ItemCore.bookSorcery;
        }
        else if(i==1){
            if(itemstack.getItem()==ItemCore.bookSorcery) return false;
            FluidStack f=FluidContainerRegistry.getFluidForFilledItem(itemstack);
            return f!=null && f.getFluid()==BlockCore.mana;
        }
        return false;
    }
    @Override
    public int[] getAccessibleSlotsFromSide(int var1) {
        if(var1==0 || var1==1) return slotsBook;
        return slotsFluid;
    }
    @Override
    public boolean canInsertItem(int i, ItemStack itemstack, int j) {
        return i!=2 && isItemValidForSlot(i, itemstack);
    }
    @Override
    public boolean canExtractItem(int i, ItemStack itemstack, int j) {
        return i==0 || i==2;
    }
}