Integrate delayed eating to item eat code

This commit is contained in:
Eliy21 2024-01-15 05:56:24 +00:00
parent 4c8efca4e6
commit 996a5a6d47
1 changed files with 76 additions and 44 deletions

View File

@ -38,7 +38,23 @@ function minetest.do_item_eat(hp_change, replace_with_item, itemstack, user, poi
local can_eat_when_full = creative or (mcl_hunger.active == false)
or minetest.get_item_group(itemstack:get_name(), "can_eat_when_full") == 1
-- Don't allow eating when player has full hunger bar (some exceptional items apply)
if can_eat_when_full or (mcl_hunger.get_hunger(user) < 20) then
if not no_eat_delay and not mcl_hunger.eat_internal[name].is_eating and not mcl_hunger.eat_internal[name].do_item_eat and (can_eat_when_full or (mcl_hunger.get_hunger(user) < 20)) then
local itemname = itemstack:get_name()
mcl_hunger.eat_internal[name].is_eating = true
mcl_hunger.eat_internal[name].itemname = itemname
mcl_hunger.eat_internal[name].item_definition = minetest.registered_items[itemname]
mcl_hunger.eat_internal[name].hp_change = hp_change
mcl_hunger.eat_internal[name].replace_with_item = replace_with_item
mcl_hunger.eat_internal[name].itemstack = itemstack
mcl_hunger.eat_internal[name].user = user
mcl_hunger.eat_internal[name].pointed_thing = pointed_thing
elseif (mcl_hunger.eat_internal[name].do_item_eat or no_eat_delay) and (can_eat_when_full or (mcl_hunger.get_hunger(user) < 20)) then
if mcl_hunger.eat_internal[name]._custom_itemstack and
mcl_hunger.eat_internal[name]._custom_wrapper and
mcl_hunger.eat_internal[name]._custom_itemstack == itemstack then
mcl_hunger.eat_internal[name]._custom_wrapper(name)
end
itemstack = mcl_hunger.eat(hp_change, replace_with_item, itemstack, user, pointed_thing)
for _, callback in pairs(minetest.registered_on_item_eats) do
local result = callback(hp_change, replace_with_item, itemstack, user, pointed_thing, old_itemstack)
@ -47,6 +63,7 @@ function minetest.do_item_eat(hp_change, replace_with_item, itemstack, user, poi
end
end
mcl_hunger.last_eat[name] = os.time()
user:get_inventory():set_stack("main", user:get_wield_index(), itemstack)
end
end
@ -129,49 +146,9 @@ function mcl_hunger.item_eat(hunger_change, replace_with_item, poisontime, poiso
--local hp = user:get_hp()
local pos = user:get_pos()
-- player height
pos.y = pos.y + 1.5
local foodtype = minetest.get_item_group(itemname, "food")
if foodtype == 3 then
-- Item is a drink, only play drinking sound (no particle)
minetest.sound_play("survival_thirst_drink", {
max_hear_distance = 12,
gain = 1.0,
pitch = 1 + math.random(-10, 10)*0.005,
object = user,
}, true)
else
-- Assume the item is a food
-- Add eat particle effect and sound
local def = minetest.registered_items[itemname]
local texture = def.inventory_image
if not texture or texture == "" then
texture = def.wield_image
end
-- Special item definition field: _food_particles
-- If false, force item to not spawn any food partiles when eaten
if def._food_particles ~= false and texture and texture ~= "" then
local v = user:get_velocity() or user:get_player_velocity()
for i = 0, math.min(math.max(8, hunger_change*2), 25) do
minetest.add_particle({
pos = { x = pos.x, y = pos.y, z = pos.z },
velocity = vector.add(v, { x = math.random(-1, 1), y = math.random(1, 2), z = math.random(-1, 1) }),
acceleration = { x = 0, y = math.random(-9, -5), z = 0 },
expirationtime = 1,
size = math.random(1, 2),
collisiondetection = true,
vertical = false,
texture = "[combine:3x3:" .. -i .. "," .. -i .. "=" .. texture,
})
end
end
minetest.sound_play("mcl_hunger_bite", {
max_hear_distance = 12,
gain = 1.0,
pitch = 1 + math.random(-10, 10)*0.005,
object = user,
}, true)
end
local def = minetest.registered_items[itemname]
mcl_hunger.eat_effects(user, itemname, pos, hunger_change, def)
if mcl_hunger.active and hunger_change then
-- Add saturation (must be defined in item table)
@ -226,6 +203,61 @@ function mcl_hunger.item_eat(hunger_change, replace_with_item, poisontime, poiso
end
end
function mcl_hunger.eat_effects(user, itemname, pos, hunger_change, item_def, pitch)
if user and itemname and pos and hunger_change and item_def then
local name = user:get_player_name()
if mcl_hunger.eat_internal[name] and mcl_hunger.eat_internal[name].do_item_eat then
pitch = 0.95
end
local def = item_def
-- player height
pos.y = pos.y + 1.5
local foodtype = minetest.get_item_group(itemname, "food")
if foodtype == 3 then
-- Item is a drink, only play drinking sound (no particle)
minetest.sound_play("survival_thirst_drink", {
max_hear_distance = 12,
gain = 1.0,
pitch = pitch or 1 + math.random(-10, 10)*0.005,
object = user,
}, true)
else
-- Assume the item is a food
-- Add eat particle effect and sound
--local def = minetest.registered_items[itemname]
local texture = def.inventory_image
if not texture or texture == "" then
texture = def.wield_image
end
-- Special item definition field: _food_particles
-- If false, force item to not spawn any food partiles when eaten
if def._food_particles ~= false and texture and texture ~= "" then
local v = user:get_velocity() or user:get_player_velocity()
for i = 0, math.min(math.max(8, hunger_change*2), 25) do
minetest.add_particle({
pos = { x = pos.x, y = pos.y, z = pos.z },
velocity = vector.add(v, { x = math.random(-1, 1), y = math.random(1, 2), z = math.random(-1, 1) }),
acceleration = { x = 0, y = math.random(-9, -5), z = 0 },
expirationtime = 1,
size = math.random(1, 2),
collisiondetection = true,
vertical = false,
texture = "[combine:3x3:" .. -i .. "," .. -i .. "=" .. texture,
})
end
end
minetest.sound_play("mcl_hunger_bite", {
max_hear_distance = 12,
gain = 1.0,
pitch = pitch or 1 + math.random(-10, 10)*0.005,
object = user,
}, true)
end
else
return false
end
end
if mcl_hunger.active then
-- player-action based hunger changes
minetest.register_on_dignode(function(pos, oldnode, player)