summaryrefslogtreecommitdiff
path: root/src/main/java/jp/plusplus/fbs/container/inventory/InventoryBasket.java
blob: 9db54850310d16327aa85d6402050948fac83b1a (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
package jp.plusplus.fbs.container.inventory;

import jp.plusplus.fbs.alchemy.AlchemyRegistry;
import jp.plusplus.fbs.alchemy.IAlchemyMaterial;
import jp.plusplus.fbs.alchemy.IAlchemyProduct;
import jp.plusplus.fbs.item.ItemCharm;
import jp.plusplus.fbs.item.ItemCore;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.entity.player.InventoryPlayer;
import net.minecraft.inventory.IInventory;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.nbt.NBTTagList;
import net.minecraft.util.StatCollector;

/**
 * Created by plusplus_F on 2015/11/11.
 */
public class InventoryBasket implements IInventory {
    private InventoryPlayer inventoryPlayer;
    private ItemStack currentItem;
    private ItemStack[] items;
    private int currentIndex;

    public InventoryBasket(InventoryPlayer inventory) {
        inventoryPlayer = inventory;
        currentItem = inventoryPlayer.getCurrentItem();
        currentIndex=inventory.currentItem;
        items = new ItemStack[54];
    }
    public InventoryBasket(InventoryPlayer inventory, int index) {
        inventoryPlayer = inventory;
        this.currentItem = inventory.getStackInSlot(index);
        currentIndex=index;
        items = new ItemStack[54];
    }

    @Override
    public int getSizeInventory() {
        return items.length;
    }

    @Override
    public ItemStack getStackInSlot(int slot) {
        return items[slot];
    }

    @Override
    public ItemStack decrStackSize(int i, int size) {
        if (this.items[i] != null) {
            ItemStack itemstack;

            if (this.items[i].stackSize <= size) {
                itemstack = this.items[i];
                this.items[i] = null;
                this.markDirty();
                return itemstack;
            } else {
                itemstack = this.items[i].splitStack(size);

                if (this.items[i].stackSize == 0) {
                    this.items[i] = null;
                }

                this.markDirty();
                return itemstack;
            }
        }
        return null;
    }

    @Override
    public ItemStack getStackInSlotOnClosing(int i) {
        if (this.items[i] != null) {
            ItemStack itemstack = this.items[i];
            this.items[i] = null;
            return itemstack;
        }
        return null;
    }

    @Override
    public void setInventorySlotContents(int i, ItemStack item) {
        this.items[i] = item;

        if (item != null && item.stackSize > this.getInventoryStackLimit()) {
            item.stackSize = this.getInventoryStackLimit();
        }

        this.markDirty();
    }

    @Override
    public String getInventoryName() {
        return StatCollector.translateToLocal(ItemCore.basket.getUnlocalizedName() + ".name");
    }

    @Override
    public boolean hasCustomInventoryName() {
        return false;
    }

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

    @Override
    public void markDirty() {
    }

    @Override
    public boolean isUseableByPlayer(EntityPlayer p_70300_1_) {
        return true;
    }

    @Override
    public void openInventory() {
        if (!currentItem.hasTagCompound()) {
            currentItem.setTagCompound(new NBTTagCompound());
            currentItem.getTagCompound().setTag("Items", new NBTTagList());
        }
        else if(!currentItem.getTagCompound().hasKey("Items")){
            currentItem.getTagCompound().setTag("Items", new NBTTagList());
        }

        NBTTagList tags = (NBTTagList) currentItem.getTagCompound().getTag("Items");
        for (int i = 0; i < tags.tagCount(); i++) {
            NBTTagCompound tagCompound = tags.getCompoundTagAt(i);
            int slot = tagCompound.getByte("Slot");
            if (slot >= 0 && slot < items.length) {
                items[slot] = ItemStack.loadItemStackFromNBT(tagCompound);
            }
        }
    }

    @Override
    public void closeInventory() {
        NBTTagList tagList = new NBTTagList();
        for (int i = 0; i < items.length; i++) {
            if (items[i] != null) {
                NBTTagCompound compound = new NBTTagCompound();
                compound.setByte("Slot", (byte) i);
                items[i].writeToNBT(compound);
                tagList.appendTag(compound);
            }
        }
        ItemStack result = new ItemStack(currentItem.getItem(), 1, currentItem.getItemDamage());
        result.setTagCompound(new NBTTagCompound());;
        result.getTagCompound().setTag("Items", tagList);
        result.getTagCompound().setInteger("Color", currentItem.getTagCompound().getInteger("Color"));

        inventoryPlayer.mainInventory[currentIndex] = result;
    }

    @Override
    public boolean isItemValidForSlot(int i, ItemStack item) {
        Item it=item.getItem();
        return it instanceof IAlchemyMaterial || it instanceof IAlchemyProduct || AlchemyRegistry.isBasketItem(item);
    }
}