-
Notifications
You must be signed in to change notification settings - Fork 95
/
NamedComponentRegistry.java
149 lines (124 loc) · 4.29 KB
/
NamedComponentRegistry.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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
package slimeknights.mantle.data.registry;
import com.google.common.collect.BiMap;
import com.google.common.collect.HashBiMap;
import com.google.gson.JsonElement;
import com.google.gson.JsonPrimitive;
import com.google.gson.JsonSyntaxException;
import io.netty.handler.codec.DecoderException;
import net.minecraft.network.FriendlyByteBuf;
import net.minecraft.resources.ResourceLocation;
import slimeknights.mantle.data.loadable.Loadable;
import slimeknights.mantle.util.JsonHelper;
import javax.annotation.Nullable;
/**
* Generic registry of a component named by a resource location. Supports any arbitrary object without making any changes to it.
* @param <T> Type of the component being registered.
*/
public class NamedComponentRegistry<T> implements Loadable<T> {
/** Registered box expansion types */
private final BiMap<ResourceLocation,T> values = HashBiMap.create();
/** Name to make exceptions clearer */
private final String errorText;
public NamedComponentRegistry(String errorText) {
this.errorText = errorText + " ";
}
/** Registers the value with the given name */
public <V extends T> V register(ResourceLocation name, V value) {
if (values.putIfAbsent(name, value) != null) {
throw new IllegalArgumentException("Duplicate registration " + name);
}
return value;
}
/** Gets a value or null if missing */
@Nullable
public T getValue(ResourceLocation name) {
return values.get(name);
}
/** Gets the key associated with a value */
@Nullable
public ResourceLocation getOptionalKey(T value) {
return values.inverse().get(value);
}
/** Gets the key associated with a value */
public ResourceLocation getKey(T value) {
ResourceLocation key = getOptionalKey(value);
if (key == null) {
throw new IllegalStateException(errorText + value);
}
return key;
}
/* Json */
@Override
public T convert(JsonElement element, String key) {
ResourceLocation name = JsonHelper.convertToResourceLocation(element, key);
T value = getValue(name);
if (value == null) {
throw new JsonSyntaxException(errorText + name + " at '" + key + '\'');
}
return value;
}
@Override
public JsonElement serialize(T object) {
return new JsonPrimitive(getKey(object).toString());
}
/* Network */
/** Writes the value to the buffer */
@Override
public void encode(FriendlyByteBuf buffer, T value) {
buffer.writeResourceLocation(getKey(value));
}
/** Writes the value to the buffer */
public void encodeOptional(FriendlyByteBuf buffer, @Nullable T value) {
// if null, just write an empty string, that is not a valid resource location anyways and saves us a byte
if (value != null) {
buffer.writeUtf(getKey(value).toString());
} else {
buffer.writeUtf("");
}
}
/** Reads the given value from the network by resource location */
private T decodeInternal(ResourceLocation name) {
T value = getValue(name);
if (value == null) {
throw new DecoderException(errorText + name);
}
return value;
}
/** Parse the value from JSON */
@Override
public T decode(FriendlyByteBuf buffer) {
return decodeInternal(buffer.readResourceLocation());
}
/** Parse the value from JSON */
@Nullable
public T decodeOptional(FriendlyByteBuf buffer) {
// empty string is not a valid resource location, so its a nice value to use for null, saves us a byte
String key = buffer.readUtf(Short.MAX_VALUE);
if (key.isEmpty()) {
return null;
}
return decodeInternal(new ResourceLocation(key));
}
/* Deprecated aliases */
/** @deprecated use {@link #decode(FriendlyByteBuf)} */
@Deprecated(forRemoval = true)
public void toNetwork(T src, FriendlyByteBuf buffer) {
encode(buffer, src);
}
/** @deprecated use {@link #decode(FriendlyByteBuf)} */
@Deprecated(forRemoval = true)
public T fromNetwork(FriendlyByteBuf buffer) {
return decode(buffer);
}
/** @deprecated use {@link #decode(FriendlyByteBuf)} */
@Deprecated(forRemoval = true)
public void toNetworkOptional(@Nullable T src, FriendlyByteBuf buffer) {
encodeOptional(buffer, src);
}
/** @deprecated use {@link #decode(FriendlyByteBuf)} */
@Nullable
@Deprecated(forRemoval = true)
public T fromNetworkOptional(FriendlyByteBuf buffer) {
return decodeOptional(buffer);
}
}