Skip to content

Commit 52128ae

Browse files
authored
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.
1 parent e9bc59e commit 52128ae

File tree

5 files changed

+47
-1
lines changed

5 files changed

+47
-1
lines changed

builtin/game/register.lua

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -610,6 +610,7 @@ core.registered_on_modchannel_message, core.register_on_modchannel_message = mak
610610
core.registered_on_player_inventory_actions, core.register_on_player_inventory_action = make_registration()
611611
core.registered_allow_player_inventory_actions, core.register_allow_player_inventory_action = make_registration()
612612
core.registered_on_rightclickplayers, core.register_on_rightclickplayer = make_registration()
613+
core.registered_on_liquid_transformed, core.register_on_liquid_transformed = make_registration()
613614

614615
--
615616
-- Compatibility for on_mapgen_init()

doc/lua_api.txt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4859,6 +4859,12 @@ Call these functions only at load time!
48594859
* Called when an incoming mod channel message is received
48604860
* You should have joined some channels to receive events.
48614861
* If message comes from a server mod, `sender` field is an empty string.
4862+
* `minetest.register_on_liquid_transformed(function(post_list, node_list))`
4863+
* Called after liquid nodes are modified by the engine's liquid transformation
4864+
process.
4865+
* `pos_list` is an array of all modified positions.
4866+
* `node_list` is an array of the old node that was previously at the position
4867+
with the corresponding index in pos_list.
48624868

48634869
Setting-related
48644870
---------------

src/map.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -829,7 +829,7 @@ void Map::transformLiquids(std::map<v3s16, MapBlock*> &modified_blocks,
829829
m_transforming_liquid.push_back(iter);
830830

831831
voxalgo::update_lighting_nodes(this, changed_nodes, modified_blocks);
832-
832+
env->getScriptIface()->on_liquid_transformed(changed_nodes);
833833

834834
/* ----------------------------------------------------------------------
835835
* Manage the queue so that it does not grow indefinately

src/script/cpp_api/s_env.cpp

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@ with this program; if not, write to the Free Software Foundation, Inc.,
2525
#include "mapgen/mapgen.h"
2626
#include "lua_api/l_env.h"
2727
#include "server.h"
28+
#include "script/common/c_content.h"
29+
2830

2931
void ScriptApiEnv::environment_OnGenerated(v3s16 minp, v3s16 maxp,
3032
u32 blockseed)
@@ -267,3 +269,35 @@ void ScriptApiEnv::on_emerge_area_completion(
267269
luaL_unref(L, LUA_REGISTRYINDEX, state->args_ref);
268270
}
269271
}
272+
273+
void ScriptApiEnv::on_liquid_transformed(
274+
const std::vector<std::pair<v3s16, MapNode>> &list)
275+
{
276+
SCRIPTAPI_PRECHECKHEADER
277+
278+
// Get core.registered_on_liquid_transformed
279+
lua_getglobal(L, "core");
280+
lua_getfield(L, -1, "registered_on_liquid_transformed");
281+
luaL_checktype(L, -1, LUA_TTABLE);
282+
lua_remove(L, -2);
283+
284+
// Skip converting list and calling hook if there are
285+
// no registered callbacks.
286+
if(lua_objlen(L, -1) < 1) return;
287+
288+
// Convert the list to a pos array and a node array for lua
289+
int index = 1;
290+
const NodeDefManager *ndef = getEnv()->getGameDef()->ndef();
291+
lua_createtable(L, list.size(), 0);
292+
lua_createtable(L, list.size(), 0);
293+
for(std::pair<v3s16, MapNode> p : list) {
294+
lua_pushnumber(L, index);
295+
push_v3s16(L, p.first);
296+
lua_rawset(L, -4);
297+
lua_pushnumber(L, index++);
298+
pushnode(L, p.second, ndef);
299+
lua_rawset(L, -3);
300+
}
301+
302+
runCallbacks(2, RUN_CALLBACKS_MODE_FIRST);
303+
}

src/script/cpp_api/s_env.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ with this program; if not, write to the Free Software Foundation, Inc.,
2121

2222
#include "cpp_api/s_base.h"
2323
#include "irr_v3d.h"
24+
#include "mapnode.h"
25+
#include <vector>
2426

2527
class ServerEnvironment;
2628
struct ScriptCallbackState;
@@ -41,5 +43,8 @@ class ScriptApiEnv : virtual public ScriptApiBase
4143
void on_emerge_area_completion(v3s16 blockpos, int action,
4244
ScriptCallbackState *state);
4345

46+
// Called after liquid transform changes
47+
void on_liquid_transformed(const std::vector<std::pair<v3s16, MapNode>> &list);
48+
4449
void initializeEnvironment(ServerEnvironment *env);
4550
};

0 commit comments

Comments
 (0)