tidy code

This commit is contained in:
tenplus1 2024-08-14 11:43:07 +01:00
parent 1c220e4d4b
commit 0de1209966
2 changed files with 14 additions and 17 deletions

View File

@ -1,15 +1,19 @@
-- global -- global
pova = {debug = false} pova = {debug = false}
-- local -- local
local pova_list = {} local pova_list = {}
local min, max = math.min, math.max local min, max = math.min, math.max
-- time each override loop runs, 0 to disable -- time each override loop runs, 0 to disable
local pova_loop = minetest.settings:get_bool("pova_loop") or 1.0 local pova_loop = minetest.settings:get_bool("pova_loop") or 1.0
-- if enabled activate main loop that totals override list on timer -- if enabled activate main loop that totals override list on timer
if pova_loop > 0 then if pova_loop > 0 then
local timer = 0 local timer = 0
@ -18,9 +22,7 @@ if pova_loop > 0 then
timer = timer + dtime timer = timer + dtime
if timer < pova_loop then if timer < pova_loop then return end
return
end
timer = 0 timer = 0
@ -31,19 +33,17 @@ if pova_loop > 0 then
end) end)
end end
-- global functions -- global functions
pova.add_override = function(name, item, def) pova.add_override = function(name, item, def)
-- nil check
if not name or not item then return end if not name or not item then return end
-- priority defaults to 50 when not included -- priority defaults to 50 when not included
def.priority = tonumber(def.priority) or 50 def.priority = tonumber(def.priority) or 50
-- if same item is assigned with lower priority then change ignored -- if same item is assigned with lower priority then change ignored
if pova_list[name][item] if pova_list[name][item] and pova_list[name][item].priority
and pova_list[name][item].priority
and pova_list[name][item].priority > def.priority then and pova_list[name][item].priority > def.priority then
return return
end end
@ -66,9 +66,7 @@ pova.do_override = function(player)
local name = player and player:get_player_name() local name = player and player:get_player_name()
-- somehow player is missing on list -- somehow player is missing on list
if not name or not pova_list[name] then if not name or not pova_list[name] then return end
return
end
-- set base defaults -- set base defaults
local speed, jump, gravity = 1, 1, 1 local speed, jump, gravity = 1, 1, 1
@ -84,8 +82,7 @@ pova.do_override = function(player)
for id, var in pairs(pova_list[name]) do for id, var in pairs(pova_list[name]) do
-- skip any custom names -- skip any custom names
if var if var and id ~= "default" and id ~= "min" and id ~= "max" and id ~= "force" then
and id ~= "default" and id ~= "min" and id ~= "max" and id ~= "force" then
-- add any additional changes -- add any additional changes
speed = speed + (pova_list[name][id].speed or 0) speed = speed + (pova_list[name][id].speed or 0)
@ -124,14 +121,15 @@ pova.do_override = function(player)
player:set_physics_override({speed = speed, jump = jump, gravity = gravity}) player:set_physics_override({speed = speed, jump = jump, gravity = gravity})
end end
-- set player table on join -- set player table on join
minetest.register_on_joinplayer(function(player) minetest.register_on_joinplayer(function(player)
pova_list[ player:get_player_name() ] = {} pova_list[ player:get_player_name() ] = {}
pova.do_override(player) pova.do_override(player)
end) end)
-- reset player table on respawn -- reset player table on respawn
minetest.register_on_respawnplayer(function(player) minetest.register_on_respawnplayer(function(player)
if player then if player then
pova_list[ player:get_player_name() ] = {} pova_list[ player:get_player_name() ] = {}
@ -140,17 +138,19 @@ minetest.register_on_respawnplayer(function(player)
end) end)
-- blank player table on leave -- blank player table on leave
minetest.register_on_leaveplayer(function(player) minetest.register_on_leaveplayer(function(player)
if player then if player then
pova_list[ player:get_player_name() ] = nil pova_list[ player:get_player_name() ] = nil
end end
end) end)
-- axe tool to showcase features -- axe tool to showcase features
minetest.register_craftitem("pova:axe", { minetest.register_craftitem("pova:axe", {
description = "Test Axe (left to apply, right to remove effects)", description = "Test Axe (left to apply, right to remove effects)",
inventory_image = "default_tool_steelaxe.png", inventory_image = "default_tool_steelaxe.png",
groups = {not_in_creative_inventory = 1},
on_use = function(itemstack, user, pointed_thing) on_use = function(itemstack, user, pointed_thing)
@ -198,5 +198,4 @@ minetest.register_craftitem("pova:axe", {
end end
}) })
print("[MOD] Pova loaded") print("[MOD] Pova loaded")

View File

@ -1,5 +1,3 @@
name = pova name = pova
description = Gives mod makers a set of functions to safely apply player physics overrides. description = Gives mod makers a set of functions to safely apply player physics overrides.
depends =
optional_depends =
min_minetest_version = 5.0 min_minetest_version = 5.0