]> git.lizzy.rs Git - dragonfireclient.git/blob - src/script/cpp_api/s_player.cpp
Merge remote-tracking branch 'origin/master'
[dragonfireclient.git] / src / script / cpp_api / s_player.cpp
1 /*
2 Minetest
3 Copyright (C) 2013 celeron55, Perttu Ahola <celeron55@gmail.com>
4
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.
9
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.
14
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.
18 */
19
20 #include "cpp_api/s_player.h"
21
22 void ScriptApiPlayer::on_newplayer(ServerActiveObject *player)
23 {
24         SCRIPTAPI_PRECHECKHEADER
25
26         // Get minetest.registered_on_newplayers
27         lua_getglobal(L, "minetest");
28         lua_getfield(L, -1, "registered_on_newplayers");
29         // Call callbacks
30         objectrefGetOrCreate(player);
31         runCallbacks(1, RUN_CALLBACKS_MODE_FIRST);
32 }
33
34 void ScriptApiPlayer::on_dieplayer(ServerActiveObject *player)
35 {
36         SCRIPTAPI_PRECHECKHEADER
37
38         // Get minetest.registered_on_dieplayers
39         lua_getglobal(L, "minetest");
40         lua_getfield(L, -1, "registered_on_dieplayers");
41         // Call callbacks
42         objectrefGetOrCreate(player);
43         runCallbacks(1, RUN_CALLBACKS_MODE_FIRST);
44 }
45
46 bool ScriptApiPlayer::on_respawnplayer(ServerActiveObject *player)
47 {
48         SCRIPTAPI_PRECHECKHEADER
49
50         // Get minetest.registered_on_respawnplayers
51         lua_getglobal(L, "minetest");
52         lua_getfield(L, -1, "registered_on_respawnplayers");
53         // Call callbacks
54         objectrefGetOrCreate(player);
55         runCallbacks(1, RUN_CALLBACKS_MODE_OR);
56         bool positioning_handled_by_some = lua_toboolean(L, -1);
57         return positioning_handled_by_some;
58 }
59
60 void ScriptApiPlayer::on_joinplayer(ServerActiveObject *player)
61 {
62         SCRIPTAPI_PRECHECKHEADER
63
64         // Get minetest.registered_on_joinplayers
65         lua_getglobal(L, "minetest");
66         lua_getfield(L, -1, "registered_on_joinplayers");
67         // Call callbacks
68         objectrefGetOrCreate(player);
69         runCallbacks(1, RUN_CALLBACKS_MODE_FIRST);
70 }
71
72 void ScriptApiPlayer::on_leaveplayer(ServerActiveObject *player)
73 {
74         SCRIPTAPI_PRECHECKHEADER
75
76         // Get minetest.registered_on_leaveplayers
77         lua_getglobal(L, "minetest");
78         lua_getfield(L, -1, "registered_on_leaveplayers");
79         // Call callbacks
80         objectrefGetOrCreate(player);
81         runCallbacks(1, RUN_CALLBACKS_MODE_FIRST);
82 }
83
84 void ScriptApiPlayer::on_playerReceiveFields(ServerActiveObject *player,
85                 const std::string &formname,
86                 const std::map<std::string, std::string> &fields)
87 {
88         SCRIPTAPI_PRECHECKHEADER
89
90         // Get minetest.registered_on_chat_messages
91         lua_getglobal(L, "minetest");
92         lua_getfield(L, -1, "registered_on_player_receive_fields");
93         // Call callbacks
94         // param 1
95         objectrefGetOrCreate(player);
96         // param 2
97         lua_pushstring(L, formname.c_str());
98         // param 3
99         lua_newtable(L);
100         for(std::map<std::string, std::string>::const_iterator
101                         i = fields.begin(); i != fields.end(); i++){
102                 const std::string &name = i->first;
103                 const std::string &value = i->second;
104                 lua_pushstring(L, name.c_str());
105                 lua_pushlstring(L, value.c_str(), value.size());
106                 lua_settable(L, -3);
107         }
108         runCallbacks(3, RUN_CALLBACKS_MODE_OR_SC);
109 }
110 ScriptApiPlayer::~ScriptApiPlayer() {
111 }
112
113