From b387e993bb6cc00e245cea9c71469e0da83a62ca Mon Sep 17 00:00:00 2001 From: Ceikry Date: Tue, 20 Dec 2022 08:40:59 +0000 Subject: [PATCH] Fixed a bug when buying items from shops with a full inventory --- .../rs09/game/content/global/shops/Shop.kt | 3 +++ Server/src/test/kotlin/ShopTests.kt | 22 +++++++++++++++++++ 2 files changed, 25 insertions(+) diff --git a/Server/src/main/kotlin/rs09/game/content/global/shops/Shop.kt b/Server/src/main/kotlin/rs09/game/content/global/shops/Shop.kt index c4b71153d..18d39ba29 100644 --- a/Server/src/main/kotlin/rs09/game/content/global/shops/Shop.kt +++ b/Server/src/main/kotlin/rs09/game/content/global/shops/Shop.kt @@ -290,6 +290,9 @@ class Shop(val title: String, val stock: Array, val general: Boolean = { if(removeItem(player, cost)) { + if (item.amount == 0 && amountInInventory(player, cost.id) == 0) { + item.amount = 1 + } if(!hasSpaceFor(player, item)) { addItem(player, cost.id, cost.amount) sendMessage(player, "You don't have enough inventory space to buy that many.") diff --git a/Server/src/test/kotlin/ShopTests.kt b/Server/src/test/kotlin/ShopTests.kt index 7ce228f22..611a1ca9b 100644 --- a/Server/src/test/kotlin/ShopTests.kt +++ b/Server/src/test/kotlin/ShopTests.kt @@ -340,4 +340,26 @@ class ShopTests { Assertions.assertEquals(remainingGP, testPlayer.inventory.getAmount(995), "Coins were deducted for buying 0-stock item!") Assertions.assertNull(testPlayer.inventory.get(1), "Player received purchased item despite being 0-stock!") } + + @Test fun buyingItemWithOtherwiseFullInventoryStillBuysTheItemAndTakesTheCoins() { + testPlayer.setAttribute("shop-cont", general.getContainer(testPlayer)) + testPlayer.setAttribute("shop-main", false) + testPlayer.inventory.clear() + testPlayer.inventory.add(Item(Items.ABYSSAL_WHIP_4151, 27)) //fill 27 slots + general.playerStock.add(Item(992, 1)) + var slot = general.getStockSlot(992).second + var price = general.getBuyPrice(testPlayer, slot) + + testPlayer.inventory.add(price) + + //Buy out existing stock + var status: Shop.TransactionStatus = Shop.TransactionStatus.Success() + + Assertions.assertDoesNotThrow { + status = general.buy(testPlayer, slot, 1) + } + Assertions.assertEquals(true, status is Shop.TransactionStatus.Success) + Assertions.assertEquals(0, testPlayer.inventory.getAmount(995)) + Assertions.assertEquals(1, testPlayer.inventory.getAmount(992)) + } }