diff options
author | Zithia Satazaki <qmanxt@gmail.com> | 2023-10-29 23:15:26 -0400 |
---|---|---|
committer | Zithia Satazaki <qmanxt@gmail.com> | 2023-10-29 23:15:26 -0400 |
commit | fa6a770cfc26bc50cfec700cacef5aaefa72f77d (patch) | |
tree | 14060876c2ae64f9817ffb411557e87213eebc3b /source/game/scripting/StarPlayerLuaBindings.cpp | |
parent | 7c29196effe8d304c900eec1b35cafeebe344fb8 (diff) |
add `player.actionBarItem` and `player.setActionBarItem` (still needs a bit of work)
Diffstat (limited to 'source/game/scripting/StarPlayerLuaBindings.cpp')
-rw-r--r-- | source/game/scripting/StarPlayerLuaBindings.cpp | 48 |
1 files changed, 48 insertions, 0 deletions
diff --git a/source/game/scripting/StarPlayerLuaBindings.cpp b/source/game/scripting/StarPlayerLuaBindings.cpp index 4fd0a5b..8b6e7d5 100644 --- a/source/game/scripting/StarPlayerLuaBindings.cpp +++ b/source/game/scripting/StarPlayerLuaBindings.cpp @@ -152,6 +152,54 @@ LuaCallbacks LuaBindings::makePlayerCallbacks(Player* player) { inventory->selectActionBarLocation(SelectedActionBarLocation(item)); } }); + + callbacks.registerCallback("actionBarItem", [player](MVariant<int, String> const& slot, Maybe<bool> offHand) -> Json { + auto inventory = player->inventory(); + if (!slot) return {}; + else if (auto index = slot.ptr<int>()) { + CustomBarIndex wrapped = (*index - 1) % (unsigned)inventory->customBarIndexes(); + Maybe<InventorySlot> s; + if (offHand.value(false)) s = inventory->customBarSecondarySlot(wrapped); + else s = inventory->customBarPrimarySlot(wrapped); + if (s.isNothing()) return {}; + return itemSafeDescriptor(inventory->itemsAt(s.value())).toJson(); + } else { + return itemSafeDescriptor(inventory->essentialItem(EssentialItemNames.getLeft(slot.get<String>()))).toJson(); + } + }); + + callbacks.registerCallback("setActionBarItem", [player](MVariant<int, String> const& slot, bool offHand, Json const& item) { + auto inventory = player->inventory(); + auto itemDatabase = Root::singleton().itemDatabase(); + + if (!slot) return; + else if (auto index = slot.ptr<int>()) { + CustomBarIndex wrapped = (*index - 1) % (unsigned)inventory->customBarIndexes(); + + if (item.type() == Json::Type::Object && item.contains("name")) { + auto itm = itemDatabase->item(ItemDescriptor(item)); + + Maybe<InventorySlot> found; + inventory->forEveryItem([player, &found, &itm](InventorySlot const& slot, ItemPtr const& item){ + if (!found.isNothing()) return; + if (item->matches(itm, true)) found = slot; + }); + if (!found.isNothing()) { + if (offHand) inventory->setCustomBarSecondarySlot(wrapped, found.value()); + else inventory->setCustomBarPrimarySlot(wrapped, found.value()); + } + } else { + if (offHand) inventory->setCustomBarSecondarySlot(wrapped, {}); + else inventory->setCustomBarPrimarySlot(wrapped, {}); + } + + } else { + // place into essential item slot + //if (item.isNothing()) inventory->setEssentialItem(EssentialItemNames.getLeft(slot.get<String>()), {}); + inventory->setEssentialItem(EssentialItemNames.getLeft(slot.get<String>()), itemDatabase->item(ItemDescriptor(item))); + // TODO: why does this always clear the slot. it's literally the same code as giveEssentialItem + } + }); callbacks.registerCallback("setDamageTeam", [player](String const& typeName, Maybe<uint16_t> teamNumber) { player->setTeam(EntityDamageTeam(TeamTypeNames.getLeft(typeName), teamNumber.value(0))); |