Skip to content

Commit def9dfa

Browse files
committed
Supporting json.
1 parent 9e27638 commit def9dfa

26 files changed

Lines changed: 292 additions & 56 deletions

File tree

api/src/main/java/kr/toxicity/libraries/datacomponent/api/Codec.java

Lines changed: 30 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,15 @@ public interface Codec<T> {
1212
JsonElement encode(@NotNull T t) throws IllegalStateException;
1313
@NotNull
1414
T decode(@NotNull JsonElement t) throws IllegalStateException;
15+
@NotNull
16+
Class<T> returnType();
1517

16-
Codec<Integer> INTEGER = of(JsonPrimitive::new, JsonElement::getAsInt);
17-
Codec<String> STRING = of(JsonPrimitive::new, JsonElement::getAsString);
18-
Codec<Boolean> BOOL = of(JsonPrimitive::new, JsonElement::getAsBoolean);
18+
Codec<Integer> INTEGER = of(Integer.TYPE, JsonPrimitive::new, JsonElement::getAsInt);
19+
Codec<String> STRING = of(String.class, JsonPrimitive::new, JsonElement::getAsString);
20+
Codec<Boolean> BOOL = of(Boolean.TYPE, JsonPrimitive::new, JsonElement::getAsBoolean);
1921

20-
static <T> @NotNull Codec<T> of(@NotNull Function<T, JsonElement> encoder, @NotNull Function<JsonElement, T> decoder) {
21-
return new Codec<T>() {
22+
static <T> @NotNull Codec<T> of(@NotNull Class<T> tClass, @NotNull Function<T, JsonElement> encoder, @NotNull Function<JsonElement, T> decoder) {
23+
return new Codec<>() {
2224
@Override
2325
public @NotNull JsonElement encode(@NotNull T t) throws IllegalStateException {
2426
return encoder.apply(t);
@@ -28,6 +30,29 @@ public interface Codec<T> {
2830
public @NotNull T decode(@NotNull JsonElement t) throws IllegalStateException {
2931
return decoder.apply(t);
3032
}
33+
34+
@Override
35+
public @NotNull Class<T> returnType() {
36+
return tClass;
37+
}
38+
};
39+
}
40+
default <R> @NotNull Codec<R> map(@NotNull Class<R> rClass, @NotNull Converter<R, T> converter) {
41+
return new Codec<>() {
42+
@Override
43+
public @NotNull JsonElement encode(@NotNull R r) throws IllegalStateException {
44+
return Codec.this.encode(converter.asVanilla(r));
45+
}
46+
47+
@Override
48+
public @NotNull R decode(@NotNull JsonElement t) throws IllegalStateException {
49+
return converter.asWrapper(Codec.this.decode(t));
50+
}
51+
52+
@Override
53+
public @NotNull Class<R> returnType() {
54+
return rClass;
55+
}
3156
};
3257
}
3358
}

api/src/main/java/kr/toxicity/libraries/datacomponent/api/Converter.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,4 +21,17 @@ static <T, R> Converter<T, R> of(@NotNull Function<T, R> vanilla, @NotNull Funct
2121
}
2222
};
2323
}
24+
default Converter<R, T> reversed() {
25+
return new Converter<>() {
26+
@Override
27+
public @NotNull T asVanilla(@NotNull R r) {
28+
return Converter.this.asWrapper(r);
29+
}
30+
31+
@Override
32+
public @NotNull R asWrapper(@NotNull T t) {
33+
return Converter.this.asVanilla(t);
34+
}
35+
};
36+
}
2437
}
Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
package kr.toxicity.libraries.datacomponent.api;
22

3+
import com.google.gson.JsonObject;
34
import org.jetbrains.annotations.NotNull;
45

56
public interface DataComponent {
6-
@NotNull Codec<DataComponent> codec();
7+
void set(@NotNull ItemAdapter adapter);
8+
@NotNull
9+
JsonObject get();
710
}

api/src/main/java/kr/toxicity/libraries/datacomponent/api/DataComponentAPI.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ public static void api(@NotNull DataComponentAPI api) {
1919

2020
public abstract @NotNull MinecraftVersion current();
2121
public abstract @NotNull NMS nms();
22+
public abstract @NotNull Serializer serializer();
2223

2324
public @NotNull ItemAdapter adapter(@NotNull ItemStack itemStack) {
2425
return nms().adapter(itemStack);

api/src/main/java/kr/toxicity/libraries/datacomponent/api/DataComponentType.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ static Registry<? extends DataComponentType<?>> registry() {
99
return NMS.nms().componentRegistry();
1010
}
1111
@NotNull String key();
12+
@NotNull Codec<T> codec();
1213

1314
@Nullable T set(@NotNull ItemAdapter adapter, @Nullable T t);
1415
@Nullable T get(@NotNull ItemAdapter adapter);

api/src/main/java/kr/toxicity/libraries/datacomponent/api/ItemAdapter.java

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -26,12 +26,7 @@ default JsonElement getToJson(@NotNull DataComponentType<?> type) {
2626
}
2727

2828
default void deserialize(@NotNull JsonObject object) {
29-
object.entrySet().forEach(e -> {
30-
var type = DataComponentType.registry().get(e.getKey());
31-
if (type != null) {
32-
setToJson(type, e.getValue());
33-
}
34-
});
29+
DataComponentAPI.api().serializer().serialize(object).set(this);
3530
}
3631
default @NotNull JsonObject serialize() throws IllegalStateException {
3732
var result = new JsonObject();
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package kr.toxicity.libraries.datacomponent.api;
2+
3+
import com.google.gson.JsonObject;
4+
import org.jetbrains.annotations.NotNull;
5+
6+
public interface Serializer {
7+
@NotNull DataComponent serialize(@NotNull JsonObject element);
8+
}
Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,14 @@
11
package kr.toxicity.libraries.datacomponent.api.wrapper;
22

3+
import kr.toxicity.libraries.datacomponent.api.Codec;
4+
import kr.toxicity.libraries.datacomponent.api.NMS;
35
import org.jetbrains.annotations.NotNull;
46

57
import java.util.List;
68

7-
public record AdventureModePredicate(@NotNull List<BlockPredicate> predicates, boolean showInTooltip) {
9+
public record AdventureModePredicate(@NotNull List<BlockPredicate> predicates, boolean showInTooltip) implements ComponentData<AdventureModePredicate> {
10+
@Override
11+
public Codec<AdventureModePredicate> codec() {
12+
return NMS.nms().canBreak().codec();
13+
}
814
}
Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,14 @@
11
package kr.toxicity.libraries.datacomponent.api.wrapper;
22

3+
import kr.toxicity.libraries.datacomponent.api.Codec;
4+
import kr.toxicity.libraries.datacomponent.api.NMS;
35
import org.jetbrains.annotations.NotNull;
46

57
import java.util.Map;
68

7-
public record BlockItemStateProperties(@NotNull Map<String, String> properties) {
9+
public record BlockItemStateProperties(@NotNull Map<String, String> properties) implements ComponentData<BlockItemStateProperties> {
10+
@Override
11+
public Codec<BlockItemStateProperties> codec() {
12+
return NMS.nms().blockState().codec();
13+
}
814
}
Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,15 @@
11
package kr.toxicity.libraries.datacomponent.api.wrapper;
22

3+
import kr.toxicity.libraries.datacomponent.api.Codec;
4+
import kr.toxicity.libraries.datacomponent.api.NMS;
35
import org.bukkit.inventory.ItemStack;
46
import org.jetbrains.annotations.NotNull;
57

68
import java.util.List;
79

8-
public record BundleContents(@NotNull List<ItemStack> itemStacks) {
10+
public record BundleContents(@NotNull List<ItemStack> itemStacks) implements ComponentData<BundleContents> {
11+
@Override
12+
public Codec<BundleContents> codec() {
13+
return NMS.nms().bundleContents().codec();
14+
}
915
}

0 commit comments

Comments
 (0)