TileEntityDispenser .java

net.minecraft.tileentity.TileEntityDispenser (class, 266 lines): members: getDispenseSlot, addItemStack, stacks, RNG, setCustomName, customName, getGuiID.

net.minecraft.tileentityclass266 linescommonextends TileEntityLockable6.6 KB

Implements

API Surface (23)

int getSizeInventory()
ItemStack getStackInSlot(int index)
ItemStack decrStackSize(int index, int count)
ItemStack removeStackFromSlot(int index)
int getDispenseSlot()
void setInventorySlotContents(int index, ItemStack stack)
int addItemStack(ItemStack stack)
String getName()
void setCustomName(String customName)
boolean hasCustomName()
void readFromNBT(NBTTagCompound compound)
void writeToNBT(NBTTagCompound compound)
int getInventoryStackLimit()
boolean isUseableByPlayer(EntityPlayer player)
void openInventory(EntityPlayer player)
void closeInventory(EntityPlayer player)
boolean isItemValidForSlot(int index, ItemStack stack)
String getGuiID()
Container createContainer(InventoryPlayer playerInventory, EntityPlayer playerIn)
int getField(int id)
void setField(int id, int value)
int getFieldCount()
void clear()

Fields & Constants (3)

RNG
stacks
customName

String literals (5)

"container.dispenser"
"Items"
"Slot"
"CustomName"
"minecraft:dispenser"

Full Source

