11package com .github .pinont .singularitylib .api .items ;
22
3+ import com .github .pinont .singularitylib .api .utils .Console ;
34import com .google .common .collect .Sets ;
45import com .github .pinont .singularitylib .api .enums .AttributeType ;
56import com .github .pinont .singularitylib .api .enums .PersisDataType ;
1819import org .bukkit .persistence .PersistentDataType ;
1920import org .bukkit .plugin .Plugin ;
2021import org .jetbrains .annotations .NotNull ;
22+ import org .jetbrains .annotations .Nullable ;
2123
2224import java .util .*;
2325
@@ -43,6 +45,10 @@ public static Set<ItemInteraction> getInteractions() {
4345 return ITEM_INTERACTIONS ;
4446 }
4547
48+ public ItemCreator clone () {
49+ return new ItemCreator (this .create ());
50+ }
51+
4652 public ItemCreator (Material type ) {
4753 this (new ItemStack (type ));
4854 }
@@ -51,60 +57,49 @@ public ItemCreator(Material type, int amount) {
5157 this (new ItemStack (type , amount ));
5258 }
5359
54- public ItemCreator (@ NotNull Material type , int amount , short damage ) {
55- this (new ItemStack (type , amount , damage , null ));
56- }
57-
58- public ItemCreator (@ NotNull Material type , int amount , short damage , Byte data ) {
59- this (new ItemStack (type , amount , damage , data ));
60- }
61-
6260 public ItemCreator (@ NotNull ItemStack item ) {
6361 this .item = item ;
6462 this .meta = item .getItemMeta ();
6563 this .type = item .getType ();
64+ this .amount = item .getAmount ();
6665 this .name = item .getItemMeta ().getDisplayName ().isEmpty () ? Common .normalizeStringName (item .getType ().name ()) : item .getItemMeta ().getDisplayName ();
6766 data = meta != null ? meta .getPersistentDataContainer () : null ;
6867 }
6968
7069 public String getName () {
71- return name ;
70+ return this . create (). getItemMeta (). getDisplayName () ;
7271 }
7372
7473 public ItemMeta getItemMeta () {
75- return meta ;
74+ return this . create (). getItemMeta () ;
7675 }
7776
7877 public Material getType () {
79- return type ;
78+ return this . create (). getType () ;
8079 }
8180
8281 public int getAmount () {
83- return amount ;
82+ return this . create (). getAmount () ;
8483 }
8584
8685 public ItemStack create () {
87- item .setType (type );
88- if (meta == null ) {meta = item .getItemMeta ();}
89- if (meta != null ) {
90- meta .lore (lore );
86+ this . item .setType (type );
87+ if (this . meta == null ) {this . meta = this . item .getItemMeta ();}
88+ if (this . meta != null ) {
89+ this . meta .lore (this . lore );
9190 }
92- item .setItemMeta (meta );
93- item .setDurability (durability );
94- item .setAmount (amount );
95- return item ;
91+ this . item .setItemMeta (this . meta );
92+ this . item .setDurability (this . durability );
93+ this . item .setAmount (this . amount );
94+ return this . item ;
9695 }
9796
9897 public Boolean hasTag (String tag ) {
99- return Objects .requireNonNull (item .getItemMeta ()).getPersistentDataContainer ().has (new NamespacedKey (plugin , tag ), PersistentDataType .STRING );
100- }
101-
102- public String getTag (String key ) {
103- return getKey (key );
98+ return data .has (new NamespacedKey (plugin , tag ), PersistentDataType .STRING );
10499 }
105100
106101 public String getKey (String key ) {
107- return data . get ( new NamespacedKey ( plugin , key ), PersistentDataType . STRING );
102+ return getTagValue ( key );
108103 }
109104
110105 public ItemCreator addLore (String lore ) {
@@ -156,13 +151,12 @@ public ItemCreator setAmount(int amount) {
156151 return this ;
157152 }
158153
159- public ItemCreator setName (String name ) {
160- meta .displayName (common .colorize (name ));
161- meta .itemName (common .colorize (name ));
162- return this ;
154+ public ItemCreator setName (@ Nullable String name ) {
155+ return this .setName (common .colorize (name != null ? name : "" ));
163156 }
164157
165- public ItemCreator setName (Component name ) {
158+ public ItemCreator setName (@ Nullable Component name ) {
159+ if (name == null ) name = common .colorize ("" );
166160 meta .displayName (name );
167161 meta .itemName (name );
168162 return this ;
@@ -208,23 +202,86 @@ public ItemCreator setModelData(int data) {
208202 return this ;
209203 }
210204
211- public ItemCreator setTag (String tag ) {
212- data .set (new NamespacedKey (plugin , tag ), PersistentDataType .STRING , tag );
205+ private void sendTagNamingPatternWarn (String tag ) {
206+ Console .logWarning ("Tag naming pattern warning: '" + tag + "' tag should not contain spaces and should be lowercase. It has been converted to '" + tag .replace (" " , "_" ).toLowerCase () + "'" );
207+ }
208+
209+ private String formatTag (String tag ) {
210+ String tagFormatted = String .join (" " , tag .replace (" " , "_" ));
211+ if (!tagFormatted .equals (tag )) {
212+ sendTagNamingPatternWarn (tag );
213+ tag = tagFormatted ;
214+ }
215+ return tag ;
216+ }
217+
218+ public ItemCreator addTags (String ... tags ) {
219+ for (String tag : tags ) {
220+ String formatTag = formatTag (tag );
221+ data .set (new NamespacedKey (plugin , formatTag ), PersistentDataType .STRING , tag );
222+ }
213223 return this ;
214224 }
215225
216- public ItemCreator setTag (String key , String value ) {
226+ public ItemCreator addTag (String key ) {
227+ String formatTag = formatTag (key );
228+ addTag (formatTag , key );
229+ return this ;
230+ }
231+
232+ public ItemCreator addTag (String key , String value ) {
233+ key = formatTag (key );
217234 data .set (new NamespacedKey (plugin , key ), PersistentDataType .STRING , value );
218235 return this ;
219236 }
220237
221- public ItemCreator setDurability (int durability ) {
222- this .durability = durability < 0 ? 0 : (short ) durability ;
238+ public ItemCreator replaceTag (String oldKey , String newKey ) {
239+ oldKey = formatTag (oldKey );
240+ newKey = formatTag (newKey );
241+ if (hasTag (oldKey )) {
242+ if (oldKey .equals (getTagValue (oldKey ))) {
243+ removeTag (oldKey );
244+ data .set (new NamespacedKey (plugin , newKey ), PersistentDataType .STRING , newKey );
245+ return this ;
246+ }
247+ String value = getTagValue (oldKey );
248+ removeTag (oldKey );
249+ data .set (new NamespacedKey (plugin , newKey ), PersistentDataType .STRING , value );
250+ } else {
251+ sendConsoleMessage (ChatColor .RED + "Item tag not found: " + oldKey );
252+ }
253+ return this ;
254+ }
255+
256+ public String getTagValue (String key ) {
257+ key = formatTag (key );
258+ return data .get (new NamespacedKey (plugin , key ), PersistentDataType .STRING );
259+ }
260+
261+ public ItemCreator setTagValue (String key , String newValue ) {
262+ key = formatTag (key );
263+ if (hasTag (key )) {
264+ removeTag (key );
265+ data .set (new NamespacedKey (plugin , key ), PersistentDataType .STRING , newValue );
266+ } else {
267+ sendConsoleMessage (ChatColor .RED + "Item tag not found: " + key );
268+ }
269+ return this ;
270+ }
271+
272+ public ItemCreator removeTag (String tag ) {
273+ tag = formatTag (tag );
274+ if (hasTag (tag )) {
275+ data .remove (new NamespacedKey (plugin , tag ));
276+ } else {
277+ sendConsoleMessage (ChatColor .RED + "Item tag not found: " + tag );
278+ }
223279 return this ;
224280 }
225281
226- public ItemMeta getMeta () {
227- return Objects .requireNonNull (item ).getItemMeta ();
282+ public ItemCreator setDurability (int durability ) {
283+ this .durability = durability < 0 ? 0 : (short ) durability ;
284+ return this ;
228285 }
229286
230287 public static Object getItemPersistData (ItemStack item , String key , PersistentDataType type ) {
0 commit comments