Fixed certain recipes matching when they shouldn't

This commit is contained in:
Sollace 2022-10-22 00:07:30 +02:00
parent fce836bf3b
commit c62c4e475b
3 changed files with 54 additions and 14 deletions

View file

@ -1,6 +1,8 @@
package com.minelittlepony.unicopia.ability.magic.spell.crafting; package com.minelittlepony.unicopia.ability.magic.spell.crafting;
import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.stream.Collectors;
import com.google.gson.JsonObject; import com.google.gson.JsonObject;
import com.minelittlepony.unicopia.ability.magic.spell.effect.SpellType; 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.container.inventory.SpellbookInventory;
import com.minelittlepony.unicopia.item.GemstoneItem; import com.minelittlepony.unicopia.item.GemstoneItem;
import com.minelittlepony.unicopia.item.URecipes; 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.item.ItemStack;
import net.minecraft.network.PacketByteBuf; import net.minecraft.network.PacketByteBuf;
@ -66,17 +70,44 @@ public class SpellCraftingRecipe implements SpellbookRecipe {
@Override @Override
public int getPriority() { public int getPriority() {
return 0; return requiredItems.isEmpty() ? 0 : -1;
} }
@Override @Override
public boolean matches(SpellbookInventory inventory, World world) { 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 @Override
public ItemStack craft(SpellbookInventory inventory) { public ItemStack craft(SpellbookInventory inventory) {
return output.copy(); return getOutput().copy();
} }
@Override @Override

View file

@ -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.Caster;
import com.minelittlepony.unicopia.ability.magic.spell.Situation; 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.ability.magic.spell.trait.Trait;
import com.minelittlepony.unicopia.block.state.StateMaps; import com.minelittlepony.unicopia.block.state.StateMaps;
import com.minelittlepony.unicopia.particle.MagicParticleEffect; import com.minelittlepony.unicopia.particle.MagicParticleEffect;
@ -14,6 +15,9 @@ import net.minecraft.util.math.BlockPos;
import net.minecraft.util.math.Vec3d; import net.minecraft.util.math.Vec3d;
public class ScorchSpell extends FireSpell { public class ScorchSpell extends FireSpell {
public static final SpellTraits DEFAULT_TRAITS = new SpellTraits.Builder()
.with(Trait.FIRE, 10)
.build();
protected ScorchSpell(CustomisedSpellType<?> type) { protected ScorchSpell(CustomisedSpellType<?> type) {
super(type); super(type);

View file

@ -102,17 +102,7 @@ class IngredientTree implements SpellbookRecipe.CraftingTreeBuilder {
int left = x + column * colWidth + 3 + (addLabels && row > 0 ? colWidth : 0); int left = x + column * colWidth + 3 + (addLabels && row > 0 ? colWidth : 0);
int top = y + row * rowHeight + 3; int top = y + row * rowHeight + 3;
var button = container.addButton(new IngredientButton(left, top, colWidth, rowHeight, entry, !addLabels || ii == 0 ? "" : "+", false)); container.addButton(new IngredientButton(left, top, colWidth, rowHeight, entry, !addLabels || ii == 0 ? "" : "+", false)).onClick(sender -> entry.onClick());
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);
}
});
}
ii++; ii++;
} }
@ -173,6 +163,8 @@ class IngredientTree implements SpellbookRecipe.CraftingTreeBuilder {
void render(MatrixStack matrices, int mouseX, int mouseY, float tickDelta); void render(MatrixStack matrices, int mouseX, int mouseY, float tickDelta);
Tooltip getTooltip(); Tooltip getTooltip();
void onClick();
} }
class Stacks implements IngredientTree.Entry { class Stacks implements IngredientTree.Entry {
@ -212,6 +204,11 @@ class IngredientTree implements SpellbookRecipe.CraftingTreeBuilder {
return stacks[index].getTooltip(MinecraftClient.getInstance().player, TooltipContext.Default.NORMAL); return stacks[index].getTooltip(MinecraftClient.getInstance().player, TooltipContext.Default.NORMAL);
}; };
} }
@Override
public void onClick() {
}
} }
class HiddenStacks extends Stacks { class HiddenStacks extends Stacks {
@ -276,5 +273,13 @@ class IngredientTree implements SpellbookRecipe.CraftingTreeBuilder {
public Tooltip getTooltip() { public Tooltip getTooltip() {
return Tooltip.of(ItemTraitsTooltipRenderer.isKnown(trait) ? trait.getTooltip() : trait.getObfuscatedTooltip(), 200); 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);
}
}
} }
} }