Fork of Poseidon providing Bukkit #1060 to older Beta versions (b1.0-b1.7.3)
1package net.minecraft.server;
2
3public class InventoryPlayer implements IInventory {
4
5 public ItemStack[] items = new ItemStack[36];
6 public ItemStack[] armor = new ItemStack[4];
7 public ItemStack[] craft = new ItemStack[4];
8 public int itemInHandIndex = 0;
9 public EntityHuman d; // CraftBukkit - private -> public
10 private ItemStack f;
11 public boolean e = false;
12
13 // CraftBukkit start
14 public ItemStack[] getContents() {
15 return this.items;
16 }
17
18 public ItemStack[] getArmorContents() {
19 return this.armor;
20 }
21 // CraftBukkit end
22
23 public InventoryPlayer(EntityHuman entityhuman) {
24 this.d = entityhuman;
25 }
26
27 public ItemStack getItemInHand() {
28 return this.itemInHandIndex < 9 && this.itemInHandIndex >= 0 ? this.items[this.itemInHandIndex] : null;
29 }
30
31 public static int e() {
32 return 9;
33 }
34
35 private int d(int i) {
36 for (int j = 0; j < this.items.length; ++j) {
37 if (this.items[j] != null && this.items[j].id == i) {
38 return j;
39 }
40 }
41
42 return -1;
43 }
44
45 private int firstPartial(ItemStack itemstack) {
46 for (int i = 0; i < this.items.length; ++i) {
47 if (this.items[i] != null && this.items[i].id == itemstack.id && this.items[i].isStackable() && this.items[i].count < this.items[i].getMaxStackSize() && this.items[i].count < this.getMaxStackSize() && (!this.items[i].usesData() || this.items[i].getData() == itemstack.getData())) {
48 return i;
49 }
50 }
51
52 return -1;
53 }
54
55 // CraftBukkit start - watch method above! :D
56 public int canHold(ItemStack itemstack) {
57 int remains = itemstack.count;
58 for (int i = 0; i < this.items.length; ++i) {
59 if (this.items[i] == null) return itemstack.count;
60
61 // Taken from firstPartial(ItemStack)
62 if (this.items[i] != null && this.items[i].id == itemstack.id && this.items[i].isStackable() && this.items[i].count < this.items[i].getMaxStackSize() && this.items[i].count < this.getMaxStackSize() && (!this.items[i].usesData() || this.items[i].getData() == itemstack.getData())) {
63 remains -= (this.items[i].getMaxStackSize() < this.getMaxStackSize() ? this.items[i].getMaxStackSize() : this.getMaxStackSize()) - this.items[i].count;
64 }
65 if (remains <= 0) return itemstack.count;
66 }
67 return itemstack.count - remains;
68 }
69 // CraftBukkit end
70
71 private int k() {
72 for (int i = 0; i < this.items.length; ++i) {
73 if (this.items[i] == null) {
74 return i;
75 }
76 }
77
78 return -1;
79 }
80
81 private int e(ItemStack itemstack) {
82 int i = itemstack.id;
83 int j = itemstack.count;
84 int k = this.firstPartial(itemstack);
85
86 if (k < 0) {
87 k = this.k();
88 }
89
90 if (k < 0) {
91 return j;
92 } else {
93 if (this.items[k] == null) {
94 this.items[k] = new ItemStack(i, 0, itemstack.getData());
95 }
96
97 int l = j;
98
99 if (j > this.items[k].getMaxStackSize() - this.items[k].count) {
100 l = this.items[k].getMaxStackSize() - this.items[k].count;
101 }
102
103 if (l > this.getMaxStackSize() - this.items[k].count) {
104 l = this.getMaxStackSize() - this.items[k].count;
105 }
106
107 if (l == 0) {
108 return j;
109 } else {
110 j -= l;
111 this.items[k].count += l;
112 this.items[k].b = 5;
113 return j;
114 }
115 }
116 }
117
118 public void f() {
119 for (int i = 0; i < this.items.length; ++i) {
120 if (this.items[i] != null) {
121 this.items[i].a(this.d.world, this.d, i, this.itemInHandIndex == i);
122 }
123 }
124 }
125
126 public boolean b(int i) {
127 int j = this.d(i);
128
129 if (j < 0) {
130 return false;
131 } else {
132 if (--this.items[j].count <= 0) {
133 this.items[j] = null;
134 }
135
136 return true;
137 }
138 }
139
140 public boolean pickup(ItemStack itemstack) {
141 int i;
142
143 if (itemstack.f()) {
144 i = this.k();
145 if (i >= 0) {
146 this.items[i] = ItemStack.b(itemstack);
147 this.items[i].b = 5;
148 itemstack.count = 0;
149 return true;
150 } else {
151 return false;
152 }
153 } else {
154 do {
155 i = itemstack.count;
156 itemstack.count = this.e(itemstack);
157 } while (itemstack.count > 0 && itemstack.count < i);
158
159 return itemstack.count < i;
160 }
161 }
162
163 public ItemStack splitStack(int i, int j) {
164 ItemStack[] aitemstack = this.items;
165
166 if (i >= this.items.length) {
167 aitemstack = this.armor;
168 i -= this.items.length;
169 }
170
171 if (aitemstack[i] != null) {
172 ItemStack itemstack;
173
174 if (aitemstack[i].count <= j) {
175 itemstack = aitemstack[i];
176 aitemstack[i] = null;
177 return itemstack;
178 } else {
179 itemstack = aitemstack[i].a(j);
180 if (aitemstack[i].count == 0) {
181 aitemstack[i] = null;
182 }
183
184 return itemstack;
185 }
186 } else {
187 return null;
188 }
189 }
190
191 public void setItem(int i, ItemStack itemstack) {
192 ItemStack[] aitemstack = this.items;
193
194 if (i >= aitemstack.length) {
195 i -= aitemstack.length;
196 aitemstack = this.armor;
197 }
198
199 aitemstack[i] = itemstack;
200 }
201
202 public float a(Block block) {
203 float f = 1.0F;
204
205 if (this.items[this.itemInHandIndex] != null) {
206 f *= this.items[this.itemInHandIndex].a(block);
207 }
208
209 return f;
210 }
211
212 public NBTTagList a(NBTTagList nbttaglist) {
213 int i;
214 NBTTagCompound nbttagcompound;
215
216 for (i = 0; i < this.items.length; ++i) {
217 if (this.items[i] != null) {
218 nbttagcompound = new NBTTagCompound();
219 nbttagcompound.a("Slot", (byte) i);
220 this.items[i].a(nbttagcompound);
221 nbttaglist.a((NBTBase) nbttagcompound);
222 }
223 }
224
225 for (i = 0; i < this.armor.length; ++i) {
226 if (this.armor[i] != null) {
227 nbttagcompound = new NBTTagCompound();
228 nbttagcompound.a("Slot", (byte) (i + 100));
229 this.armor[i].a(nbttagcompound);
230 nbttaglist.a((NBTBase) nbttagcompound);
231 }
232 }
233
234 for (i = 0; i < this.craft.length; ++i) {
235 if (this.craft[i] != null) {
236 nbttagcompound = new NBTTagCompound();
237 nbttagcompound.a("Slot", (byte) (i + 80));
238 this.craft[i].a(nbttagcompound);
239 nbttaglist.a((NBTBase) nbttagcompound);
240 }
241 }
242
243 return nbttaglist;
244 }
245
246 public void b(NBTTagList nbttaglist) {
247 this.items = new ItemStack[36];
248 this.armor = new ItemStack[4];
249 this.craft = new ItemStack[4];
250
251 for (int i = 0; i < nbttaglist.c(); ++i) {
252 NBTTagCompound nbttagcompound = (NBTTagCompound) nbttaglist.a(i);
253 int j = nbttagcompound.c("Slot") & 255;
254 ItemStack itemstack = new ItemStack(nbttagcompound);
255
256 if (itemstack.getItem() != null) {
257 if (j >= 0 && j < this.items.length) {
258 this.items[j] = itemstack;
259 }
260
261 if (j >= 100 && j < this.armor.length + 100) {
262 this.armor[j - 100] = itemstack;
263 }
264
265 if (j >= 80 && j < this.craft.length + 80) {
266 this.craft[j - 80] = itemstack;
267 }
268 }
269 }
270 }
271
272 public int getSize() {
273 return this.items.length + 4;
274 }
275
276 public ItemStack getItem(int i) {
277 ItemStack[] aitemstack = this.items;
278
279 if (i >= aitemstack.length) {
280 i -= aitemstack.length;
281 aitemstack = this.armor;
282 }
283
284 return aitemstack[i];
285 }
286
287 public String getName() {
288 return "Inventory";
289 }
290
291 public int getMaxStackSize() {
292 return 64;
293 }
294
295 public int a(Entity entity) {
296 ItemStack itemstack = this.getItem(this.itemInHandIndex);
297
298 return itemstack != null ? itemstack.a(entity) : 1;
299 }
300
301 public boolean b(Block block) {
302 if (block.material.i()) {
303 return true;
304 } else {
305 ItemStack itemstack = this.getItem(this.itemInHandIndex);
306
307 return itemstack != null ? itemstack.b(block) : false;
308 }
309 }
310
311 public int g() {
312 int i = 0;
313 int j = 0;
314 int k = 0;
315
316 for (int l = 0; l < this.armor.length; ++l) {
317 if (this.armor[l] != null && this.armor[l].getItem() instanceof ItemArmor) {
318 int i1 = this.armor[l].i();
319 int j1 = this.armor[l].g();
320 int k1 = i1 - j1;
321
322 j += k1;
323 k += i1;
324 int l1 = ((ItemArmor) this.armor[l].getItem()).bl;
325
326 i += l1;
327 }
328 }
329
330 if (k == 0) {
331 return 0;
332 } else {
333 return (i - 1) * j / k + 1;
334 }
335 }
336
337 public void c(int i) {
338 for (int j = 0; j < this.armor.length; ++j) {
339 if (this.armor[j] != null && this.armor[j].getItem() instanceof ItemArmor) {
340 this.armor[j].damage(i, this.d);
341 if (this.armor[j].count == 0) {
342 this.armor[j].a(this.d);
343 this.armor[j] = null;
344 }
345 }
346 }
347 }
348
349 public void h() {
350 int i;
351
352 for (i = 0; i < this.items.length; ++i) {
353 if (this.items[i] != null) {
354 this.d.a(this.items[i], true);
355 this.items[i] = null;
356 }
357 }
358
359 for (i = 0; i < this.armor.length; ++i) {
360 if (this.armor[i] != null) {
361 this.d.a(this.armor[i], true);
362 this.armor[i] = null;
363 }
364 }
365 }
366
367 public void update() {
368 this.e = true;
369 }
370
371 public void b(ItemStack itemstack) {
372 this.f = itemstack;
373 this.d.a(itemstack);
374 }
375
376 public ItemStack j() {
377 return this.f;
378 }
379
380 public boolean a_(EntityHuman entityhuman) {
381 return this.d.dead ? false : entityhuman.g(this.d) <= 64.0D;
382 }
383
384 public boolean c(ItemStack itemstack) {
385 int i;
386
387 for (i = 0; i < this.armor.length; ++i) {
388 if (this.armor[i] != null && this.armor[i].c(itemstack)) {
389 return true;
390 }
391 }
392
393 for (i = 0; i < this.items.length; ++i) {
394 if (this.items[i] != null && this.items[i].c(itemstack)) {
395 return true;
396 }
397 }
398
399 return false;
400 }
401}