Container

GUI inventory model: Slot views, slotClick prediction, transaction IDs, shift-click merge driving C0E/S32.

net/minecraft/inventory/Container.java

Updated 2026-09-12

Packets Referenced (0)

Related Vanilla Internals

Deep Analysis

Abstract server-side model for window inventories like chests, furnaces, and player inventory. Owns Slot list, item-stack snapshots, drag state, and ICrafting listeners. Implements full click/drag/shift-click/number-key/drop logic in slotClick. Change detection fans out via detectAndSendChanges.

Key Methods (8)

slotClick

Implements all window click modes 0-6 including drag-split.

Handles pickup/place, shift transfer, hotbar swap, creative clone, drop, and double-click merge. Mode 5 manages dragEvent state machine. Server calls it from C0E click processing.

detectAndSendChanges

Diffs slots against snapshots and notifies crafting listeners.

Compares each Slot stack with inventoryItemStacks via areItemStacksEqual. On change copies stack and calls ICrafting.sendSlotContents. Drives S2F and S30 updates.

transferStackInSlot

Shift-click quick-move hook overridden by subclasses.

Base returns slot stack; subclasses implement mergeItemStack ranges. Result determines what server sends back to client window.

mergeItemStack

Merges stack into slot range respecting stackability and limits.

First pass stacks onto same item/tag, second pass fills empty slots. Honors max stack size and direction flag. Core of shift-click behavior.

onCraftGuiOpened

Registers a viewer and syncs full inventory initially.

Adds ICrafting listener, calls updateCraftingInventory with getInventory, then detectAndSendChanges. Maps to S2D open plus S30 bulk.

onContainerClosed

Drops held cursor stack when window closes.

If InventoryPlayer cursor stack non-null, drops via dropPlayerItemWithRandomChoice and clears. Prevents item duplication on close.

getNextTransactionID

Allocates short action number for click confirmations.

Increments transactionID counter. NetHandlerPlayServer echoes it in S32 confirm; client C0F uses it for resync.

canInteractWith

Abstract distance/validity check for open window.

Subclasses check block distance or entity liveness. EntityPlayer.onUpdate closes screen when false. Triggers S2E close.

Internal State

inventorySlotsOrdered Slot models mapping window slot IDs to inventories
inventoryItemStacksLast-sent snapshot for change detection
craftersICrafting viewers receiving slot and progress updates
dragSlotsSlots collected during mode-5 drag-split gesture
windowIdWindow identifier multiplexing client/server containers

Related Vanilla Internals

Full Source

