Advertisement
Scouter456

Untitled

Jun 26th, 2023
144
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.66 KB | None | 0 0
  1.  
  2. public class CultivatorRecipe implements Recipe<SimpleContainer> {
  3.     private final ResourceLocation id;
  4.     private final ItemStack output;
  5.     private final NonNullList<Ingredient> recipeItems;
  6.  
  7.     public CultivatorRecipe(ResourceLocation id, ItemStack output,
  8.                             NonNullList<Ingredient> recipeItems) {
  9.         this.id = id;
  10.         this.output = output;
  11.         this.recipeItems = recipeItems;
  12.     }
  13.  
  14.     @Override
  15.     public boolean matches(SimpleContainer pContainer, Level pLevel) {
  16.         return !recipeItems.isEmpty() && recipeItems.get(0).test(pContainer.getItem(0));
  17.     }
  18.  
  19.     @Override
  20.     public ItemStack assemble(SimpleContainer pContainer) {
  21.         return output.copy();
  22.     }
  23.  
  24.     @Override
  25.     public boolean canCraftInDimensions(int pWidth, int pHeight) {
  26.         return true;
  27.     }
  28.  
  29.     @Override
  30.     public ItemStack getResultItem() {
  31.         return output;
  32.     }
  33.  
  34.     @Override
  35.     public NonNullList<Ingredient> getIngredients() {
  36.         return recipeItems;
  37.     }
  38.  
  39.     @Override
  40.     public ResourceLocation getId() {
  41.         return id;
  42.     }
  43.  
  44.     @Override
  45.     public RecipeSerializer<?> getSerializer() {
  46.         return Serializer.INSTANCE;
  47.     }
  48.  
  49.     @Override
  50.     public RecipeType<?> getType() {
  51.         return Type.INSTANCE;
  52.     }
  53.  
  54.     public static class Type implements RecipeType<CultivatorRecipe> {
  55.         private Type() { }
  56.         public static final Type INSTANCE = new Type();
  57.     }
  58.  
  59.     public static class Serializer implements RecipeSerializer<CultivatorRecipe> {
  60.         public static final Serializer INSTANCE = new Serializer();
  61.         public static final ResourceLocation ID = new ResourceLocation(UnusualPrehistory.MODID,"cultivator");
  62.  
  63.         @Override
  64.         public CultivatorRecipe fromJson(ResourceLocation id, JsonObject json) {
  65.             ItemStack output = ShapedRecipe.itemStackFromJson(GsonHelper.getAsJsonObject(json, "output"));
  66.             JsonArray ingredients = GsonHelper.getAsJsonArray(json, "ingredients");
  67.             NonNullList<Ingredient> inputs = NonNullList.withSize(1, Ingredient.EMPTY);
  68.  
  69.             for (int i = 0; i < inputs.size(); i++) {
  70.                 inputs.set(i, Ingredient.fromJson(ingredients.get(i)));
  71.             }
  72.  
  73.             return new CultivatorRecipe(id, output, inputs);
  74.         }
  75.  
  76.         @Override
  77.         public CultivatorRecipe fromNetwork(ResourceLocation id, FriendlyByteBuf buf) {
  78.             NonNullList<Ingredient> inputs = NonNullList.withSize(buf.readInt(), Ingredient.EMPTY);
  79.  
  80.             for (int i = 0; i < inputs.size(); i++) {
  81.                 inputs.set(i, Ingredient.fromNetwork(buf));
  82.             }
  83.  
  84.             ItemStack output = buf.readItem();
  85.             return new CultivatorRecipe(id, output, inputs);
  86.         }
  87.  
  88.         @Override
  89.         public void toNetwork(FriendlyByteBuf buf, CultivatorRecipe recipe) {
  90.             buf.writeInt(recipe.getIngredients().size());
  91.             for (Ingredient ing : recipe.getIngredients()) {
  92.                 ing.toNetwork(buf);
  93.             }
  94.             buf.writeItemStack(recipe.output, false);
  95.         }
  96.  
  97.  
  98.         public RecipeSerializer<?> setRegistryName(ResourceLocation name) {
  99.             return INSTANCE;
  100.         }
  101.  
  102.  
  103.         public ResourceLocation getRegistryName() {
  104.             return ID;
  105.         }
  106.  
  107.         public Class<RecipeSerializer<?>> getRegistryType() {
  108.             return Serializer.castClass(RecipeSerializer.class);
  109.         }
  110.  
  111.         @SuppressWarnings("unchecked") // Need this wrapper, because generics
  112.         private static <G> Class<G> castClass(Class<?> cls) {
  113.             return (Class<G>)cls;
  114.         }
  115.     }
  116.  
  117.  
  118.  
  119. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement