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"
27 void ScriptApiClient::on_shutdown()
29 SCRIPTAPI_PRECHECKHEADER
31 // Get registered shutdown hooks
32 lua_getglobal(L, "core");
33 lua_getfield(L, -1, "registered_on_shutdown");
35 runCallbacks(0, RUN_CALLBACKS_MODE_FIRST);
38 bool ScriptApiClient::on_sending_message(const std::string &message)
40 SCRIPTAPI_PRECHECKHEADER
42 // Get core.registered_on_chat_messages
43 lua_getglobal(L, "core");
44 lua_getfield(L, -1, "registered_on_sending_chat_messages");
46 lua_pushstring(L, message.c_str());
47 runCallbacks(1, RUN_CALLBACKS_MODE_OR_SC);
48 bool ate = lua_toboolean(L, -1);
52 bool ScriptApiClient::on_receiving_message(const std::string &message)
54 SCRIPTAPI_PRECHECKHEADER
56 // Get core.registered_on_chat_messages
57 lua_getglobal(L, "core");
58 lua_getfield(L, -1, "registered_on_receiving_chat_messages");
60 lua_pushstring(L, message.c_str());
61 runCallbacks(1, RUN_CALLBACKS_MODE_OR_SC);
62 bool ate = lua_toboolean(L, -1);
66 void ScriptApiClient::on_damage_taken(int32_t damage_amount)
68 SCRIPTAPI_PRECHECKHEADER
70 // Get core.registered_on_chat_messages
71 lua_getglobal(L, "core");
72 lua_getfield(L, -1, "registered_on_damage_taken");
74 lua_pushinteger(L, damage_amount);
75 runCallbacks(1, RUN_CALLBACKS_MODE_OR_SC);
78 void ScriptApiClient::on_hp_modification(int32_t newhp)
80 SCRIPTAPI_PRECHECKHEADER
82 // Get core.registered_on_chat_messages
83 lua_getglobal(L, "core");
84 lua_getfield(L, -1, "registered_on_hp_modification");
86 lua_pushinteger(L, newhp);
87 runCallbacks(1, RUN_CALLBACKS_MODE_OR_SC);
90 void ScriptApiClient::on_death()
92 SCRIPTAPI_PRECHECKHEADER
94 // Get registered shutdown hooks
95 lua_getglobal(L, "core");
96 lua_getfield(L, -1, "registered_on_death");
98 runCallbacks(0, RUN_CALLBACKS_MODE_FIRST);
101 void ScriptApiClient::environment_step(float dtime)
103 SCRIPTAPI_PRECHECKHEADER
105 // Get core.registered_globalsteps
106 lua_getglobal(L, "core");
107 lua_getfield(L, -1, "registered_globalsteps");
109 lua_pushnumber(L, dtime);
111 runCallbacks(1, RUN_CALLBACKS_MODE_FIRST);
112 } catch (LuaError &e) {
113 getClient()->setFatalError(std::string("Client environment_step: ") + e.what() + "\n"
114 + script_get_backtrace(L));
118 void ScriptApiClient::on_formspec_input(const std::string &formname,
119 const StringMap &fields)
121 SCRIPTAPI_PRECHECKHEADER
123 // Get core.registered_on_chat_messages
124 lua_getglobal(L, "core");
125 lua_getfield(L, -1, "registered_on_formspec_input");
128 lua_pushstring(L, formname.c_str());
131 StringMap::const_iterator it;
132 for (it = fields.begin(); it != fields.end(); ++it) {
133 const std::string &name = it->first;
134 const std::string &value = it->second;
135 lua_pushstring(L, name.c_str());
136 lua_pushlstring(L, value.c_str(), value.size());
139 runCallbacks(2, RUN_CALLBACKS_MODE_OR_SC);
142 bool ScriptApiClient::on_dignode(v3s16 p, MapNode node)
144 SCRIPTAPI_PRECHECKHEADER
146 INodeDefManager *ndef = getClient()->ndef();
148 // Get core.registered_on_dignode
149 lua_getglobal(L, "core");
150 lua_getfield(L, -1, "registered_on_dignode");
154 pushnode(L, node, ndef);
157 runCallbacks(2, RUN_CALLBACKS_MODE_OR);
158 bool blocked = lua_toboolean(L, -1);