From b2763a558631dd158af9c1138530d08b5c0a92bb Mon Sep 17 00:00:00 2001 From: Avi Weinstock Date: Thu, 7 Jul 2022 07:45:17 +0000 Subject: [PATCH] Equipping an item into an occupied equipment slot now places the swapped item into the inventory slot of the item being equipped --- .../java/core/game/container/Container.java | 8 ++++---- .../container/impl/EquipmentContainer.java | 10 +++++----- Server/src/test/kotlin/content/EquipTests.kt | 19 +++++++++++++++++-- 3 files changed, 26 insertions(+), 11 deletions(-) diff --git a/Server/src/main/java/core/game/container/Container.java b/Server/src/main/java/core/game/container/Container.java index 237993a61..e3ac24b61 100644 --- a/Server/src/main/java/core/game/container/Container.java +++ b/Server/src/main/java/core/game/container/Container.java @@ -221,9 +221,9 @@ public class Container { if (maximum == 0) { return false; } -// if (preferredSlot > -1 && items[preferredSlot] != null) { -// preferredSlot = -1; -// } + if (preferredSlot > -1 && items[preferredSlot] != null) { + preferredSlot = -1; + } if (item.getAmount() > maximum) { item.setAmount(maximum); } @@ -1036,4 +1036,4 @@ public class Container { ", listeners=" + listeners + '}'; } -} \ No newline at end of file +} diff --git a/Server/src/main/java/core/game/container/impl/EquipmentContainer.java b/Server/src/main/java/core/game/container/impl/EquipmentContainer.java index eda3f9044..cc6fa868b 100644 --- a/Server/src/main/java/core/game/container/impl/EquipmentContainer.java +++ b/Server/src/main/java/core/game/container/impl/EquipmentContainer.java @@ -71,7 +71,7 @@ public final class EquipmentContainer extends Container { /** * Adds an item to the equipment container. * @param newItem The item to add. - * @param inventorySlot The inventory slot of the item. + * @param inventorySlot The inventory slot of the item to add. * @param fire If we should refresh. * @param fromInventory If the item is being equipped from the inventory. * @return {@code True} if succesful, {@code false} if not. @@ -106,7 +106,7 @@ public final class EquipmentContainer extends Container { return true; } - boolean successfullyRemovedAll = tryUnequipCurrent(itemsToRemove, newItem); + boolean successfullyRemovedAll = tryUnequipCurrent(itemsToRemove, newItem, inventorySlot); if(!successfullyRemovedAll) { if (fromInventory) player.getInventory().add(newItem); //add the item back in case we weren't able to remove the currently equipped item(s) @@ -132,7 +132,7 @@ public final class EquipmentContainer extends Container { } } - private boolean tryUnequipCurrent(ArrayList current, Item newItem) { + private boolean tryUnequipCurrent(ArrayList current, Item newItem, int preferredSlot) { if(current.isEmpty()) return true; int freeSlots = player.getInventory().freeSlots(); int neededSlots = getNeededSlotsToUnequip(current); @@ -157,7 +157,7 @@ public final class EquipmentContainer extends Container { boolean allAdded = allRemoved; if(allRemoved) { for (Item item : current) { - if (!player.getInventory().add(item)) { + if (!player.getInventory().add(item, true, preferredSlot)) { allAdded = false; break; } @@ -375,4 +375,4 @@ public final class EquipmentContainer extends Container { } player.getPacketDispatch().sendString("Attack bonus", 667, 34); } -} \ No newline at end of file +} diff --git a/Server/src/test/kotlin/content/EquipTests.kt b/Server/src/test/kotlin/content/EquipTests.kt index c9409eee3..ae5c59be0 100644 --- a/Server/src/test/kotlin/content/EquipTests.kt +++ b/Server/src/test/kotlin/content/EquipTests.kt @@ -102,7 +102,7 @@ class EquipTests { Assertions.assertEquals(Items.BRONZE_2H_SWORD_1307, p.equipment.get(EquipmentSlot.WEAPON.ordinal).id) InteractionListeners.run(Items.WOODEN_SHIELD_1171, InteractionListener.ITEM, "equip", p, p.inventory[1]) - Assertions.assertEquals(Items.BRONZE_2H_SWORD_1307, p.inventory[0]?.id ?: -1) + Assertions.assertEquals(Items.BRONZE_2H_SWORD_1307, p.inventory[1]?.id ?: -1) } @Test fun equippingShieldShouldNotUnequipOneHandedWeapon() { @@ -125,4 +125,19 @@ class EquipTests { InteractionListeners.run(Items.BRONZE_ARROW_882, InteractionListener.ITEM, "equip", p, p.inventory[0]) Assertions.assertEquals(300, p.equipment[EquipmentSlot.AMMO.ordinal].amount) } -} \ No newline at end of file + + @Test fun swappingEquipmentShouldPreserveInventorySlots() { + val p = TestUtils.getMockPlayer("bill") + p.skills.staticLevels[Skills.ATTACK] = 70 + p.skills.staticLevels[Skills.DEFENCE] = 40 + p.equipment.replace(Item(Items.ABYSSAL_WHIP_4151), EquipmentSlot.WEAPON.ordinal) + p.equipment.replace(Item(Items.RUNE_DEFENDER_8850), EquipmentSlot.SHIELD.ordinal) + p.inventory.add(Item(Items.DRAGON_CLAWS_14484), true, 25) + Assertions.assertEquals(p.inventory.getSlot(Item(Items.DRAGON_CLAWS_14484)), 25) + + InteractionListeners.run(Items.DRAGON_CLAWS_14484, InteractionListener.ITEM, "equip", p, p.inventory[25]) + Assertions.assertEquals(Items.DRAGON_CLAWS_14484, p.equipment.get(EquipmentSlot.WEAPON.ordinal).id) + Assertions.assertEquals(p.inventory.getSlot(Item(Items.ABYSSAL_WHIP_4151)), 25) + Assertions.assertEquals(p.inventory.getSlot(Item(Items.RUNE_DEFENDER_8850)), 0) + } +}