From 22f28c098ce3f53e750ea31f0d035da095d649b7 Mon Sep 17 00:00:00 2001 From: Sollace Date: Mon, 12 Sep 2022 19:53:02 +0200 Subject: [PATCH] Fix duplication bug when equipping/unequipping spells --- .../unicopia/entity/player/PlayerCharmTracker.java | 2 +- .../minelittlepony/unicopia/item/GemstoneItem.java | 14 ++++++++++++-- 2 files changed, 13 insertions(+), 3 deletions(-) diff --git a/src/main/java/com/minelittlepony/unicopia/entity/player/PlayerCharmTracker.java b/src/main/java/com/minelittlepony/unicopia/entity/player/PlayerCharmTracker.java index c553dc7d..5f564c15 100644 --- a/src/main/java/com/minelittlepony/unicopia/entity/player/PlayerCharmTracker.java +++ b/src/main/java/com/minelittlepony/unicopia/entity/player/PlayerCharmTracker.java @@ -55,7 +55,7 @@ public class PlayerCharmTracker implements Tickable, NbtSerialisable { } public CustomisedSpellType getEquippedSpell(Hand hand) { - return handSpells[hand.ordinal()]; + return handSpells[hand.ordinal()] == null ? SpellType.EMPTY_KEY.withTraits() : handSpells[hand.ordinal()]; } public TypedActionResult> getSpellInHand(Hand hand) { diff --git a/src/main/java/com/minelittlepony/unicopia/item/GemstoneItem.java b/src/main/java/com/minelittlepony/unicopia/item/GemstoneItem.java index 6c59f578..3fef3ea8 100644 --- a/src/main/java/com/minelittlepony/unicopia/item/GemstoneItem.java +++ b/src/main/java/com/minelittlepony/unicopia/item/GemstoneItem.java @@ -47,13 +47,23 @@ public class GemstoneItem extends Item { CustomisedSpellType existing = charms.getEquippedSpell(hand); - if (existing != null) { - stack = existing.traits().applyTo(enchant(stack, existing.type())); + if (!existing.isEmpty()) { + + if (stack.getCount() == 1) { + stack = existing.traits().applyTo(enchant(stack, existing.type())); + } else { + user.giveItemStack(existing.traits().applyTo(enchant(stack.split(1), existing.type()))); + } } if (spell.getResult().isAccepted()) { charms.equipSpell(hand, spell.getValue()); } else { + + if (existing.isEmpty()) { + return result; + } + charms.equipSpell(hand, SpellType.EMPTY_KEY.withTraits()); } return TypedActionResult.success(stack, true);