Skip to content

Commit d28024d

Browse files
committed
deprecated some methods in ItemNBTHelper
1 parent 65d9b00 commit d28024d

File tree

1 file changed

+31
-26
lines changed

1 file changed

+31
-26
lines changed

src/main/java/org/violetmoon/zeta/util/ItemNBTHelper.java

+31-26
Original file line numberDiff line numberDiff line change
@@ -14,129 +14,134 @@
1414
import net.minecraft.nbt.ListTag;
1515
import net.minecraft.world.item.ItemStack;
1616

17+
// TBH this entire class could be Deprecated and removed
1718
public final class ItemNBTHelper {
1819

1920
/** Checks if an ItemStack has a Tag Compound **/
21+
@Deprecated(forRemoval = true) // Just use stack.hasTag()
2022
public static boolean detectNBT(ItemStack stack) {
2123
return stack.hasTag();
2224
}
2325

2426
/** Tries to initialize an NBT Tag Compound in an ItemStack,
2527
* this will not do anything if the stack already has a tag
2628
* compound **/
29+
@Deprecated(forRemoval = true) // Just use stack.getOrCreateTag()
2730
public static void initNBT(ItemStack stack) {
28-
if(!detectNBT(stack))
29-
injectNBT(stack, new CompoundTag());
31+
stack.getOrCreateTag();
3032
}
3133

3234
/** Injects an NBT Tag Compound to an ItemStack, no checks
3335
* are made previously **/
36+
@Deprecated(forRemoval = true) // Just use stack.setTag(nbt)
3437
public static void injectNBT(ItemStack stack, CompoundTag nbt) {
3538
stack.setTag(nbt);
3639
}
3740

3841
/** Gets the CompoundNBT in an ItemStack. Tries to init it
3942
* previously in case there isn't one present **/
43+
@Deprecated(forRemoval = true) // Just use stack.getOrCreateTag()
4044
public static CompoundTag getNBT(ItemStack stack) {
41-
initNBT(stack);
42-
return stack.getTag();
45+
return stack.getOrCreateTag();
4346
}
4447

4548
// SETTERS ///////////////////////////////////////////////////////////////////
4649

50+
// All these force create a tag if it doesn't exist
51+
4752
public static void setBoolean(ItemStack stack, String tag, boolean b) {
48-
getNBT(stack).putBoolean(tag, b);
53+
stack.getOrCreateTag().putBoolean(tag, b);
4954
}
5055

5156
public static void setByte(ItemStack stack, String tag, byte b) {
52-
getNBT(stack).putByte(tag, b);
57+
stack.getOrCreateTag().putByte(tag, b);
5358
}
5459

5560
public static void setShort(ItemStack stack, String tag, short s) {
56-
getNBT(stack).putShort(tag, s);
61+
stack.getOrCreateTag().putShort(tag, s);
5762
}
5863

5964
public static void setInt(ItemStack stack, String tag, int i) {
60-
getNBT(stack).putInt(tag, i);
65+
stack.getOrCreateTag().putInt(tag, i);
6166
}
6267

6368
public static void setLong(ItemStack stack, String tag, long l) {
64-
getNBT(stack).putLong(tag, l);
69+
stack.getOrCreateTag().putLong(tag, l);
6570
}
6671

6772
public static void setFloat(ItemStack stack, String tag, float f) {
68-
getNBT(stack).putFloat(tag, f);
73+
stack.getOrCreateTag().putFloat(tag, f);
6974
}
7075

7176
public static void setDouble(ItemStack stack, String tag, double d) {
72-
getNBT(stack).putDouble(tag, d);
77+
stack.getOrCreateTag().putDouble(tag, d);
7378
}
7479

7580
public static void setCompound(ItemStack stack, String tag, CompoundTag cmp) {
7681
if(!tag.equalsIgnoreCase("ench")) // not override the enchantments
77-
getNBT(stack).put(tag, cmp);
82+
stack.getOrCreateTag().put(tag, cmp);
7883
}
7984

8085
public static void setString(ItemStack stack, String tag, String s) {
81-
getNBT(stack).putString(tag, s);
86+
stack.getOrCreateTag().putString(tag, s);
8287
}
8388

8489
public static void setList(ItemStack stack, String tag, ListTag list) {
85-
getNBT(stack).put(tag, list);
90+
stack.getOrCreateTag().put(tag, list);
8691
}
8792

8893
// GETTERS ///////////////////////////////////////////////////////////////////
8994

9095

9196
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);
9398
}
9499

95-
@Deprecated
100+
@Deprecated(forRemoval = true)
96101
public static boolean verifyExistance(ItemStack stack, String tag) {
97102
return verifyExistence(stack, tag);
98103
}
99104

100105
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;
102107
}
103108

104109
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;
106111
}
107112

108113
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;
110115
}
111116

112117
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;
114119
}
115120

116121
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;
118123
}
119124

120125
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;
122127
}
123128

124129
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;
126131
}
127132

128133
/** If nullifyOnFail is true it'll return null if it doesn't find any
129134
* compounds, otherwise it'll return a new one. **/
130135
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();
132137
}
133138

134139
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;
136141
}
137142

138143
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();
140145
}
141146

142147
}

0 commit comments

Comments
 (0)