Advertisement
HalestormXV

LunarForgeRecipeSerializer

Oct 30th, 2020
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.96 KB | None | 0 0
  1. package com.halestormxv.mysterium.recipes;
  2.  
  3. import com.google.gson.JsonElement;
  4. import com.google.gson.JsonObject;
  5. import net.minecraft.item.ItemStack;
  6. import net.minecraft.item.crafting.IRecipeSerializer;
  7. import net.minecraft.item.crafting.Ingredient;
  8. import net.minecraft.item.crafting.ShapedRecipe;
  9. import net.minecraft.network.PacketBuffer;
  10. import net.minecraft.util.JSONUtils;
  11. import net.minecraft.util.ResourceLocation;
  12. import net.minecraft.util.registry.Registry;
  13. import net.minecraftforge.registries.ForgeRegistryEntry;
  14.  
  15. public class LunarForgeRecipeSerializer<T extends LunarForgeRecipe> extends ForgeRegistryEntry<IRecipeSerializer<?>> implements IRecipeSerializer<T> {
  16. private final int cookTime;
  17. private final LunarForgeRecipeSerializer.IFactory<T> factory;
  18.  
  19. public LunarForgeRecipeSerializer(LunarForgeRecipeSerializer.IFactory<T> factoryIn, int timeIn) {
  20. this.cookTime = timeIn;
  21. this.factory = factoryIn;
  22. }
  23.  
  24. @Override
  25. public T read(ResourceLocation recipeId, JsonObject json) {
  26. JsonElement jsonelement = JSONUtils.isJsonArray(json, "ingredient") ? JSONUtils.getJsonArray(json, "ingredient") : JSONUtils.getJsonObject(json, "ingredient");
  27. Ingredient ingredient = Ingredient.deserialize(jsonelement);
  28. //RESULT
  29. if (!json.has("result"))
  30. throw new com.google.gson.JsonSyntaxException("Missing result, expected to find a string or object");
  31. ItemStack resultStack;
  32. if (json.get("result").isJsonObject())
  33. resultStack = ShapedRecipe.deserializeItem(JSONUtils.getJsonObject(json, "result"));
  34. else {
  35. String theResult = JSONUtils.getString(json, "result");
  36. ResourceLocation resourcelocation = new ResourceLocation(theResult);
  37. resultStack = new ItemStack(Registry.ITEM.getValue(resourcelocation).orElseThrow(() -> new IllegalStateException("Item: " + theResult + " does not exist")));
  38. }
  39.  
  40. float f = JSONUtils.getFloat(json, "experience", 0.0F);
  41. int i = JSONUtils.getInt(json, "cookingtime", this.cookTime);
  42. return this.factory.create(recipeId, ingredient, resultStack, f, i);
  43. }
  44.  
  45. @Override
  46. public T read(ResourceLocation recipeId, PacketBuffer buffer) {
  47. Ingredient ingredient = Ingredient.read(buffer);
  48. ItemStack itemstack = buffer.readItemStack();
  49. float f = buffer.readFloat();
  50. int i = buffer.readVarInt();
  51. return this.factory.create(recipeId, ingredient, itemstack, f, i);
  52. }
  53.  
  54. @Override
  55. public void write(PacketBuffer buffer, T recipe) {
  56. recipe.getInput().write(buffer);
  57. buffer.writeItemStack(recipe.getRecipeOutput());
  58. buffer.writeFloat(recipe.xp);
  59. buffer.writeVarInt(recipe.cookTime);
  60. }
  61.  
  62. public interface IFactory<T extends LunarForgeRecipe> {
  63. T create(ResourceLocation id, Ingredient ingredientIn, ItemStack outputIn, float experienceIn, int timeIn);
  64. }
  65. }
  66.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement