/
ContentMatchIngredient.java
231 lines (199 loc) · 6.35 KB
/
ContentMatchIngredient.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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
package knightminer.inspirations.library.recipe.cauldron.ingredient;
import com.google.common.collect.ImmutableSet;
import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonSyntaxException;
import knightminer.inspirations.library.recipe.cauldron.contents.CauldronContentType;
import knightminer.inspirations.library.recipe.cauldron.contents.ICauldronContents;
import net.minecraft.network.PacketBuffer;
import slimeknights.mantle.util.JsonHelper;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.Set;
import java.util.function.Function;
import java.util.stream.Collectors;
/**
* Type that can match a value or list of values.
* @param <T> Value type
*/
public abstract class ContentMatchIngredient<T> implements ICauldronIngredient {
protected final Serializer<T> serializer;
@SuppressWarnings("WeakerAccess")
protected ContentMatchIngredient(Serializer<T> serializer) {
this.serializer = serializer;
}
/**
* Checks that the given value matches
* @param value Value to check
* @return True if the value matches
*/
protected abstract boolean testValue(T value);
/**
* Writes the fluid ingredient to JSON
* @param json JSON to write
*/
protected abstract void write(JsonObject json);
/**
* Writes the fluid ingredient to the packet buffer
* @param buffer Buffer instance
*/
protected abstract void write(PacketBuffer buffer);
@Override
public boolean test(ICauldronContents contents) {
return contents.get(serializer.type)
.map(this::testValue)
.orElse(false);
}
@Override
public Serializer<?> getSerializer() {
return serializer;
}
/** Matches a single value */
private static class Single<T> extends ContentMatchIngredient<T> {
private final T value;
private List<ICauldronContents> displayValue;
private Single(Serializer<T> serializer, T value) {
super(serializer);
this.value = value;
}
@Override
protected boolean testValue(T value) {
return this.value.equals(value);
}
@Override
protected void write(JsonObject json) {
json.add(serializer.type.getKey(), serializer.type.toJson(value));
}
@Override
protected void write(PacketBuffer buffer) {
buffer.writeVarInt(1);
serializer.type.write(value, buffer);
}
@Override
public List<ICauldronContents> getMatchingContents() {
if (displayValue == null) {
displayValue = Collections.singletonList(serializer.type.of(value));
}
return displayValue;
}
}
/** Matches from a set */
private static class Multi<T> extends ContentMatchIngredient<T> {
private final Set<T> values;
private List<ICauldronContents> displayValues;
private Multi(Serializer<T> serializer, Set<T> values) {
super(serializer);
this.values = values;
}
@Override
protected boolean testValue(T value) {
return values.contains(value);
}
@Override
protected void write(JsonObject json) {
JsonArray array = new JsonArray();
for (T value : values) {
array.add(serializer.type.toJson(value));
}
json.add(serializer.type.getKey(), array);
}
@Override
protected void write(PacketBuffer buffer) {
buffer.writeVarInt(values.size());
for (T value : values) {
serializer.type.write(value, buffer);
}
}
@Override
public List<ICauldronContents> getMatchingContents() {
if (displayValues == null) {
displayValues = values.stream().map(serializer.type::of).collect(Collectors.toList());
}
return displayValues;
}
}
/**
* Generic serializer for a content match ingredient
* @param <T> Value type of serializer
*/
public static class Serializer<T> implements ICauldronIngredientSerializer<ContentMatchIngredient<T>> {
/**
* Content type represented by this serializer
*/
private final CauldronContentType<T> type;
/**
* Creates a new serializer using the given type
* @param type Serializer type
*/
public Serializer(CauldronContentType<T> type) {
this.type = type;
}
/**
* Creates a new ingredient from the given value
* @param value Value
* @return Ingredient instance
*/
public ContentMatchIngredient<T> of(T value) {
return new Single<>(this, value);
}
/**
* Creates a new ingredient from the given values
* @param values Values
* @return Ingredient instance
*/
public ContentMatchIngredient<T> of(Collection<T> values) {
return new Multi<>(this, ImmutableSet.copyOf(values));
}
/**
* Helper to get a list of values from a string
* @param values List of values
* @return Content match ingredient
*/
private ContentMatchIngredient<T> getList(List<T> values) {
// if a single element, save effort
if (values.size() == 1) {
return of(values.get(0));
}
return of(values);
}
@Override
public ContentMatchIngredient<T> read(JsonObject json) {
// actual element can be a string or array
String key = this.type.getKey();
JsonElement element = JsonHelper.getElement(json, key);
// single name
if (element.isJsonPrimitive()) {
return of(type.getValue(element, key));
}
// array of names
if (element.isJsonArray()) {
List<T> values = JsonHelper.parseList(element.getAsJsonArray(), "names", type::getValue, Function.identity());
return getList(values);
}
// error
throw new JsonSyntaxException("Invalid '" + this.type.getKey() + "', must be a single value or an array");
}
@Override
public void write(ContentMatchIngredient<T> ingredient, JsonObject json) {
ingredient.write(json);
}
@Override
public ContentMatchIngredient<T> read(PacketBuffer buffer) {
// read all values from the buffer
int size = buffer.readVarInt();
List<T> values = new ArrayList<>(size);
for (int i = 0; i < size; i++) {
values.add(type.read(buffer));
}
// parse list
return getList(values);
}
@Override
public void write(ContentMatchIngredient<T> ingredient, PacketBuffer buffer) {
ingredient.write(buffer);
}
}
}