1
0
Fork 0

Make campfires cook items

This commit is contained in:
PrairieWind 2023-05-30 12:44:15 -06:00
parent 40e072ab89
commit 5e3f820d77
4 changed files with 84 additions and 9 deletions

View File

@ -1,6 +1,72 @@
local S = minetest.get_translator(minetest.get_current_modname()) local S = minetest.get_translator(minetest.get_current_modname())
mcl_campfires = {} mcl_campfires = {}
local drop_items = mcl_util.drop_items_from_meta_container("main")
local function on_blast(pos)
local node = minetest.get_node(pos)
drop_items(pos, node)
minetest.remove_node(pos)
end
-- on_rightclick function to take items that are cookable in a campfire, and put them in the campfire inventory
function mcl_campfires.take_item(pos, node, player, itemstack)
local is_creative = minetest.is_creative_enabled(player:get_player_name())
local inv = player:get_inventory()
local campfire_meta = minetest.get_meta(pos)
local campfire_inv = campfire_meta:get_inventory()
local timer = minetest.get_node_timer(pos)
local stack = itemstack:peek_item(1)
if minetest.get_item_group(itemstack:get_name(), "campfire_cookable") ~= 0 then
local cookable = minetest.get_craft_result({method = "cooking", width = 1, items = {itemstack}})
if cookable then
for space = 1, 4 do -- Cycle through spots
local spot = campfire_inv:get_stack("main", space)
if not spot or spot == (ItemStack("") or ItemStack("nil")) then -- Check if the spot is empty or not
if not is_creative then itemstack:take_item(1) end -- Take the item if in creative
campfire_inv:set_stack("main", space, stack) -- Set the inventory itemstack at the empty spot
campfire_meta:set_int("cooktime_"..tostring(space), 30) -- Set the cook time meta
break
end
end
end
timer:start(1) -- Start cook timer
end
end
-- on_timer function to run the cook timer and cook items.
function mcl_campfires.cook_item(pos, elapsed)
local meta = minetest.get_meta(pos)
local inv = meta:get_inventory()
local continue = 0
-- Cycle through slots to cook them.
for i = 1, 4 do
local time_r = meta:get_int("cooktime_"..tostring(i))
local item = inv:get_stack("main", i)
if item ~= (ItemStack("") or ItemStack("nil")) then
-- Item hasn't been cooked completely, continue cook timer countdown.
if time_r and time_r ~= 0 and time_r > 0 then
meta:set_int("cooktime_"..tostring(i), time_r - 1)
-- Item cook timer is up, finish cooking process and drop cooked item.
elseif time_r <= 0 then
local cooked = minetest.get_craft_result({method = "cooking", width = 1, items = {item}})
if cooked then
minetest.add_item(pos, cooked.item) -- Drop Cooked Item
inv:set_stack("main", i, "") -- Clear Inventory
continue = continue + 1 -- Indicate that the slot is clear.
end
end
end
end
-- Not all slots are empty, continue timer.
if continue ~= 4 then
return true
-- Slots are empty, stop node timer.
else
return false
end
end
function mcl_campfires.register_campfire(name, def) function mcl_campfires.register_campfire(name, def)
-- Define Campfire -- Define Campfire
minetest.register_node(name, { minetest.register_node(name, {
@ -66,13 +132,20 @@ function mcl_campfires.register_campfire(name, def)
groups = { handy=1, axey=1, material_wood=1, campfire=1, lit_campfire=1 }, groups = { handy=1, axey=1, material_wood=1, campfire=1, lit_campfire=1 },
paramtype = "light", paramtype = "light",
paramtype2 = "facedir", paramtype2 = "facedir",
on_construct = function(pos)
local meta = minetest.get_meta(pos)
local inv = meta:get_inventory()
inv:set_size("main", 4)
end,
on_rightclick = function (pos, node, player, itemstack, pointed_thing) on_rightclick = function (pos, node, player, itemstack, pointed_thing)
if player:get_wielded_item():get_name():find("shovel") then if player:get_wielded_item():get_name():find("shovel") then
node.name = name node.name = name
minetest.set_node(pos, node) minetest.set_node(pos, node)
minetest.sound_play("fire_extinguish_flame", {pos = pos, gain = 0.25, max_hear_distance = 16}, true) minetest.sound_play("fire_extinguish_flame", {pos = pos, gain = 0.25, max_hear_distance = 16}, true)
end end
mcl_campfires.take_item(pos, node, player, itemstack)
end, end,
on_timer = mcl_campfires.cook_item,
drop = def.drops, drop = def.drops,
_mcl_silk_touch_drop = {name.."_lit"}, _mcl_silk_touch_drop = {name.."_lit"},
light_source = def.lightlevel, light_source = def.lightlevel,
@ -88,6 +161,8 @@ function mcl_campfires.register_campfire(name, def)
_mcl_blast_resistance = 2, _mcl_blast_resistance = 2,
_mcl_hardness = 2, _mcl_hardness = 2,
damage_per_second = def.damage, damage_per_second = def.damage,
on_blast = on_blast,
after_dig_node = drop_items,
}) })
end end

