1 /*
2 script/cpp_api/s_player.cpp
3 Copyright (C) 2013 celeron55, Perttu Ahola <celeron55@gmail.com>
4 */
5 
6 /*
7 This file is part of Freeminer.
8 
9 Freeminer is free software: you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation, either version 3 of the License, or
12 (at your option) any later version.
13 
14 Freeminer  is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17 GNU General Public License for more details.
18 
19 You should have received a copy of the GNU General Public License
20 along with Freeminer.  If not, see <http://www.gnu.org/licenses/>.
21 */
22 
23 #include "cpp_api/s_player.h"
24 #include "cpp_api/s_internal.h"
25 #include "util/string.h"
26 
on_newplayer(ServerActiveObject * player)27 void ScriptApiPlayer::on_newplayer(ServerActiveObject *player)
28 {
29 	SCRIPTAPI_PRECHECKHEADER
30 
31 	// Get core.registered_on_newplayers
32 	lua_getglobal(L, "core");
33 	lua_getfield(L, -1, "registered_on_newplayers");
34 	// Call callbacks
35 	objectrefGetOrCreate(L, player);
36 	script_run_callbacks(L, 1, RUN_CALLBACKS_MODE_FIRST);
37 }
38 
on_dieplayer(ServerActiveObject * player)39 void ScriptApiPlayer::on_dieplayer(ServerActiveObject *player)
40 {
41 	SCRIPTAPI_PRECHECKHEADER
42 
43 	// Get core.registered_on_dieplayers
44 	lua_getglobal(L, "core");
45 	lua_getfield(L, -1, "registered_on_dieplayers");
46 	// Call callbacks
47 	objectrefGetOrCreate(L, player);
48 	script_run_callbacks(L, 1, RUN_CALLBACKS_MODE_FIRST);
49 }
50 
on_respawnplayer(ServerActiveObject * player)51 bool ScriptApiPlayer::on_respawnplayer(ServerActiveObject *player)
52 {
53 	SCRIPTAPI_PRECHECKHEADER
54 
55 	// Get core.registered_on_respawnplayers
56 	lua_getglobal(L, "core");
57 	lua_getfield(L, -1, "registered_on_respawnplayers");
58 	// Call callbacks
59 	objectrefGetOrCreate(L, player);
60 	script_run_callbacks(L, 1, RUN_CALLBACKS_MODE_OR);
61 	bool positioning_handled_by_some = lua_toboolean(L, -1);
62 	return positioning_handled_by_some;
63 }
64 
on_prejoinplayer(std::string name,std::string ip,std::string & reason)65 bool ScriptApiPlayer::on_prejoinplayer(std::string name, std::string ip, std::string &reason)
66 {
67 	SCRIPTAPI_PRECHECKHEADER
68 
69 	// Get core.registered_on_prejoinplayers
70 	lua_getglobal(L, "core");
71 	lua_getfield(L, -1, "registered_on_prejoinplayers");
72 	lua_pushstring(L, name.c_str());
73 	lua_pushstring(L, ip.c_str());
74 	script_run_callbacks(L, 2, RUN_CALLBACKS_MODE_OR);
75 	if (lua_isstring(L, -1)) {
76 		reason.assign(lua_tostring(L, -1));
77 		return true;
78 	}
79 	return false;
80 }
81 
on_joinplayer(ServerActiveObject * player)82 void ScriptApiPlayer::on_joinplayer(ServerActiveObject *player)
83 {
84 	SCRIPTAPI_PRECHECKHEADER
85 
86 	// Get core.registered_on_joinplayers
87 	lua_getglobal(L, "core");
88 	lua_getfield(L, -1, "registered_on_joinplayers");
89 	// Call callbacks
90 	objectrefGetOrCreate(L, player);
91 	script_run_callbacks(L, 1, RUN_CALLBACKS_MODE_FIRST);
92 }
93 
on_leaveplayer(ServerActiveObject * player)94 void ScriptApiPlayer::on_leaveplayer(ServerActiveObject *player)
95 {
96 	SCRIPTAPI_PRECHECKHEADER
97 
98 	// Get core.registered_on_leaveplayers
99 	lua_getglobal(L, "core");
100 	lua_getfield(L, -1, "registered_on_leaveplayers");
101 	// Call callbacks
102 	objectrefGetOrCreate(L, player);
103 	script_run_callbacks(L, 1, RUN_CALLBACKS_MODE_FIRST);
104 }
105 
on_cheat(ServerActiveObject * player,const std::string & cheat_type)106 void ScriptApiPlayer::on_cheat(ServerActiveObject *player,
107 		const std::string &cheat_type)
108 {
109 	SCRIPTAPI_PRECHECKHEADER
110 
111 	// Get core.registered_on_cheats
112 	lua_getglobal(L, "core");
113 	lua_getfield(L, -1, "registered_on_cheats");
114 	// Call callbacks
115 	objectrefGetOrCreate(L, player);
116 	lua_newtable(L);
117 	lua_pushlstring(L, cheat_type.c_str(), cheat_type.size());
118 	lua_setfield(L, -2, "type");
119 	script_run_callbacks(L, 2, RUN_CALLBACKS_MODE_FIRST);
120 }
121 
on_playerReceiveFields(ServerActiveObject * player,const std::string & formname,const std::map<std::string,std::string> & fields)122 void ScriptApiPlayer::on_playerReceiveFields(ServerActiveObject *player,
123 		const std::string &formname,
124 		const std::map<std::string, std::string> &fields)
125 {
126 	SCRIPTAPI_PRECHECKHEADER
127 
128 	// Get core.registered_on_chat_messages
129 	lua_getglobal(L, "core");
130 	lua_getfield(L, -1, "registered_on_player_receive_fields");
131 	// Call callbacks
132 	// param 1
133 	objectrefGetOrCreate(L, player);
134 	// param 2
135 	lua_pushstring(L, formname.c_str());
136 	// param 3
137 	lua_newtable(L);
138 	for(std::map<std::string, std::string>::const_iterator
139 			i = fields.begin(); i != fields.end(); i++){
140 		const std::string &name = i->first;
141 		const std::string &value = i->second;
142 		lua_pushstring(L, name.c_str());
143 		lua_pushlstring(L, value.c_str(), value.size());
144 		lua_settable(L, -3);
145 	}
146 	script_run_callbacks(L, 3, RUN_CALLBACKS_MODE_OR_SC);
147 }
~ScriptApiPlayer()148 ScriptApiPlayer::~ScriptApiPlayer() {
149 }
150 
151 
152