NaturesAura/src/main/java/de/ellpeck/naturesaura/recipes/TreeRitualRecipe.java

88 lines
3.1 KiB
Java
Raw Normal View History

2020-04-29 16:38:50 +02:00
package de.ellpeck.naturesaura.recipes;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
2021-12-04 19:17:21 +01:00
import net.minecraft.network.FriendlyByteBuf;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.item.crafting.Ingredient;
import net.minecraft.world.item.crafting.RecipeSerializer;
import net.minecraft.world.item.crafting.RecipeType;
2020-04-29 16:38:50 +02:00
import net.minecraftforge.common.crafting.CraftingHelper;
import net.minecraftforge.registries.ForgeRegistryEntry;
import javax.annotation.Nullable;
import java.util.ArrayList;
import java.util.List;
public class TreeRitualRecipe extends ModRecipe {
public final Ingredient saplingType;
public final Ingredient[] ingredients;
public final ItemStack result;
public final int time;
public TreeRitualRecipe(ResourceLocation name, Ingredient saplingType, ItemStack result, int time, Ingredient... ingredients) {
super(name);
this.saplingType = saplingType;
this.ingredients = ingredients;
this.result = result;
this.time = time;
}
@Override
2021-12-04 19:17:21 +01:00
public ItemStack getResultItem() {
2020-04-29 16:38:50 +02:00
return this.result;
}
@Override
2021-12-04 19:17:21 +01:00
public RecipeSerializer<?> getSerializer() {
2020-04-29 16:38:50 +02:00
return ModRecipes.TREE_RITUAL_SERIALIZER;
}
@Override
2021-12-04 19:17:21 +01:00
public RecipeType<?> getType() {
2020-04-29 16:38:50 +02:00
return ModRecipes.TREE_RITUAL_TYPE;
}
2021-12-04 19:17:21 +01:00
public static class Serializer extends ForgeRegistryEntry<RecipeSerializer<?>> implements RecipeSerializer<TreeRitualRecipe> {
2020-04-29 16:38:50 +02:00
@Override
2021-12-04 19:17:21 +01:00
public TreeRitualRecipe fromJson(ResourceLocation recipeId, JsonObject json) {
2020-04-29 16:38:50 +02:00
List<Ingredient> ings = new ArrayList<>();
for (JsonElement element : json.getAsJsonArray("ingredients"))
2021-12-04 19:17:21 +01:00
ings.add(Ingredient.fromJson(element));
2020-04-29 16:38:50 +02:00
return new TreeRitualRecipe(
recipeId,
2021-12-04 19:17:21 +01:00
Ingredient.fromJson(json.getAsJsonObject("sapling")),
2020-04-29 16:38:50 +02:00
CraftingHelper.getItemStack(json.getAsJsonObject("output"), true),
json.get("time").getAsInt(),
ings.toArray(new Ingredient[0]));
}
@Nullable
@Override
2021-12-04 19:17:21 +01:00
public TreeRitualRecipe fromNetwork(ResourceLocation recipeId, FriendlyByteBuf buffer) {
2020-04-29 16:38:50 +02:00
Ingredient[] ings = new Ingredient[buffer.readInt()];
for (int i = 0; i < ings.length; i++)
2021-12-04 19:17:21 +01:00
ings[i] = Ingredient.fromNetwork(buffer);
2020-04-29 16:38:50 +02:00
return new TreeRitualRecipe(
recipeId,
2021-12-04 19:17:21 +01:00
Ingredient.fromNetwork(buffer),
buffer.readItem(),
2020-04-29 16:38:50 +02:00
buffer.readInt(),
ings);
}
@Override
2021-12-04 19:17:21 +01:00
public void toNetwork(FriendlyByteBuf buffer, TreeRitualRecipe recipe) {
2020-04-29 16:38:50 +02:00
buffer.writeInt(recipe.ingredients.length);
for (Ingredient ing : recipe.ingredients)
2021-12-04 19:17:21 +01:00
ing.toNetwork(buffer);
recipe.saplingType.toNetwork(buffer);
buffer.writeItem(recipe.result);
2020-04-29 16:38:50 +02:00
buffer.writeInt(recipe.time);
}
}
}