InventoryBasic .java

net.minecraft.inventory.InventoryBasic (class, 282 lines): members: removeInventoryChangeListener, addInventoryChangeListener, inventoryContents, getField.

net.minecraft.inventoryclass282 linescommon7.3 KB

Implements

API Surface (22)

void addInventoryChangeListener(IInvBasic listener)
void removeInventoryChangeListener(IInvBasic listener)
ItemStack getStackInSlot(int index)
ItemStack decrStackSize(int index, int count)
ItemStack func_174894_a(ItemStack stack)
ItemStack removeStackFromSlot(int index)
void setInventorySlotContents(int index, ItemStack stack)
int getSizeInventory()
String getName()
boolean hasCustomName()
void setCustomName(String inventoryTitleIn)
IChatComponent getDisplayName()
int getInventoryStackLimit()
void markDirty()
boolean isUseableByPlayer(EntityPlayer player)
void openInventory(EntityPlayer player)
void closeInventory(EntityPlayer player)
boolean isItemValidForSlot(int index, ItemStack stack)
int getField(int id)
void setField(int id, int value)
int getFieldCount()
void clear()

Fields & Constants (5)

inventoryTitle
slotsCount
inventoryContents
changeListeners
hasCustomName

Full Source

InventoryBasic.java282 lines · MCP 1.8.9 · verbatimOriginal ↗
1
package net.minecraft.inventory;
2
3
import com.google.common.collect.Lists;
4
import java.util.List;
5
import net.minecraft.entity.player.EntityPlayer;
6
import net.minecraft.item.ItemStack;
7
import net.minecraft.util.ChatComponentText;
8
import net.minecraft.util.ChatComponentTranslation;
9
import net.minecraft.util.IChatComponent;
10
11
public class InventoryBasic implements IInventory
12
{
13
    private String inventoryTitle;
14
    private int slotsCount;
15
    private ItemStack[] inventoryContents;
16
    private List<IInvBasic> changeListeners;
17
    private boolean hasCustomName;
18
19
    public InventoryBasic(String title, boolean customName, int slotCount)
20
    {
21
        this.inventoryTitle = title;
22
        this.hasCustomName = customName;
23
        this.slotsCount = slotCount;
24
        this.inventoryContents = new ItemStack[slotCount];
25
    }
26
27
    public InventoryBasic(IChatComponent title, int slotCount)
28
    {
29
        this(title.getUnformattedText(), true, slotCount);
30
    }
31
32
    /**
33
     * Add a listener that will be notified when any item in this inventory is modified.
34
     *  
35
     * @param listener the listener to add
36
     */
37
    public void addInventoryChangeListener(IInvBasic listener)
38
    {
39
        if (this.changeListeners == null)
40
        {
41
            this.changeListeners = Lists.<IInvBasic>newArrayList();
42
        }
43
44
        this.changeListeners.add(listener);
45
    }
46
47
    /**
48
     * removes the specified IInvBasic from receiving further change notices
49
     *  
50
     * @param listener the listener to remove
51
     */
52
    public void removeInventoryChangeListener(IInvBasic listener)
53
    {
54
        this.changeListeners.remove(listener);
55
    }
56
57
    /**
58
     * Returns the stack in the given slot.
59
     */
60
    public ItemStack getStackInSlot(int index)
61
    {
62
        return index >= 0 && index < this.inventoryContents.length ? this.inventoryContents[index] : null;
63
    }
64
65
    /**
66
     * Removes up to a specified number of items from an inventory slot and returns them in a new stack.
67
     */
68
    public ItemStack decrStackSize(int index, int count)
69
    {
70
        if (this.inventoryContents[index] != null)
71
        {
72
            if (this.inventoryContents[index].stackSize <= count)
73
            {
74
                ItemStack itemstack1 = this.inventoryContents[index];
75
                this.inventoryContents[index] = null;
76
                this.markDirty();
77
                return itemstack1;
78
            }
79
            else
80
            {
81
                ItemStack itemstack = this.inventoryContents[index].splitStack(count);
82
83
                if (this.inventoryContents[index].stackSize == 0)
84
                {
85
                    this.inventoryContents[index] = null;
86
                }
87
88
                this.markDirty();
89
                return itemstack;
90
            }
91
        }
92
        else
93
        {
94
            return null;
95
        }
96
    }
97
98
    public ItemStack func_174894_a(ItemStack stack)
99
    {
100
        ItemStack itemstack = stack.copy();
101
102
        for (int i = 0; i < this.slotsCount; ++i)
103
        {
104
            ItemStack itemstack1 = this.getStackInSlot(i);
105
106
            if (itemstack1 == null)
107
            {
108
                this.setInventorySlotContents(i, itemstack);
109
                this.markDirty();
110
                return null;
111
            }
112
113
            if (ItemStack.areItemsEqual(itemstack1, itemstack))
114
            {
115
                int j = Math.min(this.getInventoryStackLimit(), itemstack1.getMaxStackSize());
116
                int k = Math.min(itemstack.stackSize, j - itemstack1.stackSize);
117
118
                if (k > 0)
119
                {
120
                    itemstack1.stackSize += k;
121
                    itemstack.stackSize -= k;
122
123
                    if (itemstack.stackSize <= 0)
124
                    {
125
                        this.markDirty();
126
                        return null;
127
                    }
128
                }
129
            }
130
        }
131
132
        if (itemstack.stackSize != stack.stackSize)
133
        {
134
            this.markDirty();
135
        }
136
137
        return itemstack;
138
    }
139
140
    /**
141
     * Removes a stack from the given slot and returns it.
142
     */
143
    public ItemStack removeStackFromSlot(int index)
144
    {
145
        if (this.inventoryContents[index] != null)
146
        {
147
            ItemStack itemstack = this.inventoryContents[index];
148
            this.inventoryContents[index] = null;
149
            return itemstack;
150
        }
151
        else
152
        {
153
            return null;
154
        }
155
    }
156
157
    /**
158
     * Sets the given item stack to the specified slot in the inventory (can be crafting or armor sections).
159
     */
160
    public void setInventorySlotContents(int index, ItemStack stack)
161
    {
162
        this.inventoryContents[index] = stack;
163
164
        if (stack != null && stack.stackSize > this.getInventoryStackLimit())
165
        {
166
            stack.stackSize = this.getInventoryStackLimit();
167
        }
168
169
        this.markDirty();
170
    }
171
172
    /**
173
     * Returns the number of slots in the inventory.
174
     */
175
    public int getSizeInventory()
176
    {
177
        return this.slotsCount;
178
    }
179
180
    /**
181
     * Get the name of this object. For players this returns their username
182
     */
183
    public String getName()
184
    {
185
        return this.inventoryTitle;
186
    }
187
188
    /**
189
     * Returns true if this thing is named
190
     */
191
    public boolean hasCustomName()
192
    {
193
        return this.hasCustomName;
194
    }
195
196
    /**
197
     * Sets the name of this inventory. This is displayed to the client on opening.
198
     */
199
    public void setCustomName(String inventoryTitleIn)
200
    {
201
        this.hasCustomName = true;
202
        this.inventoryTitle = inventoryTitleIn;
203
    }
204
205
    /**
206
     * Get the formatted ChatComponent that will be used for the sender's username in chat
207
     */
208
    public IChatComponent getDisplayName()
209
    {
210
        return (IChatComponent)(this.hasCustomName() ? new ChatComponentText(this.getName()) : new ChatComponentTranslation(this.getName(), new Object[0]));
211
    }
212
213
    /**
214
     * Returns the maximum stack size for a inventory slot. Seems to always be 64, possibly will be extended.
215
     */
216
    public int getInventoryStackLimit()
217
    {
218
        return 64;
219
    }
220
221
    /**
222
     * For tile entities, ensures the chunk containing the tile entity is saved to disk later - the game won't think it
223
     * hasn't changed and skip it.
224
     */
225
    public void markDirty()
226
    {
227
        if (this.changeListeners != null)
228
        {
229
            for (int i = 0; i < this.changeListeners.size(); ++i)
230
            {
231
                ((IInvBasic)this.changeListeners.get(i)).onInventoryChanged(this);
232
            }
233
        }
234
    }
235
236
    /**
237
     * Do not make give this method the name canInteractWith because it clashes with Container
238
     */
239
    public boolean isUseableByPlayer(EntityPlayer player)
240
    {
241
        return true;
242
    }
243
244
    public void openInventory(EntityPlayer player)
245
    {
246
    }
247
248
    public void closeInventory(EntityPlayer player)
249
    {
250
    }
251
252
    /**
253
     * Returns true if automation is allowed to insert the given stack (ignoring stack size) into the given slot.
254
     */
255
    public boolean isItemValidForSlot(int index, ItemStack stack)
256
    {
257
        return true;
258
    }
259
260
    public int getField(int id)
261
    {
262
        return 0;
263
    }
264
265
    public void setField(int id, int value)
266
    {
267
    }
268
269
    public int getFieldCount()
270
    {
271
        return 0;
272
    }
273
274
    public void clear()
275
    {
276
        for (int i = 0; i < this.inventoryContents.length; ++i)
277
        {
278
            this.inventoryContents[i] = null;
279
        }
280
    }
281
}
282

Deobfuscated MCP 1.8.9 source of InventoryBasic from Marcelektro/MavenMCP-1.8.9 (net/minecraft/inventory/InventoryBasic.java), stored verbatim.