Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[no squash] Fix node callbacks unit test #14406

Merged
merged 2 commits into from
Mar 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 0 additions & 5 deletions builtin/emerge/env.lua
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,6 @@ function core.get_node(pos)
return core.vmanip:get_node_at(pos)
end

function core.get_node_or_nil(pos)
local node = core.vmanip:get_node_at(pos)
return node.name ~= "ignore" and node
end

function core.get_perlin(seed, octaves, persist, spread)
local params
if type(seed) == "table" then
Expand Down
1 change: 1 addition & 0 deletions doc/lua_api.md
Original file line number Diff line number Diff line change
Expand Up @@ -5919,6 +5919,7 @@ Environment access
returns `{name="ignore", param1=0, param2=0}` for unloaded areas.
* `minetest.get_node_or_nil(pos)`
* Same as `get_node` but returns `nil` for unloaded areas.
* Note that areas may still contain "ignore" despite being loaded.
* `minetest.get_node_light(pos[, timeofday])`
* Gets the light value at the given position. Note that the light value
"inside" the node at the given position is returned, so you usually want
Expand Down
11 changes: 6 additions & 5 deletions games/devtest/mods/unittests/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -108,9 +108,9 @@ local function wait_for_player(callback)
end)
end

local function wait_for_map(player, callback)
local function wait_for_map(pos, callback)
local function check()
if core.get_node_or_nil(player:get_pos()) ~= nil then
if core.get_node(pos).name ~= "ignore" then
callback()
else
core.after(0, check)
Expand All @@ -119,8 +119,8 @@ local function wait_for_map(player, callback)
check()
end

-- This runs in a coroutine so it uses await()
function unittests.run_all()
-- This runs in a coroutine so it uses await().
local counters = { time = 0, total = 0, passed = 0 }

-- Run standalone tests first
Expand All @@ -143,10 +143,11 @@ function unittests.run_all()
end

-- Wait for the world to generate/load, run tests that require map access
local pos = player:get_pos():round():offset(0, 5, 0)
core.forceload_block(pos, true, -1)
await(function(cb)
wait_for_map(player, cb)
wait_for_map(pos, cb)
end)
local pos = vector.round(player:get_pos())
for idx = 1, #unittests.list do
local def = unittests.list[idx]
if not def.done then
Expand Down
23 changes: 15 additions & 8 deletions games/devtest/mods/unittests/misc.lua
Original file line number Diff line number Diff line change
Expand Up @@ -99,17 +99,24 @@ local function test_clear_meta(_, pos)
end
unittests.register("test_clear_meta", test_clear_meta, {map=true})

local on_punch_called
minetest.register_on_punchnode(function()
local on_punch_called, on_place_called
core.register_on_placenode(function()
on_place_called = true
end)
core.register_on_punchnode(function()
on_punch_called = true
end)
unittests.register("test_punch_node", function(_, pos)
minetest.place_node(pos, {name="basenodes:dirt"})
local function test_node_callbacks(_, pos)
on_place_called = false
on_punch_called = false
minetest.punch_node(pos)
minetest.remove_node(pos)
-- currently failing: assert(on_punch_called)
end, {map=true})

core.place_node(pos, {name="basenodes:dirt"})
assert(on_place_called, "on_place not called")
core.punch_node(pos)
assert(on_punch_called, "on_punch not called")
core.remove_node(pos)
end
unittests.register("test_node_callbacks", test_node_callbacks, {map=true})

local function test_compress()
-- This text should be compressible, to make sure the results are... normal
Expand Down
14 changes: 14 additions & 0 deletions src/script/lua_api/l_env.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1544,6 +1544,19 @@ void ModApiEnv::InitializeClient(lua_State *L, int top)
if (!vm) \
return 0

// get_node_or_nil(pos)
int ModApiEnvVM::l_get_node_or_nil(lua_State *L)
{
GET_VM_PTR;

v3s16 pos = read_v3s16(L, 1);
if (vm->exists(pos))
pushnode(L, vm->getNodeRefUnsafe(pos));
else
lua_pushnil(L);
return 1;
}

// get_node_max_level(pos)
int ModApiEnvVM::l_get_node_max_level(lua_State *L)
{
Expand Down Expand Up @@ -1713,6 +1726,7 @@ MMVManip *ModApiEnvVM::getVManip(lua_State *L)
void ModApiEnvVM::InitializeEmerge(lua_State *L, int top)
{
// other, more trivial functions are in builtin/emerge/env.lua
API_FCT(get_node_or_nil);
API_FCT(get_node_max_level);
API_FCT(get_node_level);
API_FCT(set_node_level);
Expand Down
3 changes: 3 additions & 0 deletions src/script/lua_api/l_env.h
Original file line number Diff line number Diff line change
Expand Up @@ -250,6 +250,9 @@ class ModApiEnv : public ModApiEnvBase {
class ModApiEnvVM : public ModApiEnvBase {
private:

// get_node_or_nil(pos)
static int l_get_node_or_nil(lua_State *L);

// get_node_max_level(pos)
static int l_get_node_max_level(lua_State *L);

Expand Down