-
Notifications
You must be signed in to change notification settings - Fork 95
/
NBTLoadable.java
101 lines (86 loc) · 3.12 KB
/
NBTLoadable.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
package slimeknights.mantle.data.loadable.common;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonSyntaxException;
import com.mojang.brigadier.exceptions.CommandSyntaxException;
import com.mojang.serialization.JsonOps;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.nbt.NbtOps;
import net.minecraft.nbt.TagParser;
import net.minecraft.network.FriendlyByteBuf;
import slimeknights.mantle.data.loadable.Loadable;
import slimeknights.mantle.data.loadable.field.LoadableField;
import slimeknights.mantle.data.loadable.record.RecordLoadable;
import slimeknights.mantle.util.JsonHelper;
import javax.annotation.Nullable;
import java.util.function.Function;
/** Loadable for reading NBT, converting from a JSON object to a tag.*/
public enum NBTLoadable implements RecordLoadable<CompoundTag> {
/** Disallows reading NBT from a string in the Forge style*/
DISALLOW_STRING,
/** Allows reading NBT from a string in the forge style */
ALLOW_STRING;
@Override
public CompoundTag deserialize(JsonObject json) {
return (CompoundTag)JsonOps.INSTANCE.convertTo(NbtOps.INSTANCE, json);
}
@Override
public CompoundTag convert(JsonElement element, String key) {
if (this == ALLOW_STRING && !element.isJsonObject()) {
try {
return TagParser.parseTag(JsonHelper.DEFAULT_GSON.toJson(element));
} catch (CommandSyntaxException e) {
throw new JsonSyntaxException("Invalid NBT Entry: ", e);
}
}
return RecordLoadable.super.convert(element, key);
}
@Override
public JsonObject serialize(CompoundTag object) {
return NbtOps.INSTANCE.convertTo(JsonOps.INSTANCE, object).getAsJsonObject();
}
@Override
public void serialize(CompoundTag object, JsonObject json) {
json.entrySet().addAll(serialize(object).entrySet());
}
@Override
public CompoundTag fromNetwork(FriendlyByteBuf buffer) {
CompoundTag tag = buffer.readNbt();
if (tag == null) {
return new CompoundTag();
}
return tag;
}
@Override
public void toNetwork(CompoundTag object, FriendlyByteBuf buffer) {
buffer.writeNbt(object);
}
@Override
public <P> LoadableField<CompoundTag,P> nullableField(String key, Function<P,CompoundTag> getter) {
return new NullableNBTField<>(this, key, getter);
}
/** Special implementation of nullable field to compact the buffer since it natively handles nullable NBT */
private record NullableNBTField<P>(Loadable<CompoundTag> loadable, String key, Function<P,CompoundTag> getter) implements LoadableField<CompoundTag,P> {
@Nullable
@Override
public CompoundTag get(JsonObject json) {
return loadable.getOrDefault(json, key, null);
}
@Override
public void serialize(P parent, JsonObject json) {
CompoundTag nbt = getter.apply(parent);
if (nbt != null) {
json.add(key, loadable.serialize(nbt));
}
}
@Nullable
@Override
public CompoundTag fromNetwork(FriendlyByteBuf buffer) {
return buffer.readNbt();
}
@Override
public void toNetwork(P parent, FriendlyByteBuf buffer) {
buffer.writeNbt(getter.apply(parent));
}
}
}