]> git.lizzy.rs Git - dragonfireclient.git/blob - src/script/cpp_api/s_player.cpp
Implement minetest.register_can_bypass_userlimit (#6369)
[dragonfireclient.git] / src / script / cpp_api / s_player.cpp
1 /*
2 Minetest
3 Copyright (C) 2013 celeron55, Perttu Ahola <celeron55@gmail.com>
4
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU Lesser General Public License as published by
7 the Free Software Foundation; either version 2.1 of the License, or
8 (at your option) any later version.
9
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 GNU Lesser General Public License for more details.
14
15 You should have received a copy of the GNU Lesser General Public License along
16 with this program; if not, write to the Free Software Foundation, Inc.,
17 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18 */
19
20 #include "cpp_api/s_player.h"
21 #include "cpp_api/s_internal.h"
22 #include "common/c_converter.h"
23 #include "common/c_content.h"
24 #include "debug.h"
25 #include "util/string.h"
26
27 void ScriptApiPlayer::on_newplayer(ServerActiveObject *player)
28 {
29         SCRIPTAPI_PRECHECKHEADER
30
31         // Get core.registered_on_newplayers
32         lua_getglobal(L, "core");
33         lua_getfield(L, -1, "registered_on_newplayers");
34         // Call callbacks
35         objectrefGetOrCreate(L, player);
36         runCallbacks(1, RUN_CALLBACKS_MODE_FIRST);
37 }
38
39 void ScriptApiPlayer::on_dieplayer(ServerActiveObject *player)
40 {
41         SCRIPTAPI_PRECHECKHEADER
42
43         // Get core.registered_on_dieplayers
44         lua_getglobal(L, "core");
45         lua_getfield(L, -1, "registered_on_dieplayers");
46         // Call callbacks
47         objectrefGetOrCreate(L, player);
48         runCallbacks(1, RUN_CALLBACKS_MODE_FIRST);
49 }
50
51 bool ScriptApiPlayer::on_punchplayer(ServerActiveObject *player,
52                 ServerActiveObject *hitter,
53                 float time_from_last_punch,
54                 const ToolCapabilities *toolcap,
55                 v3f dir,
56                 s16 damage)
57 {
58         SCRIPTAPI_PRECHECKHEADER
59         // Get core.registered_on_punchplayers
60         lua_getglobal(L, "core");
61         lua_getfield(L, -1, "registered_on_punchplayers");
62         // Call callbacks
63         objectrefGetOrCreate(L, player);
64         objectrefGetOrCreate(L, hitter);
65         lua_pushnumber(L, time_from_last_punch);
66         push_tool_capabilities(L, *toolcap);
67         push_v3f(L, dir);
68         lua_pushnumber(L, damage);
69         runCallbacks(6, RUN_CALLBACKS_MODE_OR);
70         return lua_toboolean(L, -1);
71 }
72
73 s16 ScriptApiPlayer::on_player_hpchange(ServerActiveObject *player,
74         s16 hp_change)
75 {
76         SCRIPTAPI_PRECHECKHEADER
77
78         int error_handler = PUSH_ERROR_HANDLER(L);
79
80         // Get core.registered_on_player_hpchange
81         lua_getglobal(L, "core");
82         lua_getfield(L, -1, "registered_on_player_hpchange");
83         lua_remove(L, -2);
84
85         objectrefGetOrCreate(L, player);
86         lua_pushnumber(L, hp_change);
87         PCALL_RES(lua_pcall(L, 2, 1, error_handler));
88         hp_change = lua_tointeger(L, -1);
89         lua_pop(L, 2); // Pop result and error handler
90         return hp_change;
91 }
92
93 bool ScriptApiPlayer::on_respawnplayer(ServerActiveObject *player)
94 {
95         SCRIPTAPI_PRECHECKHEADER
96
97         // Get core.registered_on_respawnplayers
98         lua_getglobal(L, "core");
99         lua_getfield(L, -1, "registered_on_respawnplayers");
100         // Call callbacks
101         objectrefGetOrCreate(L, player);
102         runCallbacks(1, RUN_CALLBACKS_MODE_OR);
103         bool positioning_handled_by_some = lua_toboolean(L, -1);
104         return positioning_handled_by_some;
105 }
106
107 bool ScriptApiPlayer::on_prejoinplayer(
108         const std::string &name,
109         const std::string &ip,
110         std::string *reason)
111 {
112         SCRIPTAPI_PRECHECKHEADER
113
114         // Get core.registered_on_prejoinplayers
115         lua_getglobal(L, "core");
116         lua_getfield(L, -1, "registered_on_prejoinplayers");
117         lua_pushstring(L, name.c_str());
118         lua_pushstring(L, ip.c_str());
119         runCallbacks(2, RUN_CALLBACKS_MODE_OR);
120         if (lua_isstring(L, -1)) {
121                 reason->assign(lua_tostring(L, -1));
122                 return true;
123         }
124         return false;
125 }
126
127 bool ScriptApiPlayer::can_bypass_userlimit(const std::string &name, const std::string &ip)
128 {
129         SCRIPTAPI_PRECHECKHEADER
130
131         // Get core.registered_on_prejoinplayers
132         lua_getglobal(L, "core");
133         lua_getfield(L, -1, "registered_can_bypass_userlimit");
134         lua_pushstring(L, name.c_str());
135         lua_pushstring(L, ip.c_str());
136         runCallbacks(2, RUN_CALLBACKS_MODE_OR);
137         FATAL_ERROR_IF(!lua_isboolean(L, -1), "on_user_limitcheck must return a boolean");
138         return lua_toboolean(L, -1);
139 }
140
141 void ScriptApiPlayer::on_joinplayer(ServerActiveObject *player)
142 {
143         SCRIPTAPI_PRECHECKHEADER
144
145         // Get core.registered_on_joinplayers
146         lua_getglobal(L, "core");
147         lua_getfield(L, -1, "registered_on_joinplayers");
148         // Call callbacks
149         objectrefGetOrCreate(L, player);
150         runCallbacks(1, RUN_CALLBACKS_MODE_FIRST);
151 }
152
153 void ScriptApiPlayer::on_leaveplayer(ServerActiveObject *player,
154                 bool timeout)
155 {
156         SCRIPTAPI_PRECHECKHEADER
157
158         // Get core.registered_on_leaveplayers
159         lua_getglobal(L, "core");
160         lua_getfield(L, -1, "registered_on_leaveplayers");
161         // Call callbacks
162         objectrefGetOrCreate(L, player);
163         lua_pushboolean(L, timeout);
164         runCallbacks(2, RUN_CALLBACKS_MODE_FIRST);
165 }
166
167 void ScriptApiPlayer::on_cheat(ServerActiveObject *player,
168                 const std::string &cheat_type)
169 {
170         SCRIPTAPI_PRECHECKHEADER
171
172         // Get core.registered_on_cheats
173         lua_getglobal(L, "core");
174         lua_getfield(L, -1, "registered_on_cheats");
175         // Call callbacks
176         objectrefGetOrCreate(L, player);
177         lua_newtable(L);
178         lua_pushlstring(L, cheat_type.c_str(), cheat_type.size());
179         lua_setfield(L, -2, "type");
180         runCallbacks(2, RUN_CALLBACKS_MODE_FIRST);
181 }
182
183 void ScriptApiPlayer::on_playerReceiveFields(ServerActiveObject *player,
184                 const std::string &formname,
185                 const StringMap &fields)
186 {
187         SCRIPTAPI_PRECHECKHEADER
188
189         // Get core.registered_on_chat_messages
190         lua_getglobal(L, "core");
191         lua_getfield(L, -1, "registered_on_player_receive_fields");
192         // Call callbacks
193         // param 1
194         objectrefGetOrCreate(L, player);
195         // param 2
196         lua_pushstring(L, formname.c_str());
197         // param 3
198         lua_newtable(L);
199         StringMap::const_iterator it;
200         for (it = fields.begin(); it != fields.end(); ++it) {
201                 const std::string &name = it->first;
202                 const std::string &value = it->second;
203                 lua_pushstring(L, name.c_str());
204                 lua_pushlstring(L, value.c_str(), value.size());
205                 lua_settable(L, -3);
206         }
207         runCallbacks(3, RUN_CALLBACKS_MODE_OR_SC);
208 }
209