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"
26 #include "common/c_converter.h"
27 #include "common/c_content.h"
28 #include "lua_api/l_item.h"
30 int ModApiClient::l_get_current_modname(lua_State *L)
32 lua_rawgeti(L, LUA_REGISTRYINDEX, CUSTOM_RIDX_CURRENT_MOD_NAME);
37 int ModApiClient::l_get_last_run_mod(lua_State *L)
39 lua_rawgeti(L, LUA_REGISTRYINDEX, CUSTOM_RIDX_CURRENT_MOD_NAME);
40 const char *current_mod = lua_tostring(L, -1);
41 if (current_mod == NULL || current_mod[0] == '\0') {
43 lua_pushstring(L, getScriptApiBase(L)->getOrigin().c_str());
48 // set_last_run_mod(modname)
49 int ModApiClient::l_set_last_run_mod(lua_State *L)
51 if (!lua_isstring(L, 1))
54 const char *mod = lua_tostring(L, 1);
55 getScriptApiBase(L)->setOriginDirect(mod);
56 lua_pushboolean(L, true);
60 // display_chat_message(message)
61 int ModApiClient::l_display_chat_message(lua_State *L)
63 if (!lua_isstring(L, 1))
66 std::string message = luaL_checkstring(L, 1);
67 getClient(L)->pushToChatQueue(utf8_to_wide(message));
68 lua_pushboolean(L, true);
73 int ModApiClient::l_get_player_names(lua_State *L)
75 const std::list<std::string> &plist = getClient(L)->getConnectedPlayerNames();
76 lua_createtable(L, plist.size(), 0);
77 int newTable = lua_gettop(L);
79 std::list<std::string>::const_iterator iter;
80 for (iter = plist.begin(); iter != plist.end(); iter++) {
81 lua_pushstring(L, (*iter).c_str());
82 lua_rawseti(L, newTable, index);
88 // show_formspec(formspec)
89 int ModApiClient::l_show_formspec(lua_State *L)
91 if ( !lua_isstring(L, 1) || !lua_isstring(L, 2) )
95 event.type = CE_SHOW_LOCAL_FORMSPEC;
96 event.show_formspec.formname = new std::string(luaL_checkstring(L, 1));
97 event.show_formspec.formspec = new std::string(luaL_checkstring(L, 2));
98 getClient(L)->pushToEventQueue(event);
99 lua_pushboolean(L, true);
104 int ModApiClient::l_send_respawn(lua_State *L)
106 getClient(L)->sendRespawn();
111 int ModApiClient::l_gettext(lua_State *L)
113 std::string text = strgettext(std::string(luaL_checkstring(L, 1)));
114 lua_pushstring(L, text.c_str());
120 // pos = {x=num, y=num, z=num}
121 int ModApiClient::l_get_node(lua_State *L)
124 v3s16 pos = read_v3s16(L, 1);
127 MapNode n = getClient(L)->getNode(pos, &pos_ok);
129 pushnode(L, n, getClient(L)->ndef());
133 // get_node_or_nil(pos)
134 // pos = {x=num, y=num, z=num}
135 int ModApiClient::l_get_node_or_nil(lua_State *L)
138 v3s16 pos = read_v3s16(L, 1);
141 MapNode n = getClient(L)->getNode(pos, &pos_ok);
144 pushnode(L, n, getClient(L)->ndef());
152 int ModApiClient::l_get_wielded_item(lua_State *L)
154 Client *client = getClient(L);
156 Inventory local_inventory(client->idef());
157 client->getLocalInventory(local_inventory);
159 InventoryList *mlist = local_inventory.getList("main");
161 if (mlist && client->getPlayerItem() < mlist->getSize()) {
162 LuaItemStack::create(L, mlist->getItem(client->getPlayerItem()));
164 LuaItemStack::create(L, ItemStack());
169 void ModApiClient::Initialize(lua_State *L, int top)
171 API_FCT(get_current_modname);
172 API_FCT(display_chat_message);
173 API_FCT(get_player_names);
174 API_FCT(set_last_run_mod);
175 API_FCT(get_last_run_mod);
176 API_FCT(show_formspec);
177 API_FCT(send_respawn);
180 API_FCT(get_node_or_nil);
181 API_FCT(get_wielded_item);