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

99 lines
3.6 KiB
Java
Raw Normal View History

2020-04-29 16:38:50 +02:00
package de.ellpeck.naturesaura.recipes;
import com.google.gson.JsonObject;
2021-12-04 19:17:21 +01:00
import net.minecraft.core.BlockPos;
2023-07-08 12:32:27 +02:00
import net.minecraft.core.RegistryAccess;
2021-12-04 19:17:21 +01:00
import net.minecraft.network.FriendlyByteBuf;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.server.level.ServerLevel;
import net.minecraft.world.entity.Entity;
import net.minecraft.world.entity.EntityType;
import net.minecraft.world.entity.MobSpawnType;
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;
import net.minecraft.world.level.Level;
2024-02-03 14:56:07 +01:00
import net.neoforged.neoforge.registries.ForgeRegistries;
2020-04-29 16:38:50 +02:00
import javax.annotation.Nullable;
import java.util.ArrayList;
import java.util.List;
public class AnimalSpawnerRecipe extends ModRecipe {
public final Ingredient[] ingredients;
public final EntityType<?> entity;
public final int aura;
public final int time;
public AnimalSpawnerRecipe(ResourceLocation name, EntityType<?> entity, int aura, int time, Ingredient... ingredients) {
super(name);
this.ingredients = ingredients;
this.entity = entity;
this.aura = aura;
this.time = time;
}
2021-12-04 15:40:09 +01:00
public Entity makeEntity(Level level, BlockPos pos) {
2020-09-22 03:24:28 +02:00
// passed position is zero on the client, so we don't want to do initialization stuff for the entity
if (pos == BlockPos.ZERO)
2021-12-04 15:40:09 +01:00
return this.entity.create(level);
2023-07-08 12:32:27 +02:00
return this.entity.create((ServerLevel) level, null, null, pos, MobSpawnType.SPAWNER, false, false);
2020-04-29 16:38:50 +02:00
}
@Override
2023-07-08 12:32:27 +02:00
public ItemStack getResultItem(RegistryAccess access) {
2020-04-29 16:38:50 +02:00
return ItemStack.EMPTY;
}
@Override
2021-12-04 19:17:21 +01:00
public RecipeSerializer<?> getSerializer() {
2020-04-29 16:38:50 +02:00
return ModRecipes.ANIMAL_SPAWNER_SERIALIZER;
}
@Override
2021-12-04 19:17:21 +01:00
public RecipeType<?> getType() {
2020-04-29 16:38:50 +02:00
return ModRecipes.ANIMAL_SPAWNER_TYPE;
}
2022-06-27 15:24:04 +02:00
public static class Serializer implements RecipeSerializer<AnimalSpawnerRecipe> {
2021-12-04 19:17:21 +01:00
2020-04-29 16:38:50 +02:00
@Override
2021-12-04 19:17:21 +01:00
public AnimalSpawnerRecipe fromJson(ResourceLocation recipeId, JsonObject json) {
2020-04-29 16:38:50 +02:00
List<Ingredient> ingredients = new ArrayList<>();
2021-12-15 16:30:22 +01:00
for (var e : json.getAsJsonArray("ingredients"))
2021-12-04 19:17:21 +01:00
ingredients.add(Ingredient.fromJson(e));
2020-04-29 16:38:50 +02:00
return new AnimalSpawnerRecipe(recipeId,
ForgeRegistries.ENTITY_TYPES.getValue(new ResourceLocation(json.get("entity").getAsString())),
2020-04-29 16:38:50 +02:00
json.get("aura").getAsInt(),
json.get("time").getAsInt(),
ingredients.toArray(new Ingredient[0]));
}
@Nullable
@Override
2021-12-04 19:17:21 +01:00
public AnimalSpawnerRecipe fromNetwork(ResourceLocation recipeId, FriendlyByteBuf buffer) {
2021-12-15 16:30:22 +01:00
var ings = new Ingredient[buffer.readInt()];
for (var 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 AnimalSpawnerRecipe(
recipeId,
ForgeRegistries.ENTITY_TYPES.getValue(buffer.readResourceLocation()),
2020-04-29 16:38:50 +02:00
buffer.readInt(),
buffer.readInt(),
ings);
}
@Override
2021-12-04 19:17:21 +01:00
public void toNetwork(FriendlyByteBuf buffer, AnimalSpawnerRecipe recipe) {
2020-04-29 16:38:50 +02:00
buffer.writeInt(recipe.ingredients.length);
2021-12-15 16:30:22 +01:00
for (var ing : recipe.ingredients)
2021-12-04 19:17:21 +01:00
ing.toNetwork(buffer);
buffer.writeResourceLocation(ForgeRegistries.ENTITY_TYPES.getKey(recipe.entity));
2020-04-29 16:38:50 +02:00
buffer.writeInt(recipe.aura);
buffer.writeInt(recipe.time);
}
}
}