Equipping an item into an occupied equipment slot now places the swapped item into the inventory slot of the item being equipped

This commit is contained in:
Avi Weinstock
2022-07-07 07:45:17 +00:00
committed by Ryan
parent 1077459979
commit b2763a5586
3 changed files with 26 additions and 11 deletions
@@ -221,9 +221,9 @@ public class Container {
if (maximum == 0) { if (maximum == 0) {
return false; return false;
} }
// if (preferredSlot > -1 && items[preferredSlot] != null) { if (preferredSlot > -1 && items[preferredSlot] != null) {
// preferredSlot = -1; preferredSlot = -1;
// } }
if (item.getAmount() > maximum) { if (item.getAmount() > maximum) {
item.setAmount(maximum); item.setAmount(maximum);
} }
@@ -71,7 +71,7 @@ public final class EquipmentContainer extends Container {
/** /**
* Adds an item to the equipment container. * Adds an item to the equipment container.
* @param newItem The item to add. * @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 fire If we should refresh.
* @param fromInventory If the item is being equipped from the inventory. * @param fromInventory If the item is being equipped from the inventory.
* @return {@code True} if succesful, {@code false} if not. * @return {@code True} if succesful, {@code false} if not.
@@ -106,7 +106,7 @@ public final class EquipmentContainer extends Container {
return true; return true;
} }
boolean successfullyRemovedAll = tryUnequipCurrent(itemsToRemove, newItem); boolean successfullyRemovedAll = tryUnequipCurrent(itemsToRemove, newItem, inventorySlot);
if(!successfullyRemovedAll) { 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) 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<Item> current, Item newItem) { private boolean tryUnequipCurrent(ArrayList<Item> current, Item newItem, int preferredSlot) {
if(current.isEmpty()) return true; if(current.isEmpty()) return true;
int freeSlots = player.getInventory().freeSlots(); int freeSlots = player.getInventory().freeSlots();
int neededSlots = getNeededSlotsToUnequip(current); int neededSlots = getNeededSlotsToUnequip(current);
@@ -157,7 +157,7 @@ public final class EquipmentContainer extends Container {
boolean allAdded = allRemoved; boolean allAdded = allRemoved;
if(allRemoved) { if(allRemoved) {
for (Item item : current) { for (Item item : current) {
if (!player.getInventory().add(item)) { if (!player.getInventory().add(item, true, preferredSlot)) {
allAdded = false; allAdded = false;
break; break;
} }
+16 -1
View File
@@ -102,7 +102,7 @@ class EquipTests {
Assertions.assertEquals(Items.BRONZE_2H_SWORD_1307, p.equipment.get(EquipmentSlot.WEAPON.ordinal).id) 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]) 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() { @Test fun equippingShieldShouldNotUnequipOneHandedWeapon() {
@@ -125,4 +125,19 @@ class EquipTests {
InteractionListeners.run(Items.BRONZE_ARROW_882, InteractionListener.ITEM, "equip", p, p.inventory[0]) InteractionListeners.run(Items.BRONZE_ARROW_882, InteractionListener.ITEM, "equip", p, p.inventory[0])
Assertions.assertEquals(300, p.equipment[EquipmentSlot.AMMO.ordinal].amount) Assertions.assertEquals(300, p.equipment[EquipmentSlot.AMMO.ordinal].amount)
} }
@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)
}
} }