summaryrefslogtreecommitdiffstats
path: root/src/main/java/org/bukkit/craftbukkit/inventory/CraftItemStack.java
blob: 46e80e1da7f7867c57cd313c38681c91d6bfd988 (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
package org.bukkit.craftbukkit.inventory;

import java.util.HashMap;
import java.util.Map;
import net.minecraft.server.EnchantmentManager;
import net.minecraft.server.NBTTagCompound;
import net.minecraft.server.NBTTagList;
import org.bukkit.enchantments.Enchantment;
import org.bukkit.inventory.ItemStack;
import org.bukkit.Material;
import org.bukkit.configuration.serialization.DelegateDeserialization;

@DelegateDeserialization(ItemStack.class)
public class CraftItemStack extends ItemStack {
    protected net.minecraft.server.ItemStack item;

    public CraftItemStack(net.minecraft.server.ItemStack item) {
        super(
            item != null ? item.id: 0,
            item != null ? item.count : 0,
            (short)(item != null ? item.getData() : 0)
        );
        this.item = item;
    }

    public CraftItemStack(ItemStack item) {
        this(item.getTypeId(), item.getAmount(), item.getDurability());
        addUnsafeEnchantments(item.getEnchantments());
    }

    /* 'Overwritten' constructors from ItemStack, yay for Java sucking */
    public CraftItemStack(final int type) {
        this(type, 0);
    }

    public CraftItemStack(final Material type) {
        this(type, 0);
    }

    public CraftItemStack(final int type, final int amount) {
        this(type, amount, (byte) 0);
    }

    public CraftItemStack(final Material type, final int amount) {
        this(type.getId(), amount);
    }

    public CraftItemStack(final int type, final int amount, final short damage) {
        this(type, amount, damage, null);
    }

    public CraftItemStack(final Material type, final int amount, final short damage) {
        this(type.getId(), amount, damage);
    }

    public CraftItemStack(final Material type, final int amount, final short damage, final Byte data) {
        this(type.getId(), amount, damage, data);
    }

    public CraftItemStack(int type, int amount, short damage, Byte data) {
        this(new net.minecraft.server.ItemStack(type, amount, data != null ? data : damage));
    }

    /*
     * Unsure if we have to sync before each of these calls the values in 'item'
     * are all public.
     */

    @Override
    public Material getType() {
        super.setTypeId(item != null ? item.id : 0); // sync, needed?
        return super.getType();
    }

    @Override
    public int getTypeId() {
        super.setTypeId(item != null ? item.id : 0); // sync, needed?
        return item != null ? item.id : 0;
    }

    @Override
    public void setTypeId(int type) {
        if (type == 0) {
            super.setTypeId(0);
            super.setAmount(0);
            item = null;
        } else {
            if (item == null) {
                item = new net.minecraft.server.ItemStack(type, 1, 0);
                super.setAmount(1);
            } else {
                item.id = type;
                super.setTypeId(item.id);
            }
        }
    }

    @Override
    public int getAmount() {
        super.setAmount(item != null ? item.count : 0); // sync, needed?
        return (item != null ? item.count : 0);
    }

    @Override
    public void setAmount(int amount) {
        if (amount == 0) {
            super.setTypeId(0);
            super.setAmount(0);
            item = null;
        } else {
            super.setAmount(amount);
            item.count = amount;
        }
    }

    @Override
    public void setDurability(final short durability) {
        // Ignore damage if item is null
        if (item != null) {
            super.setDurability(durability);
            item.setData(durability);
        }
    }

    @Override
    public short getDurability() {
        if (item != null) {
            super.setDurability((short) item.getData()); // sync, needed?
            return (short) item.getData();
        } else {
            return -1;
        }
    }

    @Override
    public int getMaxStackSize() {
        return (item == null) ? 0 : item.getItem().getMaxStackSize();
    }

    @Override
    public void addUnsafeEnchantment(Enchantment ench, int level) {
        Map<Enchantment, Integer> enchantments = getEnchantments();
        enchantments.put(ench, level);
        rebuildEnchantments(enchantments);
    }

    @Override
    public boolean containsEnchantment(Enchantment ench) {
        return getEnchantmentLevel(ench) > 0;
    }

    @Override
    public int getEnchantmentLevel(Enchantment ench) {
        if (item == null) return 0;
        return EnchantmentManager.getEnchantmentLevel(ench.getId(), item);
    }

    @Override
    public int removeEnchantment(Enchantment ench) {
        Map<Enchantment, Integer> enchantments = getEnchantments();
        Integer previous = enchantments.remove(ench);

        rebuildEnchantments(enchantments);

        return (previous == null) ? 0 : previous;
    }

    @Override
    public Map<Enchantment, Integer> getEnchantments() {
        Map<Enchantment, Integer> result = new HashMap<Enchantment, Integer>();
        NBTTagList list = (item == null) ? null : item.getEnchantments();

        if (list == null) {
            return result;
        }

        for (int i = 0; i < list.size(); i++) {
            short id = ((NBTTagCompound) list.get(i)).getShort("id");
            short level = ((NBTTagCompound) list.get(i)).getShort("lvl");

            result.put(Enchantment.getById(id), (int) level);
        }

        return result;
    }

    private void rebuildEnchantments(Map<Enchantment, Integer> enchantments) {
        if (item == null) return;

        NBTTagCompound tag = item.tag;
        NBTTagList list = new NBTTagList("ench");

        if (tag == null) {
            tag = item.tag = new NBTTagCompound();
        }

        for (Map.Entry<Enchantment, Integer> entry : enchantments.entrySet()) {
            NBTTagCompound subtag = new NBTTagCompound();

            subtag.setShort("id", (short) entry.getKey().getId());
            subtag.setShort("lvl", (short) (int) entry.getValue());

            list.add(subtag);
        }

        if (enchantments.isEmpty()) {
            tag.remove("ench");
        } else {
            tag.set("ench", list);
        }
    }

    public net.minecraft.server.ItemStack getHandle() {
        return item;
    }

    @Override
    public CraftItemStack clone() {
        return new CraftItemStack(this.item == null ? this.item : this.item.cloneItemStack());
    }

    public static net.minecraft.server.ItemStack createNMSItemStack(ItemStack original) {
        return new CraftItemStack(original).getHandle();
    }
}