summaryrefslogtreecommitdiffstats
path: root/src/main/java/net/minecraft/server/ContainerAnvil.java
blob: eabc7b141220de8d5a7232eb489b5463b31e1871 (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
package net.minecraft.server;

import java.util.Iterator;
import java.util.Map;

// CraftBukkit start
import org.bukkit.craftbukkit.inventory.CraftInventory;
import org.bukkit.craftbukkit.inventory.CraftInventoryAnvil;
import org.bukkit.craftbukkit.inventory.CraftInventoryView;
// CraftBukkit end

public class ContainerAnvil extends Container {

    private IInventory f = new InventoryCraftResult();
    private IInventory g = new ContainerAnvilInventory(this, "Repair", 2);
    private World h;
    private int i;
    private int j;
    private int k;
    public int a = 0;
    private int l = 0;
    private String m;
    private final EntityHuman n;
    // CraftBukkit start
    private CraftInventoryView bukkitEntity = null;
    private PlayerInventory player;
    // CraftBukkit end

    public ContainerAnvil(PlayerInventory playerinventory, World world, int i, int j, int k, EntityHuman entityhuman) {
        this.player = playerinventory; // CraftBukkit
        this.h = world;
        this.i = i;
        this.j = j;
        this.k = k;
        this.n = entityhuman;
        this.a(new Slot(this.g, 0, 27, 47));
        this.a(new Slot(this.g, 1, 76, 47));
        this.a((Slot) (new SlotAnvilResult(this, this.f, 2, 134, 47, world, i, j, k)));

        int l;

        for (l = 0; l < 3; ++l) {
            for (int i1 = 0; i1 < 9; ++i1) {
                this.a(new Slot(playerinventory, i1 + l * 9 + 9, 8 + i1 * 18, 84 + l * 18));
            }
        }

        for (l = 0; l < 9; ++l) {
            this.a(new Slot(playerinventory, l, 8 + l * 18, 142));
        }
    }

    public void a(IInventory iinventory) {
        super.a(iinventory);
        if (iinventory == this.g) {
            this.d();
        }
    }

    public void d() {
        ItemStack itemstack = this.g.getItem(0);

        this.a = 0;
        int i = 0;
        byte b0 = 0;
        int j = 0;

        if (itemstack == null) {
            this.f.setItem(0, (ItemStack) null);
            this.a = 0;
        } else {
            ItemStack itemstack1 = itemstack.cloneItemStack();
            ItemStack itemstack2 = this.g.getItem(1);
            Map map = EnchantmentManager.a(itemstack1);
            int k = b0 + itemstack.getRepairCost() + (itemstack2 == null ? 0 : itemstack2.getRepairCost());

            this.l = 0;
            int l;
            int i1;
            int j1;
            int k1;
            Enchantment enchantment;
            Iterator iterator;

            if (itemstack2 != null) {
                if (itemstack1.f() && Item.byId[itemstack1.id].a(itemstack, itemstack2)) {
                    l = Math.min(itemstack1.i(), itemstack1.k() / 4);
                    if (l <= 0) {
                        this.f.setItem(0, (ItemStack) null);
                        this.a = 0;
                        return;
                    }

                    for (i1 = 0; l > 0 && i1 < itemstack2.count; ++i1) {
                        j1 = itemstack1.i() - l;
                        itemstack1.setData(j1);
                        i += Math.max(1, l / 100) + map.size();
                        l = Math.min(itemstack1.i(), itemstack1.k() / 4);
                    }

                    this.l = i1;
                } else {
                    if (itemstack1.id != itemstack2.id || !itemstack1.f()) {
                        this.f.setItem(0, (ItemStack) null);
                        this.a = 0;
                        return;
                    }

                    if (itemstack1.f()) {
                        l = itemstack.k() - itemstack.i();
                        i1 = itemstack2.k() - itemstack2.i();
                        j1 = i1 + itemstack1.k() * 12 / 100;
                        int l1 = l + j1;

                        k1 = itemstack1.k() - l1;
                        if (k1 < 0) {
                            k1 = 0;
                        }

                        if (k1 < itemstack1.getData()) {
                            itemstack1.setData(k1);
                            i += Math.max(1, j1 / 100);
                        }
                    }

                    Map map1 = EnchantmentManager.a(itemstack2);

                    iterator = map1.keySet().iterator();

                    while (iterator.hasNext()) {
                        j1 = ((Integer) iterator.next()).intValue();
                        enchantment = Enchantment.byId[j1];
                        k1 = map.containsKey(Integer.valueOf(j1)) ? ((Integer) map.get(Integer.valueOf(j1))).intValue() : 0;
                        int i2 = ((Integer) map1.get(Integer.valueOf(j1))).intValue();
                        int j2;

                        if (k1 == i2) {
                            ++i2;
                            j2 = i2;
                        } else {
                            j2 = Math.max(i2, k1);
                        }

                        i2 = j2;
                        int k2 = i2 - k1;
                        boolean flag = true;
                        Iterator iterator1 = map.keySet().iterator();

                        while (iterator1.hasNext()) {
                            int l2 = ((Integer) iterator1.next()).intValue();

                            if (l2 != j1 && !enchantment.a(Enchantment.byId[l2])) {
                                flag = false;
                                i += k2;
                            }
                        }

                        if (flag) {
                            if (i2 > enchantment.getMaxLevel()) {
                                i2 = enchantment.getMaxLevel();
                            }

                            map.put(Integer.valueOf(j1), Integer.valueOf(i2));
                            byte b1 = 0;

                            switch (enchantment.getRandomWeight()) {
                            case 1:
                                b1 = 8;
                                break;

                            case 2:
                                b1 = 4;

                            case 3:
                            case 4:
                            case 6:
                            case 7:
                            case 8:
                            case 9:
                            default:
                                break;

                            case 5:
                                b1 = 2;
                                break;

                            case 10:
                                b1 = 1;
                            }

                            i += b1 * k2;
                        }
                    }
                }
            }

            if (this.m != null && !this.m.equalsIgnoreCase(itemstack.r()) && this.m.length() > 0) {
                j = itemstack.f() ? 7 : itemstack.count * 5;
                i += j;
                if (itemstack.s()) {
                    k += j / 2;
                }

                itemstack1.c(this.m);
            }

            l = 0;

            byte b2;

            for (iterator = map.keySet().iterator(); iterator.hasNext(); k += l + k1 * b2) {
                j1 = ((Integer) iterator.next()).intValue();
                enchantment = Enchantment.byId[j1];
                k1 = ((Integer) map.get(Integer.valueOf(j1))).intValue();
                b2 = 0;
                ++l;
                switch (enchantment.getRandomWeight()) {
                case 1:
                    b2 = 8;
                    break;

                case 2:
                    b2 = 4;

                case 3:
                case 4:
                case 6:
                case 7:
                case 8:
                case 9:
                default:
                    break;

                case 5:
                    b2 = 2;
                    break;

                case 10:
                    b2 = 1;
                }
            }

            this.a = k + i;
            if (i <= 0) {
                itemstack1 = null;
            }

            if (j == i && j > 0 && this.a >= 40) {
                //System.out.println("Naming an item only, cost too high; giving discount to cap cost to 39 levels"); // CraftBukkit -remove debug
                this.a = 39;
            }

            if (this.a >= 40 && !this.n.abilities.canInstantlyBuild) {
                itemstack1 = null;
            }

            if (itemstack1 != null) {
                i1 = itemstack1.getRepairCost();
                if (itemstack2 != null && i1 < itemstack2.getRepairCost()) {
                    i1 = itemstack2.getRepairCost();
                }

                if (itemstack1.s()) {
                    i1 -= 9;
                }

                if (i1 < 0) {
                    i1 = 0;
                }

                i1 += 2;
                itemstack1.setRepairCost(i1);
                EnchantmentManager.a(map, itemstack1);
            }

            this.f.setItem(0, itemstack1);
            this.b();
        }
    }

    public void addSlotListener(ICrafting icrafting) {
        super.addSlotListener(icrafting);
        icrafting.setContainerData(this, 0, this.a);
    }

    public void b(EntityHuman entityhuman) {
        super.b(entityhuman);
        if (!this.h.isStatic) {
            for (int i = 0; i < this.g.getSize(); ++i) {
                ItemStack itemstack = this.g.splitWithoutUpdate(i);

                if (itemstack != null) {
                    entityhuman.drop(itemstack);
                }
            }
        }
    }

    public boolean a(EntityHuman entityhuman) {
        if (!this.checkReachable) return true; // CraftBukkit
        return this.h.getTypeId(this.i, this.j, this.k) != Block.ANVIL.id ? false : entityhuman.e((double) this.i + 0.5D, (double) this.j + 0.5D, (double) this.k + 0.5D) <= 64.0D;
    }

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

        if (slot != null && slot.d()) {
            ItemStack itemstack1 = slot.getItem();

            itemstack = itemstack1.cloneItemStack();
            if (i == 2) {
                if (!this.a(itemstack1, 3, 39, true)) {
                    return null;
                }

                slot.a(itemstack1, itemstack);
            } else if (i != 0 && i != 1) {
                if (i >= 3 && i < 39 && !this.a(itemstack1, 0, 2, false)) {
                    return null;
                }
            } else if (!this.a(itemstack1, 3, 39, false)) {
                return null;
            }

            if (itemstack1.count == 0) {
                slot.set((ItemStack) null);
            } else {
                slot.e();
            }

            if (itemstack1.count == itemstack.count) {
                return null;
            }

            slot.a(entityhuman, itemstack1);
        }

        return itemstack;
    }

    public void a(String s) {
        this.m = s;
        if (this.getSlot(2).d()) {
            this.getSlot(2).getItem().c(this.m);
        }

        this.d();
    }

    static IInventory a(ContainerAnvil containeranvil) {
        return containeranvil.g;
    }

    static int b(ContainerAnvil containeranvil) {
        return containeranvil.l;
    }

    // CraftBukkit start
    public CraftInventoryView getBukkitView() {
        if (bukkitEntity != null) {
            return bukkitEntity;
        }

        CraftInventory inventory = new CraftInventoryAnvil(this.g);
        bukkitEntity = new CraftInventoryView(this.player.player.getBukkitEntity(), inventory, this);
        return bukkitEntity;
    }
    // CraftBukkit end
}