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 "common/c_content.h"
23 #include "common/c_converter.h"
24 #include "cpp_api/s_base.h"
26 #include "l_internal.h"
27 #include "lua_api/l_item.h"
28 #include "mainmenumanager.h"
29 #include "util/string.h"
31 extern MainGameCallback *g_gamecallback;
33 int ModApiClient::l_get_current_modname(lua_State *L)
35 lua_rawgeti(L, LUA_REGISTRYINDEX, CUSTOM_RIDX_CURRENT_MOD_NAME);
40 int ModApiClient::l_get_last_run_mod(lua_State *L)
42 lua_rawgeti(L, LUA_REGISTRYINDEX, CUSTOM_RIDX_CURRENT_MOD_NAME);
43 const char *current_mod = lua_tostring(L, -1);
44 if (current_mod == NULL || current_mod[0] == '\0') {
46 lua_pushstring(L, getScriptApiBase(L)->getOrigin().c_str());
51 // set_last_run_mod(modname)
52 int ModApiClient::l_set_last_run_mod(lua_State *L)
54 if (!lua_isstring(L, 1))
57 const char *mod = lua_tostring(L, 1);
58 getScriptApiBase(L)->setOriginDirect(mod);
59 lua_pushboolean(L, true);
63 // display_chat_message(message)
64 int ModApiClient::l_display_chat_message(lua_State *L)
66 if (!lua_isstring(L, 1))
69 std::string message = luaL_checkstring(L, 1);
70 getClient(L)->pushToChatQueue(utf8_to_wide(message));
71 lua_pushboolean(L, true);
76 int ModApiClient::l_get_player_names(lua_State *L)
78 const std::list<std::string> &plist = getClient(L)->getConnectedPlayerNames();
79 lua_createtable(L, plist.size(), 0);
80 int newTable = lua_gettop(L);
82 std::list<std::string>::const_iterator iter;
83 for (iter = plist.begin(); iter != plist.end(); iter++) {
84 lua_pushstring(L, (*iter).c_str());
85 lua_rawseti(L, newTable, index);
91 // show_formspec(formspec)
92 int ModApiClient::l_show_formspec(lua_State *L)
94 if (!lua_isstring(L, 1) || !lua_isstring(L, 2))
98 event.type = CE_SHOW_LOCAL_FORMSPEC;
99 event.show_formspec.formname = new std::string(luaL_checkstring(L, 1));
100 event.show_formspec.formspec = new std::string(luaL_checkstring(L, 2));
101 getClient(L)->pushToEventQueue(event);
102 lua_pushboolean(L, true);
107 int ModApiClient::l_send_respawn(lua_State *L)
109 getClient(L)->sendRespawn();
114 int ModApiClient::l_disconnect(lua_State *L)
116 // Stops badly written Lua code form causing boot loops
117 if (getClient(L)->isShutdown()) {
118 lua_pushboolean(L, false);
122 g_gamecallback->disconnect();
123 lua_pushboolean(L, true);
128 int ModApiClient::l_gettext(lua_State *L)
130 std::string text = strgettext(std::string(luaL_checkstring(L, 1)));
131 lua_pushstring(L, text.c_str());
137 // pos = {x=num, y=num, z=num}
138 int ModApiClient::l_get_node(lua_State *L)
141 v3s16 pos = read_v3s16(L, 1);
144 MapNode n = getClient(L)->getNode(pos, &pos_ok);
146 pushnode(L, n, getClient(L)->ndef());
150 // get_node_or_nil(pos)
151 // pos = {x=num, y=num, z=num}
152 int ModApiClient::l_get_node_or_nil(lua_State *L)
155 v3s16 pos = read_v3s16(L, 1);
158 MapNode n = getClient(L)->getNode(pos, &pos_ok);
161 pushnode(L, n, getClient(L)->ndef());
168 int ModApiClient::l_get_wielded_item(lua_State *L)
170 Client *client = getClient(L);
172 Inventory local_inventory(client->idef());
173 client->getLocalInventory(local_inventory);
175 InventoryList *mlist = local_inventory.getList("main");
177 if (mlist && client->getPlayerItem() < mlist->getSize()) {
178 LuaItemStack::create(L, mlist->getItem(client->getPlayerItem()));
180 LuaItemStack::create(L, ItemStack());
185 void ModApiClient::Initialize(lua_State *L, int top)
187 API_FCT(get_current_modname);
188 API_FCT(display_chat_message);
189 API_FCT(get_player_names);
190 API_FCT(set_last_run_mod);
191 API_FCT(get_last_run_mod);
192 API_FCT(show_formspec);
193 API_FCT(send_respawn);
196 API_FCT(get_node_or_nil);
197 API_FCT(get_wielded_item);