-
Notifications
You must be signed in to change notification settings - Fork 497
/
SimpleInventory.java
171 lines (144 loc) · 4.02 KB
/
SimpleInventory.java
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
/**
* Copyright (c) 2011-2014, SpaceToad and the BuildCraft Team
* http://www.mod-buildcraft.com
*
* BuildCraft is distributed under the terms of the Minecraft Mod Public
* License 1.0, or MMPL. Please check the contents of the license located in
* http://www.mod-buildcraft.com/MMPL-1.0.txt
*/
package buildcraft.core.inventory;
import java.util.LinkedList;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.inventory.IInventory;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.nbt.NBTTagList;
import net.minecraft.tileentity.TileEntity;
import buildcraft.api.core.MinecraftConstants;
import buildcraft.core.utils.INBTTagable;
public class SimpleInventory implements IInventory, INBTTagable {
private final ItemStack[] _contents;
private final String _name;
private final int _stackLimit;
private final LinkedList<TileEntity> _listener = new LinkedList<TileEntity>();
public SimpleInventory(int size, String name, int stackLimit) {
_contents = new ItemStack[size];
_name = name;
_stackLimit = stackLimit;
}
@Override
public int getSizeInventory() {
return _contents.length;
}
@Override
public ItemStack getStackInSlot(int slotId) {
return _contents[slotId];
}
@Override
public ItemStack decrStackSize(int slotId, int count) {
if (slotId < _contents.length && _contents[slotId] != null) {
if (_contents[slotId].stackSize > count) {
ItemStack result = _contents[slotId].splitStack(count);
markDirty();
return result;
}
ItemStack stack = _contents[slotId];
setInventorySlotContents(slotId, null);
return stack;
}
return null;
}
@Override
public void setInventorySlotContents(int slotId, ItemStack itemstack) {
if (slotId >= _contents.length) {
return;
}
_contents[slotId] = itemstack;
if (itemstack != null && itemstack.stackSize > this.getInventoryStackLimit()) {
itemstack.stackSize = this.getInventoryStackLimit();
}
markDirty();
}
@Override
public String getInventoryName() {
return _name;
}
@Override
public int getInventoryStackLimit() {
return _stackLimit;
}
@Override
public boolean isUseableByPlayer(EntityPlayer entityplayer) {
return true;
}
@Override
public void openInventory() {
}
@Override
public void closeInventory() {
}
@Override
public void readFromNBT(NBTTagCompound data) {
readFromNBT(data, "items");
}
public void readFromNBT(NBTTagCompound data, String tag) {
NBTTagList nbttaglist = data.getTagList(tag, MinecraftConstants.NBTTagCompound);
for (int j = 0; j < nbttaglist.tagCount(); ++j) {
NBTTagCompound slot = nbttaglist.getCompoundTagAt(j);
int index;
if (slot.hasKey("index")) {
index = slot.getInteger("index");
} else {
index = slot.getByte("Slot");
}
if (index >= 0 && index < _contents.length) {
setInventorySlotContents(index, ItemStack.loadItemStackFromNBT(slot));
}
}
}
@Override
public void writeToNBT(NBTTagCompound data) {
writeToNBT(data, "items");
}
public void writeToNBT(NBTTagCompound data, String tag) {
NBTTagList slots = new NBTTagList();
for (byte index = 0; index < _contents.length; ++index) {
if (_contents[index] != null && _contents[index].stackSize > 0) {
NBTTagCompound slot = new NBTTagCompound();
slots.appendTag(slot);
slot.setByte("Slot", index);
_contents[index].writeToNBT(slot);
}
}
data.setTag(tag, slots);
}
public void addListener(TileEntity listner) {
_listener.add(listner);
}
@Override
public ItemStack getStackInSlotOnClosing(int slotId) {
if (this._contents[slotId] == null) {
return null;
}
ItemStack stackToTake = this._contents[slotId];
setInventorySlotContents(slotId, null);
return stackToTake;
}
public ItemStack[] getItemStacks() {
return _contents;
}
@Override
public boolean isItemValidForSlot(int i, ItemStack itemstack) {
return true;
}
@Override
public boolean hasCustomInventoryName() {
return false;
}
@Override
public void markDirty() {
for (TileEntity handler : _listener) {
handler.markDirty();
}
}
}