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

99 lines
3.7 KiB
Java
Raw Normal View History

2020-04-29 16:38:50 +02:00
package de.ellpeck.naturesaura.recipes;
import com.google.gson.JsonObject;
import de.ellpeck.naturesaura.NaturesAura;
2020-04-29 16:38:50 +02:00
import de.ellpeck.naturesaura.api.NaturesAuraAPI;
import de.ellpeck.naturesaura.api.aura.type.IAuraType;
import de.ellpeck.naturesaura.items.ItemAuraBottle;
import de.ellpeck.naturesaura.items.ModItems;
2021-12-04 19:17:21 +01:00
import net.minecraft.network.FriendlyByteBuf;
import net.minecraft.network.chat.TranslatableComponent;
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;
public class AltarRecipe extends ModRecipe {
public final Ingredient input;
public final ItemStack output;
public final IAuraType requiredType;
public final Ingredient catalyst;
public final int aura;
public final int time;
public AltarRecipe(ResourceLocation name, Ingredient input, ItemStack output, IAuraType requiredType, Ingredient catalyst, int aura, int time) {
super(name);
this.input = input;
this.output = output;
this.requiredType = requiredType;
this.catalyst = catalyst;
this.aura = aura;
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.output;
}
@Override
2021-12-04 19:17:21 +01:00
public RecipeSerializer<?> getSerializer() {
2020-04-29 16:38:50 +02:00
return ModRecipes.ALTAR_SERIAIZER;
}
@Override
2021-12-04 19:17:21 +01:00
public RecipeType<?> getType() {
2020-04-29 16:38:50 +02:00
return ModRecipes.ALTAR_TYPE;
}
public ItemStack getDimensionBottle() {
2021-12-15 16:30:22 +01:00
var bottle = ItemAuraBottle.setType(new ItemStack(ModItems.AURA_BOTTLE), this.requiredType);
2021-12-04 19:17:21 +01:00
bottle.setHoverName(new TranslatableComponent("info." + NaturesAura.MOD_ID + ".required_aura_type." + this.requiredType.getName()));
return bottle;
}
2021-12-04 19:17:21 +01:00
public static class Serializer extends ForgeRegistryEntry<RecipeSerializer<?>> implements RecipeSerializer<AltarRecipe> {
2020-04-29 16:38:50 +02:00
@Override
2021-12-04 19:17:21 +01:00
public AltarRecipe fromJson(ResourceLocation recipeId, JsonObject json) {
2020-04-29 16:38:50 +02:00
return new AltarRecipe(
recipeId,
2021-12-04 19:17:21 +01:00
Ingredient.fromJson(json.getAsJsonObject("input")),
2020-04-29 16:38:50 +02:00
CraftingHelper.getItemStack(json.getAsJsonObject("output"), true),
NaturesAuraAPI.AURA_TYPES.get(new ResourceLocation(json.get("aura_type").getAsString())),
2021-12-04 19:17:21 +01:00
json.has("catalyst") ? Ingredient.fromJson(json.getAsJsonObject("catalyst")) : Ingredient.EMPTY,
2020-04-29 16:38:50 +02:00
json.get("aura").getAsInt(),
json.get("time").getAsInt());
}
@Nullable
@Override
2021-12-04 19:17:21 +01:00
public AltarRecipe fromNetwork(ResourceLocation recipeId, FriendlyByteBuf buffer) {
2020-04-29 16:38:50 +02:00
return new AltarRecipe(
recipeId,
2021-12-04 19:17:21 +01:00
Ingredient.fromNetwork(buffer),
buffer.readItem(),
2020-04-29 16:38:50 +02:00
NaturesAuraAPI.AURA_TYPES.get(buffer.readResourceLocation()),
2021-12-04 19:17:21 +01:00
Ingredient.fromNetwork(buffer),
2020-04-29 16:38:50 +02:00
buffer.readInt(),
buffer.readInt());
}
@Override
2021-12-04 19:17:21 +01:00
public void toNetwork(FriendlyByteBuf buffer, AltarRecipe recipe) {
recipe.input.toNetwork(buffer);
buffer.writeItem(recipe.output);
2020-04-29 16:38:50 +02:00
buffer.writeResourceLocation(recipe.requiredType.getName());
2021-12-04 19:17:21 +01:00
recipe.catalyst.toNetwork(buffer);
2020-04-29 16:38:50 +02:00
buffer.writeInt(recipe.aura);
buffer.writeInt(recipe.time);
}
}
}