MineClone2/mods/ITEMS/mcl_cauldrons/api.lua

159 lines
6.1 KiB
Lua

local has_doc = minetest.get_modpath(minetest.get_current_modname())
local function survival_give(inv, original_item, itemstack)
if inv:room_for_item("main", itemstack) then
inv:add_item("main", itemstack)
return original_item:take_item()
else
minetest.add_item(user:get_pos(), itemstack)
return original_item
end
end
local function creative_give(inv, original_item, itemstack)
if not inv:contains_item("main", itemstack) then
inv:add_item("main", itemstack)
return original_item
end
end
local function give_item(user, original_item, itemstack)
local inv = user:get_inventory()
if inv then
if minetest.is_creative_enabled(user:get_player_name()) then
creative_give(inv, original_item, itemstack)
else
survival_give(inv, original_item, itemstack)
end
end
return itemstack
end
mcl_cauldrons.registered_cauldrons = {}
function mcl_cauldrons.register_cauldron_type(def)
for water_level = 1,3 do
local id = "mcl_cauldrons:cauldron_"..def.name.."_"..water_level
mcl_cauldrons.registered_cauldrons[id] = def
minetest.register_node(id, {
description = string.format(def.desc, water_level),
_doc_items_create_entry = false,
use_texture_alpha = minetest.features.use_texture_alpha_string_modes and "opaque" or false,
drawtype = "nodebox",
paramtype = "light",
is_ground_content = false,
groups = {pickaxey=1, not_in_creative_inventory=1, cauldron=(1+water_level), cauldron_filled=water_level, comparator_signal=water_level},
node_box = mcl_cauldrons.cauldron_nodeboxes[water_level],
collision_box = mcl_cauldrons.cauldron_nodeboxes[0],
selection_box = { type = "regular" },
tiles = {
"("..def.texture.."^[verticalframe:16:0"..")^mcl_cauldrons_cauldron_top.png",
"mcl_cauldrons_cauldron_inner.png^mcl_cauldrons_cauldron_bottom.png",
"mcl_cauldrons_cauldron_side.png"
},
sounds = mcl_sounds.node_sound_metal_defaults(),
drop = "mcl_cauldrons:cauldron",
_mcl_hardness = 2,
_mcl_blast_resistance = 2,
})
-- Add entry aliases for the Help
if has_doc then
doc.add_entry_alias("nodes", "mcl_cauldrons:cauldron", "nodes", id)
end
end
end
function mcl_cauldrons.is_cauldron(name)
return minetest.get_item_group(name, "cauldron") ~= 0
end
-------------------------------------
--Functions to get/set cauldron level
-------------------------------------
function mcl_cauldrons.get_cauldron_level(pos)
local nn = minetest.get_node(pos)
return minetest.get_item_group(nn.name, "cauldron")
end
function mcl_cauldrons.set_cauldron_level(pos, type, level)
return minetest.set_node(pos, {name = mcl_cauldrons.get_cauldron_string(type, level)})
end
function mcl_cauldrons.add_cauldron_level(pos, type, number)
local current = mcl_cauldrons.get_cauldron_level(pos)
local number = current + number
if number > 4 then number = 4 end
if number < 1 then number = 1 end
mcl_cauldrons.set_cauldron_level(pos, type, number)
return number, not current == number
end
function mcl_cauldrons.get_cauldron_string(type, level)
if mcl_cauldrons.registered_cauldrons["mcl_cauldrons:cauldron_"..type.."_"..level] then
return "mcl_cauldrons:cauldron_"..type.."_"..level
elseif level == 0 then
return "mcl_cauldrons:cauldron"
else
minetest.log("warning", "[mcl_cauldrons] trying to get string from invalid cauldron params")
return "air"
end
end
----------------------------
--Functions to take cauldron
----------------------------
function mcl_cauldrons.take_cauldron(pos, itemstack, user, sounds)
local nn = minetest.get_node(pos)
if mcl_cauldrons.registered_cauldrons[nn.name] and mcl_cauldrons.registered_cauldrons[nn.name].bucket then
if user and not minetest.is_creative_enabled(user:get_player_name()) then
minetest.set_node(pos, {name="mcl_cauldrons:cauldron"})
if sounds then minetest.sound_play(sounds.dug, {gain=1.0, pos = pos, pitch = 1 + math.random(-10, 10)*0.005}, true) end
return give_item(user, original, ItemStack(mcl_cauldrons.registered_cauldrons[nn.name].bucket))
else
minetest.set_node(pos, {name="mcl_cauldrons:cauldron"})
if sounds then minetest.sound_play(sounds.dug, {gain=1.0, pos = pos, pitch = 1 + math.random(-10, 10)*0.005}, true) end
return itemstack
end
else
minetest.set_node(pos, {name="mcl_cauldrons:cauldron"})
if sounds then minetest.sound_play(sounds.dug, {gain=1.0, pos = pos, pitch = 1 + math.random(-10, 10)*0.005}, true) end
return itemstack
end
end
function mcl_cauldrons.take_small_cauldron(pos, itemstack, user, sounds)
local nn = minetest.get_node(pos)
if mcl_cauldrons.registered_cauldrons[nn.name] and mcl_cauldrons.registered_cauldrons[nn.name].bottle then
if user and not minetest.is_creative_enabled(user:get_player_name()) then
local number, changed = mcl_cauldrons.add_cauldron_level(pos, mcl_cauldrons.registered_cauldrons[nn.name].name, -1)
if changed then
if sounds then minetest.sound_play(sounds.dug, {gain=1.0, pos = pos, pitch = 1 + math.random(-10, 10)*0.005}, true) end
local item_name = mcl_cauldrons.registered_cauldrons[nn.name].bottle
local inv = placer:get_inventory()
minetest.sound_play("mcl_potions_bottle_fill", {pos=pointed_thing.under, gain=0.5, max_hear_range=16}, true)
if minetest.is_creative_enabled(placer:get_player_name()) then
-- Don't replace empty bottle in creative for convenience reasons
if not inv:contains_item("main", item_name) then
inv:add_item("main", item_name)
end
elseif itemstack:get_count() == 1 then
return item_name
else
return give_item(user, ItemStack(mcl_cauldrons.registered_cauldrons[nn.name].bucket))
end
end
else
local number = mcl_cauldrons.add_cauldron_level(pos, mcl_cauldrons.registered_cauldrons[nn.name].name, -1)
if number ~= 0 then
if sounds then minetest.sound_play(sounds.dug, {gain=1.0, pos = pos, pitch = 1 + math.random(-10, 10)*0.005}, true) end
end
return itemstack
end
else
local number = mcl_cauldrons.add_cauldron_level(pos, mcl_cauldrons.registered_cauldrons[nn.name].name, -1)
if number ~= 0 then
if sounds then minetest.sound_play(sounds.dug, {gain=1.0, pos = pos, pitch = 1 + math.random(-10, 10)*0.005}, true) end
end
return itemstack
end
end