-
Notifications
You must be signed in to change notification settings - Fork 53
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Use Codec serialisation in SimpleFinishedRecipe
- Move IngredientUtil.toJson to ModJsonUtil - Remove unused methods in ModJsonUtil
- Loading branch information
Showing
7 changed files
with
21 additions
and
99 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
26 changes: 0 additions & 26 deletions
26
src/main/java/choonster/testmod3/world/item/crafting/ingredient/IngredientUtil.java
This file was deleted.
Oops, something went wrong.