more graceful failure mode for item bag functions

This commit is contained in:
Zithia Satazaki 2023-10-30 01:30:25 -04:00
parent 943729a20e
commit 012572c199
2 changed files with 6 additions and 2 deletions

View File

@ -424,6 +424,7 @@ BackArmorPtr PlayerInventory::backCosmetic() const {
}
ItemBagConstPtr PlayerInventory::bagContents(String const& type) const {
if (!m_bags.contains(type)) return nullptr;
return m_bags.get(type);
}

View File

@ -204,19 +204,21 @@ LuaCallbacks LuaBindings::makePlayerCallbacks(Player* player) {
callbacks.registerCallback("itemBagSize", [player](String const& bag) {
auto inventory = player->inventory();
auto b = inventory->bagContents(bag);
return b->size();
if (!b) return 0;
return (int)b->size();
});
callbacks.registerCallback("itemAllowedInBag", [player](String const& bag, Json const& item) {
auto inventory = player->inventory();
auto itemDatabase = Root::singleton().itemDatabase();
if (!inventory->bagContents(bag)) return false;
return inventory->itemAllowedInBag(itemDatabase->item(ItemDescriptor(item)), bag);
});
callbacks.registerCallback("itemBagItem", [player](String const& bag, int slot) -> Json {
auto inventory = player->inventory();
auto b = inventory->bagContents(bag);
//if (b.get() == nullptr) return {};
if (!b || slot <= 0 || slot > (int)b->size()) return {};
return itemSafeDescriptor(b->at(slot - 1)).toJson();
});
@ -224,6 +226,7 @@ LuaCallbacks LuaBindings::makePlayerCallbacks(Player* player) {
auto inventory = player->inventory();
auto itemDatabase = Root::singleton().itemDatabase();
auto b = const_pointer_cast<ItemBag>(inventory->bagContents(bag)); // bit of a Naughty Access Cheat here, but
if (!b || slot <= 0 || slot > (int)b->size()) return;
b->setItem(slot - 1, itemDatabase->item(ItemDescriptor(item)));
});