From b23b33713a824c9daaef617f25758c94006a9860 Mon Sep 17 00:00:00 2001 From: Axel Cocat Date: Wed, 9 Feb 2022 23:04:35 +0100 Subject: [PATCH] feat: add chat handler methods --- ChatHandlerMethods.h | 192 +++++++++++++++++++++++++++++++++++++++++++ GlobalMethods.h | 2 +- Hooks.h | 2 +- LuaEngine.h | 3 +- LuaFunctions.cpp | 24 ++++++ PlayerHooks.cpp | 4 +- 6 files changed, 223 insertions(+), 4 deletions(-) create mode 100644 ChatHandlerMethods.h diff --git a/ChatHandlerMethods.h b/ChatHandlerMethods.h new file mode 100644 index 0000000..aa9f795 --- /dev/null +++ b/ChatHandlerMethods.h @@ -0,0 +1,192 @@ +/* +* Copyright (C) 2010 - 2016 Eluna Lua Engine +* This program is free software licensed under GPL version 3 +* Please see the included DOCS/LICENSE.md for more information +*/ + +#ifndef CHATHANDLERMETHODS_H +#define CHATHANDLERMETHODS_H + +#include "Chat.h" + +namespace LuaChatHandler +{ + /** + * Sends text to the chat handler + * + * @proto (text) + * @proto (entry) + * @param string text : text to display in chat or console + * @param uint32 entry : id of the string to display + */ + int SendSysMessage(lua_State* L, ChatHandler* handler) + { + if (lua_isnumber(L, 2)) + { + uint32 entry = Eluna::CHECKVAL(L, 2); + handler->SendSysMessage(entry); + } + else + { + std::string text = Eluna::CHECKVAL(L, 2); + handler->SendSysMessage(text); + } + return 0; + } + + /** + * Returns `true` if the [ChatHandler] comes from the console, `false` if it comes from a player + * + * @return bool isConsole + */ + int IsConsole(lua_State* L, ChatHandler* handler) + { + Eluna::Push(L, handler->IsConsole()); + return 1; + } + + /** + * Returns the [Player] associated with the handler. Returns `nil` in the case of a console handler + * + * @return [Player] player + */ + int GetPlayer(lua_State* L, ChatHandler* handler) + { + Eluna::Push(L, handler->GetPlayer()); + return 1; + } + + /** + * Sends a message to all connected players + * + * @param string text : text to send + */ + int SendGlobalSysMessage(lua_State* L, ChatHandler* handler) + { + std::string text = Eluna::CHECKVAL(L, 2); + handler->SendGlobalSysMessage(text.c_str()); + return 0; + } + + /** + * Sends a message to all connected Game Masters + * + * @param string text : text to send + */ + int SendGlobalGMSysMessage(lua_State* L, ChatHandler* handler) + { + std::string text = Eluna::CHECKVAL(L, 2); + handler->SendGlobalGMSysMessage(text.c_str()); + return 0; + } + + /** + * Checks if the current security level is lower than the specified [Player]'s account + * + * @param [Player] player + * @param [bool] strong = false : Forces non-player accounts (security level greater than `0`) to go through the regular check if set to `true`.
Also, if set to `true`, the current security level will be considered as lower than the [Player]'s security level if the two levels are equal + * @return [bool] lower + */ + int HasLowerSecurity(lua_State* L, ChatHandler* handler) + { + Player* player = Eluna::CHECKOBJ(L, 2); + bool strong = Eluna::CHECKVAL(L, 3); + Eluna::Push(L, handler->HasLowerSecurity(player, ObjectGuid::Empty, strong)); + return 1; + } + + /** + * Checks if the current security level is lower than the specified `account`'s level + * + * @param [uint32] account : the target account ID to compare security levels with + * @param [bool] strong = false : Forces non-player accounts (security level greater than `0`) to go through the regular check if set to `true`.
Also, if set to `true`, the current security level will be considered as lower than the `account`'s security level if the two levels are equal + * @return [bool] lower + */ + int HasLowerSecurityAccount(lua_State* L, ChatHandler* handler) + { + uint32 account = Eluna::CHECKVAL(L, 2); + bool strong = Eluna::CHECKVAL(L, 3); + Eluna::Push(L, handler->HasLowerSecurityAccount(nullptr, account, strong)); + return 1; + } + + /** + * Returns the selected [Player] + * + * @return [Player] player + */ + int GetSelectedPlayer(lua_State* L, ChatHandler* handler) + { + Eluna::Push(L, handler->getSelectedPlayer()); + return 1; + } + + /** + * Returns the selected [Creature] + * + * @return [Creature] creature + */ + int GetSelectedCreature(lua_State* L, ChatHandler* handler) + { + Eluna::Push(L, handler->getSelectedCreature()); + return 1; + } + + /** + * Returns the selected [Unit] + * + * @return [Unit] unit + */ + int GetSelectedUnit(lua_State* L, ChatHandler* handler) + { + Eluna::Push(L, handler->getSelectedUnit()); + return 1; + } + + /** + * Returns the selected [WorldObject] + * + * @return [WorldObject] object + */ + int GetSelectedObject(lua_State* L, ChatHandler* handler) + { + Eluna::Push(L, handler->getSelectedObject()); + return 1; + } + + /** + * Returns the selected [Player] or the current [Player] if nothing is targeted or the target is not a player + * + * @return [Player] player + */ + int GetSelectedPlayerOrSelf(lua_State* L, ChatHandler* handler) + { + Eluna::Push(L, handler->getSelectedPlayerOrSelf()); + return 1; + } + + /** + * Checks if the `securityLevel` is available + * + * @param [uint32] securityLevel + * @return [bool] isAvailable + */ + int IsAvailable(lua_State* L, ChatHandler* handler) + { + uint32 securityLevel = Eluna::CHECKVAL(L, 2); + Eluna::Push(L, handler->IsAvailable(securityLevel)); + return 1; + } + + /** + * Returns `true` if other previously called [ChatHandler] methods sent an error + * + * @return [bool] sentErrorMessage + */ + int HasSentErrorMessage(lua_State* L, ChatHandler* handler) + { + Eluna::Push(L, handler->HasSentErrorMessage()); + return 1; + } +} +#endif diff --git a/GlobalMethods.h b/GlobalMethods.h index 102917a..5661d6f 100644 --- a/GlobalMethods.h +++ b/GlobalMethods.h @@ -704,7 +704,7 @@ namespace LuaGlobalFunctions * PLAYER_EVENT_ON_LEARN_TALENTS = 39, // (event, player, talentId, talentRank, spellid) * // UNUSED = 40, // (event, player) * // UNUSED = 41, // (event, player) - * PLAYER_EVENT_ON_COMMAND = 42, // (event, player, command) - player is nil if command used from console. Can return false + * PLAYER_EVENT_ON_COMMAND = 42, // (event, player, command, chatHandler) - player is nil if command used from console. Can return false * PLAYER_EVENT_ON_PET_ADDED_TO_WORLD = 43, // (event, player, pet) * }; * diff --git a/Hooks.h b/Hooks.h index a09e05f..7591baf 100644 --- a/Hooks.h +++ b/Hooks.h @@ -203,7 +203,7 @@ namespace Hooks PLAYER_EVENT_ON_LEARN_TALENTS = 39, // (event, player, talentId, talentRank, spellid) // UNUSED = 40, // (event, player) // UNUSED = 41, // (event, player) - PLAYER_EVENT_ON_COMMAND = 42, // (event, player, command) - player is nil if command used from console. Can return false + PLAYER_EVENT_ON_COMMAND = 42, // (event, player, command, chatHandler) - player is nil if command used from console. Can return false PLAYER_EVENT_ON_PET_ADDED_TO_WORLD = 43, // (event, player, pet) PLAYER_EVENT_COUNT diff --git a/LuaEngine.h b/LuaEngine.h index aae41bc..63d720b 100644 --- a/LuaEngine.h +++ b/LuaEngine.h @@ -13,6 +13,7 @@ #include "Group.h" #include "Item.h" +#include "Chat.h" #ifndef TRINITY #include "Player.h" #endif @@ -353,7 +354,7 @@ public: /* Custom */ void OnTimedEvent(int funcRef, uint32 delay, uint32 calls, WorldObject* obj); - bool OnCommand(Player* player, const char* text); + bool OnCommand(ChatHandler& handler, const char* text); void OnWorldUpdate(uint32 diff); void OnLootItem(Player* pPlayer, Item* pItem, uint32 count, ObjectGuid guid); void OnLootMoney(Player* pPlayer, uint32 amount); diff --git a/LuaFunctions.cpp b/LuaFunctions.cpp index b31694c..a7acbee 100644 --- a/LuaFunctions.cpp +++ b/LuaFunctions.cpp @@ -36,6 +36,7 @@ extern "C" #include "CorpseMethods.h" #include "VehicleMethods.h" #include "BattleGroundMethods.h" +#include "ChatHandlerMethods.h" luaL_Reg GlobalMethods[] = { @@ -1296,6 +1297,26 @@ ElunaRegister BattleGroundMethods[] = { NULL, NULL } }; +ElunaRegister ChatHandlerMethods[] = +{ + { "SendSysMessage", &LuaChatHandler::SendSysMessage }, + { "IsConsole", &LuaChatHandler::IsConsole }, + { "GetPlayer", &LuaChatHandler::GetPlayer }, + { "SendGlobalSysMessage", &LuaChatHandler::SendGlobalSysMessage }, + { "SendGlobalGMSysMessage", &LuaChatHandler::SendGlobalGMSysMessage }, + { "HasLowerSecurity", &LuaChatHandler::HasLowerSecurity }, + { "HasLowerSecurityAccount", &LuaChatHandler::HasLowerSecurityAccount }, + { "GetSelectedPlayer", &LuaChatHandler::GetSelectedPlayer }, + { "GetSelectedCreature", &LuaChatHandler::GetSelectedCreature }, + { "GetSelectedUnit", &LuaChatHandler::GetSelectedUnit }, + { "GetSelectedObject", &LuaChatHandler::GetSelectedObject }, + { "GetSelectedPlayerOrSelf", &LuaChatHandler::GetSelectedPlayerOrSelf }, + { "IsAvailable", &LuaChatHandler::IsAvailable }, + { "HasSentErrorMessage", &LuaChatHandler::HasSentErrorMessage }, + + { NULL, NULL } +}; + #if (!defined(TBC) && !defined(CLASSIC)) // fix compile error about accessing vehicle destructor template<> int ElunaTemplate::CollectGarbage(lua_State* L) @@ -1429,6 +1450,9 @@ void RegisterFunctions(Eluna* E) ElunaTemplate::Register(E, "BattleGround"); ElunaTemplate::SetMethods(E, BattleGroundMethods); + ElunaTemplate::Register(E, "ChatHandler"); + ElunaTemplate::SetMethods(E, ChatHandlerMethods); + ElunaTemplate::Register(E, "WorldPacket", true); ElunaTemplate::SetMethods(E, PacketMethods); diff --git a/PlayerHooks.cpp b/PlayerHooks.cpp index 23171b2..3e1d5bb 100644 --- a/PlayerHooks.cpp +++ b/PlayerHooks.cpp @@ -39,8 +39,9 @@ void Eluna::OnLearnTalents(Player* pPlayer, uint32 talentId, uint32 talentRank, CallAllFunctions(PlayerEventBindings, key); } -bool Eluna::OnCommand(Player* player, const char* text) +bool Eluna::OnCommand(ChatHandler& handler, const char* text) { + Player* player = handler.IsConsole() ? nullptr : handler.GetSession()->GetPlayer(); // If from console, player is NULL if (!player || player->GetSession()->GetSecurity() >= SEC_ADMINISTRATOR) { @@ -56,6 +57,7 @@ bool Eluna::OnCommand(Player* player, const char* text) START_HOOK_WITH_RETVAL(PLAYER_EVENT_ON_COMMAND, true); Push(player); Push(text); + Push(&handler); return CallAllFunctionsBool(PlayerEventBindings, key, true); }