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 const char *current_mod = lua_tostring(L, -1);
50 if (current_mod == NULL || current_mod[0] == '\0') {
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
97 if (getClient(L)->checkCSMFlavourLimit(CSMFlavourLimit::CSM_FL_CHAT_MESSAGES))
100 std::string message = luaL_checkstring(L, 1);
101 getClient(L)->sendChatMessage(utf8_to_wide(message));
105 // clear_out_chat_queue()
106 int ModApiClient::l_clear_out_chat_queue(lua_State *L)
108 getClient(L)->clearOutChatQueue();
112 // get_player_names()
113 int ModApiClient::l_get_player_names(lua_State *L)
115 const std::list<std::string> &plist = getClient(L)->getConnectedPlayerNames();
116 lua_createtable(L, plist.size(), 0);
117 int newTable = lua_gettop(L);
119 std::list<std::string>::const_iterator iter;
120 for (iter = plist.begin(); iter != plist.end(); ++iter) {
121 lua_pushstring(L, (*iter).c_str());
122 lua_rawseti(L, newTable, index);
128 // show_formspec(formspec)
129 int ModApiClient::l_show_formspec(lua_State *L)
131 if (!lua_isstring(L, 1) || !lua_isstring(L, 2))
134 ClientEvent *event = new ClientEvent();
135 event->type = CE_SHOW_LOCAL_FORMSPEC;
136 event->show_formspec.formname = new std::string(luaL_checkstring(L, 1));
137 event->show_formspec.formspec = new std::string(luaL_checkstring(L, 2));
138 getClient(L)->pushToEventQueue(event);
139 lua_pushboolean(L, true);
144 int ModApiClient::l_send_respawn(lua_State *L)
146 getClient(L)->sendRespawn();
151 int ModApiClient::l_disconnect(lua_State *L)
153 // Stops badly written Lua code form causing boot loops
154 if (getClient(L)->isShutdown()) {
155 lua_pushboolean(L, false);
159 g_gamecallback->disconnect();
160 lua_pushboolean(L, true);
165 int ModApiClient::l_gettext(lua_State *L)
167 std::string text = strgettext(std::string(luaL_checkstring(L, 1)));
168 lua_pushstring(L, text.c_str());
174 // pos = {x=num, y=num, z=num}
175 int ModApiClient::l_get_node_or_nil(lua_State *L)
178 v3s16 pos = read_v3s16(L, 1);
182 MapNode n = getClient(L)->getNode(pos, &pos_ok);
185 pushnode(L, n, getClient(L)->ndef());
192 int ModApiClient::l_get_language(lua_State *L)
194 char *locale = setlocale(LC_ALL, "");
195 lua_pushstring(L, locale);
199 int ModApiClient::l_get_wielded_item(lua_State *L)
201 Client *client = getClient(L);
203 Inventory local_inventory(client->idef());
204 client->getLocalInventory(local_inventory);
206 InventoryList *mlist = local_inventory.getList("main");
208 if (mlist && client->getPlayerItem() < mlist->getSize()) {
209 LuaItemStack::create(L, mlist->getItem(client->getPlayerItem()));
211 LuaItemStack::create(L, ItemStack());
217 int ModApiClient::l_get_meta(lua_State *L)
219 v3s16 p = read_v3s16(L, 1);
220 NodeMetadata *meta = getClient(L)->getEnv().getMap().getNodeMetadata(p);
221 NodeMetaRef::createClient(L, meta);
225 int ModApiClient::l_sound_play(lua_State *L)
227 ISoundManager *sound = getClient(L)->getSoundManager();
229 SimpleSoundSpec spec;
230 read_soundspec(L, 1, spec);
236 if (lua_istable(L, 2)) {
237 getfloatfield(L, 2, "gain", gain);
238 getfloatfield(L, 2, "pitch", pitch);
239 getboolfield(L, 2, "loop", looped);
241 lua_getfield(L, 2, "pos");
242 if (!lua_isnil(L, -1)) {
243 v3f pos = read_v3f(L, -1) * BS;
245 handle = sound->playSoundAt(
246 spec.name, looped, gain * spec.gain, pos, pitch);
247 lua_pushinteger(L, handle);
252 handle = sound->playSound(spec.name, looped, gain * spec.gain, 0.0f, pitch);
253 lua_pushinteger(L, handle);
258 int ModApiClient::l_sound_stop(lua_State *L)
260 u32 handle = luaL_checkinteger(L, 1);
262 getClient(L)->getSoundManager()->stopSound(handle);
268 int ModApiClient::l_get_server_info(lua_State *L)
270 Client *client = getClient(L);
271 Address serverAddress = client->getServerAddress();
273 lua_pushstring(L, client->getAddressName().c_str());
274 lua_setfield(L, -2, "address");
275 lua_pushstring(L, serverAddress.serializeString().c_str());
276 lua_setfield(L, -2, "ip");
277 lua_pushinteger(L, serverAddress.getPort());
278 lua_setfield(L, -2, "port");
279 lua_pushinteger(L, client->getProtoVersion());
280 lua_setfield(L, -2, "protocol_version");
284 // get_item_def(itemstring)
285 int ModApiClient::l_get_item_def(lua_State *L)
287 IGameDef *gdef = getGameDef(L);
290 IItemDefManager *idef = gdef->idef();
293 if (getClient(L)->checkCSMFlavourLimit(CSMFlavourLimit::CSM_FL_READ_ITEMDEFS))
296 if (!lua_isstring(L, 1))
299 const std::string &name(lua_tostring(L, 1));
300 if (!idef->isKnown(name))
302 const ItemDefinition &def = idef->get(name);
304 push_item_definition_full(L, def);
309 // get_node_def(nodename)
310 int ModApiClient::l_get_node_def(lua_State *L)
312 IGameDef *gdef = getGameDef(L);
315 const NodeDefManager *ndef = gdef->ndef();
318 if (!lua_isstring(L, 1))
321 if (getClient(L)->checkCSMFlavourLimit(CSMFlavourLimit::CSM_FL_READ_NODEDEFS))
324 const std::string &name = lua_tostring(L, 1);
325 const ContentFeatures &cf = ndef->get(ndef->getId(name));
326 if (cf.name != name) // Unknown node. | name = <whatever>, cf.name = ignore
329 push_content_features(L, cf);
334 int ModApiClient::l_get_privilege_list(lua_State *L)
336 const Client *client = getClient(L);
338 for (const std::string &priv : client->getPrivilegeList()) {
339 lua_pushboolean(L, true);
340 lua_setfield(L, -2, priv.c_str());
345 // get_builtin_path()
346 int ModApiClient::l_get_builtin_path(lua_State *L)
348 lua_pushstring(L, BUILTIN_MOD_NAME ":");
352 void ModApiClient::Initialize(lua_State *L, int top)
354 API_FCT(get_current_modname);
356 API_FCT(display_chat_message);
357 API_FCT(send_chat_message);
358 API_FCT(clear_out_chat_queue);
359 API_FCT(get_player_names);
360 API_FCT(set_last_run_mod);
361 API_FCT(get_last_run_mod);
362 API_FCT(show_formspec);
363 API_FCT(send_respawn);
365 API_FCT(get_node_or_nil);
366 API_FCT(get_wielded_item);
371 API_FCT(get_server_info);
372 API_FCT(get_item_def);
373 API_FCT(get_node_def);
374 API_FCT(get_privilege_list);
375 API_FCT(get_builtin_path);
376 API_FCT(get_language);