1
0
Fork 0

Force 2 second delay between eating

This commit is contained in:
Wuzzy 2017-05-23 01:39:19 +02:00
parent b8cc752e79
commit 3ff572d2bc
2 changed files with 26 additions and 8 deletions

View File

@ -12,18 +12,30 @@ core.do_item_eat = function(hp_change, replace_with_item, itemstack, user, point
end end
local old_itemstack = itemstack local old_itemstack = itemstack
itemstack = mcl_hunger.eat(hp_change, replace_with_item, itemstack, user, pointed_thing)
for _, callback in pairs(core.registered_on_item_eats) do local name = user:get_player_name()
local result = callback(hp_change, replace_with_item, itemstack, user, pointed_thing, old_itemstack)
if result then -- Allow eating only after a delay of 2 seconds.
return result -- This prevents eating as an excessive speed.
-- Yes, os.time() is not a precise timer but it is good enough for our purposes.
-- FIXME: In singleplayer, there's a cheat to circumvent this, simply by pausing the game between eats.
-- This is because os.time() obviously does not care about the pause. A fix needs a different timer mechanism.
if (mcl_hunger.last_eat[name] < 0) or (os.difftime(os.time(), mcl_hunger.last_eat[name]) >= 2) then
itemstack = mcl_hunger.eat(hp_change, replace_with_item, itemstack, user, pointed_thing)
for _, callback in pairs(core.registered_on_item_eats) do
local result = callback(hp_change, replace_with_item, itemstack, user, pointed_thing, old_itemstack)
if result then
return result
end
end end
mcl_hunger.last_eat[name] = os.time()
end end
return itemstack return itemstack
end end
-- food functions -- food functions
local food = mcl_hunger.food local food = {}
function mcl_hunger.register_food(name, hunger_change, replace_with_item, poisontime, poison, exhaust, poisonchance, sound) function mcl_hunger.register_food(name, hunger_change, replace_with_item, poisontime, poison, exhaust, poisonchance, sound)
food[name] = {} food[name] = {}
@ -101,6 +113,7 @@ local poisonrandomizer = PseudoRandom(os.time())
function mcl_hunger.item_eat(hunger_change, replace_with_item, poisontime, poison, exhaust, poisonchance, sound) function mcl_hunger.item_eat(hunger_change, replace_with_item, poisontime, poison, exhaust, poisonchance, sound)
return function(itemstack, user, pointed_thing) return function(itemstack, user, pointed_thing)
local itemname = itemstack:get_name() local itemname = itemstack:get_name()
if itemstack:take_item() ~= nil and user ~= nil then if itemstack:take_item() ~= nil and user ~= nil then
local name = user:get_player_name() local name = user:get_player_name()
local hp = user:get_hp() local hp = user:get_hp()

View File

@ -28,7 +28,6 @@ mcl_hunger.active = false
if minetest.setting_getbool("enable_damage") then if minetest.setting_getbool("enable_damage") then
mcl_hunger.active = true mcl_hunger.active = true
mcl_hunger.food = {}
-- Debug Mode. If enabled, saturation and exhaustion are shown as well. -- Debug Mode. If enabled, saturation and exhaustion are shown as well.
-- NOTE: Read-only. The setting should only be read at the beginning, this mod is not -- NOTE: Read-only. The setting should only be read at the beginning, this mod is not
@ -51,6 +50,9 @@ end
-- Count number of poisonings a player has at once -- Count number of poisonings a player has at once
mcl_hunger.poisonings = {} mcl_hunger.poisonings = {}
-- Cooldown timers for each player, to force a short delay between consuming 2 food items
mcl_hunger.last_eat = {}
-- HUD item ids -- HUD item ids
local hunger_hud = {} local hunger_hud = {}
@ -175,12 +177,15 @@ minetest.register_on_joinplayer(function(player)
local inv = player:get_inventory() local inv = player:get_inventory()
inv:set_size("hunger", 3) inv:set_size("hunger", 3)
mcl_hunger.poisonings[name] = 0 mcl_hunger.poisonings[name] = 0
mcl_hunger.last_eat[name] = -1
init_hud(player) init_hud(player)
end) end)
minetest.register_on_respawnplayer(function(player) minetest.register_on_respawnplayer(function(player)
-- reset hunger (and save) -- reset hunger (and save)
local name = player:get_player_name() local name = player:get_player_name()
mcl_hunger.last_eat[name] = -1
local h, s, e = 20, mcl_hunger.SATURATION_INIT, 0 local h, s, e = 20, mcl_hunger.SATURATION_INIT, 0
mcl_hunger.set_hunger(player, h, false) mcl_hunger.set_hunger(player, h, false)
mcl_hunger.set_saturation(player, s, false) mcl_hunger.set_saturation(player, s, false)