]> git.lizzy.rs Git - dragonfireclient.git/blob - src/script/cpp_api/s_player.cpp
Add 'on_prejoinplayer' callback
[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 #include "util/string.h"
23
24 void ScriptApiPlayer::on_newplayer(ServerActiveObject *player)
25 {
26         SCRIPTAPI_PRECHECKHEADER
27
28         // Get minetest.registered_on_newplayers
29         lua_getglobal(L, "minetest");
30         lua_getfield(L, -1, "registered_on_newplayers");
31         // Call callbacks
32         objectrefGetOrCreate(player);
33         script_run_callbacks(L, 1, RUN_CALLBACKS_MODE_FIRST);
34 }
35
36 void ScriptApiPlayer::on_dieplayer(ServerActiveObject *player)
37 {
38         SCRIPTAPI_PRECHECKHEADER
39
40         // Get minetest.registered_on_dieplayers
41         lua_getglobal(L, "minetest");
42         lua_getfield(L, -1, "registered_on_dieplayers");
43         // Call callbacks
44         objectrefGetOrCreate(player);
45         script_run_callbacks(L, 1, RUN_CALLBACKS_MODE_FIRST);
46 }
47
48 bool ScriptApiPlayer::on_respawnplayer(ServerActiveObject *player)
49 {
50         SCRIPTAPI_PRECHECKHEADER
51
52         // Get minetest.registered_on_respawnplayers
53         lua_getglobal(L, "minetest");
54         lua_getfield(L, -1, "registered_on_respawnplayers");
55         // Call callbacks
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;
60 }
61
62 bool ScriptApiPlayer::on_prejoinplayer(std::string name, std::string ip, std::string &reason)
63 {
64         SCRIPTAPI_PRECHECKHEADER
65
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));
74                 return true;
75         }
76         return false;
77 }
78
79 void ScriptApiPlayer::on_joinplayer(ServerActiveObject *player)
80 {
81         SCRIPTAPI_PRECHECKHEADER
82
83         // Get minetest.registered_on_joinplayers
84         lua_getglobal(L, "minetest");
85         lua_getfield(L, -1, "registered_on_joinplayers");
86         // Call callbacks
87         objectrefGetOrCreate(player);
88         script_run_callbacks(L, 1, RUN_CALLBACKS_MODE_FIRST);
89 }
90
91 void ScriptApiPlayer::on_leaveplayer(ServerActiveObject *player)
92 {
93         SCRIPTAPI_PRECHECKHEADER
94
95         // Get minetest.registered_on_leaveplayers
96         lua_getglobal(L, "minetest");
97         lua_getfield(L, -1, "registered_on_leaveplayers");
98         // Call callbacks
99         objectrefGetOrCreate(player);
100         script_run_callbacks(L, 1, RUN_CALLBACKS_MODE_FIRST);
101 }
102
103 void ScriptApiPlayer::on_cheat(ServerActiveObject *player,
104                 const std::string &cheat_type)
105 {
106         SCRIPTAPI_PRECHECKHEADER
107
108         // Get minetest.registered_on_cheats
109         lua_getglobal(L, "minetest");
110         lua_getfield(L, -1, "registered_on_cheats");
111         // Call callbacks
112         objectrefGetOrCreate(player);
113         lua_newtable(L);
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);
117 }
118
119 void ScriptApiPlayer::on_playerReceiveFields(ServerActiveObject *player,
120                 const std::string &formname,
121                 const std::map<std::string, std::string> &fields)
122 {
123         SCRIPTAPI_PRECHECKHEADER
124
125         // Get minetest.registered_on_chat_messages
126         lua_getglobal(L, "minetest");
127         lua_getfield(L, -1, "registered_on_player_receive_fields");
128         // Call callbacks
129         // param 1
130         objectrefGetOrCreate(player);
131         // param 2
132         lua_pushstring(L, formname.c_str());
133         // param 3
134         lua_newtable(L);
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());
141                 lua_settable(L, -3);
142         }
143         script_run_callbacks(L, 3, RUN_CALLBACKS_MODE_OR_SC);
144 }
145 ScriptApiPlayer::~ScriptApiPlayer() {
146 }
147
148