TileEntityDispenser.java266 lines · MCP 1.8.9 · verbatimOriginal ↗
1
package net.minecraft.tileentity;
2
3
import java.util.Random;
4
import net.minecraft.entity.player.EntityPlayer;
5
import net.minecraft.entity.player.InventoryPlayer;
6
import net.minecraft.inventory.Container;
7
import net.minecraft.inventory.ContainerDispenser;
8
import net.minecraft.inventory.IInventory;
9
import net.minecraft.item.ItemStack;
10
import net.minecraft.nbt.NBTTagCompound;
11
import net.minecraft.nbt.NBTTagList;
12
13
public class TileEntityDispenser extends TileEntityLockable implements IInventory
14
{
15
    private static final Random RNG = new Random();
16
    private ItemStack[] stacks = new ItemStack[9];
17
    protected String customName;
18
19
    /**
20
     * Returns the number of slots in the inventory.
21
     */
22
    public int getSizeInventory()
23
    {
24
        return 9;
25
    }
26
27
    /**
28
     * Returns the stack in the given slot.
29
     */
30
    public ItemStack getStackInSlot(int index)
31
    {
32
        return this.stacks[index];
33
    }
34
35
    /**
36
     * Removes up to a specified number of items from an inventory slot and returns them in a new stack.
37
     */
38
    public ItemStack decrStackSize(int index, int count)
39
    {
40
        if (this.stacks[index] != null)
41
        {
42
            if (this.stacks[index].stackSize <= count)
43
            {
44
                ItemStack itemstack1 = this.stacks[index];
45
                this.stacks[index] = null;
46
                this.markDirty();
47
                return itemstack1;
48
            }
49
            else
50
            {
51
                ItemStack itemstack = this.stacks[index].splitStack(count);
52
53
                if (this.stacks[index].stackSize == 0)
54
                {
55
                    this.stacks[index] = null;
56
                }
57
58
                this.markDirty();
59
                return itemstack;
60
            }
61
        }
62
        else
63
        {
64
            return null;
65
        }
66
    }
67
68
    /**
69
     * Removes a stack from the given slot and returns it.
70
     */
71
    public ItemStack removeStackFromSlot(int index)
72
    {
73
        if (this.stacks[index] != null)
74
        {
75
            ItemStack itemstack = this.stacks[index];
76
            this.stacks[index] = null;
77
            return itemstack;
78
        }
79
        else
80
        {
81
            return null;
82
        }
83
    }
84
85
    public int getDispenseSlot()
86
    {
87
        int i = -1;
88
        int j = 1;
89
90
        for (int k = 0; k < this.stacks.length; ++k)
91
        {
92
            if (this.stacks[k] != null && RNG.nextInt(j++) == 0)
93
            {
94
                i = k;
95
            }
96
        }
97
98
        return i;
99
    }
100
101
    /**
102
     * Sets the given item stack to the specified slot in the inventory (can be crafting or armor sections).
103
     */
104
    public void setInventorySlotContents(int index, ItemStack stack)
105
    {
106
        this.stacks[index] = stack;
107
108
        if (stack != null && stack.stackSize > this.getInventoryStackLimit())
109
        {
110
            stack.stackSize = this.getInventoryStackLimit();
111
        }
112
113
        this.markDirty();
114
    }
115
116
    /**
117
     * Add the given ItemStack to this Dispenser. Return the Slot the Item was placed in or -1 if no free slot is
118
     * available.
119
     */
120
    public int addItemStack(ItemStack stack)
121
    {
122
        for (int i = 0; i < this.stacks.length; ++i)
123
        {
124
            if (this.stacks[i] == null || this.stacks[i].getItem() == null)
125
            {
126
                this.setInventorySlotContents(i, stack);
127
                return i;
128
            }
129
        }
130
131
        return -1;
132
    }
133
134
    /**
135
     * Get the name of this object. For players this returns their username
136
     */
137
    public String getName()
138
    {
139
        return this.hasCustomName() ? this.customName : "container.dispenser";
140
    }
141
142
    public void setCustomName(String customName)
143
    {
144
        this.customName = customName;
145
    }
146
147
    /**
148
     * Returns true if this thing is named
149
     */
150
    public boolean hasCustomName()
151
    {
152
        return this.customName != null;
153
    }
154
155
    public void readFromNBT(NBTTagCompound compound)
156
    {
157
        super.readFromNBT(compound);
158
        NBTTagList nbttaglist = compound.getTagList("Items", 10);
159
        this.stacks = new ItemStack[this.getSizeInventory()];
160
161
        for (int i = 0; i < nbttaglist.tagCount(); ++i)
162
        {
163
            NBTTagCompound nbttagcompound = nbttaglist.getCompoundTagAt(i);
164
            int j = nbttagcompound.getByte("Slot") & 255;
165
166
            if (j >= 0 && j < this.stacks.length)
167
            {
168
                this.stacks[j] = ItemStack.loadItemStackFromNBT(nbttagcompound);
169
            }
170
        }
171
172
        if (compound.hasKey("CustomName", 8))
173
        {
174
            this.customName = compound.getString("CustomName");
175
        }
176
    }
177
178
    public void writeToNBT(NBTTagCompound compound)
179
    {
180
        super.writeToNBT(compound);
181
        NBTTagList nbttaglist = new NBTTagList();
182
183
        for (int i = 0; i < this.stacks.length; ++i)
184
        {
185
            if (this.stacks[i] != null)
186
            {
187
                NBTTagCompound nbttagcompound = new NBTTagCompound();
188
                nbttagcompound.setByte("Slot", (byte)i);
189
                this.stacks[i].writeToNBT(nbttagcompound);
190
                nbttaglist.appendTag(nbttagcompound);
191
            }
192
        }
193
194
        compound.setTag("Items", nbttaglist);
195
196
        if (this.hasCustomName())
197
        {
198
            compound.setString("CustomName", this.customName);
199
        }
200
    }
201
202
    /**
203
     * Returns the maximum stack size for a inventory slot. Seems to always be 64, possibly will be extended.
204
     */
205
    public int getInventoryStackLimit()
206
    {
207
        return 64;
208
    }
209
210
    /**
211
     * Do not make give this method the name canInteractWith because it clashes with Container
212
     */
213
    public boolean isUseableByPlayer(EntityPlayer player)
214
    {
215
        return this.worldObj.getTileEntity(this.pos) != this ? false : player.getDistanceSq((double)this.pos.getX() + 0.5D, (double)this.pos.getY() + 0.5D, (double)this.pos.getZ() + 0.5D) <= 64.0D;
216
    }
217
218
    public void openInventory(EntityPlayer player)
219
    {
220
    }
221
222
    public void closeInventory(EntityPlayer player)
223
    {
224
    }
225
226
    /**
227
     * Returns true if automation is allowed to insert the given stack (ignoring stack size) into the given slot.
228
     */
229
    public boolean isItemValidForSlot(int index, ItemStack stack)
230
    {
231
        return true;
232
    }
233
234
    public String getGuiID()
235
    {
236
        return "minecraft:dispenser";
237
    }
238
239
    public Container createContainer(InventoryPlayer playerInventory, EntityPlayer playerIn)
240
    {
241
        return new ContainerDispenser(playerInventory, this);
242
    }
243
244
    public int getField(int id)
245
    {
246
        return 0;
247
    }
248
249
    public void setField(int id, int value)
250
    {
251
    }
252
253
    public int getFieldCount()
254
    {
255
        return 0;
256
    }
257
258
    public void clear()
259
    {
260
        for (int i = 0; i < this.stacks.length; ++i)
261
        {
262
            this.stacks[i] = null;
263
        }
264
    }
265
}
266

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