From c2c9a2f4a40099eca36cf245d3db693accdbe072 Mon Sep 17 00:00:00 2001 From: Wuzzy Date: Tue, 23 May 2017 02:04:27 +0200 Subject: [PATCH] Prevent eating most things at full hunger bar --- GROUPS.md | 1 + mods/ITEMS/mcl_core/craftitems.lua | 2 +- mods/ITEMS/mcl_end/init.lua | 2 +- mods/ITEMS/mcl_mobitems/init.lua | 2 +- mods/ITEMS/mcl_potions/init.lua | 8 ++++---- mods/PLAYER/mcl_hunger/hunger.lua | 18 +++++++++++------- 6 files changed, 19 insertions(+), 14 deletions(-) diff --git a/GROUPS.md b/GROUPS.md index 07995e8a..23f0b9ca 100644 --- a/GROUPS.md +++ b/GROUPS.md @@ -46,6 +46,7 @@ Please read to learn how digging times * `disable_suffocation=1`: Disables suffocation for full solid cubes (1) * `destroys_items=1`: If an item happens to be *inside* this node, the item will be destroyed * `no_eat_delay=1`: Only for foodstuffs. When eating this, all eating delays are ignored. +* `can_eat_when_full=1`: Only for foodstuffs. This item can be eaten when the user has a full hunger bar #### Footnotes diff --git a/mods/ITEMS/mcl_core/craftitems.lua b/mods/ITEMS/mcl_core/craftitems.lua index 8e5c21d9..110e3822 100644 --- a/mods/ITEMS/mcl_core/craftitems.lua +++ b/mods/ITEMS/mcl_core/craftitems.lua @@ -152,6 +152,6 @@ minetest.register_craftitem("mcl_core:apple_gold", { -- TODO: Reduce to 4 when it's ready on_place = minetest.item_eat(8), on_secondary_use = minetest.item_eat(8), - groups = { food = 2, eatable = 8 }, + groups = { food = 2, eatable = 8, can_eat_when_full = 1 }, _mcl_saturation = 9.6, }) diff --git a/mods/ITEMS/mcl_end/init.lua b/mods/ITEMS/mcl_end/init.lua index 1f1b85af..2a4489f9 100644 --- a/mods/ITEMS/mcl_end/init.lua +++ b/mods/ITEMS/mcl_end/init.lua @@ -256,7 +256,7 @@ minetest.register_craftitem("mcl_end:chorus_fruit", { -- TODO: Teleport player on_place = minetest.item_eat(4), on_secondary_use = minetest.item_eat(4), - groups = { food = 2, eatable = 4 }, + groups = { food = 2, eatable = 4, can_eat_when_full = 1 }, _mcl_saturation = 2.4, stack_max = 64, }) diff --git a/mods/ITEMS/mcl_mobitems/init.lua b/mods/ITEMS/mcl_mobitems/init.lua index 63cc6d1a..f6a263a5 100644 --- a/mods/ITEMS/mcl_mobitems/init.lua +++ b/mods/ITEMS/mcl_mobitems/init.lua @@ -149,7 +149,7 @@ minetest.register_craftitem("mcl_mobitems:milk_bucket", { return minetest.do_item_eat(0, "bucket:bucket_empty", itemstack, player, pointed_thing) end, stack_max = 1, - groups = { food = 3 }, + groups = { food = 3, can_eat_when_full = 1 }, }) minetest.register_craftitem("mcl_mobitems:spider_eye", { diff --git a/mods/ITEMS/mcl_potions/init.lua b/mods/ITEMS/mcl_potions/init.lua index a352fd75..5efbe3f5 100644 --- a/mods/ITEMS/mcl_potions/init.lua +++ b/mods/ITEMS/mcl_potions/init.lua @@ -99,7 +99,7 @@ minetest.register_craftitem("mcl_potions:potion_water", { stack_max = 1, inventory_image = potion_image("#0000FF"), wield_image = potion_image("#0000FF"), - groups = {brewitem=1, food=3}, + groups = {brewitem=1, food=3, can_eat_when_full=1}, on_place = function(itemstack, placer, pointed_thing) if pointed_thing.type == "node" then local node = minetest.get_node(pointed_thing.under) @@ -140,7 +140,7 @@ minetest.register_craftitem("mcl_potions:potion_awkward", { stack_max = 1, inventory_image = potion_image("#0000FF"), wield_image = potion_image("#0000FF"), - groups = {brewitem=1, food=3}, + groups = {brewitem=1, food=3, can_eat_when_full=1}, on_place = minetest.item_eat(0, "mcl_potions:glass_bottle"), on_secondary_use = minetest.item_eat(0, "mcl_potions:glass_bottle"), }) @@ -151,7 +151,7 @@ minetest.register_craftitem("mcl_potions:potion_mundane", { stack_max = 1, inventory_image = potion_image("#0000FF"), wield_image = potion_image("#0000FF"), - groups = {brewitem=1, food=3}, + groups = {brewitem=1, food=3, can_eat_when_full=1}, on_place = minetest.item_eat(0, "mcl_potions:glass_bottle"), on_secondary_use = minetest.item_eat(0, "mcl_potions:glass_bottle"), }) @@ -162,7 +162,7 @@ minetest.register_craftitem("mcl_potions:potion_thick", { stack_max = 1, inventory_image = potion_image("#0000FF"), wield_image = potion_image("#0000FF"), - groups = {brewitem=1, food=3}, + groups = {brewitem=1, food=3, can_eat_when_full=1}, on_place = minetest.item_eat(0, "mcl_potions:glass_bottle"), on_secondary_use = minetest.item_eat(0, "mcl_potions:glass_bottle"), }) diff --git a/mods/PLAYER/mcl_hunger/hunger.lua b/mods/PLAYER/mcl_hunger/hunger.lua index 911c9545..126be3d4 100644 --- a/mods/PLAYER/mcl_hunger/hunger.lua +++ b/mods/PLAYER/mcl_hunger/hunger.lua @@ -23,15 +23,19 @@ core.do_item_eat = function(hp_change, replace_with_item, itemstack, user, point -- of the second the player made the first eat. -- 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 no_eat_delay or (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 + if no_eat_delay or (mcl_hunger.last_eat[name] < 0) or (os.difftime(os.time(), mcl_hunger.last_eat[name]) >= 2) then + local can_eat_when_full = 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 + 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 + mcl_hunger.last_eat[name] = os.time() end - mcl_hunger.last_eat[name] = os.time() end return itemstack