add basic API to register player globalstep

This commit is contained in:
AFCMS 2021-04-09 22:25:42 +02:00
parent f5f6e232cc
commit c7a144aa10
4 changed files with 58 additions and 41 deletions

View File

@ -1,3 +1,5 @@
local modpath = minetest.get_modpath(minetest.get_current_modname())
mcl_util = {} mcl_util = {}
-- Based on minetest.rotate_and_place -- Based on minetest.rotate_and_place
@ -418,3 +420,5 @@ function mcl_util.get_color(colorstr)
return colorstr, hex return colorstr, hex
end end
end end
dofile(modpath.."/playerstep.lua")

View File

@ -0,0 +1,19 @@
local get_connected_players = minetest.get_connected_players
mcl_util.registered_player_globalstep = {}
local registered_player_globalstep = mcl_util.registered_player_globalstep
function mcl_util.register_player_globalstep(func)
table.insert(mcl_util.registered_player_globalstep, func)
end
minetest.register_on_mods_loaded(function()
minetest.register_globalstep(function(dtime)
local players = get_connected_players()
for _,func in pairs(registered_player_globalstep) do
for _,player in pairs(players) do
func(player, dtime)
end
end
end)
end)

View File

@ -64,50 +64,43 @@ minetest.register_on_leaveplayer(function(player)
wieldindex[name] = nil wieldindex[name] = nil
end) end)
minetest.register_globalstep(function(dtime) mcl_util.register_player_globalstep(function(player, dtime)
for _, player in pairs(minetest.get_connected_players()) do local player_name = player:get_player_name()
local player_name = player:get_player_name() local wstack = player:get_wielded_item()
local wstack = player:get_wielded_item() local wname = wstack:get_name()
local wname = wstack:get_name() local windex = player:get_wield_index()
local windex = player:get_wield_index() if dtimes[player_name] and dtimes[player_name] < dlimit then
dtimes[player_name] = dtimes[player_name] + dtime
if dtimes[player_name] and dtimes[player_name] < dlimit then if dtimes[player_name] > dlimit and huds[player_name] then
dtimes[player_name] = dtimes[player_name] + dtime player:hud_change(huds[player_name], 'text', "")
if dtimes[player_name] > dlimit and huds[player_name] then
player:hud_change(huds[player_name], 'text', "")
end
end end
end
-- Update HUD when wielded item or wielded index changed -- Update HUD when wielded item or wielded index changed
if wname ~= wield[player_name] or windex ~= wieldindex[player_name] then if wname ~= wield[player_name] or windex ~= wieldindex[player_name] then
wieldindex[player_name] = windex wieldindex[player_name] = windex
wield[player_name] = wname wield[player_name] = wname
dtimes[player_name] = 0 dtimes[player_name] = 0
if huds[player_name] then
if huds[player_name] then local def = minetest.registered_items[wname]
local meta = wstack:get_meta()
local def = minetest.registered_items[wname] --[[ Get description. Order of preference:
local meta = wstack:get_meta() * description from metadata
* description from item definition
--[[ Get description. Order of preference: * itemstring ]]
* description from metadata local desc = meta:get_string("description")
* description from item definition if (desc == nil or desc == "") and def then
* itemstring ]] desc = def.description
local desc = meta:get_string("description")
if (desc == nil or desc == "") and def then
desc = def.description
end
if desc == nil or desc == "" then
desc = wname
end
-- Cut off item description after first newline
local firstnewline = string.find(desc, "\n")
if firstnewline then
desc = string.sub(desc, 1, firstnewline-1)
end
player:hud_change(huds[player_name], 'text', desc)
end end
if desc == nil or desc == "" then
desc = wname
end
-- Cut off item description after first newline
local firstnewline = string.find(desc, "\n")
if firstnewline then
desc = string.sub(desc, 1, firstnewline-1)
end
player:hud_change(huds[player_name], 'text', desc)
end end
end end
end) end)

View File

@ -1,4 +1,5 @@
name = show_wielded_item name = show_wielded_item
author = 4aiman author = 4aiman
description = Displays the name of the wielded item. description = Displays the name of the wielded item.
depends = mcl_util
optional_depends = hudbars optional_depends = hudbars