stimulators/init.lua

250 lines
5.9 KiB
Lua
Raw Permalink Normal View History

2024-11-14 11:39:02 +03:00
--[[
Copyright 2024 Andrey Stepanov <standmit@yandex.ru>
Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
"Software"), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject
to the following conditions:
The above copyright notice and this permission notice shall be included
in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY
KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
]]--
local S = minetest.get_translator("stimulators")
local fast_secs = minetest.settings:get("stimulators.fastpotion_duration") or (2 * 60)
local timer_check = 5 -- seconds per check
-- get player timer
local function get_timer(user)
if not user then
return
end
local meta = user:get_meta()
if not meta then
return ""
end
return meta:get_string("stimulators:fast_timer") or ""
end
-- do we have fast privs
local function has_fast(name)
return minetest.get_player_privs(name).fast
end
-- set player timer
local function set_timer(user, timer)
local meta = user:get_meta()
if not meta then
return
end
meta:set_string("stimulators:fast_timer", timer)
end
-- give or revoke fast priv
local function set_fast(user, set)
local name = user and user:get_player_name()
if not name then
return
end
local privs = minetest.get_player_privs(name)
privs.fast = set
minetest.set_player_privs(name, privs)
-- when fast removed set timer to temp position
if set ~= true then
set_timer(user, "-99")
end
end
-- after function
local function stimulators_set_fast(user)
local name = user and user:get_player_name()
if not name then
return
end
local timer = tonumber(get_timer(user)) or 0
-- if timer ran out then remove 'fast' privelage
if timer <= 0 and timer ~= -99 then
set_fast(user, nil)
return
end
local privs = minetest.get_player_privs(name)
-- have we already applied 'fast' privelage?
if not privs.fast then
set_fast(user, true)
end
-- handle timer
timer = timer - timer_check
-- show expiration message and play sound
if timer <= 10 then
minetest.chat_send_player(
name,
minetest.get_color_escape_sequence("#ff5500")
.. S("Fast timer about to expire!")
)
minetest.sound_play(
"default_dig_dig_immediate",
{to_player = name, gain = 1.0},
true
)
end
set_timer(user, timer) -- set update timer
-- restart checks
minetest.after(
timer_check,
function()
stimulators_set_fast(user)
end
)
end
-- on join /leave
minetest.register_on_joinplayer(
function(player)
-- wait 2 seconds before doing fast checks on player
minetest.after(
2.0,
function(player)
-- get player name and timer
local name = player and player:get_player_name()
if not name then
return
end
local timer = get_timer(player)
-- if timer is blank and player can already fast then default and return
if timer == "" and has_fast(name) then
set_timer(player, "-99")
return
end
timer = tonumber(timer) or 0
-- if timer is set to default then return
if timer == -99 then
return
end
-- if we got this far and player is fast then start contdown check
if has_fast(name) then
minetest.after(
timer_check,
function()
stimulators_set_fast(player)
end
)
end
end,
player
)
end
)
-- portion item
minetest.register_node(
"stimulators:fast_potion",
{
description = S("Fast Potion"),
drawtype = "plantlike",
tiles = {"stimulators_fast_potion.png"},
inventory_image = "stimulators_fast_potion.png",
wield_image = "stimulators_fast_potion.png",
paramtype = "light",
sunlight_propagates = true,
walkable = false,
selection_box = {
type = "fixed",
fixed = {-0.2, -0.37, -0.2, 0.2, 0.31, 0.2}
},
2024-11-29 20:50:26 +03:00
groups = {dig_immediate = 3},
2024-11-14 11:39:02 +03:00
sounds = default.node_sound_glass_defaults(),
on_use = function(itemstack, user, pointed_thing)
if user.is_fake_player then
return
end
-- get info
local name = user:get_player_name()
local privs = minetest.get_player_privs(name)
local timer = get_timer(user)
if privs.fast then
local msg = timer
if timer == "" or timer == "-99" then
msg = S("unlimited")
end
minetest.chat_send_player(
name,
minetest.get_color_escape_sequence("#ffff00")
.. S("Fast already granted, @1 seconds left!", msg)
)
return
end
set_timer(user, fast_secs) -- set fast timer
-- show time remaining
minetest.chat_send_player(
name,
minetest.get_color_escape_sequence("#1eff00")
.. S("Fast granted, you gave @1 seconds!", fast_secs)
)
stimulators_set_fast(user) -- start check
itemstack:take_item() -- take item
--return empty bottle
local inv = user:get_inventory()
if inv:room_for_item("main", {name = "vessels:glass_bottle"}) then
user:get_inventory():add_item("main", "vessels:glass_bottle")
else
minetest.add_item(user:get_pos(), {name = "vessels:glass_bottle"})
end
return itemstack
end
}
)
-- recipe
minetest.register_craft(
{
output = "stimulators:fast_potion 3",
recipe = {
{"ethereal:etherium_dust", "farming:chili_powder", "ethereal:etherium_dust"},
{"farming:sugar", "farming:salt_crystal", "farming:sugar" },
{"ethereal:etherium_dust", "wine:bottle_tequila", "ethereal:etherium_dust"}
}
}
)