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

import jp.plusplus.fbs.container.inventory.InventoryBasket;
import jp.plusplus.fbs.container.slot.SlotInventory;
import jp.plusplus.fbs.container.slot.SlotShowOnly;
import jp.plusplus.fbs.item.ItemCore;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.entity.player.InventoryPlayer;
import net.minecraft.inventory.Container;
import net.minecraft.inventory.Slot;
import net.minecraft.item.ItemStack;

/**
 * Created by plusplus_F on 2015/11/11.
 */
public class ContainerBasket  extends Container {
    private InventoryBasket inventory;

    public ContainerBasket(InventoryPlayer inventoryPlayer) {
        inventory = new InventoryBasket(inventoryPlayer);
        inventory.openInventory();

        for (int i = 0; i < inventory.getSizeInventory(); i++) {
            this.addSlotToContainer(new SlotInventory(inventory, i, 8 + (i % 9) * 18, 18 + (i / 9) * 18, 3));
        }

        //player inventory
        for (int i = 0; i < 3; i++) {
            for (int j = 0; j < 9; j++) {
                ItemStack item = inventoryPlayer.getStackInSlot(j + i * 9 + 9);
                if (item != null && item.getItem() == ItemCore.basket) {
                    this.addSlotToContainer(new SlotShowOnly(inventoryPlayer, j + i * 9 + 9, 8 + j * 18, 140 + i * 18));
                } else {
                    this.addSlotToContainer(new Slot(inventoryPlayer, j + i * 9 + 9, 8 + j * 18, 140 + i * 18));
                }
            }
        }

        //player slots
        for (int i = 0; i < 9; i++) {
            ItemStack item = inventoryPlayer.getStackInSlot(i);
            if (item != null && item.getItem() == ItemCore.basket) {
                this.addSlotToContainer(new SlotShowOnly(inventoryPlayer, i, 8 + i * 18, 198));
            } else {
                this.addSlotToContainer(new Slot(inventoryPlayer, i, 8 + i * 18, 198));
            }
        }
    }

    @Override
    public boolean canInteractWith(EntityPlayer p_75145_1_) {
        return true;
    }

    @Override
    public ItemStack transferStackInSlot(EntityPlayer p_82846_1_, int p_82846_2_) {
        ItemStack itemstack = null;
        Slot slot = (Slot) this.inventorySlots.get(p_82846_2_);

        if (slot != null && slot.getHasStack()) {
            ItemStack itemstack1 = slot.getStack();
            itemstack = itemstack1.copy();

            if(slot.getStack()==null || slot.getStack().getItem() == ItemCore.basket){
                return null;
            }
            else if (p_82846_2_ < this.inventory.getSizeInventory()) {
                if (!this.mergeItemStack(itemstack1, this.inventory.getSizeInventory(), this.inventorySlots.size(), true)) {
                    return null;
                }
            }
            else if(!inventory.isItemValidForSlot(p_82846_2_, itemstack)){
                return null;
            }
            else if (!this.mergeItemStack(itemstack1, 0, this.inventory.getSizeInventory(), false)) {
                return null;
            }
            if (itemstack1.stackSize == 0) {
                slot.putStack((ItemStack) null);
            } else {
                slot.onSlotChanged();
            }
        }

        return itemstack;
    }

    @Override
    public void onContainerClosed(EntityPlayer p_75134_1_) {
        super.onContainerClosed(p_75134_1_);
        this.inventory.closeInventory();
    }
}