/
FluidCauldronIngredient.java
85 lines (74 loc) · 2.63 KB
/
FluidCauldronIngredient.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
package knightminer.inspirations.library.recipe.cauldron.ingredient;
import com.google.gson.JsonObject;
import com.google.gson.JsonSyntaxException;
import knightminer.inspirations.library.recipe.cauldron.CauldronContentTypes;
import knightminer.inspirations.library.recipe.cauldron.CauldronIngredients;
import net.minecraft.fluid.Fluid;
import net.minecraft.network.PacketBuffer;
import net.minecraft.tags.FluidTags;
import net.minecraft.tags.ITag;
import net.minecraft.tags.TagCollectionManager;
import net.minecraft.util.JSONUtils;
import net.minecraft.util.ResourceLocation;
import java.util.List;
import java.util.Objects;
/**
* Cauldron ingredient type for fluid contents, mostly an extension of {@link ContentMatchIngredient}, but also includes tags
*/
public class FluidCauldronIngredient extends ContentMatchIngredient<Fluid> {
private final ITag<Fluid> tag;
private FluidCauldronIngredient(ITag<Fluid> tag) {
super(CauldronIngredients.FLUID);
this.tag = tag;
}
@Override
protected boolean testValue(Fluid value) {
return tag.contains(value);
}
@Override
protected void write(JsonObject json) {
json.addProperty("tag", TagCollectionManager.func_232928_e_().func_232926_c_().func_232975_b_(this.tag).toString());
}
@Override
protected void write(PacketBuffer buffer) {
List<Fluid> elements = tag.getAllElements();
buffer.writeVarInt(elements.size());
for (Fluid fluid : elements) {
buffer.writeResourceLocation(Objects.requireNonNull(fluid.getRegistryName()));
}
}
/** Specific fluid serializer class */
public static class Serializer extends ContentMatchIngredient.Serializer<Fluid> {
/**
* Creates a new serializer instance
*/
public Serializer() {
super(CauldronContentTypes.FLUID);
}
/**
* Creates a new ingredient from the given tag
* @param tag Tag instance
* @return Ingredient instance
*/
public ContentMatchIngredient<Fluid> of(ITag<Fluid> tag) {
return new FluidCauldronIngredient(tag);
}
@Override
public ContentMatchIngredient<Fluid> read(JsonObject json) {
// single fluid or array
if (json.has("name")) {
return super.read(json);
}
// tag
if (json.has("tag")) {
ResourceLocation tagName = new ResourceLocation(JSONUtils.getString(json, "tag"));
ITag<Fluid> tag = FluidTags.getCollection().get(tagName);
if (tag == null) {
throw new JsonSyntaxException("Unknown fluid tag '" + tagName + "'");
}
return of(tag);
}
throw new JsonSyntaxException("Invalid cauldron fluid ingredient, must have 'name' or 'tag");
}
}
}