support many (!) mods to new API

This commit is contained in:
AFCMS 2021-04-10 02:04:31 +02:00
parent c7a144aa10
commit bace4f5f20
30 changed files with 369 additions and 189 deletions

View File

@ -44,8 +44,8 @@ minetest.register_on_leaveplayer(function(player)
controls.players[name] = nil
end)
minetest.register_globalstep(function(dtime)
for _, player in pairs(get_connected_players()) do
mcl_util.register_player_globalstep({
execute = function(player, dtime)
local player_name = player:get_player_name()
local player_controls = player:get_player_control()
if controls.players[player_name] then
@ -71,5 +71,5 @@ minetest.register_globalstep(function(dtime)
end
end
end
end
end)
end,
})

View File

@ -1,4 +1,5 @@
name = controls
author = Arcelmi
depends = mcl_util
description = Controls framework by Arcelmi

24
mods/CORE/mcl_util/API.md Normal file
View File

@ -0,0 +1,24 @@
# mcl_util
Provide utility functions for mcl2
## mcl_util.register_player_globalstep(def)
Used to register a player globalstep.
Use this insteed of global step if you need to execute action on all players to avoid calling the same functions multiple time.
```
mcl_util.register_player_globalstep({
can_execute = function(dtime)
return true --return false if you want to skip "execute" function
end,
execute = function(player, dtime)
--do some cool stuff
--will be executed for every player at each server step
end,
after_execute = function(dtime)
end,
})
```
## mcl_util.registered_player_globalstep
Table containing all registered player globalstep.

View File

@ -3,16 +3,22 @@ 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)
function mcl_util.register_player_globalstep(def)
if not def.can_execute then def.can_execute = function() return true end end
table.insert(mcl_util.registered_player_globalstep, {can_execute=def.can_execute, func=def.execute})
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)
for i = 1, #registered_player_globalstep do
if registered_player_globalstep[i].can_execute and registered_player_globalstep[i].can_execute(dtime) then
for p = 1, #players do
registered_player_globalstep[i].func(players[p], dtime)
end
if registered_player_globalstep[i].after_execute then
registered_player_globalstep[i].after_execute(dtime)
end
end
end
end)

View File

@ -52,3 +52,33 @@ minetest.register_globalstep(function(dtime)
timer = 0
end
end)
--[[mcl_util.register_player_globalstep({
can_execute = function(dtime)
timer = timer + dtime;
if timer >= 0.3 then
return true
else
return false
end
end,
execute = function(player, dtime)
local pp = player:get_pos()
pp.y = ceil(pp.y)
local loc = vector_add(pp, {x=0,y=-1,z=0})
if loc ~= nil then
local nodeiamon = get_node(loc)
if nodeiamon ~= nil then
if on_walk[nodeiamon.name] then
on_walk[nodeiamon.name](loc, nodeiamon, player)
end
for i = 1, #registered_globals do
registered_globals[i](loc, nodeiamon, player)
end
end
end
end,
after_execute = function(dtime)
timer = 0
end,
})]]

View File

@ -1,4 +1,5 @@
name = walkover
author = lordfingle
depends = mcl_util
description = Some mode developers have shown an interest in having an `on_walk_over` event. This is useful for pressure-plates and the like.

View File

