0
0
Fork 0

replace pairs() by ipairs() in mcl_playerinfo

This commit is contained in:
AFCMS 2021-02-18 15:31:13 +00:00
parent 0531aae687
commit 25653e6178
1 changed files with 91 additions and 91 deletions

View File

@ -1,91 +1,91 @@
-- Player state for public API -- Player state for public API
mcl_playerinfo = {} mcl_playerinfo = {}
-- Get node but use fallback for nil or unknown -- Get node but use fallback for nil or unknown
local function node_ok(pos, fallback) local function node_ok(pos, fallback)
fallback = fallback or "air" fallback = fallback or "air"
local node = minetest.get_node_or_nil(pos) local node = minetest.get_node_or_nil(pos)
if not node then if not node then
return fallback return fallback
end end
if minetest.registered_nodes[node.name] then if minetest.registered_nodes[node.name] then
return node.name return node.name
end end
return fallback return fallback
end end
local time = 0 local time = 0
local get_player_nodes = function(player_pos) local get_player_nodes = function(player_pos)
local work_pos = table.copy(player_pos) local work_pos = table.copy(player_pos)
-- what is around me? -- what is around me?
work_pos.y = work_pos.y - 0.1 -- standing on work_pos.y = work_pos.y - 0.1 -- standing on
local node_stand = node_ok(work_pos) local node_stand = node_ok(work_pos)
local node_stand_below = node_ok({x=work_pos.x, y=work_pos.y-1, z=work_pos.z}) local node_stand_below = node_ok({x=work_pos.x, y=work_pos.y-1, z=work_pos.z})
work_pos.y = work_pos.y + 1.5 -- head level work_pos.y = work_pos.y + 1.5 -- head level
local node_head = node_ok(work_pos) local node_head = node_ok(work_pos)
work_pos.y = work_pos.y - 1.2 -- feet level work_pos.y = work_pos.y - 1.2 -- feet level
local node_feet = node_ok(work_pos) local node_feet = node_ok(work_pos)
return node_stand, node_stand_below, node_head, node_feet return node_stand, node_stand_below, node_head, node_feet
end end
minetest.register_globalstep(function(dtime) minetest.register_globalstep(function(dtime)
time = time + dtime time = time + dtime
-- Run the rest of the code every 0.5 seconds -- Run the rest of the code every 0.5 seconds
if time < 0.5 then if time < 0.5 then
return return
end end
-- reset time for next check -- reset time for next check
-- FIXME: Make sure a regular check interval applies -- FIXME: Make sure a regular check interval applies
time = 0 time = 0
-- check players -- check players
for _,player in pairs(minetest.get_connected_players()) do for _,player in ipairs(minetest.get_connected_players()) do
-- who am I? -- who am I?
local name = player:get_player_name() local name = player:get_player_name()
-- where am I? -- where am I?
local pos = player:get_pos() local pos = player:get_pos()
-- what is around me? -- what is around me?
local node_stand, node_stand_below, node_head, node_feet = get_player_nodes(pos) local node_stand, node_stand_below, node_head, node_feet = get_player_nodes(pos)
mcl_playerinfo[name].node_stand = node_stand mcl_playerinfo[name].node_stand = node_stand
mcl_playerinfo[name].node_stand_below = node_stand_below mcl_playerinfo[name].node_stand_below = node_stand_below
mcl_playerinfo[name].node_head = node_head mcl_playerinfo[name].node_head = node_head
mcl_playerinfo[name].node_feet = node_feet mcl_playerinfo[name].node_feet = node_feet
end end
end) end)
-- set to blank on join (for 3rd party mods) -- set to blank on join (for 3rd party mods)
minetest.register_on_joinplayer(function(player) minetest.register_on_joinplayer(function(player)
local name = player:get_player_name() local name = player:get_player_name()
mcl_playerinfo[name] = { mcl_playerinfo[name] = {
node_head = "", node_head = "",
node_feet = "", node_feet = "",
node_stand = "", node_stand = "",
node_stand_below = "", node_stand_below = "",
} }
end) end)
-- clear when player leaves -- clear when player leaves
minetest.register_on_leaveplayer(function(player) minetest.register_on_leaveplayer(function(player)
local name = player:get_player_name() local name = player:get_player_name()
mcl_playerinfo[name] = nil mcl_playerinfo[name] = nil
end) end)