2017-01-21 15:02:08 +01:00
|
|
|
/*
|
|
|
|
Minetest
|
|
|
|
Copyright (C) 2013 celeron55, Perttu Ahola <celeron55@gmail.com>
|
|
|
|
Copyright (C) 2017 nerzhul, Loic Blot <loic.blot@unix-experience.fr>
|
|
|
|
|
|
|
|
This program is free software; you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU Lesser General Public License as published by
|
|
|
|
the Free Software Foundation; either version 2.1 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU Lesser General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU Lesser General Public License along
|
|
|
|
with this program; if not, write to the Free Software Foundation, Inc.,
|
|
|
|
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "s_client.h"
|
|
|
|
#include "s_internal.h"
|
2018-11-28 20:01:49 +01:00
|
|
|
#include "client/client.h"
|
2017-01-29 18:43:44 +01:00
|
|
|
#include "common/c_converter.h"
|
|
|
|
#include "common/c_content.h"
|
2023-03-08 22:58:47 +01:00
|
|
|
#include "lua_api/l_item.h"
|
|
|
|
#include "itemdef.h"
|
2017-04-29 12:08:16 +02:00
|
|
|
#include "s_item.h"
|
2017-01-21 15:02:08 +01:00
|
|
|
|
2018-06-06 12:53:59 +02:00
|
|
|
void ScriptApiClient::on_mods_loaded()
|
|
|
|
{
|
|
|
|
SCRIPTAPI_PRECHECKHEADER
|
|
|
|
|
|
|
|
// Get registered shutdown hooks
|
|
|
|
lua_getglobal(L, "core");
|
|
|
|
lua_getfield(L, -1, "registered_on_mods_loaded");
|
|
|
|
// Call callbacks
|
2021-09-10 23:16:46 +02:00
|
|
|
try {
|
|
|
|
runCallbacks(0, RUN_CALLBACKS_MODE_FIRST);
|
|
|
|
} catch (LuaError &e) {
|
|
|
|
getClient()->setFatalError(e);
|
|
|
|
}
|
2018-06-06 12:53:59 +02:00
|
|
|
}
|
|
|
|
|
2017-01-21 15:02:08 +01:00
|
|
|
void ScriptApiClient::on_shutdown()
|
|
|
|
{
|
|
|
|
SCRIPTAPI_PRECHECKHEADER
|
|
|
|
|
|
|
|
// Get registered shutdown hooks
|
|
|
|
lua_getglobal(L, "core");
|
|
|
|
lua_getfield(L, -1, "registered_on_shutdown");
|
|
|
|
// Call callbacks
|
2021-09-10 23:16:46 +02:00
|
|
|
try {
|
|
|
|
runCallbacks(0, RUN_CALLBACKS_MODE_FIRST);
|
|
|
|
} catch (LuaError &e) {
|
|
|
|
getClient()->setFatalError(e);
|
|
|
|
}
|
2017-01-21 15:02:08 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
bool ScriptApiClient::on_sending_message(const std::string &message)
|
|
|
|
{
|
|
|
|
SCRIPTAPI_PRECHECKHEADER
|
|
|
|
|
|
|
|
// Get core.registered_on_chat_messages
|
|
|
|
lua_getglobal(L, "core");
|
2017-06-09 15:48:04 +02:00
|
|
|
lua_getfield(L, -1, "registered_on_sending_chat_message");
|
2017-01-21 15:02:08 +01:00
|
|
|
// Call callbacks
|
|
|
|
lua_pushstring(L, message.c_str());
|
2021-09-10 23:16:46 +02:00
|
|
|
try {
|
|
|
|
runCallbacks(1, RUN_CALLBACKS_MODE_OR_SC);
|
|
|
|
} catch (LuaError &e) {
|
|
|
|
getClient()->setFatalError(e);
|
|
|
|
return true;
|
|
|
|
}
|
2018-06-30 17:11:38 +02:00
|
|
|
return readParam<bool>(L, -1);
|
2017-01-21 15:02:08 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
bool ScriptApiClient::on_receiving_message(const std::string &message)
|
|
|
|
{
|
|
|
|
SCRIPTAPI_PRECHECKHEADER
|
|
|
|
|
|
|
|
// Get core.registered_on_chat_messages
|
|
|
|
lua_getglobal(L, "core");
|
2017-06-09 15:48:04 +02:00
|
|
|
lua_getfield(L, -1, "registered_on_receiving_chat_message");
|
2017-01-21 15:02:08 +01:00
|
|
|
// Call callbacks
|
|
|
|
lua_pushstring(L, message.c_str());
|
2021-09-10 23:16:46 +02:00
|
|
|
try {
|
|
|
|
runCallbacks(1, RUN_CALLBACKS_MODE_OR_SC);
|
|
|
|
} catch (LuaError &e) {
|
|
|
|
getClient()->setFatalError(e);
|
|
|
|
return true;
|
|
|
|
}
|
2018-06-30 17:11:38 +02:00
|
|
|
return readParam<bool>(L, -1);
|
2017-01-21 15:02:08 +01:00
|
|
|
}
|
2017-01-22 00:20:55 +01:00
|
|
|
|
|
|
|
void ScriptApiClient::on_damage_taken(int32_t damage_amount)
|
|
|
|
{
|
|
|
|
SCRIPTAPI_PRECHECKHEADER
|
|
|
|
|
|
|
|
// Get core.registered_on_chat_messages
|
|
|
|
lua_getglobal(L, "core");
|
|
|
|
lua_getfield(L, -1, "registered_on_damage_taken");
|
|
|
|
// Call callbacks
|
|
|
|
lua_pushinteger(L, damage_amount);
|
2021-09-10 23:16:46 +02:00
|
|
|
try {
|
|
|
|
runCallbacks(1, RUN_CALLBACKS_MODE_OR_SC);
|
|
|
|
} catch (LuaError &e) {
|
|
|
|
getClient()->setFatalError(e);
|
|
|
|
}
|
2017-01-22 00:20:55 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void ScriptApiClient::on_hp_modification(int32_t newhp)
|
|
|
|
{
|
|
|
|
SCRIPTAPI_PRECHECKHEADER
|
|
|
|
|
|
|
|
// Get core.registered_on_chat_messages
|
|
|
|
lua_getglobal(L, "core");
|
|
|
|
lua_getfield(L, -1, "registered_on_hp_modification");
|
|
|
|
// Call callbacks
|
|
|
|
lua_pushinteger(L, newhp);
|
2021-09-10 23:16:46 +02:00
|
|
|
try {
|
|
|
|
runCallbacks(1, RUN_CALLBACKS_MODE_OR_SC);
|
|
|
|
} catch (LuaError &e) {
|
|
|
|
getClient()->setFatalError(e);
|
|
|
|
}
|
2017-01-22 00:20:55 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void ScriptApiClient::on_death()
|
|
|
|
{
|
|
|
|
SCRIPTAPI_PRECHECKHEADER
|
|
|
|
|
|
|
|
// Get registered shutdown hooks
|
|
|
|
lua_getglobal(L, "core");
|
|
|
|
lua_getfield(L, -1, "registered_on_death");
|
|
|
|
// Call callbacks
|
2021-09-10 23:16:46 +02:00
|
|
|
try {
|
|
|
|
runCallbacks(0, RUN_CALLBACKS_MODE_FIRST);
|
|
|
|
} catch (LuaError &e) {
|
|
|
|
getClient()->setFatalError(e);
|
|
|
|
}
|
2017-01-22 00:20:55 +01:00
|
|
|
}
|
2017-01-22 11:17:41 +01:00
|
|
|
|
|
|
|
void ScriptApiClient::environment_step(float dtime)
|
|
|
|
{
|
|
|
|
SCRIPTAPI_PRECHECKHEADER
|
|
|
|
|
|
|
|
// Get core.registered_globalsteps
|
|
|
|
lua_getglobal(L, "core");
|
|
|
|
lua_getfield(L, -1, "registered_globalsteps");
|
|
|
|
// Call callbacks
|
|
|
|
lua_pushnumber(L, dtime);
|
|
|
|
try {
|
|
|
|
runCallbacks(1, RUN_CALLBACKS_MODE_FIRST);
|
|
|
|
} catch (LuaError &e) {
|
2021-09-10 23:16:46 +02:00
|
|
|
getClient()->setFatalError(e);
|
2017-01-22 11:17:41 +01:00
|
|
|
}
|
|
|
|
}
|
2017-01-24 17:26:15 +01:00
|
|
|
|
|
|
|
void ScriptApiClient::on_formspec_input(const std::string &formname,
|
|
|
|
const StringMap &fields)
|
|
|
|
{
|
|
|
|
SCRIPTAPI_PRECHECKHEADER
|
|
|
|
|
|
|
|
// Get core.registered_on_chat_messages
|
|
|
|
lua_getglobal(L, "core");
|
|
|
|
lua_getfield(L, -1, "registered_on_formspec_input");
|
|
|
|
// Call callbacks
|
|
|
|
// param 1
|
|
|
|
lua_pushstring(L, formname.c_str());
|
|
|
|
// param 2
|
|
|
|
lua_newtable(L);
|
|
|
|
StringMap::const_iterator it;
|
|
|
|
for (it = fields.begin(); it != fields.end(); ++it) {
|
|
|
|
const std::string &name = it->first;
|
|
|
|
const std::string &value = it->second;
|
|
|
|
lua_pushstring(L, name.c_str());
|
|
|
|
lua_pushlstring(L, value.c_str(), value.size());
|
|
|
|
lua_settable(L, -3);
|
|
|
|
}
|
2021-09-10 23:16:46 +02:00
|
|
|
try {
|
|
|
|
runCallbacks(2, RUN_CALLBACKS_MODE_OR_SC);
|
|
|
|
} catch (LuaError &e) {
|
|
|
|
getClient()->setFatalError(e);
|
|
|
|
}
|
2017-01-24 17:26:15 +01:00
|
|
|
}
|
2017-01-29 18:43:44 +01:00
|
|
|
|
|
|
|
bool ScriptApiClient::on_dignode(v3s16 p, MapNode node)
|
|
|
|
{
|
|
|
|
SCRIPTAPI_PRECHECKHEADER
|
|
|
|
|
|
|
|
// Get core.registered_on_dignode
|
|
|
|
lua_getglobal(L, "core");
|
|
|
|
lua_getfield(L, -1, "registered_on_dignode");
|
|
|
|
|
|
|
|
// Push data
|
|
|
|
push_v3s16(L, p);
|
2022-10-19 00:01:44 +02:00
|
|
|
pushnode(L, node);
|
2017-01-29 18:43:44 +01:00
|
|
|
|
|
|
|
// Call functions
|
2021-09-10 23:16:46 +02:00
|
|
|
try {
|
|
|
|
runCallbacks(2, RUN_CALLBACKS_MODE_OR);
|
|
|
|
} catch (LuaError &e) {
|
|
|
|
getClient()->setFatalError(e);
|
|
|
|
return true;
|
|
|
|
}
|
2017-03-13 15:55:30 +01:00
|
|
|
return lua_toboolean(L, -1);
|
2017-01-29 19:28:38 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
bool ScriptApiClient::on_punchnode(v3s16 p, MapNode node)
|
|
|
|
{
|
|
|
|
SCRIPTAPI_PRECHECKHEADER
|
|
|
|
|
|
|
|
// Get core.registered_on_punchgnode
|
|
|
|
lua_getglobal(L, "core");
|
|
|
|
lua_getfield(L, -1, "registered_on_punchnode");
|
|
|
|
|
|
|
|
// Push data
|
|
|
|
push_v3s16(L, p);
|
2022-10-19 00:01:44 +02:00
|
|
|
pushnode(L, node);
|
2017-01-29 19:28:38 +01:00
|
|
|
|
|
|
|
// Call functions
|
2021-09-10 23:16:46 +02:00
|
|
|
try {
|
|
|
|
runCallbacks(2, RUN_CALLBACKS_MODE_OR);
|
|
|
|
} catch (LuaError &e) {
|
|
|
|
getClient()->setFatalError(e);
|
|
|
|
return true;
|
|
|
|
}
|
2018-06-30 17:11:38 +02:00
|
|
|
return readParam<bool>(L, -1);
|
2017-01-29 19:28:38 +01:00
|
|
|
}
|
2017-03-17 07:48:29 +01:00
|
|
|
|
2017-04-29 12:08:16 +02:00
|
|
|
bool ScriptApiClient::on_placenode(const PointedThing &pointed, const ItemDefinition &item)
|
|
|
|
{
|
|
|
|
SCRIPTAPI_PRECHECKHEADER
|
|
|
|
|
|
|
|
// Get core.registered_on_placenode
|
|
|
|
lua_getglobal(L, "core");
|
|
|
|
lua_getfield(L, -1, "registered_on_placenode");
|
|
|
|
|
|
|
|
// Push data
|
2017-05-20 16:45:49 +02:00
|
|
|
push_pointed_thing(L, pointed, true);
|
2017-04-29 12:08:16 +02:00
|
|
|
push_item_definition(L, item);
|
|
|
|
|
|
|
|
// Call functions
|
2021-09-10 23:16:46 +02:00
|
|
|
try {
|
|
|
|
runCallbacks(2, RUN_CALLBACKS_MODE_OR);
|
|
|
|
} catch (LuaError &e) {
|
|
|
|
getClient()->setFatalError(e);
|
|
|
|
return true;
|
|
|
|
}
|
2018-06-30 17:11:38 +02:00
|
|
|
return readParam<bool>(L, -1);
|
2017-04-29 12:08:16 +02:00
|
|
|
}
|
|
|
|
|
2017-05-06 21:12:44 +02:00
|
|
|
bool ScriptApiClient::on_item_use(const ItemStack &item, const PointedThing &pointed)
|
|
|
|
{
|
|
|
|
SCRIPTAPI_PRECHECKHEADER
|
|
|
|
|
|
|
|
// Get core.registered_on_item_use
|
|
|
|
lua_getglobal(L, "core");
|
|
|
|
lua_getfield(L, -1, "registered_on_item_use");
|
|
|
|
|
|
|
|
// Push data
|
|
|
|
LuaItemStack::create(L, item);
|
2017-05-20 16:45:49 +02:00
|
|
|
push_pointed_thing(L, pointed, true);
|
2017-05-06 21:12:44 +02:00
|
|
|
|
|
|
|
// Call functions
|
2021-09-10 23:16:46 +02:00
|
|
|
try {
|
|
|
|
runCallbacks(2, RUN_CALLBACKS_MODE_OR);
|
|
|
|
} catch (LuaError &e) {
|
|
|
|
getClient()->setFatalError(e);
|
|
|
|
return true;
|
|
|
|
}
|
2018-06-30 17:11:38 +02:00
|
|
|
return readParam<bool>(L, -1);
|
2017-05-06 21:12:44 +02:00
|
|
|
}
|
|
|
|
|
2017-10-02 22:09:49 +02:00
|
|
|
bool ScriptApiClient::on_inventory_open(Inventory *inventory)
|
|
|
|
{
|
|
|
|
SCRIPTAPI_PRECHECKHEADER
|
|
|
|
|
|
|
|
lua_getglobal(L, "core");
|
|
|
|
lua_getfield(L, -1, "registered_on_inventory_open");
|
|
|
|
|
2022-03-29 18:06:16 +02:00
|
|
|
push_inventory_lists(L, *inventory);
|
2017-10-02 22:09:49 +02:00
|
|
|
|
2021-09-10 23:16:46 +02:00
|
|
|
try {
|
|
|
|
runCallbacks(1, RUN_CALLBACKS_MODE_OR);
|
|
|
|
} catch (LuaError &e) {
|
|
|
|
getClient()->setFatalError(e);
|
|
|
|
return true;
|
|
|
|
}
|
2018-06-30 17:11:38 +02:00
|
|
|
return readParam<bool>(L, -1);
|
2017-10-02 22:09:49 +02:00
|
|
|
}
|
|
|
|
|
2017-03-17 07:48:29 +01:00
|
|
|
void ScriptApiClient::setEnv(ClientEnvironment *env)
|
|
|
|
{
|
|
|
|
ScriptApiBase::setEnv(env);
|
|
|
|
}
|