3 Copyright (C) 2013 celeron55, Perttu Ahola <celeron55@gmail.com>
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.
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.
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.
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"
25 #include "util/string.h"
27 void ScriptApiPlayer::on_newplayer(ServerActiveObject *player)
29 SCRIPTAPI_PRECHECKHEADER
31 // Get core.registered_on_newplayers
32 lua_getglobal(L, "core");
33 lua_getfield(L, -1, "registered_on_newplayers");
35 objectrefGetOrCreate(L, player);
36 runCallbacks(1, RUN_CALLBACKS_MODE_FIRST);
39 void ScriptApiPlayer::on_dieplayer(ServerActiveObject *player)
41 SCRIPTAPI_PRECHECKHEADER
43 // Get core.registered_on_dieplayers
44 lua_getglobal(L, "core");
45 lua_getfield(L, -1, "registered_on_dieplayers");
47 objectrefGetOrCreate(L, player);
48 runCallbacks(1, RUN_CALLBACKS_MODE_FIRST);
51 bool ScriptApiPlayer::on_punchplayer(ServerActiveObject *player,
52 ServerActiveObject *hitter,
53 float time_from_last_punch,
54 const ToolCapabilities *toolcap,
58 SCRIPTAPI_PRECHECKHEADER
59 // Get core.registered_on_punchplayers
60 lua_getglobal(L, "core");
61 lua_getfield(L, -1, "registered_on_punchplayers");
63 objectrefGetOrCreate(L, player);
64 objectrefGetOrCreate(L, hitter);
65 lua_pushnumber(L, time_from_last_punch);
66 push_tool_capabilities(L, *toolcap);
68 lua_pushnumber(L, damage);
69 runCallbacks(6, RUN_CALLBACKS_MODE_OR);
70 return lua_toboolean(L, -1);
73 s16 ScriptApiPlayer::on_player_hpchange(ServerActiveObject *player,
76 SCRIPTAPI_PRECHECKHEADER
78 int error_handler = PUSH_ERROR_HANDLER(L);
80 // Get core.registered_on_player_hpchange
81 lua_getglobal(L, "core");
82 lua_getfield(L, -1, "registered_on_player_hpchange");
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
93 bool ScriptApiPlayer::on_respawnplayer(ServerActiveObject *player)
95 SCRIPTAPI_PRECHECKHEADER
97 // Get core.registered_on_respawnplayers
98 lua_getglobal(L, "core");
99 lua_getfield(L, -1, "registered_on_respawnplayers");
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;
107 bool ScriptApiPlayer::on_prejoinplayer(
108 const std::string &name,
109 const std::string &ip,
112 SCRIPTAPI_PRECHECKHEADER
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));
127 bool ScriptApiPlayer::can_bypass_userlimit(const std::string &name, const std::string &ip)
129 SCRIPTAPI_PRECHECKHEADER
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 return lua_toboolean(L, -1);
140 void ScriptApiPlayer::on_joinplayer(ServerActiveObject *player)
142 SCRIPTAPI_PRECHECKHEADER
144 // Get core.registered_on_joinplayers
145 lua_getglobal(L, "core");
146 lua_getfield(L, -1, "registered_on_joinplayers");
148 objectrefGetOrCreate(L, player);
149 runCallbacks(1, RUN_CALLBACKS_MODE_FIRST);
152 void ScriptApiPlayer::on_leaveplayer(ServerActiveObject *player,
155 SCRIPTAPI_PRECHECKHEADER
157 // Get core.registered_on_leaveplayers
158 lua_getglobal(L, "core");
159 lua_getfield(L, -1, "registered_on_leaveplayers");
161 objectrefGetOrCreate(L, player);
162 lua_pushboolean(L, timeout);
163 runCallbacks(2, RUN_CALLBACKS_MODE_FIRST);
166 void ScriptApiPlayer::on_cheat(ServerActiveObject *player,
167 const std::string &cheat_type)
169 SCRIPTAPI_PRECHECKHEADER
171 // Get core.registered_on_cheats
172 lua_getglobal(L, "core");
173 lua_getfield(L, -1, "registered_on_cheats");
175 objectrefGetOrCreate(L, player);
177 lua_pushlstring(L, cheat_type.c_str(), cheat_type.size());
178 lua_setfield(L, -2, "type");
179 runCallbacks(2, RUN_CALLBACKS_MODE_FIRST);
182 void ScriptApiPlayer::on_playerReceiveFields(ServerActiveObject *player,
183 const std::string &formname,
184 const StringMap &fields)
186 SCRIPTAPI_PRECHECKHEADER
188 // Get core.registered_on_chat_messages
189 lua_getglobal(L, "core");
190 lua_getfield(L, -1, "registered_on_player_receive_fields");
193 objectrefGetOrCreate(L, player);
195 lua_pushstring(L, formname.c_str());
198 StringMap::const_iterator it;
199 for (it = fields.begin(); it != fields.end(); ++it) {
200 const std::string &name = it->first;
201 const std::string &value = it->second;
202 lua_pushstring(L, name.c_str());
203 lua_pushlstring(L, value.c_str(), value.size());
206 runCallbacks(3, RUN_CALLBACKS_MODE_OR_SC);
209 void ScriptApiPlayer::on_auth_failure(const std::string &name, const std::string &ip)
211 SCRIPTAPI_PRECHECKHEADER
213 // Get core.registered_on_auth_failure
214 lua_getglobal(L, "core");
215 lua_getfield(L, -1, "registered_on_auth_fail");
216 lua_pushstring(L, name.c_str());
217 lua_pushstring(L, ip.c_str());
218 runCallbacks(2, RUN_CALLBACKS_MODE_FIRST);