-
Notifications
You must be signed in to change notification settings - Fork 18
/
CauldronIngredients.java
159 lines (144 loc) · 6.27 KB
/
CauldronIngredients.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
150
151
152
153
154
155
156
157
158
159
package knightminer.inspirations.library.recipe.cauldron;
import com.google.common.collect.BiMap;
import com.google.common.collect.HashBiMap;
import com.google.gson.JsonObject;
import com.google.gson.JsonSyntaxException;
import io.netty.handler.codec.DecoderException;
import knightminer.inspirations.Inspirations;
import knightminer.inspirations.library.recipe.cauldron.contents.ICauldronColor;
import knightminer.inspirations.library.recipe.cauldron.contents.ICauldronContents;
import knightminer.inspirations.library.recipe.cauldron.contents.ICauldronDye;
import knightminer.inspirations.library.recipe.cauldron.contents.ICauldronPotion;
import knightminer.inspirations.library.recipe.cauldron.contenttype.CauldronContentType;
import knightminer.inspirations.library.recipe.cauldron.ingredient.ContentMatchIngredient;
import knightminer.inspirations.library.recipe.cauldron.ingredient.FluidCauldronIngredient;
import knightminer.inspirations.library.recipe.cauldron.ingredient.ICauldronIngredient;
import knightminer.inspirations.library.recipe.cauldron.ingredient.ICauldronIngredientSerializer;
import net.minecraft.item.DyeColor;
import net.minecraft.network.PacketBuffer;
import net.minecraft.potion.Potion;
import net.minecraft.util.JSONUtils;
import net.minecraft.util.ResourceLocation;
/**
* Registry that helps with registering, serializing, and deserializing cauldron properties
*/
public class CauldronIngredients {
private static final String KEY_TYPE = "type";
private static final BiMap<ResourceLocation, ICauldronIngredientSerializer<?>> INGREDIENTS = HashBiMap.create();
/* Public constants */
/** Generic content match serializer */
public static final ContentMatchIngredient.Serializer<?,?> MATCH = register("match_content", ContentMatchIngredient.Serializer.GENERIC);
/** Fluid content match serializer */
public static final FluidCauldronIngredient.Serializer FLUID = register("fluid", new FluidCauldronIngredient.Serializer());
/** Color content match serializer */
public static final ContentMatchIngredient.Serializer<ICauldronColor,Integer> COLOR = registerMatch(CauldronContentTypes.COLOR);
/** Dye content match serializer */
public static final ContentMatchIngredient.Serializer<ICauldronDye,DyeColor> DYE = registerMatch(CauldronContentTypes.DYE);
/** Fluid content match serializer */
public static final ContentMatchIngredient.Serializer<ICauldronPotion,Potion> POTION = registerMatch(CauldronContentTypes.POTION);
/**
* Registers a new content type
* @param name Name
* @param type Type to register
*/
public static void register(ResourceLocation name, ICauldronIngredientSerializer<?> type) {
if (INGREDIENTS.containsKey(name)) {
throw new IllegalArgumentException("Duplicate cauldron ingredient serializer '" + name + "'");
}
INGREDIENTS.put(name, type);
}
/**
* Helper to register static types
* @param name Inspirations namespace name
* @param type Type to register
* @param <T> Output type
* @return Registered type
*/
private static <T extends ICauldronIngredientSerializer<?>> T register(String name, T type) {
register(Inspirations.getResource(name), type);
return type;
}
/**
* Registers a generic content match type for the given type
* @param mapType Map type instance
* @param <C> Content type
* @param <T> Map value type
* @return Registered serializer
*/
public static <C extends ICauldronContents, T> ContentMatchIngredient.Serializer<C,T> registerMatch(CauldronContentType<C,T> mapType) {
ContentMatchIngredient.Serializer<C,T> serializer = new ContentMatchIngredient.Serializer<>(mapType);
register(CauldronContentTypes.getName(mapType), serializer);
return serializer;
}
/**
* Gets the type for the given cauldron contents
* @param contents Contents object
* @param <T> Contents type class
* @return Type with the proper generics
*/
@SuppressWarnings("unchecked")
private static <T extends ICauldronIngredient> ICauldronIngredientSerializer<T> getSerializer(T contents) {
return (ICauldronIngredientSerializer<T>)contents.getSerializer();
}
/**
* Gets the name for a content type
* @param serializer Serializer name
* @return Type registry name
*/
public static ResourceLocation getName(ICauldronIngredientSerializer<?> serializer) {
ResourceLocation name = INGREDIENTS.inverse().get(serializer);
if (name == null) {
throw new IllegalArgumentException("Unregistered cauldron serializer");
}
return name;
}
/**
* Converts the given contents to JSON
* @param contents Contents
* @param <T> Contents type
* @return JSON
*/
public static <T extends ICauldronIngredient> JsonObject toJson(T contents) {
JsonObject json = new JsonObject();
ICauldronIngredientSerializer<T> serializer = getSerializer(contents);
json.addProperty(KEY_TYPE, getName(serializer).toString());
serializer.write(contents, json);
return json;
}
/**
* Reads the cauldron contents from JSON
* @param json JSON to read
* @return Cauldron contents
*/
public static ICauldronIngredient read(JsonObject json) {
ResourceLocation location = new ResourceLocation(JSONUtils.getString(json, KEY_TYPE));
ICauldronIngredientSerializer<?> serializer = INGREDIENTS.get(location);
if (serializer != null) {
return serializer.read(json);
}
throw new JsonSyntaxException("Invalid cauldron ingredient type '" + location + "'");
}
/**
* Writes the given contents to NBT
* @param contents Contents to write
* @param buffer Buffer instance
*/
public static <T extends ICauldronIngredient> void write(T contents, PacketBuffer buffer) {
ICauldronIngredientSerializer<T> serializer = getSerializer(contents);
buffer.writeResourceLocation(getName(serializer));
serializer.write(contents, buffer);
}
/**
* Reads the given contents from NBT
* @param buffer Buffer instance
* @return Cauldron contents
*/
public static ICauldronIngredient read(PacketBuffer buffer) {
ResourceLocation name = buffer.readResourceLocation();
ICauldronIngredientSerializer<?> serializer = INGREDIENTS.get(name);
if (serializer == null) {
throw new DecoderException("Invalid cauldron ingredient type '" + name + "'");
}
return serializer.read(buffer);
}
}