Skip to content

Commit fae256a

Browse files
committed
Added the nbt options to the wrapper classes.
1 parent 6f066cc commit fae256a

File tree

3 files changed

+154
-13
lines changed

3 files changed

+154
-13
lines changed

Item Creator/src/main/java/org/broken/arrow/library/itemcreator/utility/UnbreakableUtil.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,7 @@ public ItemStack applyUnbreakableTag(@Nonnull final ItemStack item, @Nonnull fin
164164

165165
CompoundTag compound = nms.getOrCreateCompound();
166166
compound.setBoolean(key, unbreakable);
167-
return nms.apply(compound);
167+
return nms.apply();
168168
}
169169

170170
/**

Item Creator/src/main/java/org/broken/arrow/library/itemcreator/utility/compound/CompoundTag.java

Lines changed: 94 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -51,14 +51,84 @@ public boolean hasKey(@Nonnull String key) {
5151
return this.compoundSession.hasKey(key);
5252
}
5353

54+
55+
/**
56+
* Remove this {@link CompoundTag} value and the given key.
57+
*
58+
* @param key the NBT key to remove.
59+
*/
60+
public void remove(@Nonnull final String key) {
61+
this.compoundSession.remove(key);
62+
}
63+
64+
/**
65+
* Sets a int value in the underlying NBTTagCompound.
66+
*
67+
* @param key the key to set
68+
* @param value the int value to assign
69+
*/
70+
public void setInt(@Nonnull final String key, final int value) {
71+
this.compoundSession.setInt(key, value);
72+
}
73+
74+
/**
75+
* Gets a int value from the underlying NBTTagCompound.
76+
*
77+
* @param key the key of the int value
78+
* @return the stored int value, or {@code -1} if unavailable
79+
*/
80+
public int getInt(@Nonnull final String key) {
81+
return this.compoundSession.getInt(key);
82+
}
83+
84+
/**
85+
* Sets a String value in the underlying NBTTagCompound.
86+
*
87+
* @param key the key to set
88+
* @param value the String value to assign
89+
*/
90+
public void setString(@Nonnull final String key, final String value) {
91+
this.compoundSession.setString(key, value);
92+
}
93+
94+
/**
95+
* Gets a string value from the underlying NBTTagCompound.
96+
*
97+
* @param key the key of the string value
98+
* @return the stored string value, or empty string if unavailable
99+
*/
100+
public String getString(@Nonnull final String key) {
101+
return this.compoundSession.getString(key);
102+
}
103+
104+
/**
105+
* Sets a byte value in the underlying NBTTagCompound.
106+
*
107+
* @param key the key to set
108+
* @param value the byte value to assign
109+
*/
110+
public void setByte(@Nonnull final String key, final byte value) {
111+
this.compoundSession.setByte(key, value);
112+
}
113+
114+
/**
115+
* Gets a byte value from the underlying NBTTagCompound.
116+
*
117+
* @param key the key of the byte value
118+
* @return the stored byte value, or {@code -1} if unavailable
119+
*/
120+
public byte getByte(@Nonnull final String key) {
121+
return this.compoundSession.getByte(key);
122+
}
123+
54124
/**
55125
* Sets a boolean value in the underlying NBTTagCompound.
56126
*
57127
* @param key the key to set
58128
* @param value the boolean value to assign
59129
*/
60-
public void setBoolean(@Nonnull String key, boolean value) {
61-
this.compoundSession.setBoolean(key,value);
130+
public void setBoolean(@Nonnull final String key, final boolean value) {
131+
this.compoundSession.setBoolean(key, value);
62132
}
63133

64134
/**
@@ -67,7 +137,28 @@ public void setBoolean(@Nonnull String key, boolean value) {
67137
* @param key the key of the boolean value
68138
* @return the stored boolean value, or {@code false} if unavailable
69139
*/
70-
public boolean getBoolean(@Nonnull String key) {
140+
public boolean getBoolean(@Nonnull final String key) {
71141
return this.compoundSession.getBoolean(key);
72142
}
143+
144+
/**
145+
* Sets a short value in the underlying NBTTagCompound.
146+
*
147+
* @param key the key to set
148+
* @param value the short value to assign
149+
*/
150+
public void setShort(@Nonnull final String key, final short value) {
151+
this.compoundSession.setShort(key, value);
152+
}
153+
154+
/**
155+
* Gets a short value from the underlying NBTTagCompound.
156+
*
157+
* @param key the key of the short value
158+
* @return the stored short value, or {@code -1} if unavailable
159+
*/
160+
public short getShort(@Nonnull final String key) {
161+
return this.compoundSession.getShort(key);
162+
}
163+
73164
}

Item Creator/src/main/java/org/broken/arrow/library/itemcreator/utility/compound/NbtData.java

Lines changed: 59 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -50,37 +50,87 @@ public boolean hasTag() {
5050
}
5151

5252
/**
53-
* Returns the existing {@link CompoundTag} if one is present,
54-
* otherwise creates a new one.
53+
* Checks whether this item contains an {@code NBTTagCompound} with the given name.
54+
* <p>
55+
* If the name is empty, the <strong>root compound</strong> is evaluated instead.
56+
* Both root and nested compounds are considered valid targets.
5557
*
56-
* @return the existing {@link CompoundTag} or a new instance if none exists.
57-
* Returns {@code null} only if reflection failed or the underlying
58-
* NBTTagCompound could not be created.
58+
* @param name the custom key of the nested compound. To target the root compound,
59+
* use an empty string or {@link #hasTag()}.
60+
* @return {@code true} if the specified (or root) compound exists
61+
*/
62+
public boolean hasTag(@Nonnull final String name) {
63+
return this.session.hasTag(name);
64+
}
65+
66+
/**
67+
* Returns the root {@link CompoundTag} of this item, creating one if it does not exist.
68+
* <p>
69+
* This method always operates on the root compound. If you want a nested compound,
70+
* use {@link #getOrCreateCompound(String)} with a specific name.
71+
*
72+
* @return the root {@link CompoundTag}, never {@code null} unless reflection failed.
5973
*/
6074
@Nullable
6175
public CompoundTag getOrCreateCompound() {
6276
return this.session.getOrCreateCompound();
6377
}
6478

6579
/**
66-
* Returns the existing {@link CompoundTag} if one is present.
80+
* Returns a {@link CompoundTag} with the given name, creating it if it does not exist.
81+
* <p>
82+
* If {@code name} is empty (""), this will return the root compound, which is equivalent
83+
* to {@link #getOrCreateCompound()}.
84+
* <p>
85+
* Use a non-empty name if you want a nested compound separate from the root.
6786
*
68-
* @return the existing {@link CompoundTag} or {@code null} if none exists
87+
* @param name the name of the nested compound, or empty string for root.
88+
* @return the existing or newly created {@link CompoundTag}, or {@code null} if reflection failed.
89+
*/
90+
@Nullable
91+
public CompoundTag getOrCreateCompound(@Nonnull final String name) {
92+
return this.session.getOrCreateCompound(name);
93+
}
94+
95+
96+
/**
97+
* Returns the root {@link CompoundTag} if present.
98+
* <p>
99+
* This method does not create a new compound. Use {@link #getOrCreateCompound()} to
100+
* create a root compound if it does not exist.
101+
*
102+
* @return the root {@link CompoundTag} if present, otherwise {@code null}.
69103
*/
70104
@Nullable
71105
public CompoundTag getCompound() {
106+
return this.session.getOrCreateCompound();
107+
}
108+
109+
/**
110+
* Returns the {@link CompoundTag} with the given name if present.
111+
* <p>
112+
* If {@code name} is empty (""), this returns the root compound.
113+
* For a nested compound, pass a non-empty name.
114+
* <p>
115+
* This method does not create a compound; use {@link #getOrCreateCompound(String)}
116+
* to create one if it does not exist.
117+
*
118+
* @param name the name of the nested compound, or empty string for root.
119+
* @return the existing {@link CompoundTag} if present, otherwise {@code null}.
120+
*/
121+
@Nullable
122+
public CompoundTag getCompound(@Nonnull final String name) {
72123
return this.session.getCompound();
73124
}
74125

75126
/**
76127
* Applies the current CompoundTag to the ItemStack and returns
77128
* a new Bukkit ItemStack instance.
78129
*
79-
* @param tag the {@link CompoundTag} instance that wraps NBTTagCompound.
80130
* @return Returns the copy of your itemStack with the nbt set.
81131
*/
82132
@Nullable
83-
public ItemStack apply(@Nonnull final CompoundTag tag) {
133+
public ItemStack apply() {
84134
return this.session.finalizeChanges();
85135
}
86136

0 commit comments

Comments
 (0)