Skip to content

Commit

Permalink
feat: add component type and wrapper for entity to sacrifice
Browse files Browse the repository at this point in the history
the component builder seems not to work the way we want

This may fix EnigmaticaModpacks/Enigmatica10#90

Closes #5 because the server run now has an example for the entity to sacrifice
  • Loading branch information
klikli-dev committed Sep 3, 2024
1 parent 88ba5ca commit a6e37fa
Show file tree
Hide file tree
Showing 6 changed files with 84 additions and 7 deletions.
4 changes: 3 additions & 1 deletion runs/client/kubejs/server_scripts/example.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,5 +27,7 @@ ServerEvents.recipes((event) => {
],
'#c:stones',
'occultism:craft_afrit'
).dummy("kubejs:dummy_ritual_thing").useItem('minecraft:egg')
).dummy("kubejs:dummy_ritual_thing")
.useItem('minecraft:egg')
.entityToSacrifice(EntityToSacrifice.of("minecraft:cows", "Cows"))//it would be better to use a translation key instead of "Cows" to allow translating to other languages. E.g. "tags.entities.cows" -> but that also needs a corresponding translation in en_us.json and other lang files.
})
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package com.klikli_dev.occultism_kubejs;

import com.google.gson.JsonObject;
import com.klikli_dev.occultism.crafting.recipe.RitualRecipe;
import com.mojang.serialization.Codec;
import com.mojang.serialization.JsonOps;
import dev.latvian.mods.kubejs.recipe.KubeRecipe;
import dev.latvian.mods.kubejs.recipe.component.RecipeComponent;
import dev.latvian.mods.rhino.Context;
import dev.latvian.mods.rhino.type.TypeInfo;

public record EntityToSacrificeComponent(String name, Codec<RitualRecipe.EntityToSacrifice> codec) implements RecipeComponent<RitualRecipe.EntityToSacrifice> {
public static final RecipeComponent<RitualRecipe.EntityToSacrifice> ENTITY_TO_SACRIFICE = new EntityToSacrificeComponent("occultism:entity_to_sacrifice", RitualRecipe.EntityToSacrifice.CODEC);

public static final TypeInfo TYPE_INFO = TypeInfo.of(EntityToSacrificeComponent.class);

@Override
public TypeInfo typeInfo() {
return TYPE_INFO;
}

@Override
public String toString() {
return this.name;
}

@Override
public RitualRecipe.EntityToSacrifice wrap(Context cx, KubeRecipe recipe, Object from) {
if (from instanceof RitualRecipe.EntityToSacrifice k) {
return k;
}

if (from instanceof JsonObject json) {
return this.codec.decode(JsonOps.INSTANCE, json).result().orElseThrow().getFirst();
}

return (RitualRecipe.EntityToSacrifice) cx.jsToJava(from, this.typeInfo());

}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package com.klikli_dev.occultism_kubejs;

import com.klikli_dev.occultism.crafting.recipe.RitualRecipe;
import com.klikli_dev.occultism.crafting.recipe.result.RecipeResult;
import com.mojang.brigadier.StringReader;
import com.mojang.brigadier.exceptions.CommandSyntaxException;
import dev.latvian.mods.kubejs.KubeJS;
import dev.latvian.mods.kubejs.component.DataComponentWrapper;
import dev.latvian.mods.kubejs.item.ItemStackJS;
import dev.latvian.mods.kubejs.typings.Info;
import dev.latvian.mods.kubejs.util.RegistryAccessContainer;
import dev.latvian.mods.rhino.Wrapper;
import net.minecraft.core.component.DataComponentPredicate;
import net.minecraft.core.registries.BuiltInRegistries;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.tags.ItemTags;
import net.minecraft.tags.TagKey;
import net.minecraft.world.entity.EntityType;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.item.Items;
import net.minecraft.world.item.crafting.Ingredient;
import org.jetbrains.annotations.Nullable;

@Info("Various entity to sacrifice related helper methods")
public interface EntityToSacrificeWrapper {

@Info("Returns an RitualRecipe.EntityToSacrifice of the input")
static RitualRecipe.EntityToSacrifice of(RitualRecipe.EntityToSacrifice in) {
return in;
}

@Info("Returns an RitualRecipe.EntityToSacrifice of the input")
static RitualRecipe.EntityToSacrifice of(TagKey<EntityType<?>> tag, String displayName) {
return new RitualRecipe.EntityToSacrifice(tag, displayName);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -55,12 +55,14 @@ public void registerRecipeSchemas(RecipeSchemaRegistry registry) {
public void registerRecipeComponents(RecipeComponentFactoryRegistry registry) {
registry.register(RecipeResultComponent.RECIPE_RESULT);
registry.register(WeightedRecipeResultComponent.WEIGHTED_RECIPE_RESULT);
registry.register(EntityToSacrificeComponent.ENTITY_TO_SACRIFICE);
}

@Override
public void registerBindings(BindingRegistry bindings) {
bindings.add("RecipeResult", RecipeResultWrapper.class);
bindings.add("WeightedRecipeResult", WeightedRecipeResultWrapper.class);
bindings.add("EntityToSacrifice", EntityToSacrificeWrapper.class);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.klikli_dev.occultism_kubejs;

import com.klikli_dev.occultism.crafting.recipe.RitualRecipe;
import dev.latvian.mods.kubejs.recipe.RecipeKey;
import dev.latvian.mods.kubejs.recipe.component.*;
import dev.latvian.mods.kubejs.recipe.schema.RecipeSchema;
Expand Down Expand Up @@ -30,11 +31,7 @@ public interface RitualRecipeSchema {
.optional(new ItemStack(BuiltInRegistries.ITEM.get(ResourceLocation.parse("occultism:ritual_dummy/custom_ritual"))))
.alwaysWrite();

RecipeKey<?> ENTITY_TO_SACRIFICE =
RecipeComponent.builder(
new RecipeComponentBuilder.Key("tag", TagKeyComponent.ENTITY_TYPE),
new RecipeComponentBuilder.Key("display_name", StringComponent.ANY)
)
RecipeKey<RitualRecipe.EntityToSacrifice> ENTITY_TO_SACRIFICE = EntityToSacrificeComponent.ENTITY_TO_SACRIFICE
.inputKey("entity_to_sacrifice").alt("entityToSacrifice").alt("sacrifice")
.defaultOptional();
RecipeKey<Ingredient> ITEM_TO_USE = IngredientComponent.INGREDIENT.inputKey("item_to_use")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@
{
"name": "entity_to_sacrifice",
"role": "input",
"type": "{tag: entity_type_tag, display_name: string}",
"type": "occultism:entity_to_sacrifice",
"optional": null,
"alternative_names": [
"entityToSacrifice",
Expand Down

0 comments on commit a6e37fa

Please sign in to comment.