0
0
Fork 0

Support delayed drinking with new potions API

This commit is contained in:
the-real-herowl 2024-04-27 00:59:22 +02:00
parent 53d640028e
commit a039e056a5
1 changed files with 43 additions and 68 deletions

View File

@ -46,51 +46,6 @@ end
-- ╚═╝░░░░░░╚════╝░░░░╚═╝░░░╚═╝░╚════╝░╚═╝░░╚══╝╚═════╝░
function return_on_use(def, effect, dur)
return function (itemstack, user, pointed_thing)
if pointed_thing.type == "node" then
if user and not user:get_player_control().sneak then
-- Use pointed node's on_rightclick function first, if present
local node = minetest.get_node(pointed_thing.under)
if user and not user:get_player_control().sneak then
if minetest.registered_nodes[node.name] and minetest.registered_nodes[node.name].on_rightclick then
return minetest.registered_nodes[node.name].on_rightclick(pointed_thing.under, node, user, itemstack) or itemstack
end
end
end
elseif pointed_thing.type == "object" then
return itemstack
end
--def.on_use(user, effect, dur) -- Will do effect immediately but not reduce item count until eating delay ends which makes it exploitable by deliberately not finishing delay
-- Wrapper for handling mcl_hunger delayed eating TODO migrate to the new function
local name = user:get_player_name()
mcl_hunger.eat_internal[name]._custom_itemstack = itemstack -- Used as comparison to make sure the custom wrapper executes only when the same item is eaten
mcl_hunger.eat_internal[name]._custom_var = {
user = user,
effect = effect,
dur = dur,
}
mcl_hunger.eat_internal[name]._custom_func = def.on_use
mcl_hunger.eat_internal[name]._custom_wrapper = function(name)
mcl_hunger.eat_internal[name]._custom_func(
mcl_hunger.eat_internal[name]._custom_var.user,
mcl_hunger.eat_internal[name]._custom_var.effect,
mcl_hunger.eat_internal[name]._custom_var.dur
)
end
local old_name, old_count = itemstack:get_name(), itemstack:get_count()
itemstack = minetest.do_item_eat(0, "mcl_potions:glass_bottle", itemstack, user, pointed_thing)
if old_name ~= itemstack:get_name() or old_count ~= itemstack:get_count() then
mcl_potions._use_potion(itemstack, user, def.color)
end
return itemstack
end
end
local function generate_on_use(effects, color, on_use, custom_effect)
return function(itemstack, user, pointed_thing)
if pointed_thing.type == "node" then
@ -104,6 +59,16 @@ local function generate_on_use(effects, color, on_use, custom_effect)
return itemstack
end
-- Wrapper for handling mcl_hunger delayed eating
local player_name = user:get_player_name()
mcl_hunger.eat_internal[player_name]._custom_itemstack = itemstack -- Used as comparison to make sure the custom wrapper executes only when the same item is eaten
mcl_hunger.eat_internal[player_name]._custom_var = {
user = user,
effects = effects,
on_use = on_use,
custom_effect = custom_effect,
}
mcl_hunger.eat_internal[player_name]._custom_func = function(itemstack, user, effects, on_use, custom_effect)
local potency = itemstack:get_meta():get_int("mcl_potions:potion_potent")
local plus = itemstack:get_meta():get_int("mcl_potions:potion_plus")
local ef_level
@ -130,6 +95,16 @@ local function generate_on_use(effects, color, on_use, custom_effect)
if on_use then on_use(user, potency+1) end
if custom_effect then custom_effect(user, potency+1, plus) end
end
mcl_hunger.eat_internal[player_name]._custom_wrapper = function(player_name)
mcl_hunger.eat_internal[player_name]._custom_func(
mcl_hunger.eat_internal[player_name]._custom_itemstack,
mcl_hunger.eat_internal[player_name]._custom_var.user,
mcl_hunger.eat_internal[player_name]._custom_var.effects,
mcl_hunger.eat_internal[player_name]._custom_var.on_use,
mcl_hunger.eat_internal[player_name]._custom_var.custom_effect
)
end
itemstack = minetest.do_item_eat(0, "mcl_potions:glass_bottle", itemstack, user, pointed_thing)
if itemstack then mcl_potions._use_potion(user, color) end