/
VariableLoaderRegistry.java
96 lines (83 loc) · 2.9 KB
/
VariableLoaderRegistry.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
package slimeknights.tconstruct.library.json.variable;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonPrimitive;
import com.google.gson.JsonSyntaxException;
import net.minecraft.network.FriendlyByteBuf;
import net.minecraft.util.GsonHelper;
import slimeknights.mantle.data.registry.GenericLoaderRegistry;
import slimeknights.mantle.data.registry.GenericLoaderRegistry.IHaveLoader;
/**
* Generic loader registry for variables, has special handling for constant float values to a custom float type
* @param <T> Object in registry
*/
public class VariableLoaderRegistry<T extends IHaveLoader> extends GenericLoaderRegistry<T> {
/** Constructor for constant values */
private final FloatFunction<? extends T> constantConstructor;
/**
* Creates a new instance with the given constructor
* @param constantConstructor Constructor for constant values, should extend {@link ConstantFloat}
*/
public VariableLoaderRegistry(FloatFunction<? extends T> constantConstructor) {
super(true);
this.constantConstructor = constantConstructor;
}
@Override
public T convert(JsonElement element, String key) throws JsonSyntaxException {
if (element.isJsonPrimitive()) {
JsonPrimitive primitive = element.getAsJsonPrimitive();
if (primitive.isNumber()) {
return constantConstructor.apply(primitive.getAsFloat());
}
}
return super.convert(element, key);
}
@Override
public T deserialize(JsonElement element) {
if (element.isJsonPrimitive()) {
JsonPrimitive primitive = element.getAsJsonPrimitive();
if (primitive.isNumber()) {
return constantConstructor.apply(primitive.getAsFloat());
}
}
return super.deserialize(element);
}
@Override
public JsonElement serialize(T src) {
if (src instanceof ConstantFloat constant) {
return new JsonPrimitive(constant.value());
}
return super.serialize(src);
}
@Override
public T fromNetwork(FriendlyByteBuf buffer) {
return super.fromNetwork(buffer);
}
/** Interface for a float constructor */
public interface FloatFunction<T> {
T apply(float value);
}
/** Class for constant float instances */
public interface ConstantFloat {
float value();
}
/** Loader for a constant float */
public record ConstantLoader<T extends ConstantFloat & IHaveLoader>(FloatFunction<T> constructor) implements IGenericLoader<T> {
@Override
public T deserialize(JsonObject json) {
return constructor.apply(GsonHelper.getAsFloat(json, "value"));
}
@Override
public void serialize(T object, JsonObject json) {
json.addProperty("value", object.value());
}
@Override
public T fromNetwork(FriendlyByteBuf buffer) {
return constructor.apply(buffer.readFloat());
}
@Override
public void toNetwork(T object, FriendlyByteBuf buffer) {
buffer.writeFloat(object.value());
}
}
}