From dbbd370326219371bc246313bdd8351a8c50676d Mon Sep 17 00:00:00 2001 From: GregHib Date: Tue, 30 Apr 2024 23:14:48 +0100 Subject: [PATCH] Fix satchel empty behaviour --- .../activity/quest/toweroflife/Satchel.kts | 30 ++++++++++--------- .../activity/quest/toweroflife/SatchelTest.kt | 10 +++---- 2 files changed, 21 insertions(+), 19 deletions(-) diff --git a/game/src/main/kotlin/world/gregs/voidps/world/activity/quest/toweroflife/Satchel.kts b/game/src/main/kotlin/world/gregs/voidps/world/activity/quest/toweroflife/Satchel.kts index 5c9cf5804..e1f3e1e48 100644 --- a/game/src/main/kotlin/world/gregs/voidps/world/activity/quest/toweroflife/Satchel.kts +++ b/game/src/main/kotlin/world/gregs/voidps/world/activity/quest/toweroflife/Satchel.kts @@ -3,6 +3,7 @@ package world.gregs.voidps.world.activity.quest.toweroflife import net.pearx.kasechange.toLowerSpaceCase import world.gregs.voidps.engine.client.message import world.gregs.voidps.engine.client.ui.interact.itemOnItem +import world.gregs.voidps.engine.entity.character.player.Player import world.gregs.voidps.engine.inv.charges import world.gregs.voidps.engine.inv.inventory import world.gregs.voidps.engine.inv.transact.operation.AddItem.add @@ -25,23 +26,24 @@ inventoryItem("Inspect", "*_satchel") { inventoryItem("Empty", "*_satchel") { var charges = player.inventory.charges(player, slot) - player.inventory.transaction { - if (charges and cake != 0) { - add("cake") - charges = charges and cake.inv() - setCharge(slot, charges) - } - if (!failed && charges and banana != 0 && inventory.spaces > 0) { - add("banana") - charges = charges and banana.inv() - setCharge(slot, charges) + charges = withdraw(player, slot, charges, "banana", banana) + charges = withdraw(player, slot, charges, "cake", cake) + withdraw(player, slot, charges, "triangle_sandwich", sandwich) +} + +fun withdraw(player: Player, slot: Int, charges: Int, id: String, food: Int): Int { + if (charges and food != 0) { + val success = player.inventory.transaction { + add(id) + setCharge(slot, charges and food.inv()) } - if (!failed && charges and sandwich != 0 && inventory.spaces > 0) { - add("triangle_sandwich") - charges = charges and sandwich.inv() - setCharge(slot, charges) + if (success) { + return charges and food.inv() + } else { + player.message("You don't have enough free space to empty your satchel.") } } + return charges } itemOnItem("cake", "*_satchel") { player -> diff --git a/game/src/test/kotlin/world/gregs/voidps/world/activity/quest/toweroflife/SatchelTest.kt b/game/src/test/kotlin/world/gregs/voidps/world/activity/quest/toweroflife/SatchelTest.kt index 58891b7bd..7078dc4fe 100644 --- a/game/src/test/kotlin/world/gregs/voidps/world/activity/quest/toweroflife/SatchelTest.kt +++ b/game/src/test/kotlin/world/gregs/voidps/world/activity/quest/toweroflife/SatchelTest.kt @@ -37,14 +37,14 @@ internal class SatchelTest : WorldTest() { player.itemOption("Empty", "plain_satchel") - assertEquals(6, player.inventory.charges(player, 0)) - assertTrue(player.inventory.contains("cake")) - assertFalse(player.inventory.contains("banana")) + assertEquals(5, player.inventory.charges(player, 0)) + assertTrue(player.inventory.contains("banana")) + assertFalse(player.inventory.contains("cake")) assertFalse(player.inventory.contains("triangle_sandwich")) } @ParameterizedTest - @ValueSource(strings = [ "cake", "banana", "triangle_sandwich" ]) + @ValueSource(strings = ["cake", "banana", "triangle_sandwich"]) fun `Add item to food satchel`(item: String) { val player = createPlayer("player") player.inventory.set(0, "plain_satchel", 0) @@ -57,7 +57,7 @@ internal class SatchelTest : WorldTest() { } @ParameterizedTest - @ValueSource(strings = [ "cake", "banana", "triangle_sandwich" ]) + @ValueSource(strings = ["cake", "banana", "triangle_sandwich"]) fun `Can't add item to full satchel`(item: String) { val player = createPlayer("player") player.inventory.set(0, "plain_satchel", 7)