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 "clientenvironment.h"
23 #include "common/c_content.h"
24 #include "common/c_converter.h"
25 #include "cpp_api/s_base.h"
27 #include "l_internal.h"
28 #include "lua_api/l_item.h"
29 #include "lua_api/l_nodemeta.h"
30 #include "mainmenumanager.h"
32 #include "util/string.h"
34 extern MainGameCallback *g_gamecallback;
36 int ModApiClient::l_get_current_modname(lua_State *L)
38 lua_rawgeti(L, LUA_REGISTRYINDEX, CUSTOM_RIDX_CURRENT_MOD_NAME);
43 int ModApiClient::l_get_last_run_mod(lua_State *L)
45 lua_rawgeti(L, LUA_REGISTRYINDEX, CUSTOM_RIDX_CURRENT_MOD_NAME);
46 const char *current_mod = lua_tostring(L, -1);
47 if (current_mod == NULL || current_mod[0] == '\0') {
49 lua_pushstring(L, getScriptApiBase(L)->getOrigin().c_str());
54 // set_last_run_mod(modname)
55 int ModApiClient::l_set_last_run_mod(lua_State *L)
57 if (!lua_isstring(L, 1))
60 const char *mod = lua_tostring(L, 1);
61 getScriptApiBase(L)->setOriginDirect(mod);
62 lua_pushboolean(L, true);
66 // display_chat_message(message)
67 int ModApiClient::l_display_chat_message(lua_State *L)
69 if (!lua_isstring(L, 1))
72 std::string message = luaL_checkstring(L, 1);
73 getClient(L)->pushToChatQueue(utf8_to_wide(message));
74 lua_pushboolean(L, true);
79 int ModApiClient::l_get_player_names(lua_State *L)
81 const std::list<std::string> &plist = getClient(L)->getConnectedPlayerNames();
82 lua_createtable(L, plist.size(), 0);
83 int newTable = lua_gettop(L);
85 std::list<std::string>::const_iterator iter;
86 for (iter = plist.begin(); iter != plist.end(); iter++) {
87 lua_pushstring(L, (*iter).c_str());
88 lua_rawseti(L, newTable, index);
94 // show_formspec(formspec)
95 int ModApiClient::l_show_formspec(lua_State *L)
97 if (!lua_isstring(L, 1) || !lua_isstring(L, 2))
101 event.type = CE_SHOW_LOCAL_FORMSPEC;
102 event.show_formspec.formname = new std::string(luaL_checkstring(L, 1));
103 event.show_formspec.formspec = new std::string(luaL_checkstring(L, 2));
104 getClient(L)->pushToEventQueue(event);
105 lua_pushboolean(L, true);
110 int ModApiClient::l_send_respawn(lua_State *L)
112 getClient(L)->sendRespawn();
117 int ModApiClient::l_disconnect(lua_State *L)
119 // Stops badly written Lua code form causing boot loops
120 if (getClient(L)->isShutdown()) {
121 lua_pushboolean(L, false);
125 g_gamecallback->disconnect();
126 lua_pushboolean(L, true);
131 int ModApiClient::l_gettext(lua_State *L)
133 std::string text = strgettext(std::string(luaL_checkstring(L, 1)));
134 lua_pushstring(L, text.c_str());
140 // pos = {x=num, y=num, z=num}
141 int ModApiClient::l_get_node(lua_State *L)
144 v3s16 pos = read_v3s16(L, 1);
147 MapNode n = getClient(L)->getNode(pos, &pos_ok);
149 pushnode(L, n, getClient(L)->ndef());
153 // get_node_or_nil(pos)
154 // pos = {x=num, y=num, z=num}
155 int ModApiClient::l_get_node_or_nil(lua_State *L)
158 v3s16 pos = read_v3s16(L, 1);
161 MapNode n = getClient(L)->getNode(pos, &pos_ok);
164 pushnode(L, n, getClient(L)->ndef());
171 int ModApiClient::l_get_wielded_item(lua_State *L)
173 Client *client = getClient(L);
175 Inventory local_inventory(client->idef());
176 client->getLocalInventory(local_inventory);
178 InventoryList *mlist = local_inventory.getList("main");
180 if (mlist && client->getPlayerItem() < mlist->getSize()) {
181 LuaItemStack::create(L, mlist->getItem(client->getPlayerItem()));
183 LuaItemStack::create(L, ItemStack());
189 int ModApiClient::l_get_meta(lua_State *L)
191 v3s16 p = read_v3s16(L, 1);
192 NodeMetadata *meta = getClient(L)->getEnv().getMap().getNodeMetadata(p);
193 NodeMetaRef::createClient(L, meta);
197 int ModApiClient::l_sound_play(lua_State *L)
199 ISoundManager *sound = getClient(L)->getSoundManager();
201 SimpleSoundSpec spec;
202 read_soundspec(L, 1, spec);
207 if (lua_istable(L, 2)) {
208 getfloatfield(L, 2, "gain", gain);
209 getboolfield(L, 2, "loop", looped);
211 lua_getfield(L, 2, "pos");
212 if (!lua_isnil(L, -1)) {
213 v3f pos = read_v3f(L, -1) * BS;
216 sound->playSoundAt(spec.name, looped, gain * spec.gain, pos);
217 lua_pushinteger(L, handle);
222 handle = sound->playSound(spec.name, looped, gain * spec.gain);
223 lua_pushinteger(L, handle);
228 int ModApiClient::l_sound_stop(lua_State *L)
230 u32 handle = luaL_checkinteger(L, 1);
232 getClient(L)->getSoundManager()->stopSound(handle);
237 // get_protocol_version()
238 int ModApiClient::l_get_protocol_version(lua_State *L)
240 lua_pushinteger(L, getClient(L)->getProtoVersion());
244 void ModApiClient::Initialize(lua_State *L, int top)
246 API_FCT(get_current_modname);
247 API_FCT(display_chat_message);
248 API_FCT(get_player_names);
249 API_FCT(set_last_run_mod);
250 API_FCT(get_last_run_mod);
251 API_FCT(show_formspec);
252 API_FCT(send_respawn);
255 API_FCT(get_node_or_nil);
256 API_FCT(get_wielded_item);
261 API_FCT(get_protocol_version);