1+ package com .wizardlybump17 .wlib .adapter .v1_21_r7 ;
2+
3+ import com .wizardlybump17 .wlib .util .ReflectionUtil ;
4+ import net .minecraft .nbt .*;
5+ import org .bukkit .craftbukkit .persistence .CraftPersistentDataContainer ;
6+ import org .bukkit .inventory .meta .Damageable ;
7+ import org .bukkit .inventory .meta .ItemMeta ;
8+ import org .bukkit .persistence .PersistentDataContainer ;
9+ import org .jetbrains .annotations .NotNull ;
10+ import org .jetbrains .annotations .Nullable ;
11+
12+ import java .lang .reflect .Field ;
13+ import java .util .*;
14+
15+ public class ItemAdapter extends com .wizardlybump17 .wlib .adapter .ItemAdapter {
16+
17+ public static final @ NotNull Class <?> CRAFT_META_ITEM = ReflectionUtil .getClass ("org.bukkit.craftbukkit.inventory.CraftMetaItem" );
18+ public static final @ NotNull Field CUSTOM_TAG = ReflectionUtil .getField ("customTag" , CRAFT_META_ITEM );
19+
20+ @ Override
21+ public void transferPersistentData (@ NotNull PersistentDataContainer from , @ NotNull PersistentDataContainer to ) {
22+ Map <String , Tag > tags = ((CraftPersistentDataContainer ) to ).getRaw ();
23+ tags .clear ();
24+ tags .putAll (((CraftPersistentDataContainer ) from ).getTagsCloned ());
25+ }
26+
27+ @ Override
28+ public void copyPersistentData (@ NotNull PersistentDataContainer from , @ NotNull PersistentDataContainer to ) {
29+ ((CraftPersistentDataContainer ) to ).getRaw ().putAll (((CraftPersistentDataContainer ) from ).getTagsCloned ());
30+ }
31+
32+ @ Override
33+ public void setDamage (@ NotNull ItemMeta meta , @ Nullable Integer damage ) {
34+ if (!(meta instanceof Damageable damageable ))
35+ return ;
36+
37+ if (damage == null )
38+ damageable .resetDamage ();
39+ else
40+ damageable .setDamage (damage );
41+ }
42+
43+ @ Override
44+ public @ Nullable Integer getDamage (@ NotNull ItemMeta meta ) {
45+ return meta instanceof Damageable damageable && damageable .hasDamage () ? damageable .getDamage () : null ;
46+ }
47+
48+ @ Override
49+ public @ NotNull Map <String , Object > getCustomData (@ NotNull ItemMeta meta ) {
50+ Map <String , Object > result = new HashMap <>();
51+
52+ CompoundTag tag = ReflectionUtil .getFieldValue (CUSTOM_TAG , meta );
53+ if (tag == null )
54+ return result ;
55+
56+ for (String key : tag .keySet ()) {
57+ Tag data = tag .get (key );
58+ if (data != null )
59+ result .put (key , fromNBT (data ));
60+ }
61+
62+ return result ;
63+ }
64+
65+ @ Override
66+ public void setCustomData (@ NotNull ItemMeta meta , @ NotNull Map <String , Object > customData ) {
67+ CompoundTag tag = new CompoundTag ();
68+ for (Map .Entry <String , Object > entry : customData .entrySet ()) {
69+ String key = entry .getKey ();
70+ Object value = entry .getValue ();
71+ tag .put (key , toNBT (value ));
72+ }
73+
74+ ReflectionUtil .setFieldValue (CUSTOM_TAG , meta , tag );
75+ }
76+
77+ @ Override
78+ protected @ NotNull Tag toNBT (@ NotNull Object java ) {
79+ return switch (java ) {
80+ case Byte b -> ByteTag .valueOf (b );
81+ case Short s -> ShortTag .valueOf (s );
82+ case Integer i -> IntTag .valueOf (i );
83+ case Long l -> LongTag .valueOf (l );
84+ case Float f -> FloatTag .valueOf (f );
85+ case Double d -> DoubleTag .valueOf (d );
86+
87+ case Boolean b -> ByteTag .valueOf (b );
88+
89+ case String string -> StringTag .valueOf (string );
90+
91+ case byte [] byteArray -> new ByteArrayTag (byteArray );
92+ case Byte [] byteArray -> {
93+ byte [] array = new byte [byteArray .length ];
94+ for (int i = 0 ; i < byteArray .length ; i ++)
95+ array [i ] = byteArray [i ];
96+ yield new ByteArrayTag (array );
97+ }
98+ case int [] intArray -> new IntArrayTag (intArray );
99+ case Integer [] intArray -> {
100+ int [] array = new int [intArray .length ];
101+ for (int i = 0 ; i < intArray .length ; i ++)
102+ array [i ] = intArray [i ];
103+ yield new IntArrayTag (array );
104+ }
105+ case long [] longArray -> new LongArrayTag (longArray );
106+ case Long [] longArray -> {
107+ long [] array = new long [longArray .length ];
108+ for (int i = 0 ; i < longArray .length ; i ++)
109+ array [i ] = longArray [i ];
110+ yield new LongArrayTag (array );
111+ }
112+
113+ case Collection <?> collection -> {
114+ List <Tag > values = new ArrayList <>(collection .size ());
115+ for (Object object : collection ) {
116+ Tag tag = toNBT (object );
117+ values .add (tag );
118+ }
119+ yield new ListTag (values );
120+ }
121+
122+ case Map <?, ?> map -> {
123+ Map <String , Tag > values = new HashMap <>(map .size ());
124+ map .forEach ((key , value ) -> values .put (String .valueOf (key ), toNBT (value )));
125+
126+ CompoundTag tag = new CompoundTag ();
127+ values .forEach (tag ::put );
128+ yield tag ;
129+ }
130+
131+ default -> throw new UnsupportedOperationException ("Unsupported Java type: " + java );
132+ };
133+ }
134+
135+ @ Override
136+ protected @ NotNull Object fromNBT (@ NotNull Object nbt ) {
137+ return switch (nbt ) {
138+ case ByteTag tag -> tag .byteValue ();
139+ case ShortTag tag -> tag .shortValue ();
140+ case IntTag tag -> tag .intValue ();
141+ case LongTag tag -> tag .longValue ();
142+ case FloatTag tag -> tag .floatValue ();
143+ case DoubleTag tag -> tag .doubleValue ();
144+
145+ case StringTag tag -> tag .value ();
146+
147+ case ByteArrayTag tag -> tag .getAsByteArray ();
148+ case IntArrayTag tag -> tag .getAsIntArray ();
149+ case LongArrayTag tag -> tag .getAsLongArray ();
150+
151+ case CollectionTag tag -> {
152+ List <Object > list = new ArrayList <>(tag .size ());
153+ for (Tag value : tag )
154+ list .add (fromNBT (value ));
155+ yield list ;
156+ }
157+
158+ case CompoundTag tag -> {
159+ Map <String , Object > map = new HashMap <>(tag .size ());
160+ for (String key : tag .keySet ()) {
161+ Tag value = tag .get (key );
162+ if (value != null )
163+ map .put (key , fromNBT (value ));
164+ }
165+ yield map ;
166+ }
167+
168+ default -> throw new UnsupportedOperationException ("Unsupported NBT type: " + nbt );
169+ };
170+ }
171+ }
0 commit comments