2021-05-25 12:52:25 +02:00
|
|
|
local table = table
|
|
|
|
|
2024-04-12 21:01:15 +02:00
|
|
|
local storage = minetest.get_mod_storage()
|
|
|
|
|
2021-02-18 16:31:13 +01:00
|
|
|
-- Player state for public API
|
|
|
|
mcl_playerinfo = {}
|
2024-04-12 21:01:15 +02:00
|
|
|
local player_mod_metadata = {}
|
2021-02-18 16:31:13 +01:00
|
|
|
|
|
|
|
-- Get node but use fallback for nil or unknown
|
|
|
|
local function node_ok(pos, fallback)
|
|
|
|
|
|
|
|
fallback = fallback or "air"
|
|
|
|
|
|
|
|
local node = minetest.get_node_or_nil(pos)
|
|
|
|
|
|
|
|
if not node then
|
|
|
|
return fallback
|
|
|
|
end
|
|
|
|
|
|
|
|
if minetest.registered_nodes[node.name] then
|
|
|
|
return node.name
|
|
|
|
end
|
|
|
|
|
|
|
|
return fallback
|
|
|
|
end
|
|
|
|
|
2021-05-25 12:52:25 +02:00
|
|
|
local function get_player_nodes(player_pos)
|
2021-02-18 16:31:13 +01:00
|
|
|
local work_pos = table.copy(player_pos)
|
|
|
|
|
|
|
|
-- what is around me?
|
|
|
|
work_pos.y = work_pos.y - 0.1 -- standing on
|
|
|
|
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})
|
|
|
|
|
|
|
|
work_pos.y = work_pos.y + 1.5 -- head level
|
|
|
|
local node_head = node_ok(work_pos)
|
2022-07-17 15:14:24 +02:00
|
|
|
work_pos.y = work_pos.y + 0.5 -- top of head level, at collision box height
|
|
|
|
local node_head_top = node_ok(work_pos)
|
|
|
|
work_pos.y = work_pos.y - 0.5
|
2021-02-18 16:31:13 +01:00
|
|
|
|
|
|
|
work_pos.y = work_pos.y - 1.2 -- feet level
|
|
|
|
local node_feet = node_ok(work_pos)
|
|
|
|
|
2022-07-17 15:14:24 +02:00
|
|
|
return node_stand, node_stand_below, node_head, node_feet, node_head_top
|
2021-02-18 16:31:13 +01:00
|
|
|
end
|
|
|
|
|
2024-04-09 09:35:57 +02:00
|
|
|
local time = 0
|
2021-02-18 16:31:13 +01:00
|
|
|
minetest.register_globalstep(function(dtime)
|
|
|
|
-- Run the rest of the code every 0.5 seconds
|
2024-04-09 09:35:57 +02:00
|
|
|
time = time + dtime
|
2021-02-18 16:31:13 +01:00
|
|
|
if time < 0.5 then
|
|
|
|
return
|
|
|
|
end
|
|
|
|
|
|
|
|
-- reset time for next check
|
|
|
|
-- FIXME: Make sure a regular check interval applies
|
|
|
|
time = 0
|
|
|
|
|
|
|
|
-- check players
|
2021-03-16 17:39:06 +01:00
|
|
|
for _,player in pairs(minetest.get_connected_players()) do
|
2021-02-18 16:31:13 +01:00
|
|
|
-- who am I?
|
|
|
|
local name = player:get_player_name()
|
|
|
|
|
|
|
|
-- where am I?
|
|
|
|
local pos = player:get_pos()
|
|
|
|
|
|
|
|
-- what is around me?
|
2022-07-17 15:14:24 +02:00
|
|
|
local node_stand, node_stand_below, node_head, node_feet, node_head_top = get_player_nodes(pos)
|
2021-02-18 16:31:13 +01:00
|
|
|
mcl_playerinfo[name].node_stand = node_stand
|
|
|
|
mcl_playerinfo[name].node_stand_below = node_stand_below
|
|
|
|
mcl_playerinfo[name].node_head = node_head
|
|
|
|
mcl_playerinfo[name].node_feet = node_feet
|
2022-07-17 15:14:24 +02:00
|
|
|
mcl_playerinfo[name].node_head_top = node_head_top
|
2021-02-18 16:31:13 +01:00
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
end)
|
|
|
|
|
2024-04-12 21:01:15 +02:00
|
|
|
function mcl_playerinfo.get_mod_meta(player_name, modname)
|
|
|
|
-- Load the player's metadata
|
|
|
|
local meta = player_mod_metadata[player_name]
|
|
|
|
if not meta then
|
|
|
|
meta = minetest.deserialize(storage:get_string(player_name))
|
|
|
|
end
|
|
|
|
if not meta then
|
|
|
|
meta = {}
|
|
|
|
end
|
|
|
|
player_mod_metadata[player_name] = meta
|
|
|
|
|
|
|
|
-- Get the requested module's section of the metadata
|
|
|
|
local mod_meta = meta[modname] or {}
|
|
|
|
meta[modname] = mod_meta
|
|
|
|
return mod_meta
|
|
|
|
end
|
|
|
|
|
2021-02-18 16:31:13 +01:00
|
|
|
-- set to blank on join (for 3rd party mods)
|
|
|
|
minetest.register_on_joinplayer(function(player)
|
|
|
|
local name = player:get_player_name()
|
|
|
|
|
|
|
|
mcl_playerinfo[name] = {
|
|
|
|
node_head = "",
|
|
|
|
node_feet = "",
|
|
|
|
node_stand = "",
|
|
|
|
node_stand_below = "",
|
2022-07-17 15:14:24 +02:00
|
|
|
node_head_top = "",
|
2021-02-18 16:31:13 +01:00
|
|
|
}
|
|
|
|
end)
|
|
|
|
|
|
|
|
-- clear when player leaves
|
|
|
|
minetest.register_on_leaveplayer(function(player)
|
|
|
|
local name = player:get_player_name()
|
|
|
|
|
|
|
|
mcl_playerinfo[name] = nil
|
|
|
|
end)
|
2024-04-12 21:01:15 +02:00
|
|
|
|
|
|
|
minetest.register_on_shutdown(function()
|
|
|
|
for name,data in pairs(player_mod_metadata) do
|
|
|
|
storage:set_string(name, minetest.serialize(data))
|
|
|
|
end
|
|
|
|
end)
|