Fixed an exception thrown when buying from player stock

Fix issue where malformed stock could shift following shop items
This commit is contained in:
Ceikry
2022-05-02 10:22:36 +00:00
committed by Ryan
parent a5f3f93048
commit 8438ecdbd7
4 changed files with 57 additions and 13 deletions
@@ -1025,4 +1025,15 @@ public class Container {
return event;
}
@Override
public String toString() {
return "Container{" +
"items=" + Arrays.toString(items) +
", capacity=" + capacity +
", sortType=" + sortType +
", type=" + type +
", event=" + event +
", listeners=" + listeners +
'}';
}
}
@@ -246,13 +246,13 @@ class Shop(val title: String, val stock: Array<ShopItem>, val general: Boolean =
sendDialogue(player, "As an ironman, you cannot buy from player stock in shops.")
return TransactionStatus.Failure("Ironman buying from player stock")
}
val cont = if (isMainStock) getAttribute<Container?>(player, "shop-cont", null) ?: return TransactionStatus.Failure("Invalid shop-cont attr") else playerStock
val cont = if (isMainStock) (getAttribute<Container?>(player, "shop-cont", null) ?: return TransactionStatus.Failure("Invalid shop-cont attr")) else playerStock
val inStock = cont[slot]
val item = Item(inStock.id, amount)
if(inStock.amount < amount)
item.amount = inStock.amount
if(inStock.amount > stock[slot].amount && !getServerConfig().getBoolean(Shops.personalizedShops, false) && player.ironmanManager.isIronman)
if(isMainStock && inStock.amount > stock[slot].amount && !getServerConfig().getBoolean(Shops.personalizedShops, false) && player.ironmanManager.isIronman)
{
sendDialogue(player, "As an ironman, you cannot buy overstocked items from shops.")
return TransactionStatus.Failure("Ironman overstock purchase")
@@ -41,15 +41,6 @@ class Shops : StartupListener, TickListener, InteractionListener, InterfaceListe
{
SystemLogger.logInfo("[SHOPS] $msg")
}
}
override fun startup() {
val path = ServerConstants.CONFIG_PATH + "shops.json"
var shopCount = 0
logShop("Using JSON path: $path")
val reader = FileReader(path)
val data = JSONParser().parse(reader) as JSONArray
fun parseStock(stock: String, id: Int): ArrayList<ShopItem>{
val items = ArrayList<ShopItem>()
@@ -63,13 +54,28 @@ class Shops : StartupListener, TickListener, InteractionListener, InterfaceListe
if(amount == "inf")
amount = "-1"
val item = tokens[0].toInt()
if(idsInStock[item] != null)
if(idsInStock[item] != null) {
SystemLogger.logWarn("[SHOPS] MALFORMED STOCK IN SHOP ID $id FOR ITEM $item")
else
items.forEach { if(it.itemId == item) {
it.amount += amount.toInt()
return@map
}}
} else {
items.add(ShopItem(item, amount.toInt(), tokens.getOrNull(2)?.toIntOrNull() ?: 100))
idsInStock[item] = true
}
}
return items
}
}
override fun startup() {
val path = ServerConstants.CONFIG_PATH + "shops.json"
var shopCount = 0
logShop("Using JSON path: $path")
val reader = FileReader(path)
val data = JSONParser().parse(reader) as JSONArray
for(rawShop in data)
{