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 FATAL_ERROR_IF(!lua_isboolean(L, -1), "on_user_limitcheck must return a boolean");
138 return lua_toboolean(L, -1);
141 void ScriptApiPlayer::on_joinplayer(ServerActiveObject *player)
143 SCRIPTAPI_PRECHECKHEADER
145 // Get core.registered_on_joinplayers
146 lua_getglobal(L, "core");
147 lua_getfield(L, -1, "registered_on_joinplayers");
149 objectrefGetOrCreate(L, player);
150 runCallbacks(1, RUN_CALLBACKS_MODE_FIRST);
153 void ScriptApiPlayer::on_leaveplayer(ServerActiveObject *player,
156 SCRIPTAPI_PRECHECKHEADER
158 // Get core.registered_on_leaveplayers
159 lua_getglobal(L, "core");
160 lua_getfield(L, -1, "registered_on_leaveplayers");
162 objectrefGetOrCreate(L, player);
163 lua_pushboolean(L, timeout);
164 runCallbacks(2, RUN_CALLBACKS_MODE_FIRST);
167 void ScriptApiPlayer::on_cheat(ServerActiveObject *player,
168 const std::string &cheat_type)
170 SCRIPTAPI_PRECHECKHEADER
172 // Get core.registered_on_cheats
173 lua_getglobal(L, "core");
174 lua_getfield(L, -1, "registered_on_cheats");
176 objectrefGetOrCreate(L, player);
178 lua_pushlstring(L, cheat_type.c_str(), cheat_type.size());
179 lua_setfield(L, -2, "type");
180 runCallbacks(2, RUN_CALLBACKS_MODE_FIRST);
183 void ScriptApiPlayer::on_playerReceiveFields(ServerActiveObject *player,
184 const std::string &formname,
185 const StringMap &fields)
187 SCRIPTAPI_PRECHECKHEADER
189 // Get core.registered_on_chat_messages
190 lua_getglobal(L, "core");
191 lua_getfield(L, -1, "registered_on_player_receive_fields");
194 objectrefGetOrCreate(L, player);
196 lua_pushstring(L, formname.c_str());
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());
207 runCallbacks(3, RUN_CALLBACKS_MODE_OR_SC);