|
14 | 14 | import net.minecraft.nbt.ListTag;
|
15 | 15 | import net.minecraft.world.item.ItemStack;
|
16 | 16 |
|
| 17 | +// TBH this entire class could be Deprecated and removed |
17 | 18 | public final class ItemNBTHelper {
|
18 | 19 |
|
19 | 20 | /** Checks if an ItemStack has a Tag Compound **/
|
| 21 | + @Deprecated(forRemoval = true) // Just use stack.hasTag() |
20 | 22 | public static boolean detectNBT(ItemStack stack) {
|
21 | 23 | return stack.hasTag();
|
22 | 24 | }
|
23 | 25 |
|
24 | 26 | /** Tries to initialize an NBT Tag Compound in an ItemStack,
|
25 | 27 | * this will not do anything if the stack already has a tag
|
26 | 28 | * compound **/
|
| 29 | + @Deprecated(forRemoval = true) // Just use stack.getOrCreateTag() |
27 | 30 | public static void initNBT(ItemStack stack) {
|
28 |
| - if(!detectNBT(stack)) |
29 |
| - injectNBT(stack, new CompoundTag()); |
| 31 | + stack.getOrCreateTag(); |
30 | 32 | }
|
31 | 33 |
|
32 | 34 | /** Injects an NBT Tag Compound to an ItemStack, no checks
|
33 | 35 | * are made previously **/
|
| 36 | + @Deprecated(forRemoval = true) // Just use stack.setTag(nbt) |
34 | 37 | public static void injectNBT(ItemStack stack, CompoundTag nbt) {
|
35 | 38 | stack.setTag(nbt);
|
36 | 39 | }
|
37 | 40 |
|
38 | 41 | /** Gets the CompoundNBT in an ItemStack. Tries to init it
|
39 | 42 | * previously in case there isn't one present **/
|
| 43 | + @Deprecated(forRemoval = true) // Just use stack.getOrCreateTag() |
40 | 44 | public static CompoundTag getNBT(ItemStack stack) {
|
41 |
| - initNBT(stack); |
42 |
| - return stack.getTag(); |
| 45 | + return stack.getOrCreateTag(); |
43 | 46 | }
|
44 | 47 |
|
45 | 48 | // SETTERS ///////////////////////////////////////////////////////////////////
|
46 | 49 |
|
| 50 | + // All these force create a tag if it doesn't exist |
| 51 | + |
47 | 52 | public static void setBoolean(ItemStack stack, String tag, boolean b) {
|
48 |
| - getNBT(stack).putBoolean(tag, b); |
| 53 | + stack.getOrCreateTag().putBoolean(tag, b); |
49 | 54 | }
|
50 | 55 |
|
51 | 56 | public static void setByte(ItemStack stack, String tag, byte b) {
|
52 |
| - getNBT(stack).putByte(tag, b); |
| 57 | + stack.getOrCreateTag().putByte(tag, b); |
53 | 58 | }
|
54 | 59 |
|
55 | 60 | public static void setShort(ItemStack stack, String tag, short s) {
|
56 |
| - getNBT(stack).putShort(tag, s); |
| 61 | + stack.getOrCreateTag().putShort(tag, s); |
57 | 62 | }
|
58 | 63 |
|
59 | 64 | public static void setInt(ItemStack stack, String tag, int i) {
|
60 |
| - getNBT(stack).putInt(tag, i); |
| 65 | + stack.getOrCreateTag().putInt(tag, i); |
61 | 66 | }
|
62 | 67 |
|
63 | 68 | public static void setLong(ItemStack stack, String tag, long l) {
|
64 |
| - getNBT(stack).putLong(tag, l); |
| 69 | + stack.getOrCreateTag().putLong(tag, l); |
65 | 70 | }
|
66 | 71 |
|
67 | 72 | public static void setFloat(ItemStack stack, String tag, float f) {
|
68 |
| - getNBT(stack).putFloat(tag, f); |
| 73 | + stack.getOrCreateTag().putFloat(tag, f); |
69 | 74 | }
|
70 | 75 |
|
71 | 76 | public static void setDouble(ItemStack stack, String tag, double d) {
|
72 |
| - getNBT(stack).putDouble(tag, d); |
| 77 | + stack.getOrCreateTag().putDouble(tag, d); |
73 | 78 | }
|
74 | 79 |
|
75 | 80 | public static void setCompound(ItemStack stack, String tag, CompoundTag cmp) {
|
76 | 81 | if(!tag.equalsIgnoreCase("ench")) // not override the enchantments
|
77 |
| - getNBT(stack).put(tag, cmp); |
| 82 | + stack.getOrCreateTag().put(tag, cmp); |
78 | 83 | }
|
79 | 84 |
|
80 | 85 | public static void setString(ItemStack stack, String tag, String s) {
|
81 |
| - getNBT(stack).putString(tag, s); |
| 86 | + stack.getOrCreateTag().putString(tag, s); |
82 | 87 | }
|
83 | 88 |
|
84 | 89 | public static void setList(ItemStack stack, String tag, ListTag list) {
|
85 |
| - getNBT(stack).put(tag, list); |
| 90 | + stack.getOrCreateTag().put(tag, list); |
86 | 91 | }
|
87 | 92 |
|
88 | 93 | // GETTERS ///////////////////////////////////////////////////////////////////
|
89 | 94 |
|
90 | 95 |
|
91 | 96 | public static boolean verifyExistence(ItemStack stack, String tag) {
|
92 |
| - return !stack.isEmpty() && detectNBT(stack) && getNBT(stack).contains(tag); |
| 97 | + return !stack.isEmpty() && stack.hasTag() && stack.getOrCreateTag().contains(tag); |
93 | 98 | }
|
94 | 99 |
|
95 |
| - @Deprecated |
| 100 | + @Deprecated(forRemoval = true) |
96 | 101 | public static boolean verifyExistance(ItemStack stack, String tag) {
|
97 | 102 | return verifyExistence(stack, tag);
|
98 | 103 | }
|
99 | 104 |
|
100 | 105 | public static boolean getBoolean(ItemStack stack, String tag, boolean defaultExpected) {
|
101 |
| - return verifyExistence(stack, tag) ? getNBT(stack).getBoolean(tag) : defaultExpected; |
| 106 | + return verifyExistence(stack, tag) ? stack.getOrCreateTag().getBoolean(tag) : defaultExpected; |
102 | 107 | }
|
103 | 108 |
|
104 | 109 | public static byte getByte(ItemStack stack, String tag, byte defaultExpected) {
|
105 |
| - return verifyExistence(stack, tag) ? getNBT(stack).getByte(tag) : defaultExpected; |
| 110 | + return verifyExistence(stack, tag) ? stack.getOrCreateTag().getByte(tag) : defaultExpected; |
106 | 111 | }
|
107 | 112 |
|
108 | 113 | public static short getShort(ItemStack stack, String tag, short defaultExpected) {
|
109 |
| - return verifyExistence(stack, tag) ? getNBT(stack).getShort(tag) : defaultExpected; |
| 114 | + return verifyExistence(stack, tag) ? stack.getOrCreateTag().getShort(tag) : defaultExpected; |
110 | 115 | }
|
111 | 116 |
|
112 | 117 | public static int getInt(ItemStack stack, String tag, int defaultExpected) {
|
113 |
| - return verifyExistence(stack, tag) ? getNBT(stack).getInt(tag) : defaultExpected; |
| 118 | + return verifyExistence(stack, tag) ? stack.getOrCreateTag().getInt(tag) : defaultExpected; |
114 | 119 | }
|
115 | 120 |
|
116 | 121 | public static long getLong(ItemStack stack, String tag, long defaultExpected) {
|
117 |
| - return verifyExistence(stack, tag) ? getNBT(stack).getLong(tag) : defaultExpected; |
| 122 | + return verifyExistence(stack, tag) ? stack.getOrCreateTag().getLong(tag) : defaultExpected; |
118 | 123 | }
|
119 | 124 |
|
120 | 125 | public static float getFloat(ItemStack stack, String tag, float defaultExpected) {
|
121 |
| - return verifyExistence(stack, tag) ? getNBT(stack).getFloat(tag) : defaultExpected; |
| 126 | + return verifyExistence(stack, tag) ? stack.getOrCreateTag().getFloat(tag) : defaultExpected; |
122 | 127 | }
|
123 | 128 |
|
124 | 129 | public static double getDouble(ItemStack stack, String tag, double defaultExpected) {
|
125 |
| - return verifyExistence(stack, tag) ? getNBT(stack).getDouble(tag) : defaultExpected; |
| 130 | + return verifyExistence(stack, tag) ? stack.getOrCreateTag().getDouble(tag) : defaultExpected; |
126 | 131 | }
|
127 | 132 |
|
128 | 133 | /** If nullifyOnFail is true it'll return null if it doesn't find any
|
129 | 134 | * compounds, otherwise it'll return a new one. **/
|
130 | 135 | public static CompoundTag getCompound(ItemStack stack, String tag, boolean nullifyOnFail) {
|
131 |
| - return verifyExistence(stack, tag) ? getNBT(stack).getCompound(tag) : nullifyOnFail ? null : new CompoundTag(); |
| 136 | + return verifyExistence(stack, tag) ? stack.getOrCreateTag().getCompound(tag) : nullifyOnFail ? null : new CompoundTag(); |
132 | 137 | }
|
133 | 138 |
|
134 | 139 | public static String getString(ItemStack stack, String tag, String defaultExpected) {
|
135 |
| - return verifyExistence(stack, tag) ? getNBT(stack).getString(tag) : defaultExpected; |
| 140 | + return verifyExistence(stack, tag) ? stack.getOrCreateTag().getString(tag) : defaultExpected; |
136 | 141 | }
|
137 | 142 |
|
138 | 143 | public static ListTag getList(ItemStack stack, String tag, int objtype, boolean nullifyOnFail) {
|
139 |
| - return verifyExistence(stack, tag) ? getNBT(stack).getList(tag, objtype) : nullifyOnFail ? null : new ListTag(); |
| 144 | + return verifyExistence(stack, tag) ? stack.getOrCreateTag().getList(tag, objtype) : nullifyOnFail ? null : new ListTag(); |
140 | 145 | }
|
141 | 146 |
|
142 | 147 | }
|
0 commit comments