summaryrefslogtreecommitdiffstats
path: root/src/main/java/net/minecraft/server/Container.java
blob: 6aed2de429573328bc08ad0f1b44548ff6fa2268 (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
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 org.bukkit.craftbukkit.inventory.CraftInventory;
import org.bukkit.inventory.InventoryView;
// CraftBukkit end

public abstract class Container {

    public List a = new ArrayList();
    public List b = new ArrayList();
    public int windowId = 0;
    private short e = 0;
    protected List listeners = new ArrayList();
    private Set f = 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.g = this.b.size();
        this.b.add(slot);
        this.a.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();
        Iterator iterator = this.b.iterator();

        while (iterator.hasNext()) {
            Slot slot = (Slot) iterator.next();

            arraylist.add(slot.getItem());
        }

        return arraylist;
    }

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

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

                while (iterator.hasNext()) {
                    ICrafting icrafting = (ICrafting) iterator.next();

                    icrafting.a(this, i, itemstack1);
                }
            }
        }
    }

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

    public Slot a(IInventory iinventory, int i) {
        Iterator iterator = this.b.iterator();

        Slot slot;

        do {
            if (!iterator.hasNext()) {
                return null;
            }

            slot = (Slot) iterator.next();
        } while (!slot.a(iinventory, i));

        return slot;
    }

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

    public ItemStack b(EntityHuman entityhuman, int i) {
        Slot slot = (Slot) this.b.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;
        Slot slot;
        ItemStack itemstack1;
        int l;
        ItemStack itemstack2;

        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());
                        playerinventory.setCarried((ItemStack) null);
                    }

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

                        if (itemstack3.count == 0) {
                            // CraftBukkit end
                            playerinventory.setCarried((ItemStack) null);
                        }
                    }
                }
            } else if (k == 1) {
                slot = (Slot) this.b.get(i);
                if (slot != null && slot.a(entityhuman)) {
                    itemstack1 = this.b(entityhuman, i);
                    if (itemstack1 != null) {
                        int i1 = itemstack1.id;

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

                slot = (Slot) this.b.get(i);
                if (slot != null) {
                    itemstack1 = slot.getItem();
                    ItemStack itemstack3 = playerinventory.getCarried();

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

                    if (itemstack1 == null) {
                        if (itemstack3 != null && slot.isAllowed(itemstack3)) {
                            l = j == 0 ? itemstack3.count : 1;
                            if (l > slot.a()) {
                                l = slot.a();
                            }

                            // CraftBukkit start
                            if (itemstack3.count >= l) {
                                slot.set(itemstack3.a(l));
                            }
                            // CraftBukkit end

                            if (itemstack3.count == 0) {
                                playerinventory.setCarried((ItemStack) null);
                            }
                        }
                    } else if (slot.a(entityhuman)) {
                        if (itemstack3 == null) {
                            l = j == 0 ? itemstack1.count : (itemstack1.count + 1) / 2;
                            itemstack2 = slot.a(l);
                            playerinventory.setCarried(itemstack2);
                            if (itemstack1.count == 0) {
                                slot.set((ItemStack) null);
                            }

                            slot.a(entityhuman, playerinventory.getCarried());
                        } else if (slot.isAllowed(itemstack3)) {
                            if (itemstack1.id == itemstack3.id && (!itemstack1.usesData() || itemstack1.getData() == itemstack3.getData()) && ItemStack.equals(itemstack1, itemstack3)) {
                                l = j == 0 ? itemstack3.count : 1;
                                if (l > slot.a() - itemstack1.count) {
                                    l = slot.a() - itemstack1.count;
                                }

                                if (l > itemstack3.getMaxStackSize() - itemstack1.count) {
                                    l = itemstack3.getMaxStackSize() - itemstack1.count;
                                }

                                itemstack3.a(l);
                                if (itemstack3.count == 0) {
                                    playerinventory.setCarried((ItemStack) null);
                                }

                                itemstack1.count += l;
                            } else if (itemstack3.count <= slot.a()) {
                                slot.set(itemstack3);
                                playerinventory.setCarried(itemstack1);
                            }
                        } else if (itemstack1.id == itemstack3.id && itemstack3.getMaxStackSize() > 1 && (!itemstack1.usesData() || itemstack1.getData() == itemstack3.getData()) && ItemStack.equals(itemstack1, itemstack3)) {
                            l = itemstack1.count;
                            if (l > 0 && l + itemstack3.count <= itemstack3.getMaxStackSize()) {
                                itemstack3.count += l;
                                itemstack1 = slot.a(l);
                                if (itemstack1.count == 0) {
                                    slot.set((ItemStack) null);
                                }

                                slot.a(entityhuman, playerinventory.getCarried());
                            }
                        }
                    }

                    slot.e();
                }
            }
        } else if (k == 2 && j >= 0 && j < 9) {
            slot = (Slot) this.b.get(i);
            if (slot.a(entityhuman)) {
                itemstack1 = playerinventory.getItem(j);
                boolean flag = itemstack1 == null || slot.inventory == playerinventory && slot.isAllowed(itemstack1);

                l = -1;
                if (!flag) {
                    l = playerinventory.i();
                    flag |= l > -1;
                }

                if (slot.d() && flag) {
                    itemstack2 = slot.getItem();
                    playerinventory.setItem(j, itemstack2);
                    if ((slot.inventory != playerinventory || !slot.isAllowed(itemstack1)) && itemstack1 != null) {
                        if (l > -1) {
                            playerinventory.pickup(itemstack1);
                            slot.set((ItemStack) null);
                            slot.a(entityhuman, itemstack2);
                        }
                    } else {
                        slot.set(itemstack1);
                        slot.a(entityhuman, itemstack2);
                    }
                } else if (!slot.d() && itemstack1 != null && slot.isAllowed(itemstack1)) {
                    playerinventory.setItem(j, (ItemStack) null);
                    slot.set(itemstack1);
                }
            }
        } else if (k == 3 && entityhuman.abilities.canInstantlyBuild && playerinventory.getCarried() == null && i > 0) {
            slot = (Slot) this.b.get(i);
            if (slot != null && slot.d()) {
                itemstack1 = slot.getItem().cloneItemStack();
                itemstack1.count = itemstack1.getMaxStackSize();
                playerinventory.setCarried(itemstack1);
            }
        }

        return itemstack;
    }

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

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

        if (playerinventory.getCarried() != null) {
            entityhuman.drop(playerinventory.getCarried());
            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 b(EntityHuman entityhuman) {
        return !this.f.contains(entityhuman);
    }

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

    public abstract boolean c(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.b.get(k);
                itemstack1 = slot.getItem();
                if (itemstack1 != null && itemstack1.id == itemstack.id && (!itemstack.usesData() || itemstack.getData() == itemstack1.getData()) && ItemStack.equals(itemstack, itemstack1)) {
                    int l = itemstack1.count + itemstack.count;

                    if (l <= itemstack.getMaxStackSize()) {
                        itemstack.count = 0;
                        itemstack1.count = l;
                        slot.e();
                        flag1 = true;
                    } else if (itemstack1.count < itemstack.getMaxStackSize()) {
                        itemstack.count -= itemstack.getMaxStackSize() - itemstack1.count;
                        itemstack1.count = itemstack.getMaxStackSize();
                        slot.e();
                        flag1 = true;
                    }
                }

                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.b.get(k);
                itemstack1 = slot.getItem();
                if (itemstack1 == null) {
                    slot.set(itemstack.cloneItemStack());
                    slot.e();
                    itemstack.count = 0;
                    flag1 = true;
                    break;
                }

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

        return flag1;
    }
}