From 52128ae11e8b1a7ce66a87c53f1b15f3aabe69f4 Mon Sep 17 00:00:00 2001 From: Warr1024 Date: Fri, 9 Jul 2021 09:08:40 -0400 Subject: Add API for mods to hook liquid transformation events (#11405) Add API for mods to hook liquid transformation events Without this API, there is no reliable way for mods to be notified when liquid transform modifies nodes and mods are forced to poll for changes. This allows mods to detect changes to flowing liquid nodes and liquid renewal using event-driven logic. --- src/script/cpp_api/s_env.cpp | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) (limited to 'src/script/cpp_api/s_env.cpp') diff --git a/src/script/cpp_api/s_env.cpp b/src/script/cpp_api/s_env.cpp index c4a39a869..c11de3757 100644 --- a/src/script/cpp_api/s_env.cpp +++ b/src/script/cpp_api/s_env.cpp @@ -25,6 +25,8 @@ with this program; if not, write to the Free Software Foundation, Inc., #include "mapgen/mapgen.h" #include "lua_api/l_env.h" #include "server.h" +#include "script/common/c_content.h" + void ScriptApiEnv::environment_OnGenerated(v3s16 minp, v3s16 maxp, u32 blockseed) @@ -267,3 +269,35 @@ void ScriptApiEnv::on_emerge_area_completion( luaL_unref(L, LUA_REGISTRYINDEX, state->args_ref); } } + +void ScriptApiEnv::on_liquid_transformed( + const std::vector> &list) +{ + SCRIPTAPI_PRECHECKHEADER + + // Get core.registered_on_liquid_transformed + lua_getglobal(L, "core"); + lua_getfield(L, -1, "registered_on_liquid_transformed"); + luaL_checktype(L, -1, LUA_TTABLE); + lua_remove(L, -2); + + // Skip converting list and calling hook if there are + // no registered callbacks. + if(lua_objlen(L, -1) < 1) return; + + // Convert the list to a pos array and a node array for lua + int index = 1; + const NodeDefManager *ndef = getEnv()->getGameDef()->ndef(); + lua_createtable(L, list.size(), 0); + lua_createtable(L, list.size(), 0); + for(std::pair p : list) { + lua_pushnumber(L, index); + push_v3s16(L, p.first); + lua_rawset(L, -4); + lua_pushnumber(L, index++); + pushnode(L, p.second, ndef); + lua_rawset(L, -3); + } + + runCallbacks(2, RUN_CALLBACKS_MODE_FIRST); +} \ No newline at end of file -- cgit v1.2.3