From c62c4e475bace8957265c2de983475a5ebeb128f Mon Sep 17 00:00:00 2001 From: Sollace Date: Sat, 22 Oct 2022 00:07:30 +0200 Subject: [PATCH] Fixed certain recipes matching when they shouldn't --- .../spell/crafting/SpellCraftingRecipe.java | 37 +++++++++++++++++-- .../magic/spell/effect/ScorchSpell.java | 4 ++ .../client/gui/spellbook/IngredientTree.java | 27 ++++++++------ 3 files changed, 54 insertions(+), 14 deletions(-) diff --git a/src/main/java/com/minelittlepony/unicopia/ability/magic/spell/crafting/SpellCraftingRecipe.java b/src/main/java/com/minelittlepony/unicopia/ability/magic/spell/crafting/SpellCraftingRecipe.java index bf347394..b6d68ed1 100644 --- a/src/main/java/com/minelittlepony/unicopia/ability/magic/spell/crafting/SpellCraftingRecipe.java +++ b/src/main/java/com/minelittlepony/unicopia/ability/magic/spell/crafting/SpellCraftingRecipe.java @@ -1,6 +1,8 @@ package com.minelittlepony.unicopia.ability.magic.spell.crafting; +import java.util.ArrayList; import java.util.List; +import java.util.stream.Collectors; import com.google.gson.JsonObject; import com.minelittlepony.unicopia.ability.magic.spell.effect.SpellType; @@ -8,6 +10,8 @@ import com.minelittlepony.unicopia.ability.magic.spell.trait.SpellTraits; import com.minelittlepony.unicopia.container.inventory.SpellbookInventory; import com.minelittlepony.unicopia.item.GemstoneItem; import com.minelittlepony.unicopia.item.URecipes; +import com.minelittlepony.unicopia.util.InventoryUtil; +import com.mojang.datafixers.util.Pair; import net.minecraft.item.ItemStack; import net.minecraft.network.PacketByteBuf; @@ -66,17 +70,44 @@ public class SpellCraftingRecipe implements SpellbookRecipe { @Override public int getPriority() { - return 0; + return requiredItems.isEmpty() ? 0 : -1; } @Override public boolean matches(SpellbookInventory inventory, World world) { - return material.test(inventory.getItemToModify()) && requiredTraits.test(inventory.getTraits()); + + if (!material.test(inventory.getItemToModify())) { + return false; + } + + if (requiredItems.isEmpty()) { + return requiredTraits.test(inventory.getTraits()); + } + + var outstandingRequirements = new ArrayList<>(requiredItems); + var ingredients = InventoryUtil.slots(inventory) + .filter(slot -> !inventory.getStack(slot).isEmpty()) + .map(slot -> Pair.of(slot, inventory.getStack(slot))) + .collect(Collectors.toList()); + + outstandingRequirements.removeIf(requirement -> { + var found = ingredients.stream().filter(pair -> requirement.test(pair.getSecond())).findAny(); + found.ifPresent(ingredients::remove); + return found.isPresent(); + }); + + if (!outstandingRequirements.isEmpty()) { + return false; + } + + return requiredTraits.test(SpellTraits.union( + ingredients.stream().map(pair -> SpellTraits.of(pair.getSecond()).multiply(pair.getFirst())).toArray(SpellTraits[]::new) + )); } @Override public ItemStack craft(SpellbookInventory inventory) { - return output.copy(); + return getOutput().copy(); } @Override diff --git a/src/main/java/com/minelittlepony/unicopia/ability/magic/spell/effect/ScorchSpell.java b/src/main/java/com/minelittlepony/unicopia/ability/magic/spell/effect/ScorchSpell.java index 5649d089..1d2198a8 100644 --- a/src/main/java/com/minelittlepony/unicopia/ability/magic/spell/effect/ScorchSpell.java +++ b/src/main/java/com/minelittlepony/unicopia/ability/magic/spell/effect/ScorchSpell.java @@ -2,6 +2,7 @@ package com.minelittlepony.unicopia.ability.magic.spell.effect; import com.minelittlepony.unicopia.ability.magic.Caster; import com.minelittlepony.unicopia.ability.magic.spell.Situation; +import com.minelittlepony.unicopia.ability.magic.spell.trait.SpellTraits; import com.minelittlepony.unicopia.ability.magic.spell.trait.Trait; import com.minelittlepony.unicopia.block.state.StateMaps; import com.minelittlepony.unicopia.particle.MagicParticleEffect; @@ -14,6 +15,9 @@ import net.minecraft.util.math.BlockPos; import net.minecraft.util.math.Vec3d; public class ScorchSpell extends FireSpell { + public static final SpellTraits DEFAULT_TRAITS = new SpellTraits.Builder() + .with(Trait.FIRE, 10) + .build(); protected ScorchSpell(CustomisedSpellType type) { super(type); diff --git a/src/main/java/com/minelittlepony/unicopia/client/gui/spellbook/IngredientTree.java b/src/main/java/com/minelittlepony/unicopia/client/gui/spellbook/IngredientTree.java index 4ad93049..5da3bd93 100644 --- a/src/main/java/com/minelittlepony/unicopia/client/gui/spellbook/IngredientTree.java +++ b/src/main/java/com/minelittlepony/unicopia/client/gui/spellbook/IngredientTree.java @@ -102,17 +102,7 @@ class IngredientTree implements SpellbookRecipe.CraftingTreeBuilder { int left = x + column * colWidth + 3 + (addLabels && row > 0 ? colWidth : 0); int top = y + row * rowHeight + 3; - var button = container.addButton(new IngredientButton(left, top, colWidth, rowHeight, entry, !addLabels || ii == 0 ? "" : "+", false)); - - if (entry instanceof Traits traits) { - final Trait trait = traits.trait; - button.onClick(sender -> { - if (MinecraftClient.getInstance().currentScreen instanceof SpellbookScreen spellbook) { - spellbook.getState().setCurrentPageId(SpellbookChapterList.TRAIT_DEX_ID); - spellbook.getTraitDex().pageTo(spellbook, trait); - } - }); - } + container.addButton(new IngredientButton(left, top, colWidth, rowHeight, entry, !addLabels || ii == 0 ? "" : "+", false)).onClick(sender -> entry.onClick()); ii++; } @@ -173,6 +163,8 @@ class IngredientTree implements SpellbookRecipe.CraftingTreeBuilder { void render(MatrixStack matrices, int mouseX, int mouseY, float tickDelta); Tooltip getTooltip(); + + void onClick(); } class Stacks implements IngredientTree.Entry { @@ -212,6 +204,11 @@ class IngredientTree implements SpellbookRecipe.CraftingTreeBuilder { return stacks[index].getTooltip(MinecraftClient.getInstance().player, TooltipContext.Default.NORMAL); }; } + + @Override + public void onClick() { + + } } class HiddenStacks extends Stacks { @@ -276,5 +273,13 @@ class IngredientTree implements SpellbookRecipe.CraftingTreeBuilder { public Tooltip getTooltip() { return Tooltip.of(ItemTraitsTooltipRenderer.isKnown(trait) ? trait.getTooltip() : trait.getObfuscatedTooltip(), 200); } + + @Override + public void onClick() { + if (MinecraftClient.getInstance().currentScreen instanceof SpellbookScreen spellbook) { + spellbook.getState().setCurrentPageId(SpellbookChapterList.TRAIT_DEX_ID); + spellbook.getTraitDex().pageTo(spellbook, trait); + } + } } } \ No newline at end of file