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 "clientenvironment.h"
26 #include "common/c_content.h"
27 #include "common/c_converter.h"
28 #include "cpp_api/s_base.h"
30 #include "l_internal.h"
31 #include "lua_api/l_item.h"
32 #include "lua_api/l_nodemeta.h"
33 #include "gui/mainmenumanager.h"
35 #include "util/string.h"
38 int ModApiClient::l_get_current_modname(lua_State *L)
40 lua_rawgeti(L, LUA_REGISTRYINDEX, CUSTOM_RIDX_CURRENT_MOD_NAME);
45 int ModApiClient::l_get_last_run_mod(lua_State *L)
47 lua_rawgeti(L, LUA_REGISTRYINDEX, CUSTOM_RIDX_CURRENT_MOD_NAME);
48 const char *current_mod = lua_tostring(L, -1);
49 if (current_mod == NULL || current_mod[0] == '\0') {
51 lua_pushstring(L, getScriptApiBase(L)->getOrigin().c_str());
56 // set_last_run_mod(modname)
57 int ModApiClient::l_set_last_run_mod(lua_State *L)
59 if (!lua_isstring(L, 1))
62 const char *mod = lua_tostring(L, 1);
63 getScriptApiBase(L)->setOriginDirect(mod);
64 lua_pushboolean(L, true);
69 int ModApiClient::l_print(lua_State *L)
72 std::string text = luaL_checkstring(L, 1);
73 rawstream << text << std::endl;
77 // display_chat_message(message)
78 int ModApiClient::l_display_chat_message(lua_State *L)
80 if (!lua_isstring(L, 1))
83 std::string message = luaL_checkstring(L, 1);
84 getClient(L)->pushToChatQueue(new ChatMessage(utf8_to_wide(message)));
85 lua_pushboolean(L, true);
89 // send_chat_message(message)
90 int ModApiClient::l_send_chat_message(lua_State *L)
92 if (!lua_isstring(L, 1))
95 // If server disabled this API, discard
96 if (getClient(L)->checkCSMFlavourLimit(CSMFlavourLimit::CSM_FL_CHAT_MESSAGES))
99 std::string message = luaL_checkstring(L, 1);
100 getClient(L)->sendChatMessage(utf8_to_wide(message));
104 // clear_out_chat_queue()
105 int ModApiClient::l_clear_out_chat_queue(lua_State *L)
107 getClient(L)->clearOutChatQueue();
111 // get_player_names()
112 int ModApiClient::l_get_player_names(lua_State *L)
114 const std::list<std::string> &plist = getClient(L)->getConnectedPlayerNames();
115 lua_createtable(L, plist.size(), 0);
116 int newTable = lua_gettop(L);
118 std::list<std::string>::const_iterator iter;
119 for (iter = plist.begin(); iter != plist.end(); ++iter) {
120 lua_pushstring(L, (*iter).c_str());
121 lua_rawseti(L, newTable, index);
127 // show_formspec(formspec)
128 int ModApiClient::l_show_formspec(lua_State *L)
130 if (!lua_isstring(L, 1) || !lua_isstring(L, 2))
133 ClientEvent *event = new ClientEvent();
134 event->type = CE_SHOW_LOCAL_FORMSPEC;
135 event->show_formspec.formname = new std::string(luaL_checkstring(L, 1));
136 event->show_formspec.formspec = new std::string(luaL_checkstring(L, 2));
137 getClient(L)->pushToEventQueue(event);
138 lua_pushboolean(L, true);
143 int ModApiClient::l_send_respawn(lua_State *L)
145 getClient(L)->sendRespawn();
150 int ModApiClient::l_disconnect(lua_State *L)
152 // Stops badly written Lua code form causing boot loops
153 if (getClient(L)->isShutdown()) {
154 lua_pushboolean(L, false);
158 g_gamecallback->disconnect();
159 lua_pushboolean(L, true);
164 int ModApiClient::l_gettext(lua_State *L)
166 std::string text = strgettext(std::string(luaL_checkstring(L, 1)));
167 lua_pushstring(L, text.c_str());
173 // pos = {x=num, y=num, z=num}
174 int ModApiClient::l_get_node_or_nil(lua_State *L)
177 v3s16 pos = read_v3s16(L, 1);
181 MapNode n = getClient(L)->getNode(pos, &pos_ok);
184 pushnode(L, n, getClient(L)->ndef());
191 int ModApiClient::l_get_language(lua_State *L)
193 char *locale = setlocale(LC_ALL, "");
194 lua_pushstring(L, locale);
198 int ModApiClient::l_get_wielded_item(lua_State *L)
200 Client *client = getClient(L);
202 Inventory local_inventory(client->idef());
203 client->getLocalInventory(local_inventory);
205 InventoryList *mlist = local_inventory.getList("main");
207 if (mlist && client->getPlayerItem() < mlist->getSize()) {
208 LuaItemStack::create(L, mlist->getItem(client->getPlayerItem()));
210 LuaItemStack::create(L, ItemStack());
216 int ModApiClient::l_get_meta(lua_State *L)
218 v3s16 p = read_v3s16(L, 1);
219 NodeMetadata *meta = getClient(L)->getEnv().getMap().getNodeMetadata(p);
220 NodeMetaRef::createClient(L, meta);
224 int ModApiClient::l_sound_play(lua_State *L)
226 ISoundManager *sound = getClient(L)->getSoundManager();
228 SimpleSoundSpec spec;
229 read_soundspec(L, 1, spec);
235 if (lua_istable(L, 2)) {
236 getfloatfield(L, 2, "gain", gain);
237 getfloatfield(L, 2, "pitch", pitch);
238 getboolfield(L, 2, "loop", looped);
240 lua_getfield(L, 2, "pos");
241 if (!lua_isnil(L, -1)) {
242 v3f pos = read_v3f(L, -1) * BS;
244 handle = sound->playSoundAt(
245 spec.name, looped, gain * spec.gain, pos, pitch);
246 lua_pushinteger(L, handle);
251 handle = sound->playSound(spec.name, looped, gain * spec.gain, 0.0f, pitch);
252 lua_pushinteger(L, handle);
257 int ModApiClient::l_sound_stop(lua_State *L)
259 u32 handle = luaL_checkinteger(L, 1);
261 getClient(L)->getSoundManager()->stopSound(handle);
267 int ModApiClient::l_get_server_info(lua_State *L)
269 Client *client = getClient(L);
270 Address serverAddress = client->getServerAddress();
272 lua_pushstring(L, client->getAddressName().c_str());
273 lua_setfield(L, -2, "address");
274 lua_pushstring(L, serverAddress.serializeString().c_str());
275 lua_setfield(L, -2, "ip");
276 lua_pushinteger(L, serverAddress.getPort());
277 lua_setfield(L, -2, "port");
278 lua_pushinteger(L, client->getProtoVersion());
279 lua_setfield(L, -2, "protocol_version");
283 // get_item_def(itemstring)
284 int ModApiClient::l_get_item_def(lua_State *L)
286 IGameDef *gdef = getGameDef(L);
289 IItemDefManager *idef = gdef->idef();
292 if (getClient(L)->checkCSMFlavourLimit(CSMFlavourLimit::CSM_FL_READ_ITEMDEFS))
295 if (!lua_isstring(L, 1))
298 const std::string &name(lua_tostring(L, 1));
299 if (!idef->isKnown(name))
301 const ItemDefinition &def = idef->get(name);
303 push_item_definition_full(L, def);
308 // get_node_def(nodename)
309 int ModApiClient::l_get_node_def(lua_State *L)
311 IGameDef *gdef = getGameDef(L);
314 INodeDefManager *ndef = gdef->ndef();
317 if (!lua_isstring(L, 1))
320 if (getClient(L)->checkCSMFlavourLimit(CSMFlavourLimit::CSM_FL_READ_NODEDEFS))
323 const std::string &name = lua_tostring(L, 1);
324 const ContentFeatures &cf = ndef->get(ndef->getId(name));
325 if (cf.name != name) // Unknown node. | name = <whatever>, cf.name = ignore
328 push_content_features(L, cf);
333 int ModApiClient::l_take_screenshot(lua_State *L)
335 Client *client = getClient(L);
336 client->makeScreenshot();
340 int ModApiClient::l_get_privilege_list(lua_State *L)
342 const Client *client = getClient(L);
344 for (const std::string &priv : client->getPrivilegeList()) {
345 lua_pushboolean(L, true);
346 lua_setfield(L, -2, priv.c_str());
351 // get_builtin_path()
352 int ModApiClient::l_get_builtin_path(lua_State *L)
354 lua_pushstring(L, BUILTIN_MOD_NAME ":");
358 void ModApiClient::Initialize(lua_State *L, int top)
360 API_FCT(get_current_modname);
362 API_FCT(display_chat_message);
363 API_FCT(send_chat_message);
364 API_FCT(clear_out_chat_queue);
365 API_FCT(get_player_names);
366 API_FCT(set_last_run_mod);
367 API_FCT(get_last_run_mod);
368 API_FCT(show_formspec);
369 API_FCT(send_respawn);
371 API_FCT(get_node_or_nil);
372 API_FCT(get_wielded_item);
377 API_FCT(get_server_info);
378 API_FCT(get_item_def);
379 API_FCT(get_node_def);
380 API_FCT(take_screenshot);
381 API_FCT(get_privilege_list);
382 API_FCT(get_builtin_path);
383 API_FCT(get_language);