Container.java800 lines · MCP 1.8.9 · verbatimOriginal ↗
1
package net.minecraft.inventory;
2
3
import com.google.common.collect.Lists;
4
import com.google.common.collect.Sets;
5
import java.util.List;
6
import java.util.Set;
7
import net.minecraft.entity.player.EntityPlayer;
8
import net.minecraft.entity.player.InventoryPlayer;
9
import net.minecraft.item.Item;
10
import net.minecraft.item.ItemStack;
11
import net.minecraft.tileentity.TileEntity;
12
import net.minecraft.util.MathHelper;
13
14
public abstract class Container
15
{
16
    public List<ItemStack> inventoryItemStacks = Lists.<ItemStack>newArrayList();
17
    public List<Slot> inventorySlots = Lists.<Slot>newArrayList();
18
    public int windowId;
19
    private short transactionID;
20
21
    /**
22
     * The current drag mode (0 : evenly split, 1 : one item by slot, 2 : not used ?)
23
     */
24
    private int dragMode = -1;
25
26
    /** The current drag event (0 : start, 1 : add slot : 2 : end) */
27
    private int dragEvent;
28
    private final Set<Slot> dragSlots = Sets.<Slot>newHashSet();
29
    protected List<ICrafting> crafters = Lists.<ICrafting>newArrayList();
30
    private Set<EntityPlayer> playerList = Sets.<EntityPlayer>newHashSet();
31
32
    /**
33
     * Adds an item slot to this container
34
     */
35
    protected Slot addSlotToContainer(Slot slotIn)
36
    {
37
        slotIn.slotNumber = this.inventorySlots.size();
38
        this.inventorySlots.add(slotIn);
39
        this.inventoryItemStacks.add((ItemStack)null);
40
        return slotIn;
41
    }
42
43
    public void onCraftGuiOpened(ICrafting listener)
44
    {
45
        if (this.crafters.contains(listener))
46
        {
47
            throw new IllegalArgumentException("Listener already listening");
48
        }
49
        else
50
        {
51
            this.crafters.add(listener);
52
            listener.updateCraftingInventory(this, this.getInventory());
53
            this.detectAndSendChanges();
54
        }
55
    }
56
57
    /**
58
     * Remove the given Listener. Method name is for legacy.
59
     */
60
    public void removeCraftingFromCrafters(ICrafting listeners)
61
    {
62
        this.crafters.remove(listeners);
63
    }
64
65
    public List<ItemStack> getInventory()
66
    {
67
        List<ItemStack> list = Lists.<ItemStack>newArrayList();
68
69
        for (int i = 0; i < this.inventorySlots.size(); ++i)
70
        {
71
            list.add(((Slot)this.inventorySlots.get(i)).getStack());
72
        }
73
74
        return list;
75
    }
76
77
    /**
78
     * Looks for changes made in the container, sends them to every listener.
79
     */
80
    public void detectAndSendChanges()
81
    {
82
        for (int i = 0; i < this.inventorySlots.size(); ++i)
83
        {
84
            ItemStack itemstack = ((Slot)this.inventorySlots.get(i)).getStack();
85
            ItemStack itemstack1 = (ItemStack)this.inventoryItemStacks.get(i);
86
87
            if (!ItemStack.areItemStacksEqual(itemstack1, itemstack))
88
            {
89
                itemstack1 = itemstack == null ? null : itemstack.copy();
90
                this.inventoryItemStacks.set(i, itemstack1);
91
92
                for (int j = 0; j < this.crafters.size(); ++j)
93
                {
94
                    ((ICrafting)this.crafters.get(j)).sendSlotContents(this, i, itemstack1);
95
                }
96
            }
97
        }
98
    }
99
100
    /**
101
     * Handles the given Button-click on the server, currently only used by enchanting. Name is for legacy.
102
     */
103
    public boolean enchantItem(EntityPlayer playerIn, int id)
104
    {
105
        return false;
106
    }
107
108
    public Slot getSlotFromInventory(IInventory inv, int slotIn)
109
    {
110
        for (int i = 0; i < this.inventorySlots.size(); ++i)
111
        {
112
            Slot slot = (Slot)this.inventorySlots.get(i);
113
114
            if (slot.isHere(inv, slotIn))
115
            {
116
                return slot;
117
            }
118
        }
119
120
        return null;
121
    }
122
123
    public Slot getSlot(int slotId)
124
    {
125
        return (Slot)this.inventorySlots.get(slotId);
126
    }
127
128
    /**
129
     * Take a stack from the specified inventory slot.
130
     */
131
    public ItemStack transferStackInSlot(EntityPlayer playerIn, int index)
132
    {
133
        Slot slot = (Slot)this.inventorySlots.get(index);
134
        return slot != null ? slot.getStack() : null;
135
    }
136
137
    /**
138
     * Handles slot click.
139
     */
140
    public ItemStack slotClick(int slotId, int clickedButton, int mode, EntityPlayer playerIn)
141
    {
142
        ItemStack itemstack = null;
143
        InventoryPlayer inventoryplayer = playerIn.inventory;
144
145
        if (mode == 5)
146
        {
147
            int i = this.dragEvent;
148
            this.dragEvent = getDragEvent(clickedButton);
149
150
            if ((i != 1 || this.dragEvent != 2) && i != this.dragEvent)
151
            {
152
                this.resetDrag();
153
            }
154
            else if (inventoryplayer.getItemStack() == null)
155
            {
156
                this.resetDrag();
157
            }
158
            else if (this.dragEvent == 0)
159
            {
160
                this.dragMode = extractDragMode(clickedButton);
161
162
                if (isValidDragMode(this.dragMode, playerIn))
163
                {
164
                    this.dragEvent = 1;
165
                    this.dragSlots.clear();
166
                }
167
                else
168
                {
169
                    this.resetDrag();
170
                }
171
            }
172
            else if (this.dragEvent == 1)
173
            {
174
                Slot slot = (Slot)this.inventorySlots.get(slotId);
175
176
                if (slot != null && canAddItemToSlot(slot, inventoryplayer.getItemStack(), true) && slot.isItemValid(inventoryplayer.getItemStack()) && inventoryplayer.getItemStack().stackSize > this.dragSlots.size() && this.canDragIntoSlot(slot))
177
                {
178
                    this.dragSlots.add(slot);
179
                }
180
            }
181
            else if (this.dragEvent == 2)
182
            {
183
                if (!this.dragSlots.isEmpty())
184
                {
185
                    ItemStack itemstack3 = inventoryplayer.getItemStack().copy();
186
                    int j = inventoryplayer.getItemStack().stackSize;
187
188
                    for (Slot slot1 : this.dragSlots)
189
                    {
190
                        if (slot1 != null && canAddItemToSlot(slot1, inventoryplayer.getItemStack(), true) && slot1.isItemValid(inventoryplayer.getItemStack()) && inventoryplayer.getItemStack().stackSize >= this.dragSlots.size() && this.canDragIntoSlot(slot1))
191
                        {
192
                            ItemStack itemstack1 = itemstack3.copy();
193
                            int k = slot1.getHasStack() ? slot1.getStack().stackSize : 0;
194
                            computeStackSize(this.dragSlots, this.dragMode, itemstack1, k);
195
196
                            if (itemstack1.stackSize > itemstack1.getMaxStackSize())
197
                            {
198
                                itemstack1.stackSize = itemstack1.getMaxStackSize();
199
                            }
200
201
                            if (itemstack1.stackSize > slot1.getItemStackLimit(itemstack1))
202
                            {
203
                                itemstack1.stackSize = slot1.getItemStackLimit(itemstack1);
204
                            }
205
206
                            j -= itemstack1.stackSize - k;
207
                            slot1.putStack(itemstack1);
208
                        }
209
                    }
210
211
                    itemstack3.stackSize = j;
212
213
                    if (itemstack3.stackSize <= 0)
214
                    {
215
                        itemstack3 = null;
216
                    }
217
218
                    inventoryplayer.setItemStack(itemstack3);
219
                }
220
221
                this.resetDrag();
222
            }
223
            else
224
            {
225
                this.resetDrag();
226
            }
227
        }
228
        else if (this.dragEvent != 0)
229
        {
230
            this.resetDrag();
231
        }
232
        else if ((mode == 0 || mode == 1) && (clickedButton == 0 || clickedButton == 1))
233
        {
234
            if (slotId == -999)
235
            {
236
                if (inventoryplayer.getItemStack() != null)
237
                {
238
                    if (clickedButton == 0)
239
                    {
240
                        playerIn.dropPlayerItemWithRandomChoice(inventoryplayer.getItemStack(), true);
241
                        inventoryplayer.setItemStack((ItemStack)null);
242
                    }
243
244
                    if (clickedButton == 1)
245
                    {
246
                        playerIn.dropPlayerItemWithRandomChoice(inventoryplayer.getItemStack().splitStack(1), true);
247
248
                        if (inventoryplayer.getItemStack().stackSize == 0)
249
                        {
250
                            inventoryplayer.setItemStack((ItemStack)null);
251
                        }
252
                    }
253
                }
254
            }
255
            else if (mode == 1)
256
            {
257
                if (slotId < 0)
258
                {
259
                    return null;
260
                }
261
262
                Slot slot6 = (Slot)this.inventorySlots.get(slotId);
263
264
                if (slot6 != null && slot6.canTakeStack(playerIn))
265
                {
266
                    ItemStack itemstack8 = this.transferStackInSlot(playerIn, slotId);
267
268
                    if (itemstack8 != null)
269
                    {
270
                        Item item = itemstack8.getItem();
271
                        itemstack = itemstack8.copy();
272
273
                        if (slot6.getStack() != null && slot6.getStack().getItem() == item)
274
                        {
275
                            this.retrySlotClick(slotId, clickedButton, true, playerIn);
276
                        }
277
                    }
278
                }
279
            }
280
            else
281
            {
282
                if (slotId < 0)
283
                {
284
                    return null;
285
                }
286
287
                Slot slot7 = (Slot)this.inventorySlots.get(slotId);
288
289
                if (slot7 != null)
290
                {
291
                    ItemStack itemstack9 = slot7.getStack();
292
                    ItemStack itemstack10 = inventoryplayer.getItemStack();
293
294
                    if (itemstack9 != null)
295
                    {
296
                        itemstack = itemstack9.copy();
297
                    }
298
299
                    if (itemstack9 == null)
300
                    {
301
                        if (itemstack10 != null && slot7.isItemValid(itemstack10))
302
                        {
303
                            int k2 = clickedButton == 0 ? itemstack10.stackSize : 1;
304
305
                            if (k2 > slot7.getItemStackLimit(itemstack10))
306
                            {
307
                                k2 = slot7.getItemStackLimit(itemstack10);
308
                            }
309
310
                            if (itemstack10.stackSize >= k2)
311
                            {
312
                                slot7.putStack(itemstack10.splitStack(k2));
313
                            }
314
315
                            if (itemstack10.stackSize == 0)
316
                            {
317
                                inventoryplayer.setItemStack((ItemStack)null);
318
                            }
319
                        }
320
                    }
321
                    else if (slot7.canTakeStack(playerIn))
322
                    {
323
                        if (itemstack10 == null)
324
                        {
325
                            int j2 = clickedButton == 0 ? itemstack9.stackSize : (itemstack9.stackSize + 1) / 2;
326
                            ItemStack itemstack12 = slot7.decrStackSize(j2);
327
                            inventoryplayer.setItemStack(itemstack12);
328
329
                            if (itemstack9.stackSize == 0)
330
                            {
331
                                slot7.putStack((ItemStack)null);
332
                            }
333
334
                            slot7.onPickupFromSlot(playerIn, inventoryplayer.getItemStack());
335
                        }
336
                        else if (slot7.isItemValid(itemstack10))
337
                        {
338
                            if (itemstack9.getItem() == itemstack10.getItem() && itemstack9.getMetadata() == itemstack10.getMetadata() && ItemStack.areItemStackTagsEqual(itemstack9, itemstack10))
339
                            {
340
                                int i2 = clickedButton == 0 ? itemstack10.stackSize : 1;
341
342
                                if (i2 > slot7.getItemStackLimit(itemstack10) - itemstack9.stackSize)
343
                                {
344
                                    i2 = slot7.getItemStackLimit(itemstack10) - itemstack9.stackSize;
345
                                }
346
347
                                if (i2 > itemstack10.getMaxStackSize() - itemstack9.stackSize)
348
                                {
349
                                    i2 = itemstack10.getMaxStackSize() - itemstack9.stackSize;
350
                                }
351
352
                                itemstack10.splitStack(i2);
353
354
                                if (itemstack10.stackSize == 0)
355
                                {
356
                                    inventoryplayer.setItemStack((ItemStack)null);
357
                                }
358
359
                                itemstack9.stackSize += i2;
360
                            }
361
                            else if (itemstack10.stackSize <= slot7.getItemStackLimit(itemstack10))
362
                            {
363
                                slot7.putStack(itemstack10);
364
                                inventoryplayer.setItemStack(itemstack9);
365
                            }
366
                        }
367
                        else if (itemstack9.getItem() == itemstack10.getItem() && itemstack10.getMaxStackSize() > 1 && (!itemstack9.getHasSubtypes() || itemstack9.getMetadata() == itemstack10.getMetadata()) && ItemStack.areItemStackTagsEqual(itemstack9, itemstack10))
368
                        {
369
                            int l1 = itemstack9.stackSize;
370
371
                            if (l1 > 0 && l1 + itemstack10.stackSize <= itemstack10.getMaxStackSize())
372
                            {
373
                                itemstack10.stackSize += l1;
374
                                itemstack9 = slot7.decrStackSize(l1);
375
376
                                if (itemstack9.stackSize == 0)
377
                                {
378
                                    slot7.putStack((ItemStack)null);
379
                                }
380
381
                                slot7.onPickupFromSlot(playerIn, inventoryplayer.getItemStack());
382
                            }
383
                        }
384
                    }
385
386
                    slot7.onSlotChanged();
387
                }
388
            }
389
        }
390
        else if (mode == 2 && clickedButton >= 0 && clickedButton < 9)
391
        {
392
            Slot slot5 = (Slot)this.inventorySlots.get(slotId);
393
394
            if (slot5.canTakeStack(playerIn))
395
            {
396
                ItemStack itemstack7 = inventoryplayer.getStackInSlot(clickedButton);
397
                boolean flag = itemstack7 == null || slot5.inventory == inventoryplayer && slot5.isItemValid(itemstack7);
398
                int k1 = -1;
399
400
                if (!flag)
401
                {
402
                    k1 = inventoryplayer.getFirstEmptyStack();
403
                    flag |= k1 > -1;
404
                }
405
406
                if (slot5.getHasStack() && flag)
407
                {
408
                    ItemStack itemstack11 = slot5.getStack();
409
                    inventoryplayer.setInventorySlotContents(clickedButton, itemstack11.copy());
410
411
                    if ((slot5.inventory != inventoryplayer || !slot5.isItemValid(itemstack7)) && itemstack7 != null)
412
                    {
413
                        if (k1 > -1)
414
                        {
415
                            inventoryplayer.addItemStackToInventory(itemstack7);
416
                            slot5.decrStackSize(itemstack11.stackSize);
417
                            slot5.putStack((ItemStack)null);
418
                            slot5.onPickupFromSlot(playerIn, itemstack11);
419
                        }
420
                    }
421
                    else
422
                    {
423
                        slot5.decrStackSize(itemstack11.stackSize);
424
                        slot5.putStack(itemstack7);
425
                        slot5.onPickupFromSlot(playerIn, itemstack11);
426
                    }
427
                }
428
                else if (!slot5.getHasStack() && itemstack7 != null && slot5.isItemValid(itemstack7))
429
                {
430
                    inventoryplayer.setInventorySlotContents(clickedButton, (ItemStack)null);
431
                    slot5.putStack(itemstack7);
432
                }
433
            }
434
        }
435
        else if (mode == 3 && playerIn.capabilities.isCreativeMode && inventoryplayer.getItemStack() == null && slotId >= 0)
436
        {
437
            Slot slot4 = (Slot)this.inventorySlots.get(slotId);
438
439
            if (slot4 != null && slot4.getHasStack())
440
            {
441
                ItemStack itemstack6 = slot4.getStack().copy();
442
                itemstack6.stackSize = itemstack6.getMaxStackSize();
443
                inventoryplayer.setItemStack(itemstack6);
444
            }
445
        }
446
        else if (mode == 4 && inventoryplayer.getItemStack() == null && slotId >= 0)
447
        {
448
            Slot slot3 = (Slot)this.inventorySlots.get(slotId);
449
450
            if (slot3 != null && slot3.getHasStack() && slot3.canTakeStack(playerIn))
451
            {
452
                ItemStack itemstack5 = slot3.decrStackSize(clickedButton == 0 ? 1 : slot3.getStack().stackSize);
453
                slot3.onPickupFromSlot(playerIn, itemstack5);
454
                playerIn.dropPlayerItemWithRandomChoice(itemstack5, true);
455
            }
456
        }
457
        else if (mode == 6 && slotId >= 0)
458
        {
459
            Slot slot2 = (Slot)this.inventorySlots.get(slotId);
460
            ItemStack itemstack4 = inventoryplayer.getItemStack();
461
462
            if (itemstack4 != null && (slot2 == null || !slot2.getHasStack() || !slot2.canTakeStack(playerIn)))
463
            {
464
                int i1 = clickedButton == 0 ? 0 : this.inventorySlots.size() - 1;
465
                int j1 = clickedButton == 0 ? 1 : -1;
466
467
                for (int l2 = 0; l2 < 2; ++l2)
468
                {
469
                    for (int i3 = i1; i3 >= 0 && i3 < this.inventorySlots.size() && itemstack4.stackSize < itemstack4.getMaxStackSize(); i3 += j1)
470
                    {
471
                        Slot slot8 = (Slot)this.inventorySlots.get(i3);
472
473
                        if (slot8.getHasStack() && canAddItemToSlot(slot8, itemstack4, true) && slot8.canTakeStack(playerIn) && this.canMergeSlot(itemstack4, slot8) && (l2 != 0 || slot8.getStack().stackSize != slot8.getStack().getMaxStackSize()))
474
                        {
475
                            int l = Math.min(itemstack4.getMaxStackSize() - itemstack4.stackSize, slot8.getStack().stackSize);
476
                            ItemStack itemstack2 = slot8.decrStackSize(l);
477
                            itemstack4.stackSize += l;
478
479
                            if (itemstack2.stackSize <= 0)
480
                            {
481
                                slot8.putStack((ItemStack)null);
482
                            }
483
484
                            slot8.onPickupFromSlot(playerIn, itemstack2);
485
                        }
486
                    }
487
                }
488
            }
489
490
            this.detectAndSendChanges();
491
        }
492
493
        return itemstack;
494
    }
495
496
    /**
497
     * Called to determine if the current slot is valid for the stack merging (double-click) code. The stack passed in
498
     * is null for the initial slot that was double-clicked.
499
     */
500
    public boolean canMergeSlot(ItemStack stack, Slot slotIn)
501
    {
502
        return true;
503
    }
504
505
    /**
506
     * Retries slotClick() in case of failure
507
     */
508
    protected void retrySlotClick(int slotId, int clickedButton, boolean mode, EntityPlayer playerIn)
509
    {
510
        this.slotClick(slotId, clickedButton, 1, playerIn);
511
    }
512
513
    /**
514
     * Called when the container is closed.
515
     */
516
    public void onContainerClosed(EntityPlayer playerIn)
517
    {
518
        InventoryPlayer inventoryplayer = playerIn.inventory;
519
520
        if (inventoryplayer.getItemStack() != null)
521
        {
522
            playerIn.dropPlayerItemWithRandomChoice(inventoryplayer.getItemStack(), false);
523
            inventoryplayer.setItemStack((ItemStack)null);
524
        }
525
    }
526
527
    /**
528
     * Callback for when the crafting matrix is changed.
529
     */
530
    public void onCraftMatrixChanged(IInventory inventoryIn)
531
    {
532
        this.detectAndSendChanges();
533
    }
534
535
    /**
536
     * args: slotID, itemStack to put in slot
537
     */
538
    public void putStackInSlot(int slotID, ItemStack stack)
539
    {
540
        this.getSlot(slotID).putStack(stack);
541
    }
542
543
    /**
544
     * places itemstacks in first x slots, x being aitemstack.lenght
545
     */
546
    public void putStacksInSlots(ItemStack[] p_75131_1_)
547
    {
548
        for (int i = 0; i < p_75131_1_.length; ++i)
549
        {
550
            this.getSlot(i).putStack(p_75131_1_[i]);
551
        }
552
    }
553
554
    public void updateProgressBar(int id, int data)
555
    {
556
    }
557
558
    /**
559
     * Gets a unique transaction ID. Parameter is unused.
560
     */
561
    public short getNextTransactionID(InventoryPlayer p_75136_1_)
562
    {
563
        ++this.transactionID;
564
        return this.transactionID;
565
    }
566
567
    /**
568
     * gets whether or not the player can craft in this inventory or not
569
     */
570
    public boolean getCanCraft(EntityPlayer p_75129_1_)
571
    {
572
        return !this.playerList.contains(p_75129_1_);
573
    }
574
575
    /**
576
     * sets whether the player can craft in this inventory or not
577
     */
578
    public void setCanCraft(EntityPlayer p_75128_1_, boolean p_75128_2_)
579
    {
580
        if (p_75128_2_)
581
        {
582
            this.playerList.remove(p_75128_1_);
583
        }
584
        else
585
        {
586
            this.playerList.add(p_75128_1_);
587
        }
588
    }
589
590
    public abstract boolean canInteractWith(EntityPlayer playerIn);
591
592
    /**
593
     * Merges provided ItemStack with the first avaliable one in the container/player inventor between minIndex
594
     * (included) and maxIndex (excluded). Args : stack, minIndex, maxIndex, negativDirection. /!\ the Container
595
     * implementation do not check if the item is valid for the slot
596
     */
597
    protected boolean mergeItemStack(ItemStack stack, int startIndex, int endIndex, boolean reverseDirection)
598
    {
599
        boolean flag = false;
600
        int i = startIndex;
601
602
        if (reverseDirection)
603
        {
604
            i = endIndex - 1;
605
        }
606
607
        if (stack.isStackable())
608
        {
609
            while (stack.stackSize > 0 && (!reverseDirection && i < endIndex || reverseDirection && i >= startIndex))
610
            {
611
                Slot slot = (Slot)this.inventorySlots.get(i);
612
                ItemStack itemstack = slot.getStack();
613
614
                if (itemstack != null && itemstack.getItem() == stack.getItem() && (!stack.getHasSubtypes() || stack.getMetadata() == itemstack.getMetadata()) && ItemStack.areItemStackTagsEqual(stack, itemstack))
615
                {
616
                    int j = itemstack.stackSize + stack.stackSize;
617
618
                    if (j <= stack.getMaxStackSize())
619
                    {
620
                        stack.stackSize = 0;
621
                        itemstack.stackSize = j;
622
                        slot.onSlotChanged();
623
                        flag = true;
624
                    }
625
                    else if (itemstack.stackSize < stack.getMaxStackSize())
626
                    {
627
                        stack.stackSize -= stack.getMaxStackSize() - itemstack.stackSize;
628
                        itemstack.stackSize = stack.getMaxStackSize();
629
                        slot.onSlotChanged();
630
                        flag = true;
631
                    }
632
                }
633
634
                if (reverseDirection)
635
                {
636
                    --i;
637
                }
638
                else
639
                {
640
                    ++i;
641
                }
642
            }
643
        }
644
645
        if (stack.stackSize > 0)
646
        {
647
            if (reverseDirection)
648
            {
649
                i = endIndex - 1;
650
            }
651
            else
652
            {
653
                i = startIndex;
654
            }
655
656
            while (!reverseDirection && i < endIndex || reverseDirection && i >= startIndex)
657
            {
658
                Slot slot1 = (Slot)this.inventorySlots.get(i);
659
                ItemStack itemstack1 = slot1.getStack();
660
661
                if (itemstack1 == null)
662
                {
663
                    slot1.putStack(stack.copy());
664
                    slot1.onSlotChanged();
665
                    stack.stackSize = 0;
666
                    flag = true;
667
                    break;
668
                }
669
670
                if (reverseDirection)
671
                {
672
                    --i;
673
                }
674
                else
675
                {
676
                    ++i;
677
                }
678
            }
679
        }
680
681
        return flag;
682
    }
683
684
    /**
685
     * Extracts the drag mode. Args : eventButton. Return (0 : evenly split, 1 : one item by slot, 2 : not used ?)
686
     */
687
    public static int extractDragMode(int p_94529_0_)
688
    {
689
        return p_94529_0_ >> 2 & 3;
690
    }
691
692
    /**
693
     * Args : clickedButton, Returns (0 : start drag, 1 : add slot, 2 : end drag)
694
     */
695
    public static int getDragEvent(int p_94532_0_)
696
    {
697
        return p_94532_0_ & 3;
698
    }
699
700
    public static int func_94534_d(int p_94534_0_, int p_94534_1_)
701
    {
702
        return p_94534_0_ & 3 | (p_94534_1_ & 3) << 2;
703
    }
704
705
    public static boolean isValidDragMode(int dragModeIn, EntityPlayer player)
706
    {
707
        return dragModeIn == 0 ? true : (dragModeIn == 1 ? true : dragModeIn == 2 && player.capabilities.isCreativeMode);
708
    }
709
710
    /**
711
     * Reset the drag fields
712
     */
713
    protected void resetDrag()
714
    {
715
        this.dragEvent = 0;
716
        this.dragSlots.clear();
717
    }
718
719
    /**
720
     * Checks if it's possible to add the given itemstack to the given slot.
721
     */
722
    public static boolean canAddItemToSlot(Slot slotIn, ItemStack stack, boolean stackSizeMatters)
723
    {
724
        boolean flag = slotIn == null || !slotIn.getHasStack();
725
726
        if (slotIn != null && slotIn.getHasStack() && stack != null && stack.isItemEqual(slotIn.getStack()) && ItemStack.areItemStackTagsEqual(slotIn.getStack(), stack))
727
        {
728
            flag |= slotIn.getStack().stackSize + (stackSizeMatters ? 0 : stack.stackSize) <= stack.getMaxStackSize();
729
        }
730
731
        return flag;
732
    }
733
734
    /**
735
     * Compute the new stack size, Returns the stack with the new size. Args : dragSlots, dragMode, dragStack,
736
     * slotStackSize
737
     */
738
    public static void computeStackSize(Set<Slot> p_94525_0_, int p_94525_1_, ItemStack p_94525_2_, int p_94525_3_)
739
    {
740
        switch (p_94525_1_)
741
        {
742
            case 0:
743
                p_94525_2_.stackSize = MathHelper.floor_float((float)p_94525_2_.stackSize / (float)p_94525_0_.size());
744
                break;
745
746
            case 1:
747
                p_94525_2_.stackSize = 1;
748
                break;
749
750
            case 2:
751
                p_94525_2_.stackSize = p_94525_2_.getItem().getItemStackLimit();
752
        }
753
754
        p_94525_2_.stackSize += p_94525_3_;
755
    }
756
757
    /**
758
     * Returns true if the player can "drag-spilt" items into this slot,. returns true by default. Called to check if
759
     * the slot can be added to a list of Slots to split the held ItemStack across.
760
     */
761
    public boolean canDragIntoSlot(Slot p_94531_1_)
762
    {
763
        return true;
764
    }
765
766
    /**
767
     * Like the version that takes an inventory. If the given TileEntity is not an Inventory, 0 is returned instead.
768
     */
769
    public static int calcRedstone(TileEntity te)
770
    {
771
        return te instanceof IInventory ? calcRedstoneFromInventory((IInventory)te) : 0;
772
    }
773
774
    public static int calcRedstoneFromInventory(IInventory inv)
775
    {
776
        if (inv == null)
777
        {
778
            return 0;
779
        }
780
        else
781
        {
782
            int i = 0;
783
            float f = 0.0F;
784
785
            for (int j = 0; j < inv.getSizeInventory(); ++j)
786
            {
787
                ItemStack itemstack = inv.getStackInSlot(j);
788
789
                if (itemstack != null)
790
                {
791
                    f += (float)itemstack.stackSize / (float)Math.min(inv.getInventoryStackLimit(), itemstack.getMaxStackSize());
792
                    ++i;
793
                }
794
            }
795
796
            f = f / (float)inv.getSizeInventory();
797
            return MathHelper.floor_float(f * 14.0F) + (i > 0 ? 1 : 0);
798
        }
799
    }
800
}

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