Advertisement
Guest User

My Recipe Serializer

a guest
Apr 4th, 2020
194
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.39 KB | None | 0 0
  1. public class MyRecipeSerializer implements RecipeSerializer<MyRecipe> {
  2.  
  3.     private MyRecipeSerializer() {}
  4.  
  5.     public static final MyRecipeSerializer INSTANCE = new <MyRecipeSerializer();
  6.     public static final Identifier ID = new Identifier("modid:my_recipe");
  7.  
  8.     @Override
  9.     public MyRecipe read(Identifier id, JsonObject json) {
  10.         MyRecipeJsonFormat recipeJson = new Gson().fromJson(json, MyRecipeJsonFormat.class);
  11.  
  12.         if(recipeJson.input == null || recipeJson.outputItem == null) { throw new JsonSyntaxException("Missing Attributes in Recipe!"); }
  13.         Ingredient input = Ingredient.fromJson(recipeJson.getInput());
  14.         Item outputItem = Registry.ITEM.getOrEmpty(new Identifier(recipeJson.getOutputItemId())).orElseThrow(() -> new JsonSyntaxException("The Item " + recipeJson.outputItem + " does not exist!"));
  15.         ItemStack outputStack = new ItemStack(outputItem, recipeJson.getOutputCount());
  16.  
  17.         return new MyRecipe(input, outputStack, id);
  18.     }
  19.  
  20.     @Override
  21.     public MyRecipe read(Identifier id, PacketByteBuf buf) {
  22.  
  23.         Ingredient input = Ingredient.fromPacket(buf);
  24.         ItemStack output = buf.readItemStack();
  25.  
  26.         return new MyRecipe(input, output, id);
  27.     }
  28.  
  29.     @Override
  30.     public void write(PacketByteBuf buf, MyRecipe recipe) {
  31.         recipe.getInput().write(buf);
  32.         buf.writeItemStack(recipe.getOutput());
  33.     }
  34. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement