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, const PlayerHPChangeReason &reason)
41 SCRIPTAPI_PRECHECKHEADER
44 lua_getglobal(L, "core");
45 lua_getfield(L, -1, "registered_on_dieplayers");
48 objectrefGetOrCreate(L, player);
49 pushPlayerHPChangeReason(L, reason);
52 runCallbacks(2, RUN_CALLBACKS_MODE_FIRST);
55 bool ScriptApiPlayer::on_punchplayer(ServerActiveObject *player,
56 ServerActiveObject *hitter,
57 float time_from_last_punch,
58 const ToolCapabilities *toolcap,
62 SCRIPTAPI_PRECHECKHEADER
63 // Get core.registered_on_punchplayers
64 lua_getglobal(L, "core");
65 lua_getfield(L, -1, "registered_on_punchplayers");
67 objectrefGetOrCreate(L, player);
68 objectrefGetOrCreate(L, hitter);
69 lua_pushnumber(L, time_from_last_punch);
70 push_tool_capabilities(L, *toolcap);
72 lua_pushnumber(L, damage);
73 runCallbacks(6, RUN_CALLBACKS_MODE_OR);
74 return lua_toboolean(L, -1);
77 s16 ScriptApiPlayer::on_player_hpchange(ServerActiveObject *player,
78 s16 hp_change, const PlayerHPChangeReason &reason)
80 SCRIPTAPI_PRECHECKHEADER
82 int error_handler = PUSH_ERROR_HANDLER(L);
84 // Get core.registered_on_player_hpchange
85 lua_getglobal(L, "core");
86 lua_getfield(L, -1, "registered_on_player_hpchange");
90 objectrefGetOrCreate(L, player);
91 lua_pushnumber(L, hp_change);
92 pushPlayerHPChangeReason(L, reason);
95 PCALL_RES(lua_pcall(L, 3, 1, error_handler));
96 hp_change = lua_tointeger(L, -1);
97 lua_pop(L, 2); // Pop result and error handler
101 bool ScriptApiPlayer::on_respawnplayer(ServerActiveObject *player)
103 SCRIPTAPI_PRECHECKHEADER
105 // Get core.registered_on_respawnplayers
106 lua_getglobal(L, "core");
107 lua_getfield(L, -1, "registered_on_respawnplayers");
109 objectrefGetOrCreate(L, player);
110 runCallbacks(1, RUN_CALLBACKS_MODE_OR);
111 bool positioning_handled_by_some = lua_toboolean(L, -1);
112 return positioning_handled_by_some;
115 bool ScriptApiPlayer::on_prejoinplayer(
116 const std::string &name,
117 const std::string &ip,
120 SCRIPTAPI_PRECHECKHEADER
122 // Get core.registered_on_prejoinplayers
123 lua_getglobal(L, "core");
124 lua_getfield(L, -1, "registered_on_prejoinplayers");
125 lua_pushstring(L, name.c_str());
126 lua_pushstring(L, ip.c_str());
127 runCallbacks(2, RUN_CALLBACKS_MODE_OR);
128 if (lua_isstring(L, -1)) {
129 reason->assign(lua_tostring(L, -1));
135 bool ScriptApiPlayer::can_bypass_userlimit(const std::string &name, const std::string &ip)
137 SCRIPTAPI_PRECHECKHEADER
139 // Get core.registered_on_prejoinplayers
140 lua_getglobal(L, "core");
141 lua_getfield(L, -1, "registered_can_bypass_userlimit");
142 lua_pushstring(L, name.c_str());
143 lua_pushstring(L, ip.c_str());
144 runCallbacks(2, RUN_CALLBACKS_MODE_OR);
145 return lua_toboolean(L, -1);
148 void ScriptApiPlayer::on_joinplayer(ServerActiveObject *player)
150 SCRIPTAPI_PRECHECKHEADER
152 // Get core.registered_on_joinplayers
153 lua_getglobal(L, "core");
154 lua_getfield(L, -1, "registered_on_joinplayers");
156 objectrefGetOrCreate(L, player);
157 runCallbacks(1, RUN_CALLBACKS_MODE_FIRST);
160 void ScriptApiPlayer::on_leaveplayer(ServerActiveObject *player,
163 SCRIPTAPI_PRECHECKHEADER
165 // Get core.registered_on_leaveplayers
166 lua_getglobal(L, "core");
167 lua_getfield(L, -1, "registered_on_leaveplayers");
169 objectrefGetOrCreate(L, player);
170 lua_pushboolean(L, timeout);
171 runCallbacks(2, RUN_CALLBACKS_MODE_FIRST);
174 void ScriptApiPlayer::on_cheat(ServerActiveObject *player,
175 const std::string &cheat_type)
177 SCRIPTAPI_PRECHECKHEADER
179 // Get core.registered_on_cheats
180 lua_getglobal(L, "core");
181 lua_getfield(L, -1, "registered_on_cheats");
183 objectrefGetOrCreate(L, player);
185 lua_pushlstring(L, cheat_type.c_str(), cheat_type.size());
186 lua_setfield(L, -2, "type");
187 runCallbacks(2, RUN_CALLBACKS_MODE_FIRST);
190 void ScriptApiPlayer::on_playerReceiveFields(ServerActiveObject *player,
191 const std::string &formname,
192 const StringMap &fields)
194 SCRIPTAPI_PRECHECKHEADER
196 // Get core.registered_on_chat_messages
197 lua_getglobal(L, "core");
198 lua_getfield(L, -1, "registered_on_player_receive_fields");
201 objectrefGetOrCreate(L, player);
203 lua_pushstring(L, formname.c_str());
206 StringMap::const_iterator it;
207 for (it = fields.begin(); it != fields.end(); ++it) {
208 const std::string &name = it->first;
209 const std::string &value = it->second;
210 lua_pushstring(L, name.c_str());
211 lua_pushlstring(L, value.c_str(), value.size());
214 runCallbacks(3, RUN_CALLBACKS_MODE_OR_SC);
217 void ScriptApiPlayer::on_auth_failure(const std::string &name, const std::string &ip)
219 SCRIPTAPI_PRECHECKHEADER
221 // Get core.registered_on_auth_failure
222 lua_getglobal(L, "core");
223 lua_getfield(L, -1, "registered_on_auth_fail");
224 lua_pushstring(L, name.c_str());
225 lua_pushstring(L, ip.c_str());
226 runCallbacks(2, RUN_CALLBACKS_MODE_FIRST);