summaryrefslogtreecommitdiff
path: root/src/main/java/jp/plusplus/fbs/container/ContainerFBSWorkbench.java
blob: 8a8e33578f807230a3dc3485b660b4ee5233e154 (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
package jp.plusplus.fbs.container;

import cpw.mods.fml.relauncher.Side;
import cpw.mods.fml.relauncher.SideOnly;
import jp.plusplus.fbs.block.BlockCore;
import jp.plusplus.fbs.container.slot.SlotCrafting;
import jp.plusplus.fbs.container.slot.SlotTakeOnly;
import jp.plusplus.fbs.tileentity.TileEntityFBSWorkbench;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.inventory.Container;
import net.minecraft.inventory.ICrafting;
import net.minecraft.inventory.Slot;
import net.minecraft.item.ItemStack;
import net.minecraftforge.fluids.FluidStack;

/**
 * Createdby pluslus_Fon 2015/06/14.
 */
public class ContainerFBSWorkbench extends Container {
    protected TileEntityFBSWorkbench entity;
    public int lastAmount;

    public ContainerFBSWorkbench(EntityPlayer player, TileEntityFBSWorkbench tileEntity) {
        this.entity = tileEntity;

        //inventory's inventory
        for(int i=0;i<9;i++) {
            this.addSlotToContainer(new Slot(this.entity, i, 30 + (i % 3) * 18, 17 + (i / 3) * 18));
        }
        this.addSlotToContainer(new SlotCrafting(player, this.entity, 9, 124, 35));

        this.addSlotToContainer(new Slot(this.entity, 10, 66, 71));
        this.addSlotToContainer(new SlotTakeOnly(this.entity, 11, 30, 71));

        //player inventory
        for (int i = 0; i < 3; i++) {
            for (int j = 0; j < 9; j++) {
                this.addSlotToContainer(new Slot(player.inventory, j + i * 9 + 9, 8 + j * 18, 103 + i * 18));
            }
        }

        //player slots
        for (int i = 0; i < 9; i++) {
            this.addSlotToContainer(new Slot(player.inventory, i, 8 + i * 18, 161));
        }
    }

    @Override
    public void addCraftingToCrafters(ICrafting par1ICrafting) {
        super.addCraftingToCrafters(par1ICrafting);
        par1ICrafting.sendProgressBarUpdate(this, 0, entity.tank.getFluidAmount());
    }
    @Override
    public void detectAndSendChanges() {
        super.detectAndSendChanges();
        for (int i = 0; i < this.crafters.size(); i++) {
            ICrafting icrafting = (ICrafting) this.crafters.get(i);
            if (lastAmount != entity.tank.getFluidAmount()) {
                icrafting.sendProgressBarUpdate(this, 0, entity.tank.getFluidAmount());
            }
        }
        lastAmount = entity.tank.getFluidAmount();
    }
    @Override
    @SideOnly(Side.CLIENT)
    public void updateProgressBar(int par1, int par2) {
        if (par1 == 0) {
            for (int i = 0; i < this.crafters.size(); i++) {
                ICrafting icrafting = (ICrafting) this.crafters.get(i);
                if (lastAmount != entity.tank.getFluidAmount()) {
                    icrafting.sendProgressBarUpdate(this, 0, entity.tank.getFluidAmount());
                }
            }
            if (par2 > 0) {
                if (entity.tank.getFluid() == null) entity.tank.setFluid(new FluidStack(BlockCore.mana, par2));
                else entity.tank.setAmount(par2);
            } else {
                entity.tank.setFluid(null);
            }
        }
    }


    @Override
    public boolean canInteractWith(EntityPlayer entityPlayer) {
        return entity.isUseableByPlayer(entityPlayer);
    }

    @Override
    public ItemStack transferStackInSlot(EntityPlayer par1EntityPlayer, int par2) {
        ItemStack itemStack = null;
        Slot slot = (Slot) this.inventorySlots.get(par2);

        if (slot != null && slot.getHasStack()) {
            ItemStack stack = slot.getStack();
            itemStack = stack.copy();

            if (par2 >= 0 && par2 <= 11) {
                if(par2==9){
                    if(entity.product!=null){
                        entity.tank.drain(entity.product.mana*stack.stackSize, true);
                    }
                }
                if (!this.mergeItemStack(stack, 12, 48, true)) {
                    return null;
                }
                slot.onSlotChange(stack, itemStack);
            } else {
                if (entity.isItemValidForSlot(10, stack)) {
                    if (!this.mergeItemStack(stack, 10, 11, false)) {
                        return null;
                    }
                } else if (par2 >= 12 && par2 < 39) {
                    if (!this.mergeItemStack(stack, 39, 48, false)) {
                        return null;
                    }
                } else if (par2 >= 39 && par2 < 48 && !this.mergeItemStack(stack, 12, 39, false)) {
                    return null;
                }
            }

            if (stack.stackSize == 0) {
                slot.putStack((ItemStack) null);
            } else {
                slot.onSlotChanged();
            }

            if (stack.stackSize == itemStack.stackSize) {
                return null;
            }
            slot.onPickupFromSlot(par1EntityPlayer, stack);
        }

        return itemStack;
    }
}