/
ModuleWithKey.java
64 lines (56 loc) · 1.78 KB
/
ModuleWithKey.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
package slimeknights.tconstruct.library.modifiers.util;
import com.google.gson.JsonObject;
import net.minecraft.network.FriendlyByteBuf;
import net.minecraft.resources.ResourceLocation;
import slimeknights.mantle.data.loadable.Loadables;
import slimeknights.mantle.data.loadable.field.LoadableField;
import slimeknights.mantle.util.JsonHelper;
import slimeknights.tconstruct.library.modifiers.Modifier;
import javax.annotation.Nullable;
/**
* Shared boilerplate for a module with a nullable key that can alternatively be the modifier ID
*/
public interface ModuleWithKey {
/** Field for building loadables */
LoadableField<ResourceLocation,ModuleWithKey> FIELD = Loadables.RESOURCE_LOCATION.nullableField("key", ModuleWithKey::key);
/** Gets the key for the module */
default ResourceLocation getKey(Modifier modifier) {
ResourceLocation key = key();
if (key != null) {
return key;
}
return modifier.getId();
}
/** Gets the key field from the record */
@Nullable
ResourceLocation key();
/**
* Parses the key from JSON
* @param json Json object
* @return Key, or null if not present
*/
@Nullable
static ResourceLocation parseKey(JsonObject json) {
if (json.has("key")) {
return JsonHelper.getResourceLocation(json, "key");
}
return null;
}
/** Reads the key from the network */
@Nullable
static ResourceLocation fromNetwork(FriendlyByteBuf buffer) {
if (buffer.readBoolean()) {
return buffer.readResourceLocation();
}
return null;
}
/** Writes the key to the network */
static void toNetwork(@Nullable ResourceLocation key, FriendlyByteBuf buffer) {
if (key != null) {
buffer.writeBoolean(true);
buffer.writeResourceLocation(key);
} else {
buffer.writeBoolean(false);
}
}
}