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"
23 void ScriptApiPlayer::on_newplayer(ServerActiveObject *player)
25 SCRIPTAPI_PRECHECKHEADER
27 // Get minetest.registered_on_newplayers
28 lua_getglobal(L, "minetest");
29 lua_getfield(L, -1, "registered_on_newplayers");
31 objectrefGetOrCreate(player);
32 script_run_callbacks(L, 1, RUN_CALLBACKS_MODE_FIRST);
35 void ScriptApiPlayer::on_dieplayer(ServerActiveObject *player)
37 SCRIPTAPI_PRECHECKHEADER
39 // Get minetest.registered_on_dieplayers
40 lua_getglobal(L, "minetest");
41 lua_getfield(L, -1, "registered_on_dieplayers");
43 objectrefGetOrCreate(player);
44 script_run_callbacks(L, 1, RUN_CALLBACKS_MODE_FIRST);
47 bool ScriptApiPlayer::on_respawnplayer(ServerActiveObject *player)
49 SCRIPTAPI_PRECHECKHEADER
51 // Get minetest.registered_on_respawnplayers
52 lua_getglobal(L, "minetest");
53 lua_getfield(L, -1, "registered_on_respawnplayers");
55 objectrefGetOrCreate(player);
56 script_run_callbacks(L, 1, RUN_CALLBACKS_MODE_OR);
57 bool positioning_handled_by_some = lua_toboolean(L, -1);
58 return positioning_handled_by_some;
61 void ScriptApiPlayer::on_joinplayer(ServerActiveObject *player)
63 SCRIPTAPI_PRECHECKHEADER
65 // Get minetest.registered_on_joinplayers
66 lua_getglobal(L, "minetest");
67 lua_getfield(L, -1, "registered_on_joinplayers");
69 objectrefGetOrCreate(player);
70 script_run_callbacks(L, 1, RUN_CALLBACKS_MODE_FIRST);
73 void ScriptApiPlayer::on_leaveplayer(ServerActiveObject *player)
75 SCRIPTAPI_PRECHECKHEADER
77 // Get minetest.registered_on_leaveplayers
78 lua_getglobal(L, "minetest");
79 lua_getfield(L, -1, "registered_on_leaveplayers");
81 objectrefGetOrCreate(player);
82 script_run_callbacks(L, 1, RUN_CALLBACKS_MODE_FIRST);
85 void ScriptApiPlayer::on_cheat(ServerActiveObject *player,
86 const std::string &cheat_type)
88 SCRIPTAPI_PRECHECKHEADER
90 // Get minetest.registered_on_cheats
91 lua_getglobal(L, "minetest");
92 lua_getfield(L, -1, "registered_on_cheats");
94 objectrefGetOrCreate(player);
96 lua_pushlstring(L, cheat_type.c_str(), cheat_type.size());
97 lua_setfield(L, -2, "type");
98 script_run_callbacks(L, 2, RUN_CALLBACKS_MODE_FIRST);
101 void ScriptApiPlayer::on_playerReceiveFields(ServerActiveObject *player,
102 const std::string &formname,
103 const std::map<std::string, std::string> &fields)
105 SCRIPTAPI_PRECHECKHEADER
107 // Get minetest.registered_on_chat_messages
108 lua_getglobal(L, "minetest");
109 lua_getfield(L, -1, "registered_on_player_receive_fields");
112 objectrefGetOrCreate(player);
114 lua_pushstring(L, formname.c_str());
117 for(std::map<std::string, std::string>::const_iterator
118 i = fields.begin(); i != fields.end(); i++){
119 const std::string &name = i->first;
120 const std::string &value = i->second;
121 lua_pushstring(L, name.c_str());
122 lua_pushlstring(L, value.c_str(), value.size());
125 script_run_callbacks(L, 3, RUN_CALLBACKS_MODE_OR_SC);
127 ScriptApiPlayer::~ScriptApiPlayer() {