rework flight potion checks and timer
This commit is contained in:
parent
f1572e8be0
commit
dfb4b1bcf8
114
flight.lua
114
flight.lua
@ -4,7 +4,41 @@ local S = ethereal.intllib
|
|||||||
local is_50 = minetest.has_feature("object_use_texture_alpha")
|
local is_50 = minetest.has_feature("object_use_texture_alpha")
|
||||||
|
|
||||||
|
|
||||||
-- disable flight
|
local function get_timer(user)
|
||||||
|
|
||||||
|
if not user then return end
|
||||||
|
|
||||||
|
if is_50 then
|
||||||
|
|
||||||
|
local meta = user:get_meta()
|
||||||
|
|
||||||
|
if not meta then return "" end
|
||||||
|
|
||||||
|
return meta:get_string("ethereal:fly_timer") or ""
|
||||||
|
else
|
||||||
|
return user:get_attribute("ethereal:fly_timer") or ""
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
|
||||||
|
local function has_fly(name)
|
||||||
|
return minetest.get_player_privs(name).fly
|
||||||
|
end
|
||||||
|
|
||||||
|
|
||||||
|
local function set_timer(user, timer)
|
||||||
|
|
||||||
|
if is_50 then
|
||||||
|
|
||||||
|
local meta = user:get_meta()
|
||||||
|
|
||||||
|
meta:set_string("ethereal:fly_timer", timer)
|
||||||
|
else
|
||||||
|
user:set_attribute("ethereal:fly_timer", timer)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
|
||||||
local function set_flight(user, set)
|
local function set_flight(user, set)
|
||||||
|
|
||||||
if not user then return end
|
if not user then return end
|
||||||
@ -16,16 +50,9 @@ local function set_flight(user, set)
|
|||||||
|
|
||||||
minetest.set_player_privs(name, privs)
|
minetest.set_player_privs(name, privs)
|
||||||
|
|
||||||
-- when 'fly' removed set timer to temp value for checks
|
-- when flight removed set timer to temp position
|
||||||
if not set then
|
if set ~= true then
|
||||||
|
set_timer(user, "-99")
|
||||||
if is_50 then
|
|
||||||
local meta = user:get_meta() ; if not meta then return end
|
|
||||||
|
|
||||||
meta:set_string("ethereal:fly_timer", "-99")
|
|
||||||
else
|
|
||||||
user:set_attribute("ethereal:fly_timer", "-99")
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
@ -35,18 +62,7 @@ local function ethereal_set_flight(user)
|
|||||||
|
|
||||||
if not user then return end
|
if not user then return end
|
||||||
|
|
||||||
local timer, meta
|
local timer = tonumber(get_timer(user)) or 0
|
||||||
|
|
||||||
if is_50 then
|
|
||||||
|
|
||||||
meta = user:get_meta() ; if not meta then return end
|
|
||||||
|
|
||||||
timer = tonumber(meta:get_string("ethereal:fly_timer") or "") or 0
|
|
||||||
else
|
|
||||||
timer = tonumber(user:get_attribute("ethereal:fly_timer") or "") or 0
|
|
||||||
end
|
|
||||||
|
|
||||||
if not timer then return end -- nil check
|
|
||||||
|
|
||||||
-- if timer ran out then remove 'fly' privelage
|
-- if timer ran out then remove 'fly' privelage
|
||||||
if timer <= 0 and timer ~= -99 then
|
if timer <= 0 and timer ~= -99 then
|
||||||
@ -68,7 +84,7 @@ local function ethereal_set_flight(user)
|
|||||||
timer = timer - timer_check
|
timer = timer - timer_check
|
||||||
|
|
||||||
-- show expiration message and play sound
|
-- show expiration message and play sound
|
||||||
if timer < 10 then
|
if timer <= 10 then
|
||||||
|
|
||||||
minetest.chat_send_player(name,
|
minetest.chat_send_player(name,
|
||||||
minetest.get_color_escape_sequence("#ff5500")
|
minetest.get_color_escape_sequence("#ff5500")
|
||||||
@ -78,13 +94,10 @@ local function ethereal_set_flight(user)
|
|||||||
{to_player = name, gain = 1.0}, true)
|
{to_player = name, gain = 1.0}, true)
|
||||||
end
|
end
|
||||||
|
|
||||||
-- store new timer setting
|
-- set updated timer
|
||||||
if is_50 then
|
set_timer(user, timer)
|
||||||
meta:set_string("ethereal:fly_timer", timer)
|
|
||||||
else
|
|
||||||
user:set_attribute("ethereal:fly_timer", timer)
|
|
||||||
end
|
|
||||||
|
|
||||||
|
-- restart checks
|
||||||
minetest.after(timer_check, function()
|
minetest.after(timer_check, function()
|
||||||
ethereal_set_flight(user)
|
ethereal_set_flight(user)
|
||||||
end)
|
end)
|
||||||
@ -96,31 +109,27 @@ minetest.register_on_joinplayer(function(player)
|
|||||||
|
|
||||||
if not player then return end
|
if not player then return end
|
||||||
|
|
||||||
local timer, meta
|
-- get player name and timer
|
||||||
|
local name = player:get_player_name()
|
||||||
|
local timer = get_timer(player)
|
||||||
|
|
||||||
if is_50 then
|
-- if timer is blank and player can already fly then default and return
|
||||||
|
if timer == "" and has_fly(name) then
|
||||||
|
|
||||||
meta = player:get_meta() ; if not meta then return end
|
set_timer(player, "-99")
|
||||||
timer = meta:get_string("ethereal:fly_timer") or ""
|
|
||||||
else
|
|
||||||
timer = player:get_attribute("ethereal:fly_timer") or ""
|
|
||||||
end
|
|
||||||
|
|
||||||
-- first run to set fly timer if no number found
|
|
||||||
if timer == "" then
|
|
||||||
ethereal_set_flight(player)
|
|
||||||
return
|
return
|
||||||
end
|
end
|
||||||
|
|
||||||
timer = tonumber(timer) or 0
|
timer = tonumber(timer) or 0
|
||||||
|
|
||||||
|
-- if timer is set to default then return
|
||||||
if timer == -99 then
|
if timer == -99 then
|
||||||
return
|
return
|
||||||
end
|
end
|
||||||
|
|
||||||
local privs = minetest.get_player_privs(player:get_player_name())
|
-- if we got this far and player is flying then start countdown check
|
||||||
|
if has_fly(name) then
|
||||||
if privs.fly and timer then
|
|
||||||
|
|
||||||
minetest.after(timer_check, function()
|
minetest.after(timer_check, function()
|
||||||
ethereal_set_flight(player)
|
ethereal_set_flight(player)
|
||||||
@ -151,15 +160,7 @@ minetest.register_node("ethereal:flight_potion", {
|
|||||||
-- get privs
|
-- get privs
|
||||||
local name = user:get_player_name()
|
local name = user:get_player_name()
|
||||||
local privs = minetest.get_player_privs(name)
|
local privs = minetest.get_player_privs(name)
|
||||||
local timer, meta
|
local timer = get_timer(user)
|
||||||
|
|
||||||
if is_50 then
|
|
||||||
|
|
||||||
meta = user:get_meta() ; if not meta then return end
|
|
||||||
timer = meta:get_string("ethereal:fly_timer") or ""
|
|
||||||
else
|
|
||||||
timer = user:get_attribute("ethereal:fly_timer") or ""
|
|
||||||
end
|
|
||||||
|
|
||||||
if privs.fly then
|
if privs.fly then
|
||||||
|
|
||||||
@ -176,16 +177,15 @@ minetest.register_node("ethereal:flight_potion", {
|
|||||||
return
|
return
|
||||||
end
|
end
|
||||||
|
|
||||||
if is_50 then
|
-- set flight timer
|
||||||
meta:set_string("ethereal:fly_timer", flight_secs)
|
set_timer(user, flight_secs)
|
||||||
else
|
|
||||||
user:set_attribute("ethereal:fly_timer", flight_secs)
|
|
||||||
end
|
|
||||||
|
|
||||||
|
-- show time remaining
|
||||||
minetest.chat_send_player(name,
|
minetest.chat_send_player(name,
|
||||||
minetest.get_color_escape_sequence("#1eff00")
|
minetest.get_color_escape_sequence("#1eff00")
|
||||||
.. S("Flight granted, you have @1 seconds!", flight_secs))
|
.. S("Flight granted, you have @1 seconds!", flight_secs))
|
||||||
|
|
||||||
|
-- start check
|
||||||
ethereal_set_flight(user)
|
ethereal_set_flight(user)
|
||||||
|
|
||||||
-- take item
|
-- take item
|
||||||
|
Loading…
x
Reference in New Issue
Block a user