Improve minetest.item_eat compability

This commit is contained in:
Wuzzy 2023-01-24 18:06:41 +01:00
parent ab383b2d5e
commit 6e494c4a99
9 changed files with 38 additions and 37 deletions

View File

@ -26,7 +26,7 @@ minetest.register_craftitem(
_rp_hunger_sat = 30,
inventory_image = "mobs_meat_raw.png",
groups = { food = 2 },
on_use = minetest.item_eat("auto"),
on_use = minetest.item_eat(0),
})
-- Cooked meat
@ -39,7 +39,7 @@ minetest.register_craftitem(
_rp_hunger_sat = 70,
inventory_image = "mobs_meat_cooked.png",
groups = { food = 2 },
on_use = minetest.item_eat("auto"),
on_use = minetest.item_eat(0),
})
minetest.register_craft(

View File

@ -83,7 +83,7 @@ minetest.register_craftitem(
_rp_hunger_sat = 30,
inventory_image = "mobs_pork_raw.png",
groups = { food = 2 },
on_use = minetest.item_eat("auto"),
on_use = minetest.item_eat(0),
})
-- Cooked porkchop
@ -96,7 +96,7 @@ minetest.register_craftitem(
_rp_hunger_sat = 50,
inventory_image = "mobs_pork_cooked.png",
groups = { food = 2 },
on_use = minetest.item_eat("auto"),
on_use = minetest.item_eat(0),
})
minetest.register_craft(

View File

@ -32,7 +32,7 @@ minetest.register_node(
after_dig_node = function(pos, node, metadata, digger)
util.dig_up(pos, node, digger)
end,
on_use = minetest.item_eat("auto"),
on_use = minetest.item_eat(0),
})
-- Papyrus

View File

@ -318,7 +318,7 @@ minetest.register_node(
walkable = false,
floodable = true,
groups = {snappy = 3, handy = 2, leafdecay = 3, leafdecay_drop = 1, food = 2},
on_use = minetest.item_eat("auto"),
on_use = minetest.item_eat(0),
on_place = create_on_place_fruit_function("rp_default:apple"),
sounds = rp_sounds.node_sound_defaults(),
})
@ -372,7 +372,7 @@ minetest.register_node(
walkable = false,
floodable = true,
groups = {snappy = 3, handy = 3, leafdecay = 3, leafdecay_drop = 1, food = 2},
on_use = minetest.item_eat("auto"),
on_use = minetest.item_eat(0),
on_place = create_on_place_fruit_function("rp_default:acorn"),
sounds = rp_sounds.node_sound_defaults(),
})

View File

@ -391,7 +391,7 @@ minetest.register_node(
}
},
groups = {clam = 1, fleshy = 3, oddly_breakable_by_hand = 2, choppy = 3, attached_node = 1, food = 2},
on_use = minetest.item_eat("auto"),
on_use = minetest.item_eat(0),
sounds = rp_sounds.node_sound_defaults(),
-- Place node as the 'nopearl' clam to make sure the player can't

View File

@ -34,7 +34,7 @@ minetest.register_craftitem(
_rp_hunger_sat = 50,
inventory_image = "farming_bread.png",
groups = { food = 2 },
on_use = minetest.item_eat("auto"),
on_use = minetest.item_eat(0),
})
minetest.register_craftitem(
@ -45,7 +45,7 @@ minetest.register_craftitem(
_rp_hunger_sat = 15,
inventory_image = "farming_asparagus.png",
groups = { food = 2 },
on_use = minetest.item_eat("auto"),
on_use = minetest.item_eat(0),
})
minetest.register_craftitem(
@ -56,7 +56,7 @@ minetest.register_craftitem(
_rp_hunger_sat = 40,
inventory_image = "farming_asparagus_cooked.png",
groups = { food = 2 },
on_use = minetest.item_eat("auto"),
on_use = minetest.item_eat(0),
})
minetest.register_craftitem(
@ -67,7 +67,7 @@ minetest.register_craftitem(
_rp_hunger_sat = 35,
inventory_image = "farming_potato_baked.png",
groups = { food = 2 },
on_use = minetest.item_eat("auto"),
on_use = minetest.item_eat(0),
})
-- Craft recipes

View File

@ -76,7 +76,7 @@ farming.register_plant_nodes("rp_farming:potato", {
[1] = {
_rp_hunger_food = 3,
_rp_hunger_sat = 30,
on_use = minetest.item_eat("auto"),
on_use = minetest.item_eat(0),
},
},
stage_extra_groups = {
@ -126,7 +126,7 @@ farming.register_plant_nodes("rp_farming:carrot", {
[1] = {
_rp_hunger_food = 2,
_rp_hunger_sat = 20,
on_use = minetest.item_eat("auto"),
on_use = minetest.item_eat(0),
},
},
stage_extra_groups = {

View File

@ -27,14 +27,8 @@ to do the following:
* `_rp_hunger_food`: How many food points this item has
* `_rp_hunger_sat`: How many saturation points this item restores
* When calling `minetest.item_eat` or `minetest.do_item_eat`,
set `hp_change` to `"auto"`. This signals to `rp_hunger` that
the item definition fields should be used.
Legacy support: For compability reasons, calling `minetest.item_eat`
or `minetest.do_item_eat` with a numeric value for `hp_change` still
works, but this is not recommended.
If `hp_change` is a number, it will be interpreted as the food points
and the saturation is always 2.
always set `hp_change` to `0`. This is required for
compability reasons.
## Functions

View File

@ -292,39 +292,46 @@ local function on_respawnplayer_nohunger(player)
end
end
local function on_item_eat(hpdata, replace_with_item, itemstack,
local function on_item_eat(hp_change, replace_with_item, itemstack,
player, pointed_thing)
if not player then
return
end
if not hp_change then
minetest.log("error", "[rp_hunger] minetest.item_eat called with nil hp_change (item="..itemstack:get_name()..")!")
return
end
local food = 0
local saturation = 0
if type(hpdata) == "number" then
-- Legacy method for compability with Minetest API: number changes food points
food = hpdata
saturation = 2 -- add a small default saturation
elseif type(hpdata) == "table" then
if type(hp_change) == "table" then
-- Legacy support for old Repixture versions: table form:
-- { hp = <food points>, sat = <saturation }
food = hpdata.hp
saturation = hpdata.sat
elseif hpdata == "auto" then
-- Recommended method: automatic; take food data from item definition
food = hp_change.hp
saturation = hp_change.sat
elseif type(hp_change) == "number" then
-- Recommended method: Try to take food data from item definition
local def = itemstack:get_definition()
if def then
food = def._rp_hunger_food
saturation = def._rp_hunger_sat
if not food or not saturation then
if not food or not saturation then
minetest.log("error", "[rp_hunger] Missing _rp_hunger_food and/or _rp_hunger_sat field in item definition (item="..itemstack:get_name()..")!")
-- Fallback
food = 0
saturation = 0
return
end
else
-- Fallback
if not food then
food = 0
end
if not saturation then
saturation = 0
end
end
else
minetest.log("error", "[rp_hunger] minetest.item_eat called with invalid hp_change (item="..itemstack:get_name()..")!")
return
end
local name = player:get_player_name()
@ -505,7 +512,7 @@ end
-- Eating food when hunger is disabled.
-- This just removes the food.
local function fake_on_item_eat(hpdata, replace_with_item, itemstack,
local function fake_on_item_eat(hp_change, replace_with_item, itemstack,
player, pointed_thing)
local headpos = player:get_pos()
headpos.y = headpos.y + 1