package com.raoulvdberge.refinedstorage.recipe;

import com.google.gson.JsonObject;
import com.mojang.realmsclient.util.JsonUtils;
import net.minecraft.enchantment.Enchantment;
import net.minecraft.item.ItemStack;
import net.minecraft.item.crafting.IRecipeSerializer;
import net.minecraft.network.PacketBuffer;
import net.minecraft.util.ResourceLocation;
import net.minecraftforge.registries.ForgeRegistries;
import net.minecraftforge.registries.ForgeRegistryEntry;

import javax.annotation.Nullable;

public class UpgradeWithEnchantedBookRecipeSerializer extends ForgeRegistryEntry<IRecipeSerializer<?>> implements IRecipeSerializer<UpgradeWithEnchantedBookRecipe> {
    @Override
    public UpgradeWithEnchantedBookRecipe read(ResourceLocation recipeId, JsonObject json) {
        ItemStack result = new ItemStack(ForgeRegistries.ITEMS.getValue(new ResourceLocation(json.getAsJsonPrimitive("result").getAsString())));
        Enchantment enchantment = ForgeRegistries.ENCHANTMENTS.getValue(new ResourceLocation(json.getAsJsonObject("enchantment").getAsJsonPrimitive("id").getAsString()));
        int level = JsonUtils.func_225172_a("level", json.getAsJsonObject("enchantment"), 1);

        return new UpgradeWithEnchantedBookRecipe(recipeId, enchantment, level, result);
    }

    @Nullable
    @Override
    public UpgradeWithEnchantedBookRecipe read(ResourceLocation recipeId, PacketBuffer buffer) {
        ItemStack result = buffer.readItemStack();
        Enchantment enchantment = ForgeRegistries.ENCHANTMENTS.getValue(buffer.readResourceLocation());
        int level = buffer.readInt();

        return new UpgradeWithEnchantedBookRecipe(recipeId, enchantment, level, result);
    }

    @Override
    public void write(PacketBuffer buffer, UpgradeWithEnchantedBookRecipe recipe) {
        buffer.writeItemStack(recipe.getResult());
        buffer.writeResourceLocation(recipe.getEnchant().enchantment.getRegistryName());
        buffer.writeInt(recipe.getEnchant().enchantmentLevel);
    }
}

@@ -81,55 +81,8 @@
 ct;%0A
-import com.mojang.realmsclient.util.JsonUtils;%0A
 impo
@@ -717,24 +717,99 @@
 ect json) %7B%0A
+        JsonObject enchantmentInfo = json.getAsJsonObject(%22enchantment%22);%0A%0A
         Item
@@ -1034,38 +1034,16 @@
 ocation(
-json.getAsJsonObject(%22
 enchantm
@@ -1045,18 +1045,20 @@
 hantment
-%22)
+Info
 .getAsJs
@@ -1088,24 +1088,25 @@
 String()));%0A
+%0A
         int 
@@ -1117,81 +1117,140 @@
 l = 
-JsonUtils.func_225172_a(%22level%22, json.getAsJsonObject(%22enchantment%22), 1);
+1;%0A        if (enchantmentInfo.has(%22level%22)) %7B%0A            level = enchantmentInfo.getAsJsonPrimitive(%22level%22).getAsInt();%0A        %7D
 %0A%0A  

Fix server crash by using JsonUtils (client only)