3 Copyright (C) 2013 celeron55, Perttu Ahola <celeron55@gmail.com>
4 Copyright (C) 2017 nerzhul, Loic Blot <loic.blot@unix-experience.fr>
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU Lesser General Public License as published by
8 the Free Software Foundation; either version 2.1 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU Lesser General Public License for more details.
16 You should have received a copy of the GNU Lesser General Public License along
17 with this program; if not, write to the Free Software Foundation, Inc.,
18 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
22 #include "l_internal.h"
23 #include "util/string.h"
24 #include "cpp_api/s_base.h"
27 int ModApiClient::l_get_current_modname(lua_State *L)
29 lua_rawgeti(L, LUA_REGISTRYINDEX, CUSTOM_RIDX_CURRENT_MOD_NAME);
34 int ModApiClient::l_get_last_run_mod(lua_State *L)
36 lua_rawgeti(L, LUA_REGISTRYINDEX, CUSTOM_RIDX_CURRENT_MOD_NAME);
37 const char *current_mod = lua_tostring(L, -1);
38 if (current_mod == NULL || current_mod[0] == '\0') {
40 lua_pushstring(L, getScriptApiBase(L)->getOrigin().c_str());
45 // set_last_run_mod(modname)
46 int ModApiClient::l_set_last_run_mod(lua_State *L)
48 if (!lua_isstring(L, 1))
51 const char *mod = lua_tostring(L, 1);
52 getScriptApiBase(L)->setOriginDirect(mod);
53 lua_pushboolean(L, true);
57 // display_chat_message(message)
58 int ModApiClient::l_display_chat_message(lua_State *L)
60 if (!lua_isstring(L, 1))
63 std::string message = luaL_checkstring(L, 1);
64 getClient(L)->pushToChatQueue(utf8_to_wide(message));
65 lua_pushboolean(L, true);
69 // show_formspec(formspec)
70 int ModApiClient::l_show_formspec(lua_State *L)
72 if ( !lua_isstring(L, 1) || !lua_isstring(L, 2) )
76 event.type = CE_SHOW_LOCAL_FORMSPEC;
77 event.show_formspec.formname = new std::string(luaL_checkstring(L, 1));
78 event.show_formspec.formspec = new std::string(luaL_checkstring(L, 2));
79 getClient(L)->pushToEventQueue(event);
80 lua_pushboolean(L, true);
85 int ModApiClient::l_send_respawn(lua_State *L)
87 getClient(L)->sendRespawn();
92 int ModApiClient::l_gettext(lua_State *L)
94 std::string text = strgettext(std::string(luaL_checkstring(L, 1)));
95 lua_pushstring(L, text.c_str());
100 void ModApiClient::Initialize(lua_State *L, int top)
102 API_FCT(get_current_modname);
103 API_FCT(display_chat_message);
104 API_FCT(set_last_run_mod);
105 API_FCT(get_last_run_mod);
106 API_FCT(show_formspec);
107 API_FCT(send_respawn);