summaryrefslogtreecommitdiff
path: root/src/main/java/jp/plusplus/fbs/pottery/usable/container/InventoryPotteryUsable.java
blob: ec5ef3260656da6a7577e76241966fe1a838d067 (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
package jp.plusplus.fbs.pottery.usable.container;

import jp.plusplus.fbs.api.IPottery;
import jp.plusplus.fbs.pottery.ItemBlockPottery;
import jp.plusplus.fbs.pottery.PotteryRegistry;
import jp.plusplus.fbs.pottery.usable.PotteryBase;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.inventory.IInventory;
import net.minecraft.item.ItemBlock;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.nbt.NBTTagList;

/**
 * Created by plusplus_F on 2016/03/30.
 * インベントリ持ち魔法の壺のインベントリ
 */
public class InventoryPotteryUsable implements IInventory{
    public EntityPlayer player;
    public ItemStack potteryStack;
    public int potteryStackIndex;
    public PotteryBase potteryEffect;
    public IPottery pottery;

    protected ItemStack[] itemStacks;
    protected int inventorySize;

    public InventoryPotteryUsable(EntityPlayer player){
        this.player=player;
        this.potteryStack=player.getCurrentEquippedItem();
        this.potteryStackIndex=player.inventory.currentItem;
        this.potteryEffect= PotteryRegistry.getPotteryEffect(ItemBlockPottery.getId(potteryStack));
        this.pottery=(IPottery)((ItemBlock)potteryStack.getItem()).field_150939_a;

        switch (pottery.getSize(potteryStack.getTagCompound())){
            case MEDIUM: inventorySize=9*2; break;
            case LARGE: inventorySize=9*3; break;
            default: inventorySize=9*1; break;
        }
        itemStacks=new ItemStack[inventorySize];
    }

    @Override
    public int getSizeInventory() {
        return inventorySize;
    }

    @Override
    public ItemStack getStackInSlot(int p_70301_1_) {
        return itemStacks[p_70301_1_];
    }

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

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

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

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

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

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

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

        this.markDirty();
    }

    @Override
    public String getInventoryName() {
        return potteryStack.getDisplayName();
    }

    @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 (!potteryStack.hasTagCompound()) {
            potteryStack.setTagCompound(new NBTTagCompound());
            potteryStack.getTagCompound().setTag(PotteryBase.ITEM_STACKS, new NBTTagList());
        }
        else if(!potteryStack.getTagCompound().hasKey(PotteryBase.ITEM_STACKS)){
            potteryStack.getTagCompound().setTag(PotteryBase.ITEM_STACKS, new NBTTagList());
        }

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

        //インベントリが開くときの処理
        for(int i=0;i<inventorySize;i++){
            itemStacks[i]=potteryEffect.onInventoryOpening(player, potteryStack, i, itemStacks[i]);
        }
    }

    @Override
    public void closeInventory() {
        //インベントリが閉じるときのの処理
        for(int i=0;i<inventorySize;i++){
            itemStacks[i]=potteryEffect.onInventoryClosing(player, potteryStack, i, itemStacks[i]);
        }

        NBTTagList tagList = new NBTTagList();
        for (int i = 0; i < itemStacks.length; i++) {
            if (itemStacks[i] != null) {
                NBTTagCompound compound = new NBTTagCompound();
                compound.setByte("Slot", (byte) i);
                itemStacks[i].writeToNBT(compound);
                tagList.appendTag(compound);
            }
        }
        ItemStack result = new ItemStack(potteryStack.getItem(), 1, potteryStack.getItemDamage());
        result.setTagCompound((NBTTagCompound)potteryStack.getTagCompound().copy());
        result.getTagCompound().setTag(PotteryBase.ITEM_STACKS, tagList);

        player.inventory.mainInventory[potteryStackIndex] = result;
    }

    @Override
    public boolean isItemValidForSlot(int p_94041_1_, ItemStack p_94041_2_) {
        return potteryEffect.isItemValid(player, potteryStack, p_94041_1_, p_94041_2_);
    }
}