diff --git a/include/LuaAPI.h b/include/LuaAPI.h index 6f1705e..4c4f361 100644 --- a/include/LuaAPI.h +++ b/include/LuaAPI.h @@ -36,6 +36,7 @@ namespace MP { std::pair DropPlayer(int ID, std::optional MaybeReason); std::pair SendChatMessage(int ID, const std::string& Message); std::pair SendNotification(int ID, const std::string& Message, const std::string& Icon, const std::string& Category); + std::pair ConfirmationDialog(int ID, const std::string& Title, const std::string& Body, const sol::table& buttons, const std::string& InteractionID, const bool& warning = false, const bool& reportToServer = true, const bool& reportToExtensions = true); std::pair RemoveVehicle(int PlayerID, int VehicleID); void Set(int ConfigID, sol::object NewValue); TLuaValue Get(int ConfigID); diff --git a/src/LuaAPI.cpp b/src/LuaAPI.cpp index 10b191c..c71ff29 100644 --- a/src/LuaAPI.cpp +++ b/src/LuaAPI.cpp @@ -241,6 +241,48 @@ std::pair LuaAPI::MP::SendNotification(int ID, const std::str return Result; } +std::pair LuaAPI::MP::ConfirmationDialog(int ID, const std::string& Title, const std::string& Body, const sol::table& buttons, const std::string& InteractionID, const bool& warning, const bool& reportToServer, const bool& reportToExtensions) { + std::pair Result; + + const nlohmann::json PacketBody = { + { "title", Title }, + { "body", Body }, + { "buttons", nlohmann::json::parse(JsonEncode(buttons), nullptr, false) }, + { "interactionID", InteractionID }, + { "class", warning ? "experimental" : "" }, + { "reportToServer", reportToServer }, + { "reportToExtensions", reportToExtensions } + }; + + std::string Packet = "D" + PacketBody.dump(); + if (ID == -1) { + Engine->Network().SendToAll(nullptr, StringToVector(Packet), true, true); + Result.first = true; + } else { + auto MaybeClient = GetClient(Engine->Server(), ID); + if (MaybeClient) { + auto c = MaybeClient.value().lock(); + if (!c->IsSynced()) { + Result.first = false; + Result.second = "Player is not synced yet"; + return Result; + } + if (!Engine->Network().Respond(*c, StringToVector(Packet), true)) { + beammp_errorf("Failed to send confirmation dialog to player (id {}) - did the player disconnect?", ID); + Result.first = false; + Result.second = "Failed to send packet"; + } + Result.first = true; + } else { + beammp_lua_error("ConfirmationDialog invalid argument [1] invalid ID"); + Result.first = false; + Result.second = "Invalid Player ID"; + } + return Result; + } + return Result; +} + std::pair LuaAPI::MP::RemoveVehicle(int PID, int VID) { std::pair Result; auto MaybeClient = GetClient(Engine->Server(), PID); diff --git a/src/TLuaEngine.cpp b/src/TLuaEngine.cpp index 6bb02b5..83fa34f 100644 --- a/src/TLuaEngine.cpp +++ b/src/TLuaEngine.cpp @@ -876,6 +876,12 @@ TLuaEngine::StateThreadData::StateThreadData(const std::string& Name, TLuaStateI beammp_lua_error("SendNotification expects 2, 3 or 4 arguments."); } }); + MPTable.set_function("ConfirmationDialog", sol::overload( + &LuaAPI::MP::ConfirmationDialog, + [&](const int& ID, const std::string& Title, const std::string& Body, const sol::table& Buttons, const std::string& InteractionID) { + LuaAPI::MP::ConfirmationDialog(ID, Title, Body, Buttons, InteractionID); + } + )); MPTable.set_function("GetPlayers", [&]() -> sol::table { return Lua_GetPlayers(); });