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 bool ScriptApiClient::on_sending_message(const std::string &message)
41 SCRIPTAPI_PRECHECKHEADER
43 // Get core.registered_on_chat_messages
44 lua_getglobal(L, "core");
45 lua_getfield(L, -1, "registered_on_sending_chat_message");
47 lua_pushstring(L, message.c_str());
48 runCallbacks(1, RUN_CALLBACKS_MODE_OR_SC);
49 bool ate = lua_toboolean(L, -1);
53 bool ScriptApiClient::on_receiving_message(const std::string &message)
55 SCRIPTAPI_PRECHECKHEADER
57 // Get core.registered_on_chat_messages
58 lua_getglobal(L, "core");
59 lua_getfield(L, -1, "registered_on_receiving_chat_message");
61 lua_pushstring(L, message.c_str());
62 runCallbacks(1, RUN_CALLBACKS_MODE_OR_SC);
63 bool ate = lua_toboolean(L, -1);
67 void ScriptApiClient::on_damage_taken(int32_t damage_amount)
69 SCRIPTAPI_PRECHECKHEADER
71 // Get core.registered_on_chat_messages
72 lua_getglobal(L, "core");
73 lua_getfield(L, -1, "registered_on_damage_taken");
75 lua_pushinteger(L, damage_amount);
76 runCallbacks(1, RUN_CALLBACKS_MODE_OR_SC);
79 void ScriptApiClient::on_hp_modification(int32_t newhp)
81 SCRIPTAPI_PRECHECKHEADER
83 // Get core.registered_on_chat_messages
84 lua_getglobal(L, "core");
85 lua_getfield(L, -1, "registered_on_hp_modification");
87 lua_pushinteger(L, newhp);
88 runCallbacks(1, RUN_CALLBACKS_MODE_OR_SC);
91 void ScriptApiClient::on_death()
93 SCRIPTAPI_PRECHECKHEADER
95 // Get registered shutdown hooks
96 lua_getglobal(L, "core");
97 lua_getfield(L, -1, "registered_on_death");
99 runCallbacks(0, RUN_CALLBACKS_MODE_FIRST);
102 void ScriptApiClient::environment_step(float dtime)
104 SCRIPTAPI_PRECHECKHEADER
106 // Get core.registered_globalsteps
107 lua_getglobal(L, "core");
108 lua_getfield(L, -1, "registered_globalsteps");
110 lua_pushnumber(L, dtime);
112 runCallbacks(1, RUN_CALLBACKS_MODE_FIRST);
113 } catch (LuaError &e) {
114 getClient()->setFatalError(std::string("Client environment_step: ") + e.what() + "\n"
115 + script_get_backtrace(L));
119 void ScriptApiClient::on_formspec_input(const std::string &formname,
120 const StringMap &fields)
122 SCRIPTAPI_PRECHECKHEADER
124 // Get core.registered_on_chat_messages
125 lua_getglobal(L, "core");
126 lua_getfield(L, -1, "registered_on_formspec_input");
129 lua_pushstring(L, formname.c_str());
132 StringMap::const_iterator it;
133 for (it = fields.begin(); it != fields.end(); ++it) {
134 const std::string &name = it->first;
135 const std::string &value = it->second;
136 lua_pushstring(L, name.c_str());
137 lua_pushlstring(L, value.c_str(), value.size());
140 runCallbacks(2, RUN_CALLBACKS_MODE_OR_SC);
143 bool ScriptApiClient::on_dignode(v3s16 p, MapNode node)
145 SCRIPTAPI_PRECHECKHEADER
147 const NodeDefManager *ndef = getClient()->ndef();
149 // Get core.registered_on_dignode
150 lua_getglobal(L, "core");
151 lua_getfield(L, -1, "registered_on_dignode");
155 pushnode(L, node, ndef);
158 runCallbacks(2, RUN_CALLBACKS_MODE_OR);
159 return lua_toboolean(L, -1);
162 bool ScriptApiClient::on_punchnode(v3s16 p, MapNode node)
164 SCRIPTAPI_PRECHECKHEADER
166 const NodeDefManager *ndef = getClient()->ndef();
168 // Get core.registered_on_punchgnode
169 lua_getglobal(L, "core");
170 lua_getfield(L, -1, "registered_on_punchnode");
174 pushnode(L, node, ndef);
177 runCallbacks(2, RUN_CALLBACKS_MODE_OR);
178 bool blocked = lua_toboolean(L, -1);
182 bool ScriptApiClient::on_placenode(const PointedThing &pointed, const ItemDefinition &item)
184 SCRIPTAPI_PRECHECKHEADER
186 // Get core.registered_on_placenode
187 lua_getglobal(L, "core");
188 lua_getfield(L, -1, "registered_on_placenode");
191 push_pointed_thing(L, pointed, true);
192 push_item_definition(L, item);
195 runCallbacks(2, RUN_CALLBACKS_MODE_OR);
196 return lua_toboolean(L, -1);
199 bool ScriptApiClient::on_item_use(const ItemStack &item, const PointedThing &pointed)
201 SCRIPTAPI_PRECHECKHEADER
203 // Get core.registered_on_item_use
204 lua_getglobal(L, "core");
205 lua_getfield(L, -1, "registered_on_item_use");
208 LuaItemStack::create(L, item);
209 push_pointed_thing(L, pointed, true);
212 runCallbacks(2, RUN_CALLBACKS_MODE_OR);
213 return lua_toboolean(L, -1);
216 bool ScriptApiClient::on_inventory_open(Inventory *inventory)
218 SCRIPTAPI_PRECHECKHEADER
220 lua_getglobal(L, "core");
221 lua_getfield(L, -1, "registered_on_inventory_open");
223 std::vector<const InventoryList*> lists = inventory->getLists();
224 std::vector<const InventoryList*>::iterator iter = lists.begin();
225 lua_createtable(L, 0, lists.size());
226 for (; iter != lists.end(); iter++) {
227 const char* name = (*iter)->getName().c_str();
228 lua_pushstring(L, name);
229 push_inventory_list(L, inventory, name);
233 runCallbacks(1, RUN_CALLBACKS_MODE_OR);
234 return lua_toboolean(L, -1);
237 void ScriptApiClient::setEnv(ClientEnvironment *env)
239 ScriptApiBase::setEnv(env);