Skip to content

Commit

Permalink
fix: 🐛 Fixed tome charging recipe to properly check that tome doesn't…
Browse files Browse the repository at this point in the history
… get overcharged when using multiple charging ingredients
  • Loading branch information
P3pp3rF1y committed Oct 1, 2024
1 parent b398e67 commit a2993b4
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 8 deletions.
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ org.gradle.daemon=false

mod_id=reliquary
mod_group_id=reliquary
mod_version=2.0.43
mod_version=2.0.44
sonar_project_key=xreliquary:Reliquary
github_package_url=https://maven.pkg.github.com/P3pp3rF1y/Reliquary

Expand Down
34 changes: 27 additions & 7 deletions src/main/java/reliquary/crafting/AlkahestryChargingRecipe.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import reliquary.items.AlkahestryTomeItem;

import javax.annotation.Nullable;
import java.util.stream.Stream;

public class AlkahestryChargingRecipe implements CraftingRecipe {
private final Ingredient chargingIngredient;
Expand All @@ -31,7 +32,7 @@ private AlkahestryChargingRecipe(ResourceLocation id, Ingredient chargingIngredi
this.id = id;
this.chargingIngredient = chargingIngredient;
this.chargeToAdd = chargeToAdd;
tomeIngredient = Ingredient.of(AlkahestryTomeItem.setCharge(new ItemStack(ModItems.ALKAHESTRY_TOME.get()), 0));
tomeIngredient = new TomeIngredient(chargeToAdd);

recipeOutput = new ItemStack(ModItems.ALKAHESTRY_TOME.get());
AlkahestryTomeItem.addCharge(recipeOutput, chargeToAdd);
Expand All @@ -41,8 +42,8 @@ private AlkahestryChargingRecipe(ResourceLocation id, Ingredient chargingIngredi

@Override
public boolean matches(CraftingContainer inv, Level worldIn) {
boolean hasTome = false;
boolean hasIngredient = false;
ItemStack tome = ItemStack.EMPTY;
int numberOfIngredients = 0;

for (int x = 0; x < inv.getContainerSize(); x++) {
ItemStack slotStack = inv.getItem(x);
Expand All @@ -51,10 +52,10 @@ public boolean matches(CraftingContainer inv, Level worldIn) {
boolean inRecipe = false;
if (chargingIngredient.test(slotStack)) {
inRecipe = true;
hasIngredient = true;
} else if (!hasTome && slotStack.getItem() == ModItems.ALKAHESTRY_TOME.get() && AlkahestryTomeItem.getCharge(slotStack) + chargeToAdd <= AlkahestryTomeItem.getChargeLimit()) {
numberOfIngredients++;
} else if (tome.isEmpty()) {
inRecipe = true;
hasTome = true;
tome = slotStack;
}

if (!inRecipe) {
Expand All @@ -63,7 +64,7 @@ public boolean matches(CraftingContainer inv, Level worldIn) {
}
}

return hasIngredient && hasTome;
return numberOfIngredients > 0 && tome.is(ModItems.ALKAHESTRY_TOME.get()) && AlkahestryTomeItem.getCharge(tome) + chargeToAdd * numberOfIngredients <= AlkahestryTomeItem.getChargeLimit();
}

@Override
Expand Down Expand Up @@ -157,4 +158,23 @@ public void toNetwork(FriendlyByteBuf buffer, AlkahestryChargingRecipe recipe) {
buffer.writeInt(recipe.chargeToAdd);
}
}

private static class TomeIngredient extends Ingredient {
private final int chargeToAdd;

private TomeIngredient(int chargeToAdd) {
super(Stream.of(new Ingredient.ItemValue(new ItemStack(ModItems.ALKAHESTRY_TOME.get()))));
this.chargeToAdd = chargeToAdd;
}

@Override
public boolean test(ItemStack stack) {
return stack.is(ModItems.ALKAHESTRY_TOME.get()) && AlkahestryTomeItem.getCharge(stack) + chargeToAdd <= AlkahestryTomeItem.getChargeLimit();
}

@Override
public boolean isSimple() {
return false;
}
}
}

0 comments on commit a2993b4

Please sign in to comment.