11package io .github .chafficui .CrucialAPI .Utils .customItems ;
22
3+ import com .google .common .collect .Multimap ;
34import io .github .chafficui .CrucialAPI .exceptions .CrucialException ;
45import org .bukkit .Bukkit ;
56import org .bukkit .ChatColor ;
67import org .bukkit .Material ;
78import org .bukkit .NamespacedKey ;
9+ import org .bukkit .attribute .Attribute ;
10+ import org .bukkit .attribute .AttributeModifier ;
811import org .bukkit .inventory .ItemStack ;
912
1013import java .util .*;
@@ -18,19 +21,32 @@ public class CrucialItem {
1821 */
1922 @ Deprecated
2023 public static String getKey (ItemStack stack ){
21- if (stack != null && stack .getItemMeta () != null && stack .getItemMeta ().getLore () != null ){
22- return ChatColor .stripColor (stack .getItemMeta ().getLore ().get (stack .getItemMeta ().getLore ().size ()-1 ));
23- }
24- return null ;
24+ return getId (stack ).toString ();
2525 }
2626
27- public static UUID getId (ItemStack stack ){
28- if (stack != null && stack .getItemMeta () != null && stack .getItemMeta ().getLore () != null ){
29- String key = ChatColor .stripColor (stack .getItemMeta ().getLore ().get (stack .getItemMeta ().getLore ().size ()-1 ));
30- if (key .length () < 36 ){
27+ public static UUID getId (ItemStack stack ) {
28+
29+ if (stack != null && stack .getItemMeta () != null ) {
30+ String key = "" ;
31+ Multimap <Attribute , AttributeModifier > attributeModifiers = stack .getItemMeta ().getAttributeModifiers ();
32+ if (attributeModifiers != null ) {
33+ Collection <AttributeModifier > healthModifiers = attributeModifiers .get (Attribute .GENERIC_MAX_HEALTH );
34+ if (healthModifiers != null ) {
35+ for (AttributeModifier modifier : healthModifiers ) {
36+ if (modifier .getName ().equals ("CRUCIALITEM_ID" )) {
37+ return modifier .getUniqueId ();
38+ }
39+ }
40+ }
41+ }
42+ //TODO: remove in next major update
43+ if (stack .getItemMeta ().getLore () != null ) {
44+ key = ChatColor .stripColor (stack .getItemMeta ().getLore ().get (stack .getItemMeta ().getLore ().size () - 1 ));
45+ }
46+ if (key .length () < 36 ) {
3147 return null ;
3248 }
33- return UUID .fromString (key .substring (0 ,36 ));
49+ return UUID .fromString (key .substring (0 , 36 ));
3450 }
3551 return null ;
3652 }
@@ -57,13 +73,7 @@ public static CrucialItem getById(UUID id){
5773 */
5874 @ Deprecated
5975 public static CrucialItem getByKey (String key ){
60- for (CrucialItem crucialItem :
61- CRUCIAL_ITEMS ) {
62- if (crucialItem .getId ().equals (UUID .fromString (key .substring (0 ,36 )))){
63- return crucialItem ;
64- }
65- }
66- return null ;
76+ return getById (UUID .fromString (key .substring (0 ,36 )));
6777 }
6878
6979 //Custom Item
@@ -161,14 +171,15 @@ public void reload() throws CrucialException {
161171 }
162172
163173 private void registerRecipe () throws CrucialException {
174+ AttributeModifier modifier = new AttributeModifier (this .id , "CRUCIALITEM_ID" , 0 , AttributeModifier .Operation .ADD_NUMBER );
164175 if (isHead ){
165176 if (headOwner != null ) {
166- namespacedKey = Item .createHead (id +type , name , getUniqueLore ( ), headOwner , recipe );
177+ namespacedKey = Item .createItem (id +type , name , Stack . addAttributeModifier ( Stack . getStack ( headOwner , name , lore ), Attribute . GENERIC_MAX_HEALTH , modifier ) , recipe );
167178 } else {
168- namespacedKey = Item .createHead (id +type , name , getUniqueLore ( ), material , recipe );
179+ namespacedKey = Item .createItem (id +type , name , Stack . addAttributeModifier ( Stack . getStack ( material , name , lore ), Attribute . GENERIC_MAX_HEALTH , modifier ) , recipe );
169180 }
170181 } else {
171- namespacedKey = Item .createItem (id +type , name , getUniqueLore (), Material .getMaterial (material ), recipe );
182+ namespacedKey = Item .createItem (id +type , name , Stack . addAttributeModifier ( Stack . getStack ( Material .getMaterial (material ), name , lore ), Attribute . GENERIC_MAX_HEALTH , modifier ), recipe );
172183 }
173184 }
174185
@@ -182,14 +193,15 @@ public void delete(){
182193
183194 public ItemStack getItemStack (){
184195 if (isRegistered ){
196+ AttributeModifier modifier = new AttributeModifier (this .id , "CRUCIALITEM_ID" , 0 , AttributeModifier .Operation .ADD_NUMBER );
185197 if (isHead ){
186198 if (headOwner != null ) {
187- return Stack .getStack (headOwner , name , getUniqueLore () );
199+ return Stack .addAttributeModifier ( Stack . getStack (headOwner , name , lore ), Attribute . GENERIC_MAX_HEALTH , modifier );
188200 } else {
189- return Stack .getStack (material , name , getUniqueLore () );
201+ return Stack .addAttributeModifier ( Stack . getStack (material , name , lore ), Attribute . GENERIC_MAX_HEALTH , modifier );
190202 }
191203 } else {
192- return Stack .getStack (Material .getMaterial (material ), name , getUniqueLore () );
204+ return Stack .addAttributeModifier ( Stack . getStack (Material .getMaterial (material ), name , lore ), Attribute . GENERIC_MAX_HEALTH , modifier );
193205 }
194206 }
195207 return null ;
@@ -203,6 +215,10 @@ public String getLore(){
203215 return String .valueOf (lore );
204216 }
205217
218+ /**
219+ * @deprecated
220+ */
221+ @ Deprecated
206222 public List <String > getUniqueLore (){
207223 List <String > uniqueLore = new ArrayList <>(lore );
208224 uniqueLore .add ("" );
0 commit comments