4
4
import kr .toxicity .libraries .datacomponent .api .*;
5
5
import org .bukkit .Bukkit ;
6
6
import org .jetbrains .annotations .NotNull ;
7
+ import org .jetbrains .annotations .Nullable ;
7
8
8
- import java .lang .reflect .Type ;
9
- import java .util .ArrayList ;
10
- import java .util .Collections ;
11
- import java .util .List ;
12
- import java .util .Map ;
13
- import java .util .function .Consumer ;
9
+ import java .util .*;
14
10
15
11
@ SuppressWarnings ("unused" )
16
12
public final class DataComponentAPIImpl extends DataComponentAPI {
@@ -34,23 +30,28 @@ private DataComponentAPIImpl(@NotNull MinecraftVersion current){
34
30
throw new UnsupportedOperationException ("Unsupported minecraft version: " + current );
35
31
}
36
32
deserializer = e -> {
37
- List < Consumer < ItemAdapter > > consumer = Collections .synchronizedList (new ArrayList <>());
33
+ Map < DataComponentType <?>, JsonElement > consumer = Collections .synchronizedMap (new HashMap <>());
38
34
for (Map .Entry <String , JsonElement > entry : e .entrySet ()) {
39
35
var type = nms .componentRegistry ().get (entry .getKey ());
40
36
if (type != null ) {
41
- consumer .add ( a -> type . setToJson ( a , entry .getValue () ));
37
+ consumer .put ( type , entry .getValue ());
42
38
}
43
39
}
44
40
return new DataComponent () {
45
41
@ Override
46
42
public void set (@ NotNull ItemAdapter adapter ) {
47
- consumer .forEach (e -> e . accept ( adapter ) );
43
+ consumer .forEach (adapter :: setToJson );
48
44
}
49
45
50
46
@ Override
51
- public @ NotNull JsonObject get () {
47
+ public @ NotNull JsonObject getToJson () {
52
48
return e ;
53
49
}
50
+
51
+ @ Override
52
+ public @ Nullable JsonElement getToJson (@ NotNull DataComponentType <?> type ) {
53
+ return consumer .get (type );
54
+ }
54
55
};
55
56
};
56
57
}
@@ -60,18 +61,6 @@ public void set(@NotNull ItemAdapter adapter) {
60
61
return deserializer ;
61
62
}
62
63
63
- private record RegistryTypeAdapter <T >(DataComponentType <T > type ) implements JsonSerializer <T >, JsonDeserializer <T > {
64
- @ Override
65
- public JsonElement serialize (T src , Type typeOfSrc , JsonSerializationContext context ) {
66
- return type .codec ().encode (src );
67
- }
68
-
69
- @ Override
70
- public T deserialize (JsonElement json , Type typeOfT , JsonDeserializationContext context ) throws JsonParseException {
71
- return type .codec ().decode (json );
72
- }
73
- }
74
-
75
64
static void load () {
76
65
DataComponentAPI .api (new DataComponentAPIImpl ());
77
66
}
0 commit comments