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"
23 #include "client/client.h"
24 #include "client/clientevent.h"
25 #include "client/sound.h"
26 #include "client/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)
120 if (getClient(L)->checkCSMRestrictionFlag(
121 CSMRestrictionFlags::CSM_RF_READ_PLAYERINFO)) {
126 const std::list<std::string> &plist = getClient(L)->getConnectedPlayerNames();
127 lua_createtable(L, plist.size(), 0);
128 int newTable = lua_gettop(L);
130 std::list<std::string>::const_iterator iter;
131 for (iter = plist.begin(); iter != plist.end(); ++iter) {
132 lua_pushstring(L, (*iter).c_str());
133 lua_rawseti(L, newTable, index);
139 // show_formspec(formspec)
140 int ModApiClient::l_show_formspec(lua_State *L)
142 if (!lua_isstring(L, 1) || !lua_isstring(L, 2))
145 ClientEvent *event = new ClientEvent();
146 event->type = CE_SHOW_LOCAL_FORMSPEC;
147 event->show_formspec.formname = new std::string(luaL_checkstring(L, 1));
148 event->show_formspec.formspec = new std::string(luaL_checkstring(L, 2));
149 getClient(L)->pushToEventQueue(event);
150 lua_pushboolean(L, true);
155 int ModApiClient::l_send_respawn(lua_State *L)
157 getClient(L)->sendRespawn();
162 int ModApiClient::l_disconnect(lua_State *L)
164 // Stops badly written Lua code form causing boot loops
165 if (getClient(L)->isShutdown()) {
166 lua_pushboolean(L, false);
170 g_gamecallback->disconnect();
171 lua_pushboolean(L, true);
176 int ModApiClient::l_gettext(lua_State *L)
178 std::string text = strgettext(std::string(luaL_checkstring(L, 1)));
179 lua_pushstring(L, text.c_str());
185 // pos = {x=num, y=num, z=num}
186 int ModApiClient::l_get_node_or_nil(lua_State *L)
189 v3s16 pos = read_v3s16(L, 1);
193 MapNode n = getClient(L)->getNode(pos, &pos_ok);
196 pushnode(L, n, getClient(L)->ndef());
203 int ModApiClient::l_get_language(lua_State *L)
205 char *locale = setlocale(LC_ALL, "");
206 lua_pushstring(L, locale);
210 int ModApiClient::l_get_wielded_item(lua_State *L)
212 Client *client = getClient(L);
214 Inventory local_inventory(client->idef());
215 client->getLocalInventory(local_inventory);
217 InventoryList *mlist = local_inventory.getList("main");
219 if (mlist && client->getPlayerItem() < mlist->getSize()) {
220 LuaItemStack::create(L, mlist->getItem(client->getPlayerItem()));
222 LuaItemStack::create(L, ItemStack());
228 int ModApiClient::l_get_meta(lua_State *L)
230 v3s16 p = read_v3s16(L, 1);
231 NodeMetadata *meta = getClient(L)->getEnv().getMap().getNodeMetadata(p);
232 NodeMetaRef::createClient(L, meta);
236 int ModApiClient::l_sound_play(lua_State *L)
238 ISoundManager *sound = getClient(L)->getSoundManager();
240 SimpleSoundSpec spec;
241 read_soundspec(L, 1, spec);
247 if (lua_istable(L, 2)) {
248 getfloatfield(L, 2, "gain", gain);
249 getfloatfield(L, 2, "pitch", pitch);
250 getboolfield(L, 2, "loop", looped);
252 lua_getfield(L, 2, "pos");
253 if (!lua_isnil(L, -1)) {
254 v3f pos = read_v3f(L, -1) * BS;
256 handle = sound->playSoundAt(
257 spec.name, looped, gain * spec.gain, pos, pitch);
258 lua_pushinteger(L, handle);
263 handle = sound->playSound(spec.name, looped, gain * spec.gain, 0.0f, pitch);
264 lua_pushinteger(L, handle);
269 int ModApiClient::l_sound_stop(lua_State *L)
271 u32 handle = luaL_checkinteger(L, 1);
273 getClient(L)->getSoundManager()->stopSound(handle);
279 int ModApiClient::l_get_server_info(lua_State *L)
281 Client *client = getClient(L);
282 Address serverAddress = client->getServerAddress();
284 lua_pushstring(L, client->getAddressName().c_str());
285 lua_setfield(L, -2, "address");
286 lua_pushstring(L, serverAddress.serializeString().c_str());
287 lua_setfield(L, -2, "ip");
288 lua_pushinteger(L, serverAddress.getPort());
289 lua_setfield(L, -2, "port");
290 lua_pushinteger(L, client->getProtoVersion());
291 lua_setfield(L, -2, "protocol_version");
295 // get_item_def(itemstring)
296 int ModApiClient::l_get_item_def(lua_State *L)
298 IGameDef *gdef = getGameDef(L);
301 IItemDefManager *idef = gdef->idef();
305 if (getClient(L)->checkCSMRestrictionFlag(
306 CSMRestrictionFlags::CSM_RF_READ_ITEMDEFS))
310 if (!lua_isstring(L, 1))
313 std::string name = readParam<std::string>(L, 1);
314 if (!idef->isKnown(name))
316 const ItemDefinition &def = idef->get(name);
318 push_item_definition_full(L, def);
323 // get_node_def(nodename)
324 int ModApiClient::l_get_node_def(lua_State *L)
326 IGameDef *gdef = getGameDef(L);
329 const NodeDefManager *ndef = gdef->ndef();
332 if (!lua_isstring(L, 1))
336 if (getClient(L)->checkCSMRestrictionFlag(
337 CSMRestrictionFlags::CSM_RF_READ_NODEDEFS))
341 std::string name = readParam<std::string>(L, 1);
342 const ContentFeatures &cf = ndef->get(ndef->getId(name));
343 if (cf.name != name) // Unknown node. | name = <whatever>, cf.name = ignore
346 push_content_features(L, cf);
351 int ModApiClient::l_get_privilege_list(lua_State *L)
353 const Client *client = getClient(L);
355 for (const std::string &priv : client->getPrivilegeList()) {
356 lua_pushboolean(L, true);
357 lua_setfield(L, -2, priv.c_str());
362 // get_builtin_path()
363 int ModApiClient::l_get_builtin_path(lua_State *L)
365 lua_pushstring(L, BUILTIN_MOD_NAME ":");
369 void ModApiClient::Initialize(lua_State *L, int top)
371 API_FCT(get_current_modname);
373 API_FCT(display_chat_message);
374 API_FCT(send_chat_message);
375 API_FCT(clear_out_chat_queue);
376 API_FCT(get_player_names);
377 API_FCT(set_last_run_mod);
378 API_FCT(get_last_run_mod);
379 API_FCT(show_formspec);
380 API_FCT(send_respawn);
382 API_FCT(get_node_or_nil);
383 API_FCT(get_wielded_item);
388 API_FCT(get_server_info);
389 API_FCT(get_item_def);
390 API_FCT(get_node_def);
391 API_FCT(get_privilege_list);
392 API_FCT(get_builtin_path);
393 API_FCT(get_language);