View File

@ -1,3 +1,3 @@
name = mcl_campfires name = mcl_campfires
depends = mcl_sounds depends = mcl_sounds, mcl_util
author = PrairieWind, Gerold55 author = PrairieWind, Gerold55

View File

@ -435,7 +435,7 @@ minetest.register_craftitem("mcl_fishing:fish_raw", {
on_place = minetest.item_eat(2), on_place = minetest.item_eat(2),
on_secondary_use = minetest.item_eat(2), on_secondary_use = minetest.item_eat(2),
stack_max = 64, stack_max = 64,
groups = { food=2, eatable = 2, smoker_cookable = 1 }, groups = { food=2, eatable = 2, smoker_cookable = 1, campfire_cookable = 1 },
_mcl_saturation = 0.4, _mcl_saturation = 0.4,
}) })
@ -465,7 +465,7 @@ minetest.register_craftitem("mcl_fishing:salmon_raw", {
on_place = minetest.item_eat(2), on_place = minetest.item_eat(2),
on_secondary_use = minetest.item_eat(2), on_secondary_use = minetest.item_eat(2),
stack_max = 64, stack_max = 64,
groups = { food=2, eatable = 2, smoker_cookable = 1 }, groups = { food=2, eatable = 2, smoker_cookable = 1, campfire_cookable = 1 },
_mcl_saturation = 0.4, _mcl_saturation = 0.4,
}) })

View File

@ -20,7 +20,7 @@ minetest.register_craftitem("mcl_mobitems:mutton", {
wield_image = "mcl_mobitems_mutton_raw.png", wield_image = "mcl_mobitems_mutton_raw.png",
on_place = minetest.item_eat(2), on_place = minetest.item_eat(2),
on_secondary_use = minetest.item_eat(2), on_secondary_use = minetest.item_eat(2),
groups = { food = 2, eatable = 2, smoker_cookable = 1 }, groups = { food = 2, eatable = 2, smoker_cookable = 1, campfire_cookable = 1 },
_mcl_saturation = 1.2, _mcl_saturation = 1.2,
stack_max = 64, stack_max = 64,
}) })
@ -44,7 +44,7 @@ minetest.register_craftitem("mcl_mobitems:beef", {
wield_image = "mcl_mobitems_beef_raw.png", wield_image = "mcl_mobitems_beef_raw.png",
on_place = minetest.item_eat(3), on_place = minetest.item_eat(3),
on_secondary_use = minetest.item_eat(3), on_secondary_use = minetest.item_eat(3),
groups = { food = 2, eatable = 3, smoker_cookable = 1 }, groups = { food = 2, eatable = 3, smoker_cookable = 1, campfire_cookable = 1 },
_mcl_saturation = 1.8, _mcl_saturation = 1.8,
stack_max = 64, stack_max = 64,
}) })
@ -69,7 +69,7 @@ minetest.register_craftitem("mcl_mobitems:chicken", {
wield_image = "mcl_mobitems_chicken_raw.png", wield_image = "mcl_mobitems_chicken_raw.png",
on_place = minetest.item_eat(2), on_place = minetest.item_eat(2),
on_secondary_use = minetest.item_eat(2), on_secondary_use = minetest.item_eat(2),
groups = { food = 2, eatable = 2, smoker_cookable = 1 }, groups = { food = 2, eatable = 2, smoker_cookable = 1, campfire_cookable = 1 },
_mcl_saturation = 1.2, _mcl_saturation = 1.2,
stack_max = 64, stack_max = 64,
}) })
@ -93,7 +93,7 @@ minetest.register_craftitem("mcl_mobitems:porkchop", {
wield_image = "mcl_mobitems_porkchop_raw.png", wield_image = "mcl_mobitems_porkchop_raw.png",
on_place = minetest.item_eat(3), on_place = minetest.item_eat(3),
on_secondary_use = minetest.item_eat(3), on_secondary_use = minetest.item_eat(3),
groups = { food = 2, eatable = 3, smoker_cookable = 1 }, groups = { food = 2, eatable = 3, smoker_cookable = 1, campfire_cookable = 1 },
_mcl_saturation = 1.8, _mcl_saturation = 1.8,
stack_max = 64, stack_max = 64,
}) })
@ -117,7 +117,7 @@ minetest.register_craftitem("mcl_mobitems:rabbit", {
wield_image = "mcl_mobitems_rabbit_raw.png", wield_image = "mcl_mobitems_rabbit_raw.png",
on_place = minetest.item_eat(3), on_place = minetest.item_eat(3),
on_secondary_use = minetest.item_eat(3), on_secondary_use = minetest.item_eat(3),
groups = { food = 2, eatable = 3, smoker_cookable = 1 }, groups = { food = 2, eatable = 3, smoker_cookable = 1, campfire_cookable = 1 },
_mcl_saturation = 1.8, _mcl_saturation = 1.8,
stack_max = 64, stack_max = 64,
}) })