]> git.lizzy.rs Git - dragonfireclient.git/blob - src/script/lua_api/l_client.cpp
[CSM] Add function to get player names in range (#5435)
[dragonfireclient.git] / src / script / lua_api / l_client.cpp
1 /*
2 Minetest
3 Copyright (C) 2013 celeron55, Perttu Ahola <celeron55@gmail.com>
4 Copyright (C) 2017 nerzhul, Loic Blot <loic.blot@unix-experience.fr>
5
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.
10
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.
15
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.
19 */
20
21 #include "l_client.h"
22 #include "l_internal.h"
23 #include "util/string.h"
24 #include "cpp_api/s_base.h"
25 #include "gettext.h"
26 #include "common/c_converter.h"
27 #include "common/c_content.h"
28 #include "lua_api/l_item.h"
29
30 int ModApiClient::l_get_current_modname(lua_State *L)
31 {
32         lua_rawgeti(L, LUA_REGISTRYINDEX, CUSTOM_RIDX_CURRENT_MOD_NAME);
33         return 1;
34 }
35
36 // get_last_run_mod()
37 int ModApiClient::l_get_last_run_mod(lua_State *L)
38 {
39         lua_rawgeti(L, LUA_REGISTRYINDEX, CUSTOM_RIDX_CURRENT_MOD_NAME);
40         const char *current_mod = lua_tostring(L, -1);
41         if (current_mod == NULL || current_mod[0] == '\0') {
42                 lua_pop(L, 1);
43                 lua_pushstring(L, getScriptApiBase(L)->getOrigin().c_str());
44         }
45         return 1;
46 }
47
48 // set_last_run_mod(modname)
49 int ModApiClient::l_set_last_run_mod(lua_State *L)
50 {
51         if (!lua_isstring(L, 1))
52                 return 0;
53
54         const char *mod = lua_tostring(L, 1);
55         getScriptApiBase(L)->setOriginDirect(mod);
56         lua_pushboolean(L, true);
57         return 1;
58 }
59
60 // display_chat_message(message)
61 int ModApiClient::l_display_chat_message(lua_State *L)
62 {
63         if (!lua_isstring(L, 1))
64                 return 0;
65
66         std::string message = luaL_checkstring(L, 1);
67         getClient(L)->pushToChatQueue(utf8_to_wide(message));
68         lua_pushboolean(L, true);
69         return 1;
70 }
71
72 // get_player_names()
73 int ModApiClient::l_get_player_names(lua_State *L)
74 {
75         const std::list<std::string> &plist = getClient(L)->getConnectedPlayerNames();
76         lua_createtable(L, plist.size(), 0);
77         int newTable = lua_gettop(L);
78         int index = 1;
79         std::list<std::string>::const_iterator iter;
80         for (iter = plist.begin(); iter != plist.end(); iter++) {
81                 lua_pushstring(L, (*iter).c_str());
82                 lua_rawseti(L, newTable, index);
83                 index++;
84         }
85         return 1;
86 }
87
88 // show_formspec(formspec)
89 int ModApiClient::l_show_formspec(lua_State *L)
90 {
91         if ( !lua_isstring(L, 1) || !lua_isstring(L, 2) )
92                 return 0;
93
94         ClientEvent event;
95         event.type = CE_SHOW_LOCAL_FORMSPEC;
96         event.show_formspec.formname = new std::string(luaL_checkstring(L, 1));
97         event.show_formspec.formspec = new std::string(luaL_checkstring(L, 2));
98         getClient(L)->pushToEventQueue(event);
99         lua_pushboolean(L, true);
100         return 1;
101 }
102
103 // send_respawn()
104 int ModApiClient::l_send_respawn(lua_State *L)
105 {
106         getClient(L)->sendRespawn();
107         return 0;
108 }
109
110 // gettext(text)
111 int ModApiClient::l_gettext(lua_State *L)
112 {
113         std::string text = strgettext(std::string(luaL_checkstring(L, 1)));
114         lua_pushstring(L, text.c_str());
115
116         return 1;
117 }
118
119 // get_node(pos)
120 // pos = {x=num, y=num, z=num}
121 int ModApiClient::l_get_node(lua_State *L)
122 {
123         // pos
124         v3s16 pos = read_v3s16(L, 1);
125         // Do it
126         bool pos_ok;
127         MapNode n = getClient(L)->getNode(pos, &pos_ok);
128         // Return node
129         pushnode(L, n, getClient(L)->ndef());
130         return 1;
131 }
132
133 // get_node_or_nil(pos)
134 // pos = {x=num, y=num, z=num}
135 int ModApiClient::l_get_node_or_nil(lua_State *L)
136 {
137         // pos
138         v3s16 pos = read_v3s16(L, 1);
139         // Do it
140         bool pos_ok;
141         MapNode n = getClient(L)->getNode(pos, &pos_ok);
142         if (pos_ok) {
143                 // Return node
144                 pushnode(L, n, getClient(L)->ndef());
145         }
146         else {
147                 lua_pushnil(L);
148         }
149         return 1;
150 }
151
152 int ModApiClient::l_get_wielded_item(lua_State *L)
153 {
154         Client *client = getClient(L);
155
156         Inventory local_inventory(client->idef());
157         client->getLocalInventory(local_inventory);
158
159         InventoryList *mlist = local_inventory.getList("main");
160
161         if (mlist && client->getPlayerItem() < mlist->getSize()) {
162                 LuaItemStack::create(L, mlist->getItem(client->getPlayerItem()));
163         } else {
164                 LuaItemStack::create(L, ItemStack());
165         }
166         return 1;
167 }
168
169 void ModApiClient::Initialize(lua_State *L, int top)
170 {
171         API_FCT(get_current_modname);
172         API_FCT(display_chat_message);
173         API_FCT(get_player_names);
174         API_FCT(set_last_run_mod);
175         API_FCT(get_last_run_mod);
176         API_FCT(show_formspec);
177         API_FCT(send_respawn);
178         API_FCT(gettext);
179         API_FCT(get_node);
180         API_FCT(get_node_or_nil);
181         API_FCT(get_wielded_item);
182 }