@ -7,7 +7,6 @@ local is_in_void = mcl_worlds.is_in_void
local get_spawn_pos = mcl_spawn.get_player_spawn_pos
local death_msg = mcl_death_messages.player_damage
local send_chat = minetest.chat_send_player
local get_connected = minetest.get_connected_players
local voidtimer = 0
local VOID_DAMAGE_FREQ = 0.5
@ -54,14 +53,15 @@ minetest.register_on_mods_loaded(function()
end)
-- Hurt players or teleport them back to spawn if they are too deep in the void
minetest.register_globalstep(function(dtime)
mcl_util.register_player_globalstep({
can_execute = function(dtime)
voidtimer = voidtimer + dtime
if voidtimer > VOID_DAMAGE_FREQ then
voidtimer = 0
local enable_damage = minetest.settings:get_bool("enable_damage")
local players = get_connected()
for p=1, #players do
local player = players[p]
return true
end
end,
execute = function(player, dtime)
local pos = player:get_pos()
local void, void_deadly = is_in_void(pos)
if void_deadly then
@ -85,6 +85,5 @@ minetest.register_globalstep(function(dtime)
end
end
end
end
end
end)
end,
})

View File

@ -1,6 +1,8 @@
local S = minetest.get_translator("doc_items")
local N = function(s) return s end
local math = math
doc.sub.items = {}
-- Template texts
@ -1397,16 +1399,18 @@ TODO: If possible, try to replace this functionality by updating the revealed it
soon the player obtained a new item (probably needs new Minetest callbacks). ]]
local checktime = 8
local timer = 0
minetest.register_globalstep(function(dtime)
timer = timer + dtime
if timer > checktime then
local players = minetest.get_connected_players()
for p=1, #players do
reveal_items_in_inventory(players[p])
end
mcl_util.register_player_globalstep({
can_execute = function(dtime)
timer = timer + dtime
if timer > checktime then return true end
end,
execute = function(player, dtime)
reveal_items_in_inventory(player)
end,
after_execute = function(dtime)
timer = math.fmod(timer, checktime)
end
end)
end,
})
minetest.register_on_mods_loaded(gather_descs)

View File

@ -1,4 +1,4 @@
name = doc_items
author = Wuzzy
description = Adds automatically generated help texts for items.
depends = doc
depends = doc, mcl_util

View File

