Skip to content

Commit

Permalink
feat: allow any summon ritual to specify a tag to randomly summon from
Browse files Browse the repository at this point in the history
  • Loading branch information
klikli-dev committed Aug 8, 2024
1 parent 8c89553 commit f0b397e
Show file tree
Hide file tree
Showing 9 changed files with 187 additions and 93 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
"activation_item": {
"item": "occultism:book_of_binding_bound_foliot"
},
"entity_tag_to_summon": "occultism:random_animals_to_summon_list",
"ingredients": [
{
"tag": "c:crops"
Expand Down Expand Up @@ -47,5 +48,5 @@
"count": 1,
"id": "occultism:ritual_dummy/summon_random_animal"
},
"ritual_type": "occultism:summon_random_animal"
"ritual_type": "occultism:summon"
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,13 @@
import com.klikli_dev.occultism.common.entity.spirit.SpiritEntity;
import com.klikli_dev.occultism.crafting.recipe.RitualRecipe;
import com.klikli_dev.occultism.registry.OccultismItems;
import com.klikli_dev.occultism.registry.OccultismTags;
import com.klikli_dev.occultism.util.ItemNBTUtil;
import net.minecraft.advancements.CriteriaTriggers;
import net.minecraft.core.BlockPos;
import net.minecraft.core.RegistryAccess;
import net.minecraft.core.particles.ParticleTypes;
import net.minecraft.core.registries.BuiltInRegistries;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.network.chat.Component;
import net.minecraft.server.level.ServerLevel;
Expand All @@ -45,6 +47,8 @@
import net.neoforged.neoforge.items.ItemHandlerHelper;
import org.jetbrains.annotations.Nullable;

import java.util.stream.StreamSupport;

public class SummonRitual extends Ritual {

private final boolean tame;
Expand Down Expand Up @@ -153,6 +157,15 @@ public void finish(Level level, BlockPos goldenBowlPosition, GoldenSacrificialBo
}

protected EntityType<?> getEntityToSummon(Level level){
if(this.recipe.getEntityTagToSummon() != null){
var options = StreamSupport.stream(BuiltInRegistries.ENTITY_TYPE.getTagOrEmpty(this.recipe.getEntityTagToSummon()).spliterator(), false).toList();

if (!options.isEmpty()) {
int index = level.random.nextInt(options.size());
return options.get(index).value();
}
}

return this.recipe.getEntityToSummon();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,12 @@
import net.minecraft.network.codec.StreamCodec;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.tags.TagKey;
import net.minecraft.world.Container;
import net.minecraft.world.entity.EntityType;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.item.crafting.*;
import net.minecraft.world.level.Level;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.util.Optional;
import java.util.function.Supplier;
Expand All @@ -59,6 +60,7 @@ public class RitualRecipe implements Recipe<SingleRecipeInput> {
ItemStack.STRICT_CODEC.fieldOf("ritual_dummy").forGetter((r) -> r.ritualDummy),
ItemStack.OPTIONAL_CODEC.fieldOf("result").forGetter((r) -> r.result),
BuiltInRegistries.ENTITY_TYPE.byNameCodec().optionalFieldOf("entity_to_summon").forGetter(r -> Optional.ofNullable(r.entityToSummon)),
TagKey.codec(Registries.ENTITY_TYPE).optionalFieldOf("entity_tag_to_summon").forGetter(r -> Optional.ofNullable(r.entityTagToSummon)),
CompoundTag.CODEC.optionalFieldOf("entity_nbt").forGetter(r -> Optional.ofNullable(r.entityNbt)),
Ingredient.CODEC.fieldOf("activation_item").forGetter((r) -> r.activationItem),
Ingredient.LIST_CODEC.fieldOf("ingredients").forGetter((r) -> r.ingredients),
Expand All @@ -69,7 +71,7 @@ public class RitualRecipe implements Recipe<SingleRecipeInput> {
EntityToSacrifice.CODEC.optionalFieldOf("entity_to_sacrifice").forGetter(r -> Optional.ofNullable(r.entityToSacrifice)),
Ingredient.CODEC.optionalFieldOf("item_to_use").forGetter(r -> Optional.ofNullable(r.itemToUse)),
Codec.STRING.optionalFieldOf("command").forGetter(r -> Optional.ofNullable(r.command))
).apply(instance, (pentacleId, ritualType, ritualDummy, result, entityToSummon, entityNbt, activationItem, ingredients, duration, spiritMaxAge, summonNumber, spiritJobType, entityToSacrifice, itemToUse, command) -> new RitualRecipe(pentacleId, ritualType, ritualDummy, result, entityToSummon.orElse(null), entityNbt.orElse(null), activationItem,
).apply(instance, (pentacleId, ritualType, ritualDummy, result, entityToSummon, entityTagToSummon, entityNbt, activationItem, ingredients, duration, spiritMaxAge, summonNumber, spiritJobType, entityToSacrifice, itemToUse, command) -> new RitualRecipe(pentacleId, ritualType, ritualDummy, result, entityToSummon.orElse(null), entityTagToSummon.orElse(null), entityNbt.orElse(null), activationItem,
NonNullList.copyOf(ingredients), duration, spiritMaxAge, summonNumber, spiritJobType.orElse(null), entityToSacrifice.orElse(null), itemToUse.orElse(Ingredient.EMPTY), command.orElse(null)))
);

Expand All @@ -84,6 +86,8 @@ public class RitualRecipe implements Recipe<SingleRecipeInput> {
(r) -> r.result,
ByteBufCodecs.optional(ByteBufCodecs.registry(Registries.ENTITY_TYPE)),
(r) -> Optional.ofNullable(r.entityToSummon),
ByteBufCodecs.optional(OccultismExtraStreamCodecs.tagKey(Registries.ENTITY_TYPE)),
(r) -> Optional.ofNullable(r.entityTagToSummon),
ByteBufCodecs.optional(ByteBufCodecs.COMPOUND_TAG),
(r) -> Optional.ofNullable(r.entityNbt),
Ingredient.CONTENTS_STREAM_CODEC,
Expand All @@ -104,8 +108,8 @@ public class RitualRecipe implements Recipe<SingleRecipeInput> {
(r) -> Optional.ofNullable(r.itemToUse),
ByteBufCodecs.optional(ByteBufCodecs.STRING_UTF8),
(r) -> Optional.ofNullable(r.command),
(pentacleId, ritualType, ritualDummy, result, entityToSummon, entityNbt, activationItem, ingredients, duration, spiritMaxAge, summonNumber, spiritJobType, entityToSacrifice, itemToUse, command) ->
new RitualRecipe(pentacleId, ritualType, ritualDummy, result, entityToSummon.orElse(null), entityNbt.orElse(null), activationItem,
(pentacleId, ritualType, ritualDummy, result, entityToSummon, entityTagToSummon, entityNbt, activationItem, ingredients, duration, spiritMaxAge, summonNumber, spiritJobType, entityToSacrifice, itemToUse, command) ->
new RitualRecipe(pentacleId, ritualType, ritualDummy, result, entityToSummon.orElse(null), entityTagToSummon.orElse(null), entityNbt.orElse(null), activationItem,
NonNullList.copyOf(ingredients), duration, spiritMaxAge, summonNumber, spiritJobType.orElse(null), entityToSacrifice.orElse(null), itemToUse.orElse(Ingredient.EMPTY), command.orElse(null))
);

Expand All @@ -114,25 +118,34 @@ public class RitualRecipe implements Recipe<SingleRecipeInput> {
final NonNullList<Ingredient> ingredients;
private final ResourceLocation pentacleId;
private final ResourceLocation ritualType;
@Nullable
private final ResourceLocation spiritJobType;
private final Supplier<Ritual> ritual;
private final ItemStack ritualDummy;
private final Ingredient activationItem;
@Nullable
private final EntityToSacrifice entityToSacrifice;
@Nullable
private final EntityType<?> entityToSummon;
@Nullable
private final TagKey<EntityType<?>> entityTagToSummon;
@Nullable
private final CompoundTag entityNbt;
@Nullable
private final Ingredient itemToUse;
private final int duration;
private final int spiritMaxAge;
private final int summonNumber;
private final float durationPerIngredient;
@Nullable
private final String command;

public RitualRecipe(ResourceLocation pentacleId, ResourceLocation ritualType, ItemStack ritualDummy,
ItemStack result, EntityType<?> entityToSummon, CompoundTag entityNbt, Ingredient activationItem, NonNullList<Ingredient> ingredients, int duration, int spiritMaxAge, int summonNumber, ResourceLocation spiritJobType, EntityToSacrifice entityToSacrifice, Ingredient itemToUse, String command) {
ItemStack result, @Nullable EntityType<?> entityToSummon, @Nullable TagKey<EntityType<?>> entityTagToSummon, @Nullable CompoundTag entityNbt, Ingredient activationItem, NonNullList<Ingredient> ingredients, int duration, int spiritMaxAge, int summonNumber, @Nullable ResourceLocation spiritJobType, @Nullable EntityToSacrifice entityToSacrifice, @Nullable Ingredient itemToUse, @Nullable String command) {
this.result = result;
this.ingredients = ingredients;
this.entityToSummon = entityToSummon;
this.entityTagToSummon = entityTagToSummon;
this.entityNbt = entityNbt;
this.pentacleId = pentacleId;
this.ritualType = ritualType;
Expand All @@ -154,11 +167,11 @@ public boolean isSpecial() {
return true;
}

public String getCommand() {
public @Nullable String getCommand() {
return this.command;
}

public CompoundTag getEntityNbt() {
public @Nullable CompoundTag getEntityNbt() {
return this.entityNbt;
}

Expand Down Expand Up @@ -187,20 +200,20 @@ public float getDurationPerIngredient() {
}

@Override
public RecipeSerializer<?> getSerializer() {
public @NotNull RecipeSerializer<?> getSerializer() {
return SERIALIZER;
}

@Override
public boolean matches(SingleRecipeInput pInv, Level pLevel) {
public boolean matches(@NotNull SingleRecipeInput pInv, @NotNull Level pLevel) {
return false;
}


@Override
public ItemStack assemble(SingleRecipeInput pCraftingContainer, HolderLookup.Provider pRegistries) {
public @NotNull ItemStack assemble(@NotNull SingleRecipeInput pCraftingContainer, HolderLookup.@NotNull Provider pRegistries) {
//as we don't have an inventory this is ignored.
return null;
return ItemStack.EMPTY;
}


Expand All @@ -210,12 +223,12 @@ public boolean canCraftInDimensions(int i, int i1) {
}

@Override
public ItemStack getResultItem(HolderLookup.Provider pRegistries) {
public @NotNull ItemStack getResultItem(HolderLookup.@NotNull Provider pRegistries) {
return this.result;
}

@Override
public NonNullList<Ingredient> getIngredients() {
public @NotNull NonNullList<Ingredient> getIngredients() {
return this.ingredients;
}

Expand All @@ -232,7 +245,7 @@ public boolean matches(Level level, BlockPos goldenBowlPosition, ItemStack activ
}

@Override
public RecipeType<?> getType() {
public @NotNull RecipeType<?> getType() {
return OccultismRecipes.RITUAL_TYPE.get();
}

Expand All @@ -244,18 +257,22 @@ public boolean requiresSacrifice() {
return this.entityToSacrifice != null;
}

public Ingredient getItemToUse() {
public @Nullable Ingredient getItemToUse() {
return this.itemToUse;
}

public boolean requiresItemUse() {
return this.itemToUse != Ingredient.EMPTY;
}

public EntityType<?> getEntityToSummon() {
public @Nullable EntityType<?> getEntityToSummon() {
return this.entityToSummon;
}

public @Nullable TagKey<EntityType<?>> getEntityTagToSummon() {
return this.entityTagToSummon;
}

public ResourceLocation getRitualType() {
return this.ritualType;
}
Expand All @@ -268,7 +285,7 @@ public String getEntityToSacrificeDisplayName() {
return this.entityToSacrifice != null ? this.entityToSacrifice.displayName() : "";
}

public ResourceLocation getSpiritJobType() {
public @Nullable ResourceLocation getSpiritJobType() {
return this.spiritJobType;
}

Expand Down Expand Up @@ -298,12 +315,12 @@ public record EntityToSacrifice(TagKey<EntityType<?>> tag, String displayName) {
public static class Serializer implements RecipeSerializer<RitualRecipe> {

@Override
public MapCodec<RitualRecipe> codec() {
public @NotNull MapCodec<RitualRecipe> codec() {
return CODEC;
}

@Override
public StreamCodec<RegistryFriendlyByteBuf, RitualRecipe> streamCodec() {
public @NotNull StreamCodec<RegistryFriendlyByteBuf, RitualRecipe> streamCodec() {
return STREAM_CODEC;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -611,7 +611,7 @@ private static void summonRituals(RecipeOutput recipeOutput) {
makeLoreSpawnEgg(Items.SHEEP_SPAWN_EGG, "item.occultism.ritual_dummy.summon_random_animal"),
makeRitualDummy(ResourceLocation.fromNamespaceAndPath(Occultism.MODID, "ritual_dummy/summon_random_animal")),
30,
OccultismRituals.SUMMON_RANDOM_ANIMAL.getId(),
OccultismRituals.SUMMON.getId(),
PENTACLE_SUMMON_FOLIOT,
Ingredient.of(Tags.Items.CROPS),
Ingredient.of(Tags.Items.CROPS),
Expand All @@ -622,6 +622,7 @@ private static void summonRituals(RecipeOutput recipeOutput) {
Ingredient.of(Tags.Items.CROPS),
Ingredient.of(Tags.Items.CROPS),
Ingredient.of(Items.EGG))
.entityTagToSummon(OccultismTags.Entities.RANDOM_ANIMALS_TO_SUMMON_LIST)
.unlockedBy("has_bound_foliot", has(OccultismItems.BOOK_OF_BINDING_BOUND_FOLIOT.get()))
.save(recipeOutput, ResourceLocation.fromNamespaceAndPath(Occultism.MODID, "ritual/summon_random_animal"));

Expand Down
Loading

0 comments on commit f0b397e

Please sign in to comment.