From fa6a770cfc26bc50cfec700cacef5aaefa72f77d Mon Sep 17 00:00:00 2001 From: Zithia Satazaki Date: Sun, 29 Oct 2023 23:15:26 -0400 Subject: [PATCH] add `player.actionBarItem` and `player.setActionBarItem` (still needs a bit of work) --- .../game/scripting/StarPlayerLuaBindings.cpp | 48 +++++++++++++++++++ 1 file changed, 48 insertions(+) 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 const& slot, Maybe offHand) -> Json { + auto inventory = player->inventory(); + if (!slot) return {}; + else if (auto index = slot.ptr()) { + CustomBarIndex wrapped = (*index - 1) % (unsigned)inventory->customBarIndexes(); + Maybe 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()))).toJson(); + } + }); + + callbacks.registerCallback("setActionBarItem", [player](MVariant 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()) { + CustomBarIndex wrapped = (*index - 1) % (unsigned)inventory->customBarIndexes(); + + if (item.type() == Json::Type::Object && item.contains("name")) { + auto itm = itemDatabase->item(ItemDescriptor(item)); + + Maybe 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()), {}); + inventory->setEssentialItem(EssentialItemNames.getLeft(slot.get()), 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 teamNumber) { player->setTeam(EntityDamageTeam(TeamTypeNames.getLeft(typeName), teamNumber.value(0)));