diff --git a/include/LuaAPI.h b/include/LuaAPI.h index 928df0b1..6f1705e3 100644 --- a/include/LuaAPI.h +++ b/include/LuaAPI.h @@ -35,6 +35,7 @@ namespace MP { inline size_t GetPlayerCount() { return Engine->Server().ClientCount(); } 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 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 0bcce651..582da467 100644 --- a/src/LuaAPI.cpp +++ b/src/LuaAPI.cpp @@ -205,6 +205,37 @@ std::pair LuaAPI::MP::SendChatMessage(int ID, const std::stri return Result; } +std::pair LuaAPI::MP::SendNotification(int ID, const std::string& Message, const std::string& Icon, const std::string& Category) { + std::pair Result; + std::string Packet = "N" + Category + ":" + Icon + ":" + Message; + 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 notification to player (id {}) - did the player disconnect?", ID); + Result.first = false; + Result.second = "Failed to send packet"; + } + Result.first = true; + } else { + beammp_lua_error("SendNotification 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 5a3cc5a7..9f1f5339 100644 --- a/src/TLuaEngine.cpp +++ b/src/TLuaEngine.cpp @@ -869,6 +869,15 @@ TLuaEngine::StateThreadData::StateThreadData(const std::string& Name, TLuaStateI return Lua_GetPositionRaw(PID, VID); }); MPTable.set_function("SendChatMessage", &LuaAPI::MP::SendChatMessage); + MPTable.set_function("SendNotification", [&](sol::variadic_args Args) { + if (Args.size() == 3) { + LuaAPI::MP::SendNotification(Args.get(0), Args.get(1), Args.get(2), Args.get(1)); + } else if (Args.size() == 4) { + LuaAPI::MP::SendNotification(Args.get(0), Args.get(1), Args.get(2), Args.get(3)); + } else { + beammp_lua_error("SendNotification expects 2 or 3 arguments."); + } + }); MPTable.set_function("GetPlayers", [&]() -> sol::table { return Lua_GetPlayers(); });