Skip to content

Commit add0b86

Browse files
authored
Implement Dialog packet and add MP.ConfirmationDialog (#427)
This PR implements a new lua function and packet used for sends dialogs to the client. ## Example: https://github.com/user-attachments/assets/97bb5813-ea12-4b1d-a049-2f7ebf6b6da3 Example serverside code: ```lua --MP.ConfirmationDialog(player_id: number, title: string, body: string, buttons: object, interaction_id: string, warning: boolean = false, reportToServer: boolean = true, reportToExtensions: boolean = true) function onChatMessage(player_id, player_name, message) MP.ConfirmationDialog(player_id, "Warning", "Watch your tone buddy!!", { { label = "OK", key = "dialogOK", isCancel = true } }, "interactionID", true) end MP.RegisterEvent("onChatMessage", "onChatMessage") function dialogOK(player_id, interaction_id) MP.SendChatMessage(-1, MP.GetPlayerName(player_id) .. " clicked OK") end MP.RegisterEvent("dialogOK", "dialogOK") ``` ### Details: Each dialog can have multiple buttons, each button having it's own callback event (`key`). Each dialog can also have one button with `isCancel` being true, settings this property to true causes the button's event to be called when the users pressed `esc` to exit out of the dialog. If a dialog is created without any button being the cancel button then the user will only be able to exit the dialog by restarting the session or pressing one of the buttons. `interaction_id` will be sent as the event data with a button press event, to track from which dialog the button press came. As when multiple dialogs are opened they will stack and it will become difficult to track what button on which dialog was pressed without having multiple event handlers. Waiting on BeamMP/BeamMP#715 to be merged. --- By creating this pull request, I understand that code that is AI generated or otherwise automatically generated may be rejected without further discussion. I declare that I fully understand all code I pushed into this PR, and wrote all this code myself and own the rights to this code.
2 parents 2bd4ee9 + 403c1d5 commit add0b86

File tree

3 files changed

+49
-0
lines changed

3 files changed

+49
-0
lines changed

include/LuaAPI.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ namespace MP {
3636
std::pair<bool, std::string> DropPlayer(int ID, std::optional<std::string> MaybeReason);
3737
std::pair<bool, std::string> SendChatMessage(int ID, const std::string& Message);
3838
std::pair<bool, std::string> SendNotification(int ID, const std::string& Message, const std::string& Icon, const std::string& Category);
39+
std::pair<bool, std::string> 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);
3940
std::pair<bool, std::string> RemoveVehicle(int PlayerID, int VehicleID);
4041
void Set(int ConfigID, sol::object NewValue);
4142
TLuaValue Get(int ConfigID);

src/LuaAPI.cpp

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -241,6 +241,48 @@ std::pair<bool, std::string> LuaAPI::MP::SendNotification(int ID, const std::str
241241
return Result;
242242
}
243243

244+
std::pair<bool, std::string> 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) {
245+
std::pair<bool, std::string> Result;
246+
247+
const nlohmann::json PacketBody = {
248+
{ "title", Title },
249+
{ "body", Body },
250+
{ "buttons", nlohmann::json::parse(JsonEncode(buttons), nullptr, false) },
251+
{ "interactionID", InteractionID },
252+
{ "class", warning ? "experimental" : "" },
253+
{ "reportToServer", reportToServer },
254+
{ "reportToExtensions", reportToExtensions }
255+
};
256+
257+
std::string Packet = "D" + PacketBody.dump();
258+
if (ID == -1) {
259+
Engine->Network().SendToAll(nullptr, StringToVector(Packet), true, true);
260+
Result.first = true;
261+
} else {
262+
auto MaybeClient = GetClient(Engine->Server(), ID);
263+
if (MaybeClient) {
264+
auto c = MaybeClient.value().lock();
265+
if (!c->IsSynced()) {
266+
Result.first = false;
267+
Result.second = "Player is not synced yet";
268+
return Result;
269+
}
270+
if (!Engine->Network().Respond(*c, StringToVector(Packet), true)) {
271+
beammp_errorf("Failed to send confirmation dialog to player (id {}) - did the player disconnect?", ID);
272+
Result.first = false;
273+
Result.second = "Failed to send packet";
274+
}
275+
Result.first = true;
276+
} else {
277+
beammp_lua_error("ConfirmationDialog invalid argument [1] invalid ID");
278+
Result.first = false;
279+
Result.second = "Invalid Player ID";
280+
}
281+
return Result;
282+
}
283+
return Result;
284+
}
285+
244286
std::pair<bool, std::string> LuaAPI::MP::RemoveVehicle(int PID, int VID) {
245287
std::pair<bool, std::string> Result;
246288
auto MaybeClient = GetClient(Engine->Server(), PID);

src/TLuaEngine.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -876,6 +876,12 @@ TLuaEngine::StateThreadData::StateThreadData(const std::string& Name, TLuaStateI
876876
beammp_lua_error("SendNotification expects 2, 3 or 4 arguments.");
877877
}
878878
});
879+
MPTable.set_function("ConfirmationDialog", sol::overload(
880+
&LuaAPI::MP::ConfirmationDialog,
881+
[&](const int& ID, const std::string& Title, const std::string& Body, const sol::table& Buttons, const std::string& InteractionID) {
882+
LuaAPI::MP::ConfirmationDialog(ID, Title, Body, Buttons, InteractionID);
883+
}
884+
));
879885
MPTable.set_function("GetPlayers", [&]() -> sol::table {
880886
return Lua_GetPlayers();
881887
});

0 commit comments

Comments
 (0)