summaryrefslogtreecommitdiffstats
path: root/src/main/java/net/minecraft/server/Container.java
blob: a10108b9fd5fe6bf75855939cdff968441882293 (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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
package net.minecraft.server;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Set;

// CraftBukkit start
import java.util.HashMap;
import java.util.Map;
import org.bukkit.craftbukkit.inventory.CraftInventory;
import org.bukkit.craftbukkit.inventory.CraftItemStack;
import org.bukkit.event.Event.Result;
import org.bukkit.event.inventory.InventoryDragEvent;
import org.bukkit.event.inventory.InventoryType;
import org.bukkit.inventory.InventoryView;
// CraftBukkit end

public abstract class Container {

    public List b = new ArrayList();
    public List c = new ArrayList();
    public int windowId;
    private int dragType = -1;
    public int g; // CraftBukkit - private -> public
    private final Set h = new HashSet();
    protected List listeners = new ArrayList();
    private Set i = new HashSet();

    // CraftBukkit start
    public boolean checkReachable = true;
    public abstract InventoryView getBukkitView();
    public void transferTo(Container other, org.bukkit.craftbukkit.entity.CraftHumanEntity player) {
        InventoryView source = this.getBukkitView(), destination = other.getBukkitView();
        ((CraftInventory) source.getTopInventory()).getInventory().onClose(player);
        ((CraftInventory) source.getBottomInventory()).getInventory().onClose(player);
        ((CraftInventory) destination.getTopInventory()).getInventory().onOpen(player);
        ((CraftInventory) destination.getBottomInventory()).getInventory().onOpen(player);
    }
    // CraftBukkit end

    public Container() {}

    protected Slot a(Slot slot) {
        slot.rawSlotIndex = this.c.size();
        this.c.add(slot);
        this.b.add(null);
        return slot;
    }

    public void addSlotListener(ICrafting icrafting) {
        if (this.listeners.contains(icrafting)) {
            throw new IllegalArgumentException("Listener already listening");
        } else {
            this.listeners.add(icrafting);
            icrafting.a(this, this.a());
            this.b();
        }
    }

    public List a() {
        ArrayList arraylist = new ArrayList();

        for (int i = 0; i < this.c.size(); ++i) {
            arraylist.add(((Slot) this.c.get(i)).getItem());
        }

        return arraylist;
    }

    public void b() {
        for (int i = 0; i < this.c.size(); ++i) {
            ItemStack itemstack = ((Slot) this.c.get(i)).getItem();
            ItemStack itemstack1 = (ItemStack) this.b.get(i);

            if (!ItemStack.matches(itemstack1, itemstack)) {
                itemstack1 = itemstack == null ? null : itemstack.cloneItemStack();
                this.b.set(i, itemstack1);

                for (int j = 0; j < this.listeners.size(); ++j) {
                    ((ICrafting) this.listeners.get(j)).a(this, i, itemstack1);
                }
            }
        }
    }

    public boolean a(EntityHuman entityhuman, int i) {
        return false;
    }

    public Slot getSlot(IInventory iinventory, int i) {
        for (int j = 0; j < this.c.size(); ++j) {
            Slot slot = (Slot) this.c.get(j);

            if (slot.a(iinventory, i)) {
                return slot;
            }
        }

        return null;
    }

    public Slot getSlot(int i) {
        return (Slot) this.c.get(i);
    }

    public ItemStack b(EntityHuman entityhuman, int i) {
        Slot slot = (Slot) this.c.get(i);

        return slot != null ? slot.getItem() : null;
    }

    public ItemStack clickItem(int i, int j, int k, EntityHuman entityhuman) {
        ItemStack itemstack = null;
        PlayerInventory playerinventory = entityhuman.inventory;
        int l;
        ItemStack itemstack1;

        if (k == 5) {
            int i1 = this.g;

            this.g = c(j);
            if ((i1 != 1 || this.g != 2) && i1 != this.g) {
                this.d();
            } else if (playerinventory.getCarried() == null) {
                this.d();
            } else if (this.g == 0) {
                this.dragType = b(j);
                if (d(this.dragType)) {
                    this.g = 1;
                    this.h.clear();
                } else {
                    this.d();
                }
            } else if (this.g == 1) {
                Slot slot = (Slot) this.c.get(i);

                if (slot != null && a(slot, playerinventory.getCarried(), true) && slot.isAllowed(playerinventory.getCarried()) && playerinventory.getCarried().count > this.h.size() && this.b(slot)) {
                    this.h.add(slot);
                }
            } else if (this.g == 2) {
                if (!this.h.isEmpty()) {
                    itemstack1 = playerinventory.getCarried().cloneItemStack();
                    l = playerinventory.getCarried().count;
                    Iterator iterator = this.h.iterator();

                    Map<Integer, ItemStack> draggedSlots = new HashMap<Integer, ItemStack>(); // CraftBukkit - Store slots from drag in map (raw slot id -> new stack)
                    while (iterator.hasNext()) {
                        Slot slot1 = (Slot) iterator.next();

                        if (slot1 != null && a(slot1, playerinventory.getCarried(), true) && slot1.isAllowed(playerinventory.getCarried()) && playerinventory.getCarried().count >= this.h.size() && this.b(slot1)) {
                            ItemStack itemstack2 = itemstack1.cloneItemStack();
                            int j1 = slot1.hasItem() ? slot1.getItem().count : 0;

                            a(this.h, this.dragType, itemstack2, j1);
                            if (itemstack2.count > itemstack2.getMaxStackSize()) {
                                itemstack2.count = itemstack2.getMaxStackSize();
                            }

                            if (itemstack2.count > slot1.getMaxStackSize()) {
                                itemstack2.count = slot1.getMaxStackSize();
                            }

                            l -= itemstack2.count - j1;
                            draggedSlots.put(slot1.rawSlotIndex, itemstack2); // CraftBukkit - Put in map instead of setting
                        }
                    }

                    // CraftBukkit start - InventoryDragEvent
                    InventoryView view = getBukkitView();
                    org.bukkit.inventory.ItemStack newcursor = CraftItemStack.asCraftMirror(itemstack1);
                    newcursor.setAmount(l);
                    Map<Integer, org.bukkit.inventory.ItemStack> eventmap = new HashMap<Integer, org.bukkit.inventory.ItemStack>();
                    for (Map.Entry<Integer, ItemStack> ditem : draggedSlots.entrySet()) {
                        eventmap.put(ditem.getKey(), CraftItemStack.asBukkitCopy(ditem.getValue()));
                    }

                    // It's essential that we set the cursor to the new value here to prevent item duplication if a plugin closes the inventory.
                    ItemStack oldCursor = playerinventory.getCarried();
                    playerinventory.setCarried(CraftItemStack.asNMSCopy(newcursor));

                    InventoryDragEvent event = new InventoryDragEvent(view, (newcursor.getType() != org.bukkit.Material.AIR ? newcursor : null), CraftItemStack.asBukkitCopy(oldCursor), this.dragType == 1, eventmap);
                    entityhuman.world.getServer().getPluginManager().callEvent(event);

                    // Whether or not a change was made to the inventory that requires an update.
                    boolean needsUpdate = event.getResult() != Result.DEFAULT;

                    if (event.getResult() != Result.DENY) {
                        for (Map.Entry<Integer, ItemStack> dslot : draggedSlots.entrySet()) {
                            view.setItem(dslot.getKey(), CraftItemStack.asBukkitCopy(dslot.getValue()));
                        }
                        // The only time the carried item will be set to null is if the inventory is closed by the server.
                        // If the inventory is closed by the server, then the cursor items are dropped.  This is why we change the cursor early.
                        if (playerinventory.getCarried() != null) {
                            playerinventory.setCarried(CraftItemStack.asNMSCopy(event.getCursor()));
                            needsUpdate = true;

                        }
                    } else {
                        playerinventory.setCarried(oldCursor);
                    }

                    if (needsUpdate && entityhuman instanceof EntityPlayer) {
                        ((EntityPlayer) entityhuman).updateInventory(this);
                    }
                    // CraftBukkit end
                }

                this.d();
            } else {
                this.d();
            }
        } else if (this.g != 0) {
            this.d();
        } else {
            Slot slot2;
            int k1;
            ItemStack itemstack3;

            if ((k == 0 || k == 1) && (j == 0 || j == 1)) {
                if (i == -999) {
                    if (playerinventory.getCarried() != null && i == -999) {
                        if (j == 0) {
                            entityhuman.drop(playerinventory.getCarried(), true);
                            playerinventory.setCarried((ItemStack) null);
                        }

                        if (j == 1) {
                            // CraftBukkit start - Store a reference
                            ItemStack itemstack4 = playerinventory.getCarried();
                            if (itemstack4.count > 0) {
                                entityhuman.drop(itemstack4.a(1), true);
                            }

                            if (itemstack4.count == 0) {
                                // CraftBukkit end
                                playerinventory.setCarried((ItemStack) null);
                            }
                        }
                    }
                } else if (k == 1) {
                    if (i < 0) {
                        return null;
                    }

                    slot2 = (Slot) this.c.get(i);
                    if (slot2 != null && slot2.isAllowed(entityhuman)) {
                        itemstack1 = this.b(entityhuman, i);
                        if (itemstack1 != null) {
                            Item item = itemstack1.getItem();

                            itemstack = itemstack1.cloneItemStack();
                            if (slot2.getItem() != null && slot2.getItem().getItem() == item) {
                                this.a(i, j, true, entityhuman);
                            }
                        }
                    }
                } else {
                    if (i < 0) {
                        return null;
                    }

                    slot2 = (Slot) this.c.get(i);
                    if (slot2 != null) {
                        itemstack1 = slot2.getItem();
                        ItemStack itemstack4 = playerinventory.getCarried();

                        if (itemstack1 != null) {
                            itemstack = itemstack1.cloneItemStack();
                        }

                        if (itemstack1 == null) {
                            if (itemstack4 != null && slot2.isAllowed(itemstack4)) {
                                k1 = j == 0 ? itemstack4.count : 1;
                                if (k1 > slot2.getMaxStackSize()) {
                                    k1 = slot2.getMaxStackSize();
                                }

                                if (itemstack4.count >= k1) {
                                    slot2.set(itemstack4.a(k1));
                                }

                                if (itemstack4.count == 0) {
                                    playerinventory.setCarried((ItemStack) null);
                                // CraftBukkit start - Update client cursor if we didn't empty it
                                } else if (entityhuman instanceof EntityPlayer) {
                                    ((EntityPlayer) entityhuman).playerConnection.sendPacket(new PacketPlayOutSetSlot(-1, -1, entityhuman.inventory.getCarried()));
                                }
                                // CraftBukkit end
                            }
                        } else if (slot2.isAllowed(entityhuman)) {
                            if (itemstack4 == null) {
                                k1 = j == 0 ? itemstack1.count : (itemstack1.count + 1) / 2;
                                itemstack3 = slot2.a(k1);
                                playerinventory.setCarried(itemstack3);
                                if (itemstack1.count == 0) {
                                    slot2.set((ItemStack) null);
                                }

                                slot2.a(entityhuman, playerinventory.getCarried());
                            } else if (slot2.isAllowed(itemstack4)) {
                                if (itemstack1.getItem() == itemstack4.getItem() && itemstack1.getData() == itemstack4.getData() && ItemStack.equals(itemstack1, itemstack4)) {
                                    k1 = j == 0 ? itemstack4.count : 1;
                                    if (k1 > slot2.getMaxStackSize() - itemstack1.count) {
                                        k1 = slot2.getMaxStackSize() - itemstack1.count;
                                    }

                                    if (k1 > itemstack4.getMaxStackSize() - itemstack1.count) {
                                        k1 = itemstack4.getMaxStackSize() - itemstack1.count;
                                    }

                                    itemstack4.a(k1);
                                    if (itemstack4.count == 0) {
                                        playerinventory.setCarried((ItemStack) null);
                                    // CraftBukkit start - Update client cursor if we didn't empty it
                                    } else if (entityhuman instanceof EntityPlayer) {
                                        ((EntityPlayer) entityhuman).playerConnection.sendPacket(new PacketPlayOutSetSlot(-1, -1, entityhuman.inventory.getCarried()));
                                    }
                                    // CraftBukkit end

                                    itemstack1.count += k1;
                                } else if (itemstack4.count <= slot2.getMaxStackSize()) {
                                    slot2.set(itemstack4);
                                    playerinventory.setCarried(itemstack1);
                                }
                            } else if (itemstack1.getItem() == itemstack4.getItem() && itemstack4.getMaxStackSize() > 1 && (!itemstack1.usesData() || itemstack1.getData() == itemstack4.getData()) && ItemStack.equals(itemstack1, itemstack4)) {
                                k1 = itemstack1.count;
                                // CraftBukkit start - itemstack4.getMaxStackSize() -> maxStack
                                int maxStack = Math.min(itemstack4.getMaxStackSize(), slot2.getMaxStackSize());
                                if (k1 > 0 && k1 + itemstack4.count <= maxStack) {
                                    // CraftBukkit end
                                    itemstack4.count += k1;
                                    itemstack1 = slot2.a(k1);
                                    if (itemstack1.count == 0) {
                                        slot2.set((ItemStack) null);
                                    }

                                    slot2.a(entityhuman, playerinventory.getCarried());
                                // CraftBukkit start - Update client cursor if we didn't empty it
                                } else if (entityhuman instanceof EntityPlayer) {
                                    ((EntityPlayer) entityhuman).playerConnection.sendPacket(new PacketPlayOutSetSlot(-1, -1, entityhuman.inventory.getCarried()));
                                }
                                // CraftBukkit end
                            }
                        }

                        slot2.f();
                        // CraftBukkit start - Make sure the client has the right slot contents
                        if (entityhuman instanceof EntityPlayer && slot2.getMaxStackSize() != 64) {
                            ((EntityPlayer) entityhuman).playerConnection.sendPacket(new PacketPlayOutSetSlot(this.windowId, slot2.rawSlotIndex, slot2.getItem()));
                            // Updating a crafting inventory makes the client reset the result slot, have to send it again
                            if (this.getBukkitView().getType() == InventoryType.WORKBENCH || this.getBukkitView().getType() == InventoryType.CRAFTING) {
                                ((EntityPlayer) entityhuman).playerConnection.sendPacket(new PacketPlayOutSetSlot(this.windowId, 0, this.getSlot(0).getItem()));
                            }
                        }
                        // CraftBukkit end
                    }
                }
            } else if (k == 2 && j >= 0 && j < 9) {
                slot2 = (Slot) this.c.get(i);
                if (slot2.isAllowed(entityhuman)) {
                    itemstack1 = playerinventory.getItem(j);
                    boolean flag = itemstack1 == null || slot2.inventory == playerinventory && slot2.isAllowed(itemstack1);

                    k1 = -1;
                    if (!flag) {
                        k1 = playerinventory.getFirstEmptySlotIndex();
                        flag |= k1 > -1;
                    }

                    if (slot2.hasItem() && flag) {
                        itemstack3 = slot2.getItem();
                        playerinventory.setItem(j, itemstack3.cloneItemStack());
                        if ((slot2.inventory != playerinventory || !slot2.isAllowed(itemstack1)) && itemstack1 != null) {
                            if (k1 > -1) {
                                playerinventory.pickup(itemstack1);
                                slot2.a(itemstack3.count);
                                slot2.set((ItemStack) null);
                                slot2.a(entityhuman, itemstack3);
                            }
                        } else {
                            slot2.a(itemstack3.count);
                            slot2.set(itemstack1);
                            slot2.a(entityhuman, itemstack3);
                        }
                    } else if (!slot2.hasItem() && itemstack1 != null && slot2.isAllowed(itemstack1)) {
                        playerinventory.setItem(j, (ItemStack) null);
                        slot2.set(itemstack1);
                    }
                }
            } else if (k == 3 && entityhuman.abilities.canInstantlyBuild && playerinventory.getCarried() == null && i >= 0) {
                slot2 = (Slot) this.c.get(i);
                if (slot2 != null && slot2.hasItem()) {
                    itemstack1 = slot2.getItem().cloneItemStack();
                    itemstack1.count = itemstack1.getMaxStackSize();
                    playerinventory.setCarried(itemstack1);
                }
            } else if (k == 4 && playerinventory.getCarried() == null && i >= 0) {
                slot2 = (Slot) this.c.get(i);
                if (slot2 != null && slot2.hasItem() && slot2.isAllowed(entityhuman)) {
                    itemstack1 = slot2.a(j == 0 ? 1 : slot2.getItem().count);
                    slot2.a(entityhuman, itemstack1);
                    entityhuman.drop(itemstack1, true);
                }
            } else if (k == 6 && i >= 0) {
                slot2 = (Slot) this.c.get(i);
                itemstack1 = playerinventory.getCarried();
                if (itemstack1 != null && (slot2 == null || !slot2.hasItem() || !slot2.isAllowed(entityhuman))) {
                    l = j == 0 ? 0 : this.c.size() - 1;
                    k1 = j == 0 ? 1 : -1;

                    for (int l1 = 0; l1 < 2; ++l1) {
                        for (int i2 = l; i2 >= 0 && i2 < this.c.size() && itemstack1.count < itemstack1.getMaxStackSize(); i2 += k1) {
                            Slot slot3 = (Slot) this.c.get(i2);

                            if (slot3.hasItem() && a(slot3, itemstack1, true) && slot3.isAllowed(entityhuman) && this.a(itemstack1, slot3) && (l1 != 0 || slot3.getItem().count != slot3.getItem().getMaxStackSize())) {
                                int j2 = Math.min(itemstack1.getMaxStackSize() - itemstack1.count, slot3.getItem().count);
                                ItemStack itemstack5 = slot3.a(j2);

                                itemstack1.count += j2;
                                if (itemstack5.count <= 0) {
                                    slot3.set((ItemStack) null);
                                }

                                slot3.a(entityhuman, itemstack5);
                            }
                        }
                    }
                }

                this.b();
            }
        }

        return itemstack;
    }

    public boolean a(ItemStack itemstack, Slot slot) {
        return true;
    }

    protected void a(int i, int j, boolean flag, EntityHuman entityhuman) {
        this.clickItem(i, j, 1, entityhuman);
    }

    public void b(EntityHuman entityhuman) {
        PlayerInventory playerinventory = entityhuman.inventory;

        if (playerinventory.getCarried() != null) {
            entityhuman.drop(playerinventory.getCarried(), false);
            playerinventory.setCarried((ItemStack) null);
        }
    }

    public void a(IInventory iinventory) {
        this.b();
    }

    public void setItem(int i, ItemStack itemstack) {
        this.getSlot(i).set(itemstack);
    }

    public boolean c(EntityHuman entityhuman) {
        return !this.i.contains(entityhuman);
    }

    public void a(EntityHuman entityhuman, boolean flag) {
        if (flag) {
            this.i.remove(entityhuman);
        } else {
            this.i.add(entityhuman);
        }
    }

    public abstract boolean a(EntityHuman entityhuman);

    protected boolean a(ItemStack itemstack, int i, int j, boolean flag) {
        boolean flag1 = false;
        int k = i;

        if (flag) {
            k = j - 1;
        }

        Slot slot;
        ItemStack itemstack1;

        if (itemstack.isStackable()) {
            while (itemstack.count > 0 && (!flag && k < j || flag && k >= i)) {
                slot = (Slot) this.c.get(k);
                itemstack1 = slot.getItem();
                if (itemstack1 != null && itemstack1.getItem() == itemstack.getItem() && (!itemstack.usesData() || itemstack.getData() == itemstack1.getData()) && ItemStack.equals(itemstack, itemstack1)) {
                    int l = itemstack1.count + itemstack.count;

                    // CraftBukkit start - itemstack.getMaxStackSize() -> maxStack
                    int maxStack = Math.min(itemstack.getMaxStackSize(), slot.getMaxStackSize());
                    if (l <= maxStack) {
                        itemstack.count = 0;
                        itemstack1.count = l;
                        slot.f();
                        flag1 = true;
                    } else if (itemstack1.count < maxStack) {
                        itemstack.count -= maxStack - itemstack1.count;
                        itemstack1.count = maxStack;
                        slot.f();
                        flag1 = true;
                    }
                    // CraftBukkit end
                }

                if (flag) {
                    --k;
                } else {
                    ++k;
                }
            }
        }

        if (itemstack.count > 0) {
            if (flag) {
                k = j - 1;
            } else {
                k = i;
            }

            while (!flag && k < j || flag && k >= i) {
                slot = (Slot) this.c.get(k);
                itemstack1 = slot.getItem();
                if (itemstack1 == null) {
                    slot.set(itemstack.cloneItemStack());
                    slot.f();
                    itemstack.count = 0;
                    flag1 = true;
                    break;
                }

                if (flag) {
                    --k;
                } else {
                    ++k;
                }
            }
        }

        return flag1;
    }

    public static int b(int i) {
        return i >> 2 & 3;
    }

    public static int c(int i) {
        return i & 3;
    }

    public static boolean d(int i) {
        return i == 0 || i == 1;
    }

    protected void d() {
        this.g = 0;
        this.h.clear();
    }

    public static boolean a(Slot slot, ItemStack itemstack, boolean flag) {
        boolean flag1 = slot == null || !slot.hasItem();

        if (slot != null && slot.hasItem() && itemstack != null && itemstack.doMaterialsMatch(slot.getItem()) && ItemStack.equals(slot.getItem(), itemstack)) {
            int i = flag ? 0 : itemstack.count;

            flag1 |= slot.getItem().count + i <= itemstack.getMaxStackSize();
        }

        return flag1;
    }

    public static void a(Set set, int i, ItemStack itemstack, int j) {
        switch (i) {
        case 0:
            itemstack.count = MathHelper.d((float) itemstack.count / (float) set.size());
            break;

        case 1:
            itemstack.count = 1;
        }

        itemstack.count += j;
    }

    public boolean b(Slot slot) {
        return true;
    }

    public static int b(IInventory iinventory) {
        if (iinventory == null) {
            return 0;
        } else {
            int i = 0;
            float f = 0.0F;

            for (int j = 0; j < iinventory.getSize(); ++j) {
                ItemStack itemstack = iinventory.getItem(j);

                if (itemstack != null) {
                    f += (float) itemstack.count / (float) Math.min(iinventory.getMaxStackSize(), itemstack.getMaxStackSize());
                    ++i;
                }
            }

            f /= (float) iinventory.getSize();
            return MathHelper.d(f * 14.0F) + (i > 0 ? 1 : 0);
        }
    }
}