@ -112,7 +112,7 @@ end)
local main_timer = 0
local timer = 0
minetest.register_globalstep(function(dtime)
--[[minetest.register_globalstep(function(dtime)
main_timer = main_timer + dtime
timer = timer + dtime
if main_timer > mcl_hbarmor.tick or timer > 4 then
@ -132,4 +132,29 @@ minetest.register_globalstep(function(dtime)
end
end
if timer > 4 then timer = 0 end
end)
end)]]
mcl_util.register_player_globalstep({
can_execute = function(dtime)
main_timer = main_timer + dtime
timer = timer + dtime
if main_timer > mcl_hbarmor.tick or timer > 4 then
if minetest.settings:get_bool("enable_damage") then
if main_timer > mcl_hbarmor.tick then main_timer = 0 end
return true
end
end
if timer > 4 then timer = 0 end
end,
execute = function(player, dtime)
local name = player:get_player_name()
if mcl_hbarmor.player_active[name] == true then
local ret = mcl_hbarmor.get_armor(player)
if ret == false then
minetest.log("error", "[mcl_hbarmor] Call to mcl_hbarmor.get_armor in globalstep returned with false!")
end
-- update all hud elements
update_hud(player)
end
end,
})

View File

@ -1,4 +1,4 @@
name = mcl_hbarmor
author = BlockMen
description = Adds a HUD bar displaying the current damage of the player's armor.
depends = hudbars, mcl_armor
depends = hudbars, mcl_armor, mcl_util

View File

@ -64,7 +64,8 @@ minetest.register_on_leaveplayer(function(player)
wieldindex[name] = nil
end)
mcl_util.register_player_globalstep(function(player, dtime)
mcl_util.register_player_globalstep({
execute = function(player, dtime)
local player_name = player:get_player_name()
local wstack = player:get_wielded_item()
local wname = wstack:get_name()
@ -103,4 +104,5 @@ mcl_util.register_player_globalstep(function(player, dtime)
player:hud_change(huds[player_name], 'text', desc)
end
end
end)
end,
})

View File

@ -340,17 +340,15 @@ controls.register_on_hold(function(player, key, time)
end
end)
minetest.register_globalstep(function(dtime)
for _, player in pairs(minetest.get_connected_players()) do
mcl_util.register_player_globalstep({
execute = function(player, dtime)
local name = player:get_player_name()
local wielditem = player:get_wielded_item()
local wieldindex = player:get_wield_index()
local controls = player:get_player_control()
if type(bow_load[name]) == "number" and ((wielditem:get_name()~="mcl_bows:bow_0" and wielditem:get_name()~="mcl_bows:bow_1" and wielditem:get_name()~="mcl_bows:bow_2" and wielditem:get_name()~="mcl_bows:bow_0_enchanted" and wielditem:get_name()~="mcl_bows:bow_1_enchanted" and wielditem:get_name()~="mcl_bows:bow_2_enchanted") or wieldindex ~= bow_index[name]) then
if type(bow_load[name]) == "number" and ((wielditem:get_name()~="mcl_bows:bow_0" and wielditem:get_name()~="mcl_bows:bow_1" and wielditem:get_name()~="mcl_bows:bow_2" and wielditem:get_name()~="mcl_bows:bow_0_enchanted" and wielditem:get_name()~="mcl_bows:bow_1_enchanted" and wielditem:get_name()~="mcl_bows:bow_2_enchanted") or player:get_wield_index() ~= bow_index[name]) then
reset_bow_state(player, true)
end
end
end)
end,
})
minetest.register_on_joinplayer(function(player)
reset_bows(player)

View File

@ -1,6 +1,6 @@
name = mcl_bows
author = Arcelmi
description = This mod adds bows and arrows for MineClone 2.
depends = controls, mcl_particles, mcl_enchanting, mcl_init
depends = controls, mcl_particles, mcl_enchanting, mcl_init, mcl_util
optional_depends = awards, mcl_achievements, mcl_core, mcl_mobitems, playerphysics, doc, doc_identifier, mesecons_button

View File

@ -76,9 +76,11 @@ end
-- This timer makes sure the clocks get updated from time to time regardless of time_speed,
-- just in case some clocks in the world go wrong
local force_clock_update_timer = 0
local now
minetest.register_globalstep(function(dtime)
local now = watch.get_clock_frame()
mcl_util.register_player_globalstep({
can_execute = function(dtime)
now = watch.get_clock_frame()
force_clock_update_timer = force_clock_update_timer + dtime
random_timer = random_timer + dtime
-- This causes the random spinning of the clock
@ -86,15 +88,14 @@ minetest.register_globalstep(function(dtime)
random_frame = (random_frame + math.random(-4, 4)) % clock_frames
random_timer = 0
end
if watch.old_time == now and force_clock_update_timer < 60 then
return
end
force_clock_update_timer = 0
watch.old_time = now
for p, player in pairs(minetest.get_connected_players()) do
return true
end,
execute = function(player, dtime)
for s, stack in pairs(player:get_inventory():get_list("main")) do
local dim = mcl_worlds.pos_to_dimension(player:get_pos())
local frame
@ -111,8 +112,8 @@ minetest.register_globalstep(function(dtime)
player:get_inventory():set_stack("main", s, "mcl_clock:clock_"..frame.." "..count)
end
end
end
end)
end,
})
-- Immediately set correct clock time after crafting
minetest.register_on_craft(function(itemstack)

View File

@ -1,4 +1,4 @@
name = mcl_clock
description = A fantasy clock item roughly shows the time of day.
depends = mcl_init, mcl_worlds, mesecons
depends = mcl_init, mcl_worlds, mesecons, mcl_util
optional_depends = doc

View File

@ -12,22 +12,27 @@ local random_timer_trigger = 0.5 -- random compass spinning tick in seconds. Inc
local random_frame = math.random(0, compass_frames-1)
minetest.register_globalstep(function(dtime)
random_timer = random_timer + dtime
local math = math
if random_timer >= random_timer_trigger then
random_frame = (random_frame + math.random(-1, 1)) % compass_frames
random_timer = 0
end
for i,player in pairs(minetest.get_connected_players()) do
local function has_compass(player)
local function has_compass(player)
for _,stack in pairs(player:get_inventory():get_list("main")) do
if minetest.get_item_group(stack:get_name(), "compass") ~= 0 then
return true
end
end
return false
end
mcl_util.register_player_globalstep({
can_execute = function(dtime)
random_timer = random_timer + dtime
if random_timer >= random_timer_trigger then
random_frame = (random_frame + math.random(-1, 1)) % compass_frames
random_timer = 0
end
return true
end,
execute = function(player, dtime)
if has_compass(player) then
local pos = player:get_pos()
local dim = mcl_worlds.pos_to_dimension(pos)
@ -61,8 +66,8 @@ minetest.register_globalstep(function(dtime)
end
end
end
end
end)
end,
})
local images = {}
for frame = 0, compass_frames-1 do

View File

@ -1,4 +1,4 @@
name = mcl_compass
description = A compass item which points towards the world origin.
depends = mcl_core, mcl_worlds, mesecons
depends = mcl_core, mcl_worlds, mesecons, mcl_util
optional_depends = doc

View File

@ -19,7 +19,7 @@ local find_nodes_in_area = minetest.find_nodes_in_area
local find_node_near = minetest.find_node_near
local get_item_group = minetest.get_item_group
local get_connected_players = minetest.get_connected_players
--local get_connected_players = minetest.get_connected_players
local vector = vector
local math = math
@ -463,7 +463,7 @@ if flame_sound then
-- Cycle for updating players sounds
minetest.register_globalstep(function(dtime)
--[[minetest.register_globalstep(function(dtime)
timer = timer + dtime
if timer < cycle then
return
@ -474,7 +474,21 @@ if flame_sound then
for n = 1, #players do
mcl_fire.update_player_sound(players[n])
end
end)
end)]]
mcl_util.register_player_globalstep({
can_execute = function(dtime)
timer = timer + dtime
if timer < cycle then
return false
end
timer = 0
return true
end,
execute = function(player, dtime)
mcl_fire.update_player_sound(player)
end,
})
-- Stop sound and clear handle on player leave

View File

@ -1,3 +1,3 @@
name = mcl_fire
depends = mcl_core, mcl_worlds, mcl_sounds, mcl_particles
depends = mcl_core, mcl_worlds, mcl_sounds, mcl_particles, mcl_util
optional_depends = mcl_portals

View File

@ -97,14 +97,16 @@ end)
local updatetimer = 0
if not minetest.is_creative_enabled("") then
minetest.register_globalstep(function(dtime)
mcl_util.register_player_globalstep({
can_execute = function(dtime)
updatetimer = updatetimer + dtime
if updatetimer > 0.1 then
local players = minetest.get_connected_players()
for i=1, #players do
update_minimap(players[i])
end
if updatetimer > 0.1 then return true else return false end
end,
execute = function(player, dtime)
update_minimap(player)
end,
after_execute = function(dtime)
updatetimer = updatetimer - dtime
end
end)
end,
})
end

View File

@ -1,2 +1,2 @@
name = mcl_maps
depends = mcl_wip
depends = mcl_wip, mcl_util

View File

@ -181,6 +181,54 @@ minetest.register_globalstep(function(dtime)
end
end)
--[[mcl_util.register_player_globalstep({
can_execute = function(dtime)
main_timer = main_timer + dtime
timer = timer + dtime
if main_timer > mcl_hunger.HUD_TICK or timer > 0.5 then
if main_timer > mcl_hunger.HUD_TICK then main_timer = 0 end
return true
end
end,
execute = function(player, dtime)
local name = player:get_player_name()
local h = tonumber(mcl_hunger.get_hunger(player))
local hp = player:get_hp()
if timer > 0.5 then
-- Slow health regeneration, and hunger damage (every 4s).
-- Regeneration rate based on tutorial video <https://www.youtube.com/watch?v=zs2t-xCVHBo>.
-- Minecraft Wiki seems to be wrong in claiming that full hunger gives 0.5s regen rate.
if timerMult == 0 then
if h >= 18 and hp > 0 and hp < 20 then
-- +1 HP, +exhaustion
player:set_hp(hp+1)
mcl_hunger.exhaust(name, mcl_hunger.EXHAUST_REGEN)
mcl_hunger.update_exhaustion_hud(player, mcl_hunger.get_exhaustion(player))
elseif h == 0 then
-- Damage hungry player down to 1 HP
-- TODO: Allow starvation at higher difficulty levels
if hp-1 > 0 then
if mod_death_messages then
mcl_death_messages.player_damage(player, S("@1 starved to death.", name))
end
player:set_hp(hp-1)
end
end
end
end
end,
after_execute = function(dtime)
if timer > 0.5 then
timer = 0
timerMult = timerMult + 1
if timerMult > 7 then
timerMult = 0
end
end
end,
})]]
--[[ IF HUNGER IS NOT ENABLED ]]
else

View File

@ -1,5 +1,5 @@
name = mcl_hunger
author = BlockMen
description = Adds a simple hunger meachanic with satiation, food poisoning and different healing.
depends = hudbars
depends = hudbars, mcl_util
optional_depends = mcl_death_messages

View File

@ -147,8 +147,8 @@ local player_set_animation = mcl_player.player_set_animation
local player_attached = mcl_player.player_attached
-- Check each player and apply animations
minetest.register_globalstep(function(dtime)
for _, player in pairs(minetest.get_connected_players()) do
mcl_util.register_player_globalstep({
execute = function(player, dtime)
local name = player:get_player_name()
local model_name = player_model[name]
local model = model_name and models[model_name]
@ -167,8 +167,6 @@ minetest.register_globalstep(function(dtime)
animation_speed_mod = animation_speed_mod / 2
end
-- ask if player is swiming
local head_in_water = minetest.get_item_group(mcl_playerinfo[name].node_head, "water") ~= 0
-- ask if player is sprinting
@ -217,8 +215,8 @@ minetest.register_globalstep(function(dtime)
player_set_animation(player, "sneak_stand", animation_speed_mod)
end
end
end
end)
end,
})
-- Don't change HP if the player falls in the water or through End Portal:
minetest.register_on_player_hpchange(function(player, hp_change, reason)

View File

@ -1,3 +1,4 @@
name = mcl_player
author = celeron55
depends = mcl_util
description = Adds the 3D player model, taken from Minetest Game 0.4.16.

View File

@ -39,37 +39,51 @@ local get_player_nodes = function(player_pos)
end
minetest.register_globalstep(function(dtime)
time = time + dtime
-- Run the rest of the code every 0.5 seconds
if time < 0.5 then
return
end
-- reset time for next check
-- FIXME: Make sure a regular check interval applies
time = 0
-- check players
for _,player in pairs(minetest.get_connected_players()) do
-- who am I?
local name = player:get_player_name()
-- where am I?
local pos = player:get_pos()
-- what is around me?
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_below = node_stand_below
mcl_playerinfo[name].node_head = node_head
mcl_playerinfo[name].node_feet = node_feet
end
end)
--[[mcl_util.register_player_globalstep({
can_execute = function(dtime)
time = time + dtime
if time < 0.5 then
return false
end
-- reset time for next check
-- FIXME: Make sure a regular check interval applies
time = 0
return true
end,
execute = function(player, dtime)
local name = player:get_player_name()
local pos = player:get_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_below = node_stand_below
mcl_playerinfo[name].node_head = node_head
mcl_playerinfo[name].node_feet = node_feet
end,
})]]
-- set to blank on join (for 3rd party mods)
minetest.register_on_joinplayer(function(player)
local name = player:get_player_name()

View File

@ -1,4 +1,4 @@
name = mcl_playerinfo
author = TenPlus1
description = This is a helper mod for other mod to query the nodes around the player.
depends = mcl_init, mcl_core, mcl_particles, mcl_death_messages
depends = mcl_init, mcl_core, mcl_particles, mcl_death_messages, mcl_util

View File

@ -81,11 +81,18 @@ minetest.register_on_joinplayer(function(player)
end, player)
end)
minetest.register_globalstep(function()
--[[minetest.register_globalstep(function()
for _,player in pairs(minetest.get_connected_players()) do
wieldview:update_wielded_item(player)
end
end)
end)]]
mcl_util.register_player_globalstep({
can_execute = function(dtime) return true end,
execute = function(player, dtime)
wieldview:update_wielded_item(player)
end,
})
minetest.register_entity("wieldview:wieldnode", {
initial_properties = {

View File

@ -1,5 +1,5 @@
name = wieldview
author = stujones11
description = Makes hand wielded items visible to other players.
depends = mcl_armor
depends = mcl_armor, mcl_util