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 "util/string.h"
24 void ScriptApiPlayer::on_newplayer(ServerActiveObject *player)
26 SCRIPTAPI_PRECHECKHEADER
28 // Get minetest.registered_on_newplayers
29 lua_getglobal(L, "minetest");
30 lua_getfield(L, -1, "registered_on_newplayers");
32 objectrefGetOrCreate(player);
33 script_run_callbacks(L, 1, RUN_CALLBACKS_MODE_FIRST);
36 void ScriptApiPlayer::on_dieplayer(ServerActiveObject *player)
38 SCRIPTAPI_PRECHECKHEADER
40 // Get minetest.registered_on_dieplayers
41 lua_getglobal(L, "minetest");
42 lua_getfield(L, -1, "registered_on_dieplayers");
44 objectrefGetOrCreate(player);
45 script_run_callbacks(L, 1, RUN_CALLBACKS_MODE_FIRST);
48 bool ScriptApiPlayer::on_respawnplayer(ServerActiveObject *player)
50 SCRIPTAPI_PRECHECKHEADER
52 // Get minetest.registered_on_respawnplayers
53 lua_getglobal(L, "minetest");
54 lua_getfield(L, -1, "registered_on_respawnplayers");
56 objectrefGetOrCreate(player);
57 script_run_callbacks(L, 1, RUN_CALLBACKS_MODE_OR);
58 bool positioning_handled_by_some = lua_toboolean(L, -1);
59 return positioning_handled_by_some;
62 bool ScriptApiPlayer::on_prejoinplayer(std::string name, std::string ip, std::string &reason)
64 SCRIPTAPI_PRECHECKHEADER
66 // Get minetest.registered_on_prejoinplayers
67 lua_getglobal(L, "minetest");
68 lua_getfield(L, -1, "registered_on_prejoinplayers");
69 lua_pushstring(L, name.c_str());
70 lua_pushstring(L, ip.c_str());
71 script_run_callbacks(L, 2, RUN_CALLBACKS_MODE_OR);
72 if (lua_isstring(L, -1)) {
73 reason.assign(lua_tostring(L, -1));
79 void ScriptApiPlayer::on_joinplayer(ServerActiveObject *player)
81 SCRIPTAPI_PRECHECKHEADER
83 // Get minetest.registered_on_joinplayers
84 lua_getglobal(L, "minetest");
85 lua_getfield(L, -1, "registered_on_joinplayers");
87 objectrefGetOrCreate(player);
88 script_run_callbacks(L, 1, RUN_CALLBACKS_MODE_FIRST);
91 void ScriptApiPlayer::on_leaveplayer(ServerActiveObject *player)
93 SCRIPTAPI_PRECHECKHEADER
95 // Get minetest.registered_on_leaveplayers
96 lua_getglobal(L, "minetest");
97 lua_getfield(L, -1, "registered_on_leaveplayers");
99 objectrefGetOrCreate(player);
100 script_run_callbacks(L, 1, RUN_CALLBACKS_MODE_FIRST);
103 void ScriptApiPlayer::on_cheat(ServerActiveObject *player,
104 const std::string &cheat_type)
106 SCRIPTAPI_PRECHECKHEADER
108 // Get minetest.registered_on_cheats
109 lua_getglobal(L, "minetest");
110 lua_getfield(L, -1, "registered_on_cheats");
112 objectrefGetOrCreate(player);
114 lua_pushlstring(L, cheat_type.c_str(), cheat_type.size());
115 lua_setfield(L, -2, "type");
116 script_run_callbacks(L, 2, RUN_CALLBACKS_MODE_FIRST);
119 void ScriptApiPlayer::on_playerReceiveFields(ServerActiveObject *player,
120 const std::string &formname,
121 const std::map<std::string, std::string> &fields)
123 SCRIPTAPI_PRECHECKHEADER
125 // Get minetest.registered_on_chat_messages
126 lua_getglobal(L, "minetest");
127 lua_getfield(L, -1, "registered_on_player_receive_fields");
130 objectrefGetOrCreate(player);
132 lua_pushstring(L, formname.c_str());
135 for(std::map<std::string, std::string>::const_iterator
136 i = fields.begin(); i != fields.end(); i++){
137 const std::string &name = i->first;
138 const std::string &value = i->second;
139 lua_pushstring(L, name.c_str());
140 lua_pushlstring(L, value.c_str(), value.size());
143 script_run_callbacks(L, 3, RUN_CALLBACKS_MODE_OR_SC);
145 ScriptApiPlayer::~ScriptApiPlayer() {