From bc42a692acbab87ff6772baaebb7521fcf6a4ba4 Mon Sep 17 00:00:00 2001 From: stal111 Date: Sun, 27 Oct 2024 12:39:07 +0100 Subject: [PATCH] Refactor ApplyModifierRecipe.java --- .../HephaestusForgeCategory.java | 4 +- .../item/crafting/ApplyModifierRecipe.java | 63 ++++++------------- .../common/item/modifier/ItemModifier.java | 2 +- 3 files changed, 22 insertions(+), 47 deletions(-) diff --git a/neoforge/src/main/java/com/stal111/forbidden_arcanus/common/integration/hephaestus_forge/HephaestusForgeCategory.java b/neoforge/src/main/java/com/stal111/forbidden_arcanus/common/integration/hephaestus_forge/HephaestusForgeCategory.java index 00a9dcfd..742314d7 100644 --- a/neoforge/src/main/java/com/stal111/forbidden_arcanus/common/integration/hephaestus_forge/HephaestusForgeCategory.java +++ b/neoforge/src/main/java/com/stal111/forbidden_arcanus/common/integration/hephaestus_forge/HephaestusForgeCategory.java @@ -82,9 +82,7 @@ public void setRecipe(@NotNull IRecipeLayoutBuilder builder, @NotNull Ritual rit this.addInputs(builder, ritual.inputs(), ritual.mainIngredient()); if (this.displayEnhancers()) { - ritual.requirements().enhancers().ifPresent(holders -> { - this.addEnhancers(builder, holders); - }); + this.addEnhancers(builder, ritual.requirements().enhancers()); } this.buildRecipe(builder, ritual.requirements(), (T) ritual.result()); diff --git a/neoforge/src/main/java/com/stal111/forbidden_arcanus/common/item/crafting/ApplyModifierRecipe.java b/neoforge/src/main/java/com/stal111/forbidden_arcanus/common/item/crafting/ApplyModifierRecipe.java index 8b7dfd56..834f6916 100644 --- a/neoforge/src/main/java/com/stal111/forbidden_arcanus/common/item/crafting/ApplyModifierRecipe.java +++ b/neoforge/src/main/java/com/stal111/forbidden_arcanus/common/item/crafting/ApplyModifierRecipe.java @@ -17,31 +17,21 @@ import net.minecraft.world.level.Level; import org.jetbrains.annotations.NotNull; -/** - * Apply Modifier Recipe
- * Forbidden Arcanus - com.stal111.forbidden_arcanus.common.item.crafting.ApplyModifierRecipe - * - * @author stal111 - * @since 2021-11-29 - */ +import java.util.stream.Stream; + public record ApplyModifierRecipe(Ingredient template, Ingredient addition, Holder modifier) implements SmithingRecipe { @Override public boolean matches(@NotNull SmithingRecipeInput recipeInput, @NotNull Level level) { - ItemStack base = recipeInput.base(); - - if (ModifierHelper.hasModifier(base) || !this.isTemplateIngredient(recipeInput.template())) { - return false; - } - return this.modifier.value().isValidItem(base) && this.isAdditionIngredient(recipeInput.addition()); + return this.isTemplateIngredient(recipeInput.template()) && this.isAdditionIngredient(recipeInput.addition()) && this.isBaseIngredient(recipeInput.base()); } @NotNull @Override public ItemStack assemble(@NotNull SmithingRecipeInput recipeInput, @NotNull HolderLookup.Provider provider) { - ItemStack stack = recipeInput.base().copy(); + ItemStack stack = recipeInput.base().copyWithCount(1); ModifierHelper.setModifier(stack, this.modifier); @@ -60,10 +50,6 @@ public RecipeSerializer getSerializer() { return ModRecipeSerializers.APPLY_MODIFIER.get(); } - @Override - public boolean isSpecial() { - return true; - } @Override public boolean isTemplateIngredient(@NotNull ItemStack stack) { @@ -80,22 +66,27 @@ public boolean isAdditionIngredient(@NotNull ItemStack stack) { return this.addition.test(stack); } + @Override + public boolean isIncomplete() { + return Stream.of(this.template, this.addition).anyMatch(Ingredient::hasNoItems); + } + public static class Serializer implements RecipeSerializer { private static final MapCodec CODEC = RecordCodecBuilder.mapCodec(instance -> instance.group( - Ingredient.CODEC_NONEMPTY.fieldOf("template").forGetter(recipe -> { - return recipe.template; - }), - Ingredient.CODEC_NONEMPTY.fieldOf("addition").forGetter(recipe -> { - return recipe.addition; - }), - ItemModifier.CODEC.fieldOf("modifier").forGetter(recipe -> { - return recipe.modifier; - }) + Ingredient.CODEC_NONEMPTY.fieldOf("template").forGetter(ApplyModifierRecipe::template), + Ingredient.CODEC_NONEMPTY.fieldOf("addition").forGetter(ApplyModifierRecipe::addition), + ItemModifier.CODEC.fieldOf("modifier").forGetter(ApplyModifierRecipe::modifier) ).apply(instance, ApplyModifierRecipe::new)); - public static final StreamCodec STREAM_CODEC = StreamCodec.of( - ApplyModifierRecipe.Serializer::toNetwork, ApplyModifierRecipe.Serializer::fromNetwork + public static final StreamCodec STREAM_CODEC = StreamCodec.composite( + Ingredient.CONTENTS_STREAM_CODEC, + ApplyModifierRecipe::template, + Ingredient.CONTENTS_STREAM_CODEC, + ApplyModifierRecipe::addition, + ItemModifier.STREAM_CODEC, + ApplyModifierRecipe::modifier, + ApplyModifierRecipe::new ); @Override @@ -107,19 +98,5 @@ public static class Serializer implements RecipeSerializer public @NotNull StreamCodec streamCodec() { return STREAM_CODEC; } - - public static ApplyModifierRecipe fromNetwork(@NotNull RegistryFriendlyByteBuf buffer) { - Ingredient template = Ingredient.CONTENTS_STREAM_CODEC.decode(buffer); - Ingredient addition = Ingredient.CONTENTS_STREAM_CODEC.decode(buffer); - Holder modifier = ItemModifier.STREAM_CODEC.decode(buffer); - - return new ApplyModifierRecipe(template, addition, modifier); - } - - public static void toNetwork(@NotNull RegistryFriendlyByteBuf buffer, ApplyModifierRecipe recipe) { - Ingredient.CONTENTS_STREAM_CODEC.encode(buffer, recipe.template); - Ingredient.CONTENTS_STREAM_CODEC.encode(buffer, recipe.addition); - ItemModifier.STREAM_CODEC.encode(buffer, recipe.modifier); - } } } diff --git a/neoforge/src/main/java/com/stal111/forbidden_arcanus/common/item/modifier/ItemModifier.java b/neoforge/src/main/java/com/stal111/forbidden_arcanus/common/item/modifier/ItemModifier.java index 298b774b..f713b989 100644 --- a/neoforge/src/main/java/com/stal111/forbidden_arcanus/common/item/modifier/ItemModifier.java +++ b/neoforge/src/main/java/com/stal111/forbidden_arcanus/common/item/modifier/ItemModifier.java @@ -59,7 +59,7 @@ public void onApplied(ItemStack stack) { } public boolean isValidItem(ItemStack stack) { - if (stack.is(this.incompatibleItems) || !this.predicate.test(stack)) { + if (stack.is(this.incompatibleItems) || !this.predicate.test(stack) || ModifierHelper.hasModifier(stack)) { return false; }