Skip to content

Commit

Permalink
Port fluid ingredient and tag key serializers from tinkers
Browse files Browse the repository at this point in the history
  • Loading branch information
KnightMiner committed Jun 6, 2022
1 parent d7d565a commit e90673a
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 0 deletions.
34 changes: 34 additions & 0 deletions src/main/java/slimeknights/mantle/data/TagKeySerializer.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package slimeknights.mantle.data;

import com.google.gson.JsonDeserializationContext;
import com.google.gson.JsonDeserializer;
import com.google.gson.JsonElement;
import com.google.gson.JsonParseException;
import com.google.gson.JsonPrimitive;
import com.google.gson.JsonSerializationContext;
import com.google.gson.JsonSerializer;
import lombok.RequiredArgsConstructor;
import net.minecraft.core.Registry;
import net.minecraft.resources.ResourceKey;
import net.minecraft.tags.TagKey;
import slimeknights.mantle.util.JsonHelper;

import java.lang.reflect.Type;

/**
* Serializer for a generic tag key type
*/
@RequiredArgsConstructor
public class TagKeySerializer<T> implements JsonSerializer<TagKey<T>>, JsonDeserializer<TagKey<T>> {
private final ResourceKey<Registry<T>> registry;

@Override
public TagKey<T> deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException {
return TagKey.create(registry, JsonHelper.convertToResourceLocation(json, "tag"));
}

@Override
public JsonElement serialize(TagKey<T> src, Type typeOfSrc, JsonSerializationContext context) {
return new JsonPrimitive(src.location().toString());
}
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
package slimeknights.mantle.recipe.ingredient;

import com.google.gson.JsonArray;
import com.google.gson.JsonDeserializationContext;
import com.google.gson.JsonDeserializer;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonParseException;
import com.google.gson.JsonSerializationContext;
import com.google.gson.JsonSerializer;
import com.google.gson.JsonSyntaxException;
import lombok.AccessLevel;
import lombok.AllArgsConstructor;
Expand All @@ -19,6 +24,7 @@
import net.minecraftforge.registries.ForgeRegistries;
import slimeknights.mantle.util.JsonHelper;

import java.lang.reflect.Type;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
Expand All @@ -31,6 +37,8 @@
public abstract class FluidIngredient {
/** Empty fluid ingredient, matches nothing */
public static final FluidIngredient EMPTY = new Empty();
/** Fluid json serializer instance */
public static Serializer SERIALIZER = new Serializer();

/** Cached list of display fluids */
private List<FluidStack> displayFluids;
Expand Down Expand Up @@ -433,4 +441,19 @@ private static Compound deserialize(JsonArray array, String name) {
return new Compound(ingredients);
}
}

/** Json serializer for fluids */
public static class Serializer implements JsonDeserializer<FluidIngredient>, JsonSerializer<FluidIngredient> {
private Serializer() {}

@Override
public FluidIngredient deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException {
return FluidIngredient.deserialize(json, "ingredient");
}

@Override
public JsonElement serialize(FluidIngredient src, Type typeOfSrc, JsonSerializationContext context) {
return src.serialize();
}
}
}

0 comments on commit e90673a

Please sign in to comment.