1
0
Fork 0
mirror of https://github.com/luanti-org/luanti.git synced 2025-08-16 18:01:40 +00:00

Give more infos to on_timer() callback

closes #15817
This commit is contained in:
sfan5 2025-08-10 17:22:06 +02:00
parent 7c88996210
commit fd3588d49c
8 changed files with 31 additions and 21 deletions

View file

@ -48,6 +48,7 @@ core.features = {
remove_item_match_meta = true,
httpfetch_additional_methods = true,
object_guids = true,
on_timer_four_args = true,
}
function core.has_feature(arg)

View file

@ -177,8 +177,6 @@ The file is a key-value store of modpack details.
* `textdomain`: Textdomain used to translate title and description. Defaults to modpack name.
See [Translating content meta](#translating-content-meta).
Note: to support 0.4.x, please also create an empty modpack.txt file.
Mod directory structure
-----------------------
@ -5841,6 +5839,8 @@ Utilities
httpfetch_additional_methods = true,
-- objects have get_guid method (5.13.0)
object_guids = true,
-- The NodeTimer `on_timer` callback is passed additional `node` and `timeout` args (5.14.0)
on_timer_four_args = true,
}
```
@ -5848,6 +5848,7 @@ Utilities
* checks for *server-side* feature availability
* `arg`: string or table in format `{foo=true, bar=true}`
* `missing_features`: `{foo=true, bar=true}`
* `core.get_player_information(player_name)`: Table containing information
about a player. Example return value:
@ -10472,8 +10473,6 @@ Used by `core.register_node`.
-- itemstack will hold clicker's wielded item.
-- Shall return the leftover itemstack.
-- Note: pointed_thing can be nil, if a mod calls this function.
-- This function does not get triggered by clients <=0.4.16 if the
-- "formspec" node metadata field is set.
on_dig = function(pos, node, digger),
-- default: core.node_dig
@ -10481,10 +10480,12 @@ Used by `core.register_node`.
-- return true if the node was dug successfully, false otherwise.
-- Deprecated: returning nil is the same as returning true.
on_timer = function(pos, elapsed),
on_timer = function(pos, elapsed, node, timeout),
-- default: nil
-- called by NodeTimers, see core.get_node_timer and NodeTimerRef.
-- elapsed is the total time passed since the timer was started.
-- `elapsed`: total time passed since the timer was started.
-- `node`: node table (since 5.14)
-- `timeout`: timeout value of the just ended timer (since 5.14)
-- return true to run the timer for another cycle with the same timeout
-- value.

View file

@ -43,8 +43,9 @@ core.register_node("callbacks:callback_node", {
print_to_everything("callbacks:callback_node:after_dig_node("..core.pos_to_string(pos)..")")
end,
on_timer = function(pos, elapsed)
print_to_everything("callbacks:callback_node:on_timer(): elapsed="..dump(elapsed))
on_timer = function(pos, elapsed, node, timeout)
print_to_everything("callbacks:callback_node:on_timer(): elapsed="..
elapsed .. " node=" .. dump(node) .. " timeout=" .. timeout)
return true
end,
})

View file

@ -168,7 +168,7 @@ bool MapBlock::saveStaticObject(u16 id, const StaticObject &obj, u32 reason)
return true;
}
void MapBlock::step(float dtime, const std::function<bool(v3s16, MapNode, f32)> &on_timer_cb)
void MapBlock::step(float dtime, const std::function<bool(v3s16, MapNode, NodeTimer)> &on_timer_cb)
{
// Run callbacks for elapsed node_timers
std::vector<NodeTimer> elapsed_timers = m_node_timers.step(dtime);
@ -177,8 +177,10 @@ void MapBlock::step(float dtime, const std::function<bool(v3s16, MapNode, f32)>
for (const auto &it : elapsed_timers) {
n = getNodeNoEx(it.position);
p = it.position + getPosRelative();
if (on_timer_cb(p, n, it.elapsed))
if (on_timer_cb(p, n, it)) {
// restart
setNodeTimer(NodeTimer(it.timeout, 0, it.position));
}
if (isOrphan())
return;
}

View file

@ -312,7 +312,7 @@ public:
bool saveStaticObject(u16 id, const StaticObject &obj, u32 reason);
/// @note This method is only for Server, don't call it on client
void step(float dtime, const std::function<bool(v3s16, MapNode, f32)> &on_timer_cb);
void step(float dtime, const std::function<bool(v3s16, MapNode, NodeTimer)> &on_timer_cb);
////
//// Timestamp (see m_timestamp)

View file

@ -215,7 +215,7 @@ void ScriptApiNode::node_after_destruct(v3s16 p, MapNode node)
lua_pop(L, 1); // Pop error handler
}
bool ScriptApiNode::node_on_timer(v3s16 p, MapNode node, f32 dtime)
bool ScriptApiNode::node_on_timer(v3s16 p, MapNode node, f32 elapsed, f32 timeout)
{
SCRIPTAPI_PRECHECKHEADER
@ -229,10 +229,14 @@ bool ScriptApiNode::node_on_timer(v3s16 p, MapNode node, f32 dtime)
// Call function
push_v3s16(L, p);
lua_pushnumber(L,dtime);
PCALL_RES(lua_pcall(L, 2, 1, error_handler));
lua_remove(L, error_handler);
return readParam<bool>(L, -1, false);
lua_pushnumber(L, elapsed);
pushnode(L, node);
lua_pushnumber(L, timeout);
PCALL_RES(lua_pcall(L, 4, 1, error_handler));
bool ret = readParam<bool>(L, -1, false);
lua_pop(L, 2); // error handler, return value
return ret;
}
void ScriptApiNode::node_on_receive_fields(v3s16 p,

View file

@ -28,11 +28,12 @@ public:
void node_on_destruct(v3s16 p, MapNode node);
bool node_on_flood(v3s16 p, MapNode node, MapNode newnode);
void node_after_destruct(v3s16 p, MapNode node);
bool node_on_timer(v3s16 p, MapNode node, f32 dtime);
bool node_on_timer(v3s16 p, MapNode node, f32 elapsed, f32 timeout);
void node_on_receive_fields(v3s16 p,
const std::string &formname,
const StringMap &fields,
ServerActiveObject *sender);
public:
static struct EnumString es_DrawType[];
static struct EnumString es_ContentParamType[];

View file

@ -572,8 +572,8 @@ void ServerEnvironment::activateBlock(MapBlock *block)
return;
// Run node timers
block->step((float)dtime_s, [&](v3s16 p, MapNode n, f32 d) -> bool {
return m_script->node_on_timer(p, n, d);
block->step((float)dtime_s, [&](v3s16 p, MapNode n, NodeTimer t) -> bool {
return m_script->node_on_timer(p, n, t.elapsed, t.timeout);
});
}
@ -999,8 +999,8 @@ void ServerEnvironment::step(float dtime)
}
// Run node timers
block->step(dtime, [&](v3s16 p, MapNode n, f32 d) -> bool {
return m_script->node_on_timer(p, n, d);
block->step(dtime, [&](v3s16 p, MapNode n, NodeTimer t) -> bool {
return m_script->node_on_timer(p, n, t.elapsed, t.timeout);
});
}
}