/
NBTItem.java
156 lines (138 loc) · 5.48 KB
/
NBTItem.java
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
package net.aufdemrand.denizen.utilities.nbt;
import net.aufdemrand.denizen.utilities.arguments.Item;
import net.aufdemrand.denizen.utilities.debugging.dB;
import net.minecraft.server.v1_5_R2.NBTTagCompound;
import org.bukkit.craftbukkit.v1_5_R2.inventory.CraftItemStack;
import org.bukkit.inventory.ItemStack;
import java.lang.reflect.Field;
public class NBTItem {
public static MapOfEnchantments getEnchantments(ItemStack item) {
return new MapOfEnchantments(item);
}
/*
* Some static methods for dealing with Minecraft NBT data, which is used to store
* custom NBT.
*/
public static boolean hasCustomNBT(Item item, String key) {
NBTTagCompound tag;
net.minecraft.server.v1_5_R2.ItemStack cis = getItemStackHandle(item);
if (!cis.hasTag()) return false;
tag = cis.getTag();
dB.echoDebug(tag.toString());
// if this item has the NBTData for 'owner', there is an engraving.
return tag.hasKey(key);
}
public static String getCustomNBT(Item item, String key) {
net.minecraft.server.v1_5_R2.ItemStack cis = getItemStackHandle(item);
NBTTagCompound tag;
if (!cis.hasTag())
cis.setTag(new NBTTagCompound());
tag = cis.getTag();
// if this item has the NBTData for 'owner', return the value, which is the playername of the 'owner'.
if (tag.hasKey(key)) return tag.getString(key);
return null;
}
public static void removeCustomNBT(Item item, String key) {
net.minecraft.server.v1_5_R2.ItemStack cis = getItemStackHandle(item);
NBTTagCompound tag;
if (!cis.hasTag())
cis.setTag(new NBTTagCompound());
tag = cis.getTag();
// remove 'owner' NBTData
tag.remove(key);
}
public static void addCustomNBT(Item item, String key, String value) {
net.minecraft.server.v1_5_R2.ItemStack cis = getItemStackHandle(item);
NBTTagCompound tag = null;
// Do stuff with tag
if (!cis.hasTag())
cis.setTag(new NBTTagCompound());
tag = cis.getTag();
tag.setString(key, value);
}
public static boolean hasCustomNBT(ItemStack item, String key) {
NBTTagCompound tag;
net.minecraft.server.v1_5_R2.ItemStack cis = getItemStackHandle(item);
if (!cis.hasTag()) return false;
tag = cis.getTag();
dB.echoDebug(tag.toString());
// if this item has the NBTData for 'owner', there is an engraving.
return tag.hasKey(key);
}
public static String getCustomNBT(ItemStack item, String key) {
net.minecraft.server.v1_5_R2.ItemStack cis = getItemStackHandle(item);
NBTTagCompound tag;
if (!cis.hasTag())
cis.setTag(new NBTTagCompound());
tag = cis.getTag();
// if this item has the NBTData for 'owner', return the value, which is the playername of the 'owner'.
if (tag.hasKey(key)) return tag.getString(key);
return null;
}
public static void removeCustomNBT(ItemStack item, String key) {
net.minecraft.server.v1_5_R2.ItemStack cis = getItemStackHandle(item);
NBTTagCompound tag;
if (!cis.hasTag())
cis.setTag(new NBTTagCompound());
tag = cis.getTag();
// remove 'owner' NBTData
tag.remove(key);
}
public static void addCustomNBT(ItemStack item, String key, String value) {
net.minecraft.server.v1_5_R2.ItemStack cis = getItemStackHandle(item);
NBTTagCompound tag = null;
// Do stuff with tag
if (!cis.hasTag())
cis.setTag(new NBTTagCompound());
tag = cis.getTag();
tag.setString(key, value);
}
public static net.minecraft.server.v1_5_R2.ItemStack getItemStackHandle(Item item) {
Field f = null;
try {
// Use reflection to grant access to CraftItemStack field 'handle'
// which is not public
f = CraftItemStack.class.getDeclaredField("handle");
f.setAccessible(true);
} catch (SecurityException e) {
e.printStackTrace();
} catch (NoSuchFieldException e) {
e.printStackTrace();
}
net.minecraft.server.v1_5_R2.ItemStack is = null;
try {
// Use reflection to get handle
is = (net.minecraft.server.v1_5_R2.ItemStack) f.get(item.toCraftBukkit());
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
}
// Return the itemstack
return is;
}
public static net.minecraft.server.v1_5_R2.ItemStack getItemStackHandle(ItemStack item) {
Field f = null;
try {
// Use reflection to grant access to CraftItemStack field 'handle'
// which is not public
f = CraftItemStack.class.getDeclaredField("handle");
f.setAccessible(true);
} catch (SecurityException e) {
e.printStackTrace();
} catch (NoSuchFieldException e) {
e.printStackTrace();
}
net.minecraft.server.v1_5_R2.ItemStack is = null;
try {
// Use reflection to get handle
is = (net.minecraft.server.v1_5_R2.ItemStack) f.get(item);
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
}
// Return the itemstack
return is;
}
}