Skip to content

Commit

Permalink
feat: make ritual recipe indenpendent of shapeless recipe
Browse files Browse the repository at this point in the history
Closes #156
  • Loading branch information
klikli-dev committed Dec 23, 2023
1 parent 6206dec commit 058807d
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 17 deletions.
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,6 @@ almost_unified_version=0.5.0
almost_unified_version_range=[0.5.0,)
modonomicon_version=1.38.5
modonomicon_version_range=[1.38.1,)
theurgy_version=1.6.4
theurgy_version=1.7.1
theurgy_version_range=[1.6.3,)
per_viam_invenire_version_range=[0.1.57,)
Original file line number Diff line number Diff line change
Expand Up @@ -40,17 +40,18 @@
import net.minecraft.resources.ResourceLocation;
import net.minecraft.tags.TagKey;
import net.minecraft.util.GsonHelper;
import net.minecraft.world.Container;
import net.minecraft.world.entity.EntityType;
import net.minecraft.world.inventory.CraftingContainer;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.item.crafting.*;
import net.minecraft.world.level.Level;
import net.minecraftforge.common.crafting.CraftingHelper;
import net.minecraftforge.registries.ForgeRegistries;

import java.util.Iterator;
import java.util.function.Supplier;

public class RitualRecipe extends ShapelessRecipe {
public class RitualRecipe implements Recipe<Container> {
public static Serializer SERIALIZER = new Serializer();

private final ResourceLocation pentacleId;
Expand All @@ -70,10 +71,17 @@ public class RitualRecipe extends ShapelessRecipe {
private final String entityToSacrificeDisplayName;
private final String command;

public RitualRecipe(ResourceLocation id, String group, ResourceLocation pentacleId, ResourceLocation ritualType, ItemStack ritualDummy,
ItemStack result, EntityType<?> entityToSummon, CompoundTag entityNbt, Ingredient activationItem, NonNullList<Ingredient> input, int duration, int spiritMaxAge, ResourceLocation spiritJobType,
private final ResourceLocation id;

final ItemStack result;
final NonNullList<Ingredient> ingredients;

public RitualRecipe(ResourceLocation id, ResourceLocation pentacleId, ResourceLocation ritualType, ItemStack ritualDummy,
ItemStack result, EntityType<?> entityToSummon, CompoundTag entityNbt, Ingredient activationItem, NonNullList<Ingredient> ingredients, int duration, int spiritMaxAge, ResourceLocation spiritJobType,
TagKey<EntityType<?>> entityToSacrifice, String entityToSacrificeDisplayName, Ingredient itemToUse, String command) {
super(id, group, CraftingBookCategory.MISC, result, input);
this.id = id;
this.result = result;
this.ingredients = ingredients;
this.entityToSummon = entityToSummon;
this.entityNbt = entityNbt;
this.pentacleId = pentacleId;
Expand Down Expand Up @@ -134,16 +142,36 @@ public RecipeSerializer<?> getSerializer() {
}

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

@Override
public ItemStack assemble(CraftingContainer pInv, RegistryAccess access) {
public ItemStack assemble(Container pInv, RegistryAccess access) {
//as we don't have an inventory this is ignored.
return null;
}

@Override
public ResourceLocation getId() {
return this.id;
}

@Override
public boolean canCraftInDimensions(int i, int i1) {
return true;
}

@Override
public ItemStack getResultItem(RegistryAccess registryAccess) {
return this.result;
}

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

/**
* Custom matches method for ritual recipes
*
Expand Down Expand Up @@ -219,9 +247,6 @@ private static NonNullList<Ingredient> itemsFromJson(JsonArray pIngredientArray)

@Override
public RitualRecipe fromJson(ResourceLocation recipeId, JsonObject json) {

//do not use shapeless serializer here, because it limits max ingredients
String group = GsonHelper.getAsString(json, "group", "");
NonNullList<Ingredient> ingredients = itemsFromJson(GsonHelper.getAsJsonArray(json, "ingredients"));
if (ingredients.isEmpty()) {
throw new JsonParseException("No ingredients for shapeless recipe");
Expand Down Expand Up @@ -277,14 +302,21 @@ public RitualRecipe fromJson(ResourceLocation recipeId, JsonObject json) {

var command = GsonHelper.getAsString(json, "command", null);

return new RitualRecipe(recipeId, group, pentacleId, ritualType, ritualDummy,
return new RitualRecipe(recipeId, pentacleId, ritualType, ritualDummy,
result, entityToSummon, entityNbt, activationItem, ingredients, duration,
spiritMaxAge, spiritJobType, entityToSacrifice, entityToSacrificeDisplayName, itemToUse, command);
}

@Override
public RitualRecipe fromNetwork(ResourceLocation recipeId, FriendlyByteBuf buffer) {
ShapelessRecipe recipe = serializer.fromNetwork(recipeId, buffer);
int ingredientCount = buffer.readVarInt();
NonNullList<Ingredient> ingredients = NonNullList.withSize(ingredientCount, Ingredient.EMPTY);

for(int j = 0; j < ingredients.size(); ++j) {
ingredients.set(j, Ingredient.fromNetwork(buffer));
}

ItemStack result = buffer.readItem();

ResourceLocation ritualType = buffer.readResourceLocation();

Expand Down Expand Up @@ -325,14 +357,19 @@ public RitualRecipe fromNetwork(ResourceLocation recipeId, FriendlyByteBuf buffe

String command = buffer.readBoolean() ? buffer.readUtf() : null;

//we can pass null here because shapeless recipe does not use the registryacess
return new RitualRecipe(recipe.getId(), recipe.getGroup(), pentacleId, ritualType, ritualDummy, recipe.getResultItem(null), entityToSummon, entityNbt,
activationItem, recipe.getIngredients(), duration, spiritMaxAge, spiritJobType, entityToSacrifice, entityToSacrificeDisplayName, itemToUse, command);
return new RitualRecipe(recipeId, pentacleId, ritualType, ritualDummy, result, entityToSummon, entityNbt,
activationItem, ingredients, duration, spiritMaxAge, spiritJobType, entityToSacrifice, entityToSacrificeDisplayName, itemToUse, command);
}

@Override
public void toNetwork(FriendlyByteBuf buffer, RitualRecipe recipe) {
serializer.toNetwork(buffer, recipe);
buffer.writeVarInt(recipe.ingredients.size());

for(var ingredient : recipe.ingredients) {
ingredient.toNetwork(buffer);
}

buffer.writeItem(recipe.result);

buffer.writeResourceLocation(recipe.ritualType);

Expand Down

0 comments on commit 058807d

Please sign in to comment.