]> git.lizzy.rs Git - dragonfireclient.git/blob - src/script/cpp_api/s_player.cpp
Omnicleanup: header cleanup, add ModApiUtil shared between game and mainmenu
[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 #include "cpp_api/s_internal.h"
22
23 void ScriptApiPlayer::on_newplayer(ServerActiveObject *player)
24 {
25         SCRIPTAPI_PRECHECKHEADER
26
27         // Get minetest.registered_on_newplayers
28         lua_getglobal(L, "minetest");
29         lua_getfield(L, -1, "registered_on_newplayers");
30         // Call callbacks
31         objectrefGetOrCreate(player);
32         script_run_callbacks(L, 1, RUN_CALLBACKS_MODE_FIRST);
33 }
34
35 void ScriptApiPlayer::on_dieplayer(ServerActiveObject *player)
36 {
37         SCRIPTAPI_PRECHECKHEADER
38
39         // Get minetest.registered_on_dieplayers
40         lua_getglobal(L, "minetest");
41         lua_getfield(L, -1, "registered_on_dieplayers");
42         // Call callbacks
43         objectrefGetOrCreate(player);
44         script_run_callbacks(L, 1, RUN_CALLBACKS_MODE_FIRST);
45 }
46
47 bool ScriptApiPlayer::on_respawnplayer(ServerActiveObject *player)
48 {
49         SCRIPTAPI_PRECHECKHEADER
50
51         // Get minetest.registered_on_respawnplayers
52         lua_getglobal(L, "minetest");
53         lua_getfield(L, -1, "registered_on_respawnplayers");
54         // Call callbacks
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;
59 }
60
61 void ScriptApiPlayer::on_joinplayer(ServerActiveObject *player)
62 {
63         SCRIPTAPI_PRECHECKHEADER
64
65         // Get minetest.registered_on_joinplayers
66         lua_getglobal(L, "minetest");
67         lua_getfield(L, -1, "registered_on_joinplayers");
68         // Call callbacks
69         objectrefGetOrCreate(player);
70         script_run_callbacks(L, 1, RUN_CALLBACKS_MODE_FIRST);
71 }
72
73 void ScriptApiPlayer::on_leaveplayer(ServerActiveObject *player)
74 {
75         SCRIPTAPI_PRECHECKHEADER
76
77         // Get minetest.registered_on_leaveplayers
78         lua_getglobal(L, "minetest");
79         lua_getfield(L, -1, "registered_on_leaveplayers");
80         // Call callbacks
81         objectrefGetOrCreate(player);
82         script_run_callbacks(L, 1, RUN_CALLBACKS_MODE_FIRST);
83 }
84
85 void ScriptApiPlayer::on_cheat(ServerActiveObject *player,
86                 const std::string &cheat_type)
87 {
88         SCRIPTAPI_PRECHECKHEADER
89
90         // Get minetest.registered_on_cheats
91         lua_getglobal(L, "minetest");
92         lua_getfield(L, -1, "registered_on_cheats");
93         // Call callbacks
94         objectrefGetOrCreate(player);
95         lua_newtable(L);
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);
99 }
100
101 void ScriptApiPlayer::on_playerReceiveFields(ServerActiveObject *player,
102                 const std::string &formname,
103                 const std::map<std::string, std::string> &fields)
104 {
105         SCRIPTAPI_PRECHECKHEADER
106
107         // Get minetest.registered_on_chat_messages
108         lua_getglobal(L, "minetest");
109         lua_getfield(L, -1, "registered_on_player_receive_fields");
110         // Call callbacks
111         // param 1
112         objectrefGetOrCreate(player);
113         // param 2
114         lua_pushstring(L, formname.c_str());
115         // param 3
116         lua_newtable(L);
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());
123                 lua_settable(L, -3);
124         }
125         script_run_callbacks(L, 3, RUN_CALLBACKS_MODE_OR_SC);
126 }
127 ScriptApiPlayer::~ScriptApiPlayer() {
128 }
129
130