Skip to content

Commit

Permalink
Refactor ApplyModifierRecipe.java
Browse files Browse the repository at this point in the history
  • Loading branch information
stal111 committed Oct 27, 2024
1 parent cada4e4 commit bc42a69
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 47 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,31 +17,21 @@
import net.minecraft.world.level.Level;
import org.jetbrains.annotations.NotNull;

/**
* Apply Modifier Recipe <br>
* 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<ItemModifier> 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);

Expand All @@ -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) {
Expand All @@ -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<ApplyModifierRecipe> {

private static final MapCodec<ApplyModifierRecipe> 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<RegistryFriendlyByteBuf, ApplyModifierRecipe> STREAM_CODEC = StreamCodec.of(
ApplyModifierRecipe.Serializer::toNetwork, ApplyModifierRecipe.Serializer::fromNetwork
public static final StreamCodec<RegistryFriendlyByteBuf, ApplyModifierRecipe> STREAM_CODEC = StreamCodec.composite(
Ingredient.CONTENTS_STREAM_CODEC,
ApplyModifierRecipe::template,
Ingredient.CONTENTS_STREAM_CODEC,
ApplyModifierRecipe::addition,
ItemModifier.STREAM_CODEC,
ApplyModifierRecipe::modifier,
ApplyModifierRecipe::new
);

@Override
Expand All @@ -107,19 +98,5 @@ public static class Serializer implements RecipeSerializer<ApplyModifierRecipe>
public @NotNull StreamCodec<RegistryFriendlyByteBuf, ApplyModifierRecipe> 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<ItemModifier> 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);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down

0 comments on commit bc42a69

Please sign in to comment.