3 Copyright (C) 2013 celeron55, Perttu Ahola <celeron55@gmail.com>
4 Copyright (C) 2017 nerzhul, Loic Blot <loic.blot@unix-experience.fr>
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU Lesser General Public License as published by
8 the Free Software Foundation; either version 2.1 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU Lesser General Public License for more details.
16 You should have received a copy of the GNU Lesser General Public License along
17 with this program; if not, write to the Free Software Foundation, Inc.,
18 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
22 #include "s_internal.h"
24 #include "common/c_converter.h"
25 #include "common/c_content.h"
28 void ScriptApiClient::on_shutdown()
30 SCRIPTAPI_PRECHECKHEADER
32 // Get registered shutdown hooks
33 lua_getglobal(L, "core");
34 lua_getfield(L, -1, "registered_on_shutdown");
36 runCallbacks(0, RUN_CALLBACKS_MODE_FIRST);
39 void ScriptApiClient::on_connect()
41 SCRIPTAPI_PRECHECKHEADER
43 // get registered connect hooks
44 lua_getglobal(L, "core");
45 lua_getfield(L, -1, "registered_on_connect");
47 runCallbacks(0, RUN_CALLBACKS_MODE_FIRST);
50 bool ScriptApiClient::on_sending_message(const std::string &message)
52 SCRIPTAPI_PRECHECKHEADER
54 // Get core.registered_on_chat_messages
55 lua_getglobal(L, "core");
56 lua_getfield(L, -1, "registered_on_sending_chat_message");
58 lua_pushstring(L, message.c_str());
59 runCallbacks(1, RUN_CALLBACKS_MODE_OR_SC);
60 bool ate = lua_toboolean(L, -1);
64 bool ScriptApiClient::on_receiving_message(const std::string &message)
66 SCRIPTAPI_PRECHECKHEADER
68 // Get core.registered_on_chat_messages
69 lua_getglobal(L, "core");
70 lua_getfield(L, -1, "registered_on_receiving_chat_message");
72 lua_pushstring(L, message.c_str());
73 runCallbacks(1, RUN_CALLBACKS_MODE_OR_SC);
74 bool ate = lua_toboolean(L, -1);
78 void ScriptApiClient::on_damage_taken(int32_t damage_amount)
80 SCRIPTAPI_PRECHECKHEADER
82 // Get core.registered_on_chat_messages
83 lua_getglobal(L, "core");
84 lua_getfield(L, -1, "registered_on_damage_taken");
86 lua_pushinteger(L, damage_amount);
87 runCallbacks(1, RUN_CALLBACKS_MODE_OR_SC);
90 void ScriptApiClient::on_hp_modification(int32_t newhp)
92 SCRIPTAPI_PRECHECKHEADER
94 // Get core.registered_on_chat_messages
95 lua_getglobal(L, "core");
96 lua_getfield(L, -1, "registered_on_hp_modification");
98 lua_pushinteger(L, newhp);
99 runCallbacks(1, RUN_CALLBACKS_MODE_OR_SC);
102 void ScriptApiClient::on_death()
104 SCRIPTAPI_PRECHECKHEADER
106 // Get registered shutdown hooks
107 lua_getglobal(L, "core");
108 lua_getfield(L, -1, "registered_on_death");
110 runCallbacks(0, RUN_CALLBACKS_MODE_FIRST);
113 void ScriptApiClient::environment_step(float dtime)
115 SCRIPTAPI_PRECHECKHEADER
117 // Get core.registered_globalsteps
118 lua_getglobal(L, "core");
119 lua_getfield(L, -1, "registered_globalsteps");
121 lua_pushnumber(L, dtime);
123 runCallbacks(1, RUN_CALLBACKS_MODE_FIRST);
124 } catch (LuaError &e) {
125 getClient()->setFatalError(std::string("Client environment_step: ") + e.what() + "\n"
126 + script_get_backtrace(L));
130 void ScriptApiClient::on_formspec_input(const std::string &formname,
131 const StringMap &fields)
133 SCRIPTAPI_PRECHECKHEADER
135 // Get core.registered_on_chat_messages
136 lua_getglobal(L, "core");
137 lua_getfield(L, -1, "registered_on_formspec_input");
140 lua_pushstring(L, formname.c_str());
143 StringMap::const_iterator it;
144 for (it = fields.begin(); it != fields.end(); ++it) {
145 const std::string &name = it->first;
146 const std::string &value = it->second;
147 lua_pushstring(L, name.c_str());
148 lua_pushlstring(L, value.c_str(), value.size());
151 runCallbacks(2, RUN_CALLBACKS_MODE_OR_SC);
154 bool ScriptApiClient::on_dignode(v3s16 p, MapNode node)
156 SCRIPTAPI_PRECHECKHEADER
158 INodeDefManager *ndef = getClient()->ndef();
160 // Get core.registered_on_dignode
161 lua_getglobal(L, "core");
162 lua_getfield(L, -1, "registered_on_dignode");
166 pushnode(L, node, ndef);
169 runCallbacks(2, RUN_CALLBACKS_MODE_OR);
170 return lua_toboolean(L, -1);
173 bool ScriptApiClient::on_punchnode(v3s16 p, MapNode node)
175 SCRIPTAPI_PRECHECKHEADER
177 INodeDefManager *ndef = getClient()->ndef();
179 // Get core.registered_on_punchgnode
180 lua_getglobal(L, "core");
181 lua_getfield(L, -1, "registered_on_punchnode");
185 pushnode(L, node, ndef);
188 runCallbacks(2, RUN_CALLBACKS_MODE_OR);
189 bool blocked = lua_toboolean(L, -1);
193 bool ScriptApiClient::on_placenode(const PointedThing &pointed, const ItemDefinition &item)
195 SCRIPTAPI_PRECHECKHEADER
197 // Get core.registered_on_placenode
198 lua_getglobal(L, "core");
199 lua_getfield(L, -1, "registered_on_placenode");
202 push_pointed_thing(L, pointed, true);
203 push_item_definition(L, item);
206 runCallbacks(2, RUN_CALLBACKS_MODE_OR);
207 return lua_toboolean(L, -1);
210 bool ScriptApiClient::on_item_use(const ItemStack &item, const PointedThing &pointed)
212 SCRIPTAPI_PRECHECKHEADER
214 // Get core.registered_on_item_use
215 lua_getglobal(L, "core");
216 lua_getfield(L, -1, "registered_on_item_use");
219 LuaItemStack::create(L, item);
220 push_pointed_thing(L, pointed, true);
223 runCallbacks(2, RUN_CALLBACKS_MODE_OR);
224 return lua_toboolean(L, -1);
227 void ScriptApiClient::setEnv(ClientEnvironment *env)
229 ScriptApiBase::setEnv(env);