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 script_run_callbacks(L, 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 script_run_callbacks(L, 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 script_run_callbacks(L, 6, RUN_CALLBACKS_MODE_OR);
69 return lua_toboolean(L, -1);
72 s16 ScriptApiPlayer::on_player_hpchange(ServerActiveObject *player,
75 SCRIPTAPI_PRECHECKHEADER
77 // Get core.registered_on_player_hpchange
78 lua_getglobal(L, "core");
79 lua_getfield(L, -1, "registered_on_player_hpchange");
82 objectrefGetOrCreate(L, player);
83 lua_pushnumber(L, hp_change);
84 if (lua_pcall(L, 2, 1, m_errorhandler))
86 hp_change = lua_tointeger(L, -1);
91 bool ScriptApiPlayer::on_respawnplayer(ServerActiveObject *player)
93 SCRIPTAPI_PRECHECKHEADER
95 // Get core.registered_on_respawnplayers
96 lua_getglobal(L, "core");
97 lua_getfield(L, -1, "registered_on_respawnplayers");
99 objectrefGetOrCreate(L, player);
100 script_run_callbacks(L, 1, RUN_CALLBACKS_MODE_OR);
101 bool positioning_handled_by_some = lua_toboolean(L, -1);
102 return positioning_handled_by_some;
105 bool ScriptApiPlayer::on_prejoinplayer(
106 const std::string &name,
107 const std::string &ip,
110 SCRIPTAPI_PRECHECKHEADER
112 // Get core.registered_on_prejoinplayers
113 lua_getglobal(L, "core");
114 lua_getfield(L, -1, "registered_on_prejoinplayers");
115 lua_pushstring(L, name.c_str());
116 lua_pushstring(L, ip.c_str());
117 script_run_callbacks(L, 2, RUN_CALLBACKS_MODE_OR);
118 if (lua_isstring(L, -1)) {
119 reason->assign(lua_tostring(L, -1));
125 void ScriptApiPlayer::on_joinplayer(ServerActiveObject *player)
127 SCRIPTAPI_PRECHECKHEADER
129 // Get core.registered_on_joinplayers
130 lua_getglobal(L, "core");
131 lua_getfield(L, -1, "registered_on_joinplayers");
133 objectrefGetOrCreate(L, player);
134 script_run_callbacks(L, 1, RUN_CALLBACKS_MODE_FIRST);
137 void ScriptApiPlayer::on_leaveplayer(ServerActiveObject *player)
139 SCRIPTAPI_PRECHECKHEADER
141 // Get core.registered_on_leaveplayers
142 lua_getglobal(L, "core");
143 lua_getfield(L, -1, "registered_on_leaveplayers");
145 objectrefGetOrCreate(L, player);
146 script_run_callbacks(L, 1, RUN_CALLBACKS_MODE_FIRST);
149 void ScriptApiPlayer::on_cheat(ServerActiveObject *player,
150 const std::string &cheat_type)
152 SCRIPTAPI_PRECHECKHEADER
154 // Get core.registered_on_cheats
155 lua_getglobal(L, "core");
156 lua_getfield(L, -1, "registered_on_cheats");
158 objectrefGetOrCreate(L, player);
160 lua_pushlstring(L, cheat_type.c_str(), cheat_type.size());
161 lua_setfield(L, -2, "type");
162 script_run_callbacks(L, 2, RUN_CALLBACKS_MODE_FIRST);
165 void ScriptApiPlayer::on_playerReceiveFields(ServerActiveObject *player,
166 const std::string &formname,
167 const StringMap &fields)
169 SCRIPTAPI_PRECHECKHEADER
171 // Get core.registered_on_chat_messages
172 lua_getglobal(L, "core");
173 lua_getfield(L, -1, "registered_on_player_receive_fields");
176 objectrefGetOrCreate(L, player);
178 lua_pushstring(L, formname.c_str());
181 StringMap::const_iterator it;
182 for (it = fields.begin(); it != fields.end(); ++it) {
183 const std::string &name = it->first;
184 const std::string &value = it->second;
185 lua_pushstring(L, name.c_str());
186 lua_pushlstring(L, value.c_str(), value.size());
189 script_run_callbacks(L, 3, RUN_CALLBACKS_MODE_OR_SC);
192 ScriptApiPlayer::~ScriptApiPlayer()