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"
24 #include "util/string.h"
26 void ScriptApiPlayer::on_newplayer(ServerActiveObject *player)
28 SCRIPTAPI_PRECHECKHEADER
30 // Get core.registered_on_newplayers
31 lua_getglobal(L, "core");
32 lua_getfield(L, -1, "registered_on_newplayers");
34 objectrefGetOrCreate(L, player);
35 runCallbacks(1, RUN_CALLBACKS_MODE_FIRST);
38 void ScriptApiPlayer::on_dieplayer(ServerActiveObject *player)
40 SCRIPTAPI_PRECHECKHEADER
42 // Get core.registered_on_dieplayers
43 lua_getglobal(L, "core");
44 lua_getfield(L, -1, "registered_on_dieplayers");
46 objectrefGetOrCreate(L, player);
47 runCallbacks(1, RUN_CALLBACKS_MODE_FIRST);
50 bool ScriptApiPlayer::on_punchplayer(ServerActiveObject *player,
51 ServerActiveObject *hitter,
52 float time_from_last_punch,
53 const ToolCapabilities *toolcap,
57 SCRIPTAPI_PRECHECKHEADER
58 // Get core.registered_on_punchplayers
59 lua_getglobal(L, "core");
60 lua_getfield(L, -1, "registered_on_punchplayers");
62 objectrefGetOrCreate(L, player);
63 objectrefGetOrCreate(L, hitter);
64 lua_pushnumber(L, time_from_last_punch);
65 push_tool_capabilities(L, *toolcap);
67 lua_pushnumber(L, damage);
68 runCallbacks(6, RUN_CALLBACKS_MODE_OR);
69 return lua_toboolean(L, -1);
72 s16 ScriptApiPlayer::on_player_hpchange(ServerActiveObject *player,
75 SCRIPTAPI_PRECHECKHEADER
77 int error_handler = PUSH_ERROR_HANDLER(L);
79 // Get core.registered_on_player_hpchange
80 lua_getglobal(L, "core");
81 lua_getfield(L, -1, "registered_on_player_hpchange");
84 objectrefGetOrCreate(L, player);
85 lua_pushnumber(L, hp_change);
86 PCALL_RES(lua_pcall(L, 2, 1, error_handler));
87 hp_change = lua_tointeger(L, -1);
88 lua_pop(L, 2); // Pop result and error handler
92 bool ScriptApiPlayer::on_respawnplayer(ServerActiveObject *player)
94 SCRIPTAPI_PRECHECKHEADER
96 // Get core.registered_on_respawnplayers
97 lua_getglobal(L, "core");
98 lua_getfield(L, -1, "registered_on_respawnplayers");
100 objectrefGetOrCreate(L, player);
101 runCallbacks(1, RUN_CALLBACKS_MODE_OR);
102 bool positioning_handled_by_some = lua_toboolean(L, -1);
103 return positioning_handled_by_some;
106 bool ScriptApiPlayer::on_prejoinplayer(
107 const std::string &name,
108 const std::string &ip,
111 SCRIPTAPI_PRECHECKHEADER
113 // Get core.registered_on_prejoinplayers
114 lua_getglobal(L, "core");
115 lua_getfield(L, -1, "registered_on_prejoinplayers");
116 lua_pushstring(L, name.c_str());
117 lua_pushstring(L, ip.c_str());
118 runCallbacks(2, RUN_CALLBACKS_MODE_OR);
119 if (lua_isstring(L, -1)) {
120 reason->assign(lua_tostring(L, -1));
126 void ScriptApiPlayer::on_joinplayer(ServerActiveObject *player)
128 SCRIPTAPI_PRECHECKHEADER
130 // Get core.registered_on_joinplayers
131 lua_getglobal(L, "core");
132 lua_getfield(L, -1, "registered_on_joinplayers");
134 objectrefGetOrCreate(L, player);
135 runCallbacks(1, RUN_CALLBACKS_MODE_FIRST);
138 void ScriptApiPlayer::on_leaveplayer(ServerActiveObject *player,
141 SCRIPTAPI_PRECHECKHEADER
143 // Get core.registered_on_leaveplayers
144 lua_getglobal(L, "core");
145 lua_getfield(L, -1, "registered_on_leaveplayers");
147 objectrefGetOrCreate(L, player);
148 lua_pushboolean(L, timeout);
149 runCallbacks(2, RUN_CALLBACKS_MODE_FIRST);
152 void ScriptApiPlayer::on_cheat(ServerActiveObject *player,
153 const std::string &cheat_type)
155 SCRIPTAPI_PRECHECKHEADER
157 // Get core.registered_on_cheats
158 lua_getglobal(L, "core");
159 lua_getfield(L, -1, "registered_on_cheats");
161 objectrefGetOrCreate(L, player);
163 lua_pushlstring(L, cheat_type.c_str(), cheat_type.size());
164 lua_setfield(L, -2, "type");
165 runCallbacks(2, RUN_CALLBACKS_MODE_FIRST);
168 void ScriptApiPlayer::on_playerReceiveFields(ServerActiveObject *player,
169 const std::string &formname,
170 const StringMap &fields)
172 SCRIPTAPI_PRECHECKHEADER
174 // Get core.registered_on_chat_messages
175 lua_getglobal(L, "core");
176 lua_getfield(L, -1, "registered_on_player_receive_fields");
179 objectrefGetOrCreate(L, player);
181 lua_pushstring(L, formname.c_str());
184 StringMap::const_iterator it;
185 for (it = fields.begin(); it != fields.end(); ++it) {
186 const std::string &name = it->first;
187 const std::string &value = it->second;
188 lua_pushstring(L, name.c_str());
189 lua_pushlstring(L, value.c_str(), value.size());
192 runCallbacks(3, RUN_CALLBACKS_MODE_OR_SC);
195 ScriptApiPlayer::~ScriptApiPlayer()