Skip to content

Commit

Permalink
Use Codec serialisation in SimpleFinishedRecipe
Browse files Browse the repository at this point in the history
- Move IngredientUtil.toJson to ModJsonUtil
- Remove unused methods in ModJsonUtil
  • Loading branch information
Choonster committed Sep 24, 2023
1 parent dc886bf commit 08f7dbe
Show file tree
Hide file tree
Showing 7 changed files with 21 additions and 99 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package choonster.testmod3.data.crafting.ingredient;

import choonster.testmod3.util.ModJsonUtil;
import choonster.testmod3.world.item.crafting.ingredient.ConditionalIngredientCodec;
import choonster.testmod3.world.item.crafting.ingredient.IngredientUtil;
import com.google.gson.JsonElement;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.item.crafting.Ingredient;
Expand Down Expand Up @@ -100,7 +100,7 @@ private Result(final List<ICondition> conditions, final Ingredient ingredient) {
public JsonElement toJson(final boolean allowEmpty) {
final var data = new ConditionalIngredientCodec.Data(conditions, ingredient);

return IngredientUtil.toJson(ConditionalIngredientCodec.DATA_CODEC, data);
return ModJsonUtil.toJson(ConditionalIngredientCodec.DATA_CODEC, data);
}
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package choonster.testmod3.data.crafting.ingredient;

import choonster.testmod3.world.item.crafting.ingredient.IngredientUtil;
import choonster.testmod3.util.ModJsonUtil;
import choonster.testmod3.world.item.crafting.ingredient.MobSpawnerIngredientCodec;
import com.google.gson.JsonElement;
import net.minecraft.world.entity.EntityType;
Expand Down Expand Up @@ -103,7 +103,7 @@ private Result(final ItemStack spawner, final EntityType<?> entityType) {
public JsonElement toJson(final boolean p_299391_) {
final var data = new MobSpawnerIngredientCodec.Data(spawner, entityType);

return IngredientUtil.toJson(MobSpawnerIngredientCodec.DATA_CODEC, data);
return ModJsonUtil.toJson(MobSpawnerIngredientCodec.DATA_CODEC, data);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public void serializeRecipeData(final JsonObject json) {
super.serializeRecipeData(json);

if (resultNBT != null) {
ModJsonUtil.setCompoundTag(json.getAsJsonObject("result"), "nbt", resultNBT);
json.add("nbt", ModJsonUtil.toJson(CompoundTag.CODEC, resultNBT));
}
}

Expand Down
76 changes: 11 additions & 65 deletions src/main/java/choonster/testmod3/util/ModJsonUtil.java
Original file line number Diff line number Diff line change
@@ -1,79 +1,25 @@
package choonster.testmod3.util;

import com.google.gson.*;
import com.mojang.brigadier.exceptions.CommandSyntaxException;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.nbt.TagParser;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.util.GsonHelper;
import net.minecraft.world.level.material.Fluid;
import net.minecraftforge.registries.ForgeRegistries;

import org.jetbrains.annotations.Nullable;
import com.google.gson.JsonElement;
import com.mojang.serialization.Codec;
import com.mojang.serialization.JsonOps;
import net.minecraft.Util;

/**
* Utility methods for JSON serialisation/deserialisation.
*
* @author Choonster
*/
public class ModJsonUtil {
private static final Gson GSON = new GsonBuilder().setPrettyPrinting().disableHtmlEscaping().create();

/**
* Gets a fluid from the specified member of the JSON object.
* Serialises an object to JSON using a {@link Codec}.
*
* @param object The JSON object
* @param memberName The member containing the fluid's registry name
* @return The fluid
* @throws JsonSyntaxException If the fluid registry name is missing or invalid
* @param codec The codec
* @param input The input
* @param <T> The ingredient type
* @return The serialised JSON
*/
public static Fluid getFluid(final JsonObject object, final String memberName) {
final ResourceLocation registryName = new ResourceLocation(GsonHelper.getAsString(object, memberName));
final Fluid fluid = ForgeRegistries.FLUIDS.getValue(registryName);

if (fluid == null) {
throw new JsonSyntaxException("Unknown fluid '" + registryName + "'");
}

return fluid;
}

/**
* Gets an optional compound tag from the specified member of the JSON object.
* <p>
* The member can be an object or a string containing the JSON representation of the NBT.
*
* @param json The JSON object
* @param memberName The member name
* @return The compound tag, if any
* @throws JsonSyntaxException If the NBT is malformed
*/
@Nullable
public static CompoundTag getCompoundTag(final JsonObject json, final String memberName) {
final CompoundTag nbt;

if (json.has(memberName)) {
final JsonElement element = json.get(memberName);

try {
if (element.isJsonObject()) {
nbt = TagParser.parseTag(GSON.toJson(element));
} else {
nbt = TagParser.parseTag(GsonHelper.convertToString(element, memberName));
}
} catch (final CommandSyntaxException e) {
throw new JsonSyntaxException("Malformed NBT tag", e);
}
} else {
nbt = null;
}

return nbt;
}

public static void setCompoundTag(final JsonObject json, final String memberName, final CompoundTag nbt) {
final JsonObject object = JsonParser.parseString(nbt.toString()).getAsJsonObject();

json.add(memberName, object);
public static <T> JsonElement toJson(final Codec<T> codec, final T input) {
return Util.getOrThrow(codec.encodeStart(JsonOps.INSTANCE, input), IllegalStateException::new);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import choonster.testmod3.TestMod3;
import choonster.testmod3.serialization.VanillaCodecs;
import choonster.testmod3.util.ModFluidUtil;
import choonster.testmod3.util.ModJsonUtil;
import choonster.testmod3.util.RegistryUtil;
import com.google.gson.JsonElement;
import com.mojang.serialization.Codec;
Expand Down Expand Up @@ -37,7 +38,7 @@ public class FluidContainerIngredient extends Ingredient {
public static Codec<FluidContainerIngredient> CODEC = RecordCodecBuilder.<FluidStack>create(instance ->
VanillaCodecs.fluidStack(instance)
.and(

ResourceLocation.CODEC
.fieldOf("type")
.forGetter((x) -> TYPE)
Expand Down Expand Up @@ -116,7 +117,7 @@ public boolean test(@Nullable final ItemStack stack) {

@Override
public JsonElement toJson(final boolean allowEmpty) {
return IngredientUtil.toJson(CODEC, this);
return ModJsonUtil.toJson(CODEC, this);
}

public FluidStack getFluidStack() {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package choonster.testmod3.world.item.crafting.ingredient;

import choonster.testmod3.TestMod3;
import choonster.testmod3.util.ModJsonUtil;
import com.google.gson.JsonElement;
import com.mojang.serialization.Codec;
import com.mojang.serialization.codecs.RecordCodecBuilder;
Expand Down Expand Up @@ -49,6 +50,6 @@ public boolean isSimple() {

@Override
public JsonElement toJson(final boolean allowEmpty) {
return IngredientUtil.toJson(CODEC, this);
return ModJsonUtil.toJson(CODEC, this);
}
}

This file was deleted.

0 comments on commit 08f7dbe

Please sign in to comment.