move every function in the right place

This commit is contained in:
AFCMS 2021-03-27 12:06:52 +01:00
parent a99619774d
commit 6f23351126
2 changed files with 88 additions and 82 deletions

View File

@ -1,5 +1,18 @@
local has_doc = minetest.get_modpath(minetest.get_current_modname()) local has_doc = minetest.get_modpath(minetest.get_current_modname())
local function give_item(user, itemstack)
local inv = user:get_inventory()
if inv then
if inv:room_for_item("main", itemstack) then
inv:add_item("main", itemstack)
else
minetest.add_item(user:get_pos(), itemstack)
end
--end
end
return itemstack
end
mcl_cauldrons.registered_cauldrons = {} mcl_cauldrons.registered_cauldrons = {}
function mcl_cauldrons.register_cauldron_type(def) function mcl_cauldrons.register_cauldron_type(def)
@ -38,13 +51,25 @@ function mcl_cauldrons.is_cauldron(name)
return minetest.get_item_group(name, "cauldron") ~= 0 return minetest.get_item_group(name, "cauldron") ~= 0
end 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) function mcl_cauldrons.set_cauldron_level(pos, type, level)
return minetest.set_node(pos, {name = mcl_cauldrons.get_cauldron_string(type, level)}) return minetest.set_node(pos, {name = mcl_cauldrons.get_cauldron_string(type, level)})
end end
function mcl_cauldrons.get_cauldron_level(pos) function mcl_cauldrons.add_cauldron_level(pos, type, number)
local nn = minetest.get_node(pos) local current = mcl_cauldrons.get_cauldron_level(pos)
return minetest.get_item_group(nn.name, "cauldron") 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 end
function mcl_cauldrons.get_cauldron_string(type, level) function mcl_cauldrons.get_cauldron_string(type, level)
@ -56,4 +81,63 @@ function mcl_cauldrons.get_cauldron_string(type, level)
minetest.log("warning", "[mcl_cauldrons] trying to get string from invalid cauldron params") minetest.log("warning", "[mcl_cauldrons] trying to get string from invalid cauldron params")
return "air" return "air"
end 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, 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 end

View File

@ -6,82 +6,4 @@ mcl_cauldrons = {}
dofile(modpath.."/utils.lua") dofile(modpath.."/utils.lua")
dofile(modpath.."/api.lua") dofile(modpath.."/api.lua")
dofile(modpath.."/register.lua") dofile(modpath.."/register.lua")
local function give_item(user, itemstack)
local inv = user:get_inventory()
if inv then
if inv:room_for_item("main", itemstack) then
inv:add_item("main", itemstack)
else
minetest.add_item(user:get_pos(), itemstack)
end
--end
end
return itemstack
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.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, 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