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 "chatmessage.h"
24 #include "client/clientevent.h"
25 #include "client/sound.h"
26 #include "clientenvironment.h"
27 #include "common/c_content.h"
28 #include "common/c_converter.h"
29 #include "cpp_api/s_base.h"
31 #include "l_internal.h"
32 #include "lua_api/l_item.h"
33 #include "lua_api/l_nodemeta.h"
34 #include "gui/mainmenumanager.h"
36 #include "util/string.h"
39 int ModApiClient::l_get_current_modname(lua_State *L)
41 lua_rawgeti(L, LUA_REGISTRYINDEX, CUSTOM_RIDX_CURRENT_MOD_NAME);
46 int ModApiClient::l_get_last_run_mod(lua_State *L)
48 lua_rawgeti(L, LUA_REGISTRYINDEX, CUSTOM_RIDX_CURRENT_MOD_NAME);
49 std::string current_mod = readParam<std::string>(L, -1, "");
50 if (current_mod.empty()) {
52 lua_pushstring(L, getScriptApiBase(L)->getOrigin().c_str());
57 // set_last_run_mod(modname)
58 int ModApiClient::l_set_last_run_mod(lua_State *L)
60 if (!lua_isstring(L, 1))
63 const char *mod = lua_tostring(L, 1);
64 getScriptApiBase(L)->setOriginDirect(mod);
65 lua_pushboolean(L, true);
70 int ModApiClient::l_print(lua_State *L)
73 std::string text = luaL_checkstring(L, 1);
74 rawstream << text << std::endl;
78 // display_chat_message(message)
79 int ModApiClient::l_display_chat_message(lua_State *L)
81 if (!lua_isstring(L, 1))
84 std::string message = luaL_checkstring(L, 1);
85 getClient(L)->pushToChatQueue(new ChatMessage(utf8_to_wide(message)));
86 lua_pushboolean(L, true);
90 // send_chat_message(message)
91 int ModApiClient::l_send_chat_message(lua_State *L)
93 if (!lua_isstring(L, 1))
96 // If server disabled this API, discard
99 if (getClient(L)->checkCSMRestrictionFlag(
100 CSMRestrictionFlags::CSM_RF_CHAT_MESSAGES))
104 std::string message = luaL_checkstring(L, 1);
105 getClient(L)->sendChatMessage(utf8_to_wide(message));
109 // clear_out_chat_queue()
110 int ModApiClient::l_clear_out_chat_queue(lua_State *L)
112 getClient(L)->clearOutChatQueue();
116 // get_player_names()
117 int ModApiClient::l_get_player_names(lua_State *L)
119 const std::list<std::string> &plist = getClient(L)->getConnectedPlayerNames();
120 lua_createtable(L, plist.size(), 0);
121 int newTable = lua_gettop(L);
123 std::list<std::string>::const_iterator iter;
124 for (iter = plist.begin(); iter != plist.end(); ++iter) {
125 lua_pushstring(L, (*iter).c_str());
126 lua_rawseti(L, newTable, index);
132 // show_formspec(formspec)
133 int ModApiClient::l_show_formspec(lua_State *L)
135 if (!lua_isstring(L, 1) || !lua_isstring(L, 2))
138 ClientEvent *event = new ClientEvent();
139 event->type = CE_SHOW_LOCAL_FORMSPEC;
140 event->show_formspec.formname = new std::string(luaL_checkstring(L, 1));
141 event->show_formspec.formspec = new std::string(luaL_checkstring(L, 2));
142 getClient(L)->pushToEventQueue(event);
143 lua_pushboolean(L, true);
148 int ModApiClient::l_send_respawn(lua_State *L)
150 getClient(L)->sendRespawn();
155 int ModApiClient::l_disconnect(lua_State *L)
157 // Stops badly written Lua code form causing boot loops
158 if (getClient(L)->isShutdown()) {
159 lua_pushboolean(L, false);
163 g_gamecallback->disconnect();
164 lua_pushboolean(L, true);
169 int ModApiClient::l_gettext(lua_State *L)
171 std::string text = strgettext(std::string(luaL_checkstring(L, 1)));
172 lua_pushstring(L, text.c_str());
178 // pos = {x=num, y=num, z=num}
179 int ModApiClient::l_get_node_or_nil(lua_State *L)
182 v3s16 pos = read_v3s16(L, 1);
186 MapNode n = getClient(L)->getNode(pos, &pos_ok);
189 pushnode(L, n, getClient(L)->ndef());
196 int ModApiClient::l_get_language(lua_State *L)
198 char *locale = setlocale(LC_ALL, "");
199 lua_pushstring(L, locale);
203 int ModApiClient::l_get_wielded_item(lua_State *L)
205 Client *client = getClient(L);
207 Inventory local_inventory(client->idef());
208 client->getLocalInventory(local_inventory);
210 InventoryList *mlist = local_inventory.getList("main");
212 if (mlist && client->getPlayerItem() < mlist->getSize()) {
213 LuaItemStack::create(L, mlist->getItem(client->getPlayerItem()));
215 LuaItemStack::create(L, ItemStack());
221 int ModApiClient::l_get_meta(lua_State *L)
223 v3s16 p = read_v3s16(L, 1);
224 NodeMetadata *meta = getClient(L)->getEnv().getMap().getNodeMetadata(p);
225 NodeMetaRef::createClient(L, meta);
229 int ModApiClient::l_sound_play(lua_State *L)
231 ISoundManager *sound = getClient(L)->getSoundManager();
233 SimpleSoundSpec spec;
234 read_soundspec(L, 1, spec);
240 if (lua_istable(L, 2)) {
241 getfloatfield(L, 2, "gain", gain);
242 getfloatfield(L, 2, "pitch", pitch);
243 getboolfield(L, 2, "loop", looped);
245 lua_getfield(L, 2, "pos");
246 if (!lua_isnil(L, -1)) {
247 v3f pos = read_v3f(L, -1) * BS;
249 handle = sound->playSoundAt(
250 spec.name, looped, gain * spec.gain, pos, pitch);
251 lua_pushinteger(L, handle);
256 handle = sound->playSound(spec.name, looped, gain * spec.gain, 0.0f, pitch);
257 lua_pushinteger(L, handle);
262 int ModApiClient::l_sound_stop(lua_State *L)
264 u32 handle = luaL_checkinteger(L, 1);
266 getClient(L)->getSoundManager()->stopSound(handle);
272 int ModApiClient::l_get_server_info(lua_State *L)
274 Client *client = getClient(L);
275 Address serverAddress = client->getServerAddress();
277 lua_pushstring(L, client->getAddressName().c_str());
278 lua_setfield(L, -2, "address");
279 lua_pushstring(L, serverAddress.serializeString().c_str());
280 lua_setfield(L, -2, "ip");
281 lua_pushinteger(L, serverAddress.getPort());
282 lua_setfield(L, -2, "port");
283 lua_pushinteger(L, client->getProtoVersion());
284 lua_setfield(L, -2, "protocol_version");
288 // get_item_def(itemstring)
289 int ModApiClient::l_get_item_def(lua_State *L)
291 IGameDef *gdef = getGameDef(L);
294 IItemDefManager *idef = gdef->idef();
298 if (getClient(L)->checkCSMRestrictionFlag(
299 CSMRestrictionFlags::CSM_RF_READ_ITEMDEFS))
303 if (!lua_isstring(L, 1))
306 std::string name = readParam<std::string>(L, 1);
307 if (!idef->isKnown(name))
309 const ItemDefinition &def = idef->get(name);
311 push_item_definition_full(L, def);
316 // get_node_def(nodename)
317 int ModApiClient::l_get_node_def(lua_State *L)
319 IGameDef *gdef = getGameDef(L);
322 const NodeDefManager *ndef = gdef->ndef();
325 if (!lua_isstring(L, 1))
329 if (getClient(L)->checkCSMRestrictionFlag(
330 CSMRestrictionFlags::CSM_RF_READ_NODEDEFS))
334 std::string name = readParam<std::string>(L, 1);
335 const ContentFeatures &cf = ndef->get(ndef->getId(name));
336 if (cf.name != name) // Unknown node. | name = <whatever>, cf.name = ignore
339 push_content_features(L, cf);
344 int ModApiClient::l_get_privilege_list(lua_State *L)
346 const Client *client = getClient(L);
348 for (const std::string &priv : client->getPrivilegeList()) {
349 lua_pushboolean(L, true);
350 lua_setfield(L, -2, priv.c_str());
355 // get_builtin_path()
356 int ModApiClient::l_get_builtin_path(lua_State *L)
358 lua_pushstring(L, BUILTIN_MOD_NAME ":");
362 void ModApiClient::Initialize(lua_State *L, int top)
364 API_FCT(get_current_modname);
366 API_FCT(display_chat_message);
367 API_FCT(send_chat_message);
368 API_FCT(clear_out_chat_queue);
369 API_FCT(get_player_names);
370 API_FCT(set_last_run_mod);
371 API_FCT(get_last_run_mod);
372 API_FCT(show_formspec);
373 API_FCT(send_respawn);
375 API_FCT(get_node_or_nil);
376 API_FCT(get_wielded_item);
381 API_FCT(get_server_info);
382 API_FCT(get_item_def);
383 API_FCT(get_node_def);
384 API_FCT(get_privilege_list);
385 API_FCT(get_builtin_path);
386 API_FCT(get_language);