-
-
Notifications
You must be signed in to change notification settings - Fork 102
/
NBTItem.java
125 lines (98 loc) · 4.1 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
package net.aufdemrand.denizen.utilities.nbt;
import net.minecraft.server.v1_6_R1.EntityLiving;
import net.minecraft.server.v1_6_R1.NBTTagCompound;
import org.bukkit.craftbukkit.v1_6_R1.entity.CraftEntity;
import org.bukkit.craftbukkit.v1_6_R1.inventory.CraftItemStack;
import org.bukkit.entity.Entity;
import org.bukkit.entity.LivingEntity;
import org.bukkit.inventory.ItemStack;
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(ItemStack item, String key) {
NBTTagCompound tag;
net.minecraft.server.v1_6_R1.ItemStack cis = CraftItemStack.asNMSCopy(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_6_R1.ItemStack cis = CraftItemStack.asNMSCopy(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 ItemStack removeCustomNBT(ItemStack item, String key) {
net.minecraft.server.v1_6_R1.ItemStack cis = CraftItemStack.asNMSCopy(item);
NBTTagCompound tag;
if (!cis.hasTag())
cis.setTag(new NBTTagCompound());
tag = cis.getTag();
// remove 'owner' NBTData
tag.remove(key);
return CraftItemStack.asCraftMirror(cis);
}
public static ItemStack addCustomNBT(ItemStack item, String key, String value) {
net.minecraft.server.v1_6_R1.ItemStack cis = CraftItemStack.asNMSCopy(item);
NBTTagCompound tag = null;
// Do stuff with tag
if (!cis.hasTag())
cis.setTag(new NBTTagCompound());
tag = cis.getTag();
tag.setString(key, value);
return CraftItemStack.asCraftMirror(cis);
}
public static LivingEntity addCustomNBT(LivingEntity entity, String key, String value) {
Entity bukkitEntity = entity;
net.minecraft.server.v1_6_R1.Entity nmsEntity = ((CraftEntity) bukkitEntity).getHandle();
NBTTagCompound tag = new NBTTagCompound();
// Writes the entity's NBT data to tag
nmsEntity.c(tag);
// Add custom NBT
tag.setString(key, value);
// Write tag back
((EntityLiving)nmsEntity).a(tag);
return entity;
}
public static LivingEntity removeCustomNBT(LivingEntity entity, String key) {
Entity bukkitEntity = entity;
net.minecraft.server.v1_6_R1.Entity nmsEntity = ((CraftEntity) bukkitEntity).getHandle();
NBTTagCompound tag = new NBTTagCompound();
// Writes the entity's NBT data to tag
nmsEntity.c(tag);
// Remove custom NBT
tag.remove(key);
// Write tag back
((EntityLiving)nmsEntity).a(tag);
return entity;
}
public static boolean hasCustomNBT(LivingEntity entity, String key) {
Entity bukkitEntity = entity;
net.minecraft.server.v1_6_R1.Entity nmsEntity = ((CraftEntity) bukkitEntity).getHandle();
NBTTagCompound tag = new NBTTagCompound();
// Writes the entity's NBT data to tag
nmsEntity.c(tag);
// Check for key
return tag.hasKey(key);
}
public static String getCustomNBT(LivingEntity entity, String key) {
Entity bukkitEntity = entity;
net.minecraft.server.v1_6_R1.Entity nmsEntity = ((CraftEntity) bukkitEntity).getHandle();
NBTTagCompound tag = new NBTTagCompound();
// Writes the entity's NBT data to tag
nmsEntity.c(tag);
// Return contents of the tag
return tag.getString(key);
}
}