1
0
mirror of https://github.com/HybridDog/connected_chests.git synced 2024-12-28 06:00:17 +01:00

fix adding api

M  init.lua
D  textures/connected_chests_side.png
D  textures/connected_chests_top.png
This commit is contained in:
Hybrid Dog 2017-01-28 20:05:35 +01:00
parent 482de1c06b
commit a0df5845cd
3 changed files with 237 additions and 314 deletions

473
init.lua
View File

@ -1,110 +1,6 @@
local load_time_start = minetest.get_us_time() local load_time_start = minetest.get_us_time()
local creative_enabled = minetest.setting_getbool"creative_mode"
local big_formspec = "size[13,9]"..
"list[current_name;main;0,0;13,5;]"..
"list[current_player;main;2.5,5.2;8,4;]"..
"listring[current_name;main]"..
"listring[current_player;main]"
local function formspec_biggerinv(spec)
return big_formspec
end
local chests = {}
local chestdata = {}
local function register_chest(fromname, data)
chestdata[fromname] = data
local mod, name = fromname:split":"
-- executed when connecting the chest
chests[fromname] = function(pu, pa, par, metatable)
minetest.add_node(pu, {name=fromname .. "_connected_left", param2=par})
minetest.add_node(pa, {name=fromname .. "_connected_right", param2=par})
metatable.fields.formspec = formspec_biggerinv(metatable.fields.formspec)
metatable.fields.infotext = "Big " .. metatable.fields.infotext
local inv = metatable.inventory
inv:set_size("main", 65)
local meta = minetest.get_meta(pu)
meta:from_table(metatable)
end
-- override the original node to support connecting
local place_chest = minetest.registered_nodes[fromname].on_place
minetest.override_item(fromname, {
on_place = function(itemstack, placer, pointed_thing)
if not placer then
return
end
local pu, pa, par2 = get_pointed_info(pointed_thing, fromname)
if not pu
or not placer:get_player_control().sneak then
return place_chest(itemstack, placer, pointed_thing)
end
if minetest.is_protected(pa, placer:get_player_name()) then
return
end
connect_chests(pu, pa, par2, fromname)
if not creative_enabled then
itemstack:take_item()
return itemstack
end
end
})
end
local chests = {
["default:chest"] = function(pu, pa, par, metatable, stuff)
minetest.add_node(pu, {name="connected_chests:chest_left", param2=par})
minetest.add_node(pa, {name="connected_chests:chest_right", param2=par})
local meta = minetest.get_meta(pu)
meta:set_string("formspec", big_formspec)
meta:set_string("infotext", "Big Chest")
local inv = meta:get_inventory()
inv:set_size("main", 65)
inv:set_list("main", stuff)
end,
["default:chest_locked"] = function(pu, pa, par, stuff, name, owner)
local owner = owner or name
minetest.add_node(pu, {name="connected_chests:chest_locked_left", param2=par})
minetest.add_node(pa, {name="connected_chests:chest_locked_right", param2=par})
local meta = minetest.get_meta(pu)
meta:set_string("owner", owner)
meta:set_string("formspec", big_formspec)
meta:set_string("infotext", "Big Locked Chest (owned by "..
meta:get_string"owner"..")")
local inv = meta:get_inventory()
inv:set_size("main", 65)
inv:set_list("main", stuff)
end,
}
local function get_pointed_info(pt, name)
if not pt then
return
end
local pu = minetest.get_pointed_thing_position(pt)
local pa = minetest.get_pointed_thing_position(pt, true)
if not pu
or not pa
or pu.y ~= pa.y then
return
end
local nd_u = minetest.get_node(pu)
if nd_u.name ~= name then
return
end
return pu, pa, nd_u.param2
end
local param_tab = { local param_tab = {
["-1 0"] = 0, ["-1 0"] = 0,
["1 0"] = 2, ["1 0"] = 2,
@ -117,47 +13,6 @@ for n,i in pairs(param_tab) do
param_tab2[i] = n:split" " param_tab2[i] = n:split" "
end end
local pars = {[0]=2, 3, 0, 1}
local function connect_chests(pu, pa, old_param2, name)
local metatable = minetest.get_meta(pu):to_table()
print(dump(metatable))
local par = param_tab[pu.x-pa.x.." "..pu.z-pa.z]
local par_inverted = pars[par]
if old_param2 == par_inverted then
pu, pa = pa, pu
par = par_inverted
end
chests[name](pu, pa, par, metatable)
end
--[[
for name in pairs(chests) do
local place_chest = minetest.registered_nodes[name].on_place
minetest.override_item(name, {
on_place = function(itemstack, placer, pointed_thing)
if not placer then
return
end
local pu, pa, par2 = get_pointed_info(pointed_thing, name)
if not pu
or not placer:get_player_control().sneak then
return place_chest(itemstack, placer, pointed_thing)
end
if minetest.is_protected(pa, placer:get_player_name()) then
return
end
connect_chests(pu, pa, par2, name)
if not creative_enabled then
itemstack:take_item()
return itemstack
end
end
})
end--]]
local function return_remove_next(allowed_name) local function return_remove_next(allowed_name)
local function remove_next(pos, oldnode) local function remove_next(pos, oldnode)
if oldnode.param2 > 3 then if oldnode.param2 > 3 then
@ -173,6 +28,7 @@ local function return_remove_next(allowed_name)
return remove_next return remove_next
end end
local function return_add_next(right_name) local function return_add_next(right_name)
local function add_next(pos, node) local function add_next(pos, node)
node = node or minetest.get_node(pos) node = node or minetest.get_node(pos)
@ -193,98 +49,46 @@ local function return_add_next(right_name)
end end
local function log_access(pos, player, text) -- gives information about the positions and param to place the nodes
minetest.log("action", player:get_player_name().. local function get_pointed_info(pt, name)
" moves stuff "..text.." at "..minetest.pos_to_string(pos)) if not pt then
return
end
local pu = minetest.get_pointed_thing_position(pt)
local pa = minetest.get_pointed_thing_position(pt, true)
if not pu
or not pa
or pu.y ~= pa.y then
return
end
local nd_u = minetest.get_node(pu)
if nd_u.name ~= name then
return
end
return pu, pa, nd_u.param2
end end
-- Adds the big chests
local top_texture = "connected_chests_top.png^default_chest_top.png^([combine:16x16:5,0=default_chest_top.png^connected_chests_frame.png^[makealpha:255,126,126)^connected_chests_top.png"
local side_texture = "connected_chests_side.png^default_chest_side.png^([combine:16x16:5,0=default_chest_side.png^connected_chests_frame.png^[makealpha:255,126,126)^connected_chests_side.png"
local chest = {}
local origdef = minetest.registered_nodes["default:chest"]
for i in pairs(origdef) do
chest[i] = rawget(origdef, i)
end
chest.description = nil
chest.legacy_facedir_simple = nil
chest.after_place_node = nil
chest.on_receive_fields = nil
chest.tiles = {top_texture, top_texture, "default_obsidian_glass.png",
"default_chest_side.png", side_texture.."^[transformFX", side_texture.."^connected_chests_front.png"}
chest.drop = "default:chest 2"
chest.selection_box = {
type = "fixed",
fixed = {
{-0.5, -0.5, -0.5, 1.5, 0.5, 0.5},
},
}
chest.on_construct = return_add_next"connected_chests:chest_right"
chest.after_destruct = return_remove_next"connected_chests:chest_right"
chest.on_metadata_inventory_move = function(pos, _, _, _, _, _, player)
log_access(pos, player, "in a big chest")
end
chest.on_metadata_inventory_put = function(pos, _, _, _, player)
log_access(pos, player, "to a big chest")
end
chest.on_metadata_inventory_take = function(pos, _, _, _, player)
log_access(pos, player, "from a big chest")
end
minetest.register_node("connected_chests:chest_left", chest)
local chest_locked = {} local pars = {[0]=2, 3, 0, 1}
local origdef = minetest.registered_nodes["default:chest_locked"]
for i in pairs(origdef) do local chestdata = {}
chest_locked[i] = rawget(origdef, i)
-- executed when connecting the chests
local function connect_chests(pu, pa, old_param2, name)
local metatable = minetest.get_meta(pu):to_table()
local par = param_tab[pu.x-pa.x.." "..pu.z-pa.z]
local par_inverted = pars[par]
if old_param2 == par_inverted then
pu, pa = pa, pu
par = par_inverted
end end
chest_locked.description = nil chestdata[name].on_connect(pu, pa, par, metatable)
chest_locked.legacy_facedir_simple = nil
chest_locked.after_place_node = nil
chest_locked.on_construct = nil
chest_locked.on_receive_fields = nil
chest_locked.tiles = {top_texture, top_texture, "default_obsidian_glass.png",
"default_chest_side.png", side_texture.."^[transformFX", side_texture.."^connected_chests_front.png^connected_chests_lock.png"}
chest_locked.drop = "default:chest_locked 2"
chest_locked.selection_box = {
type = "fixed",
fixed = {
{-0.5, -0.5, -0.5, 1.5, 0.5, 0.5},
},
}
chest_locked.on_construct = return_add_next"connected_chests:chest_locked_right"
chest_locked.after_destruct = return_remove_next"connected_chests:chest_locked_right"
chest_locked.on_metadata_inventory_move = function(pos, _, _, _, _, _, player)
log_access(pos, player, "in a big locked chest")
end end
chest_locked.on_metadata_inventory_put = function(pos, _, _, _, player)
log_access(pos, player, "to a big locked chest")
end
chest_locked.on_metadata_inventory_take = function(pos, _, _, _, player)
log_access(pos, player, "from a big locked chest")
end
chest_locked.on_rightclick = function(pos, _, clicker)
local meta = minetest.get_meta(pos)
local pname = clicker:get_player_name()
if pname == meta:get_string"owner"
or pname == minetest.setting_get"name" then
minetest.show_formspec(
pname,
"connected_chests:chest_locked_left",
"size[13,9]"..
"list[nodemeta:".. pos.x .. "," .. pos.y .. "," ..pos.z .. ";main;0,0;13,5;]"..
"list[current_player;main;2.5,5.2;8,4;]"
)
end
end
minetest.register_node("connected_chests:chest_locked_left", chest_locked)
local tube_to_left, tube_to_left_locked, tube_update, tube_groups local tube_to_left, tube_to_left_locked, tube_update, tube_groups
@ -311,50 +115,117 @@ else
function tube_update() end function tube_update() end
end end
minetest.register_node("connected_chests:chest_right", {
tiles = {top_texture.."^[transformFX", top_texture.."^[transformFX", "default_chest_side.png", connected_chests = {chestdata = chestdata}
"default_obsidian_glass.png", side_texture, side_texture.."^connected_chests_front.png^[transformFX"}, --[[
paramtype2 = "facedir", connected_chests.register_chest(<original_node>, {
drop = "", get_formspec = function(metatable, pos)
pointable = false, return <formspec_of_big>
diggable = false,
on_construct = function(pos)
local node = minetest.get_node(pos)
if node.param2 > 3 then
node.param2 = node.param2%4
minetest.set_node(pos, node)
return
end
local x, z = unpack(param_tab2[node.param2])
local node_left = minetest.get_node{x=pos.x+x, y=pos.y, z=pos.z+z}
if node_left.name ~= "connected_chests:chest_left"
or node_left.param2 ~= node.param2 then
minetest.remove_node(pos)
return
end
tube_update(pos)
end, end,
after_destruct = function(pos, oldnode) lock = true, -- indicates whether a lock should be added to the texture
if oldnode.param2 > 3 then -- and has an impact on the tube function
front = <keyhole_texture>, -- if present, this texture is added to the chest
-- front
})
]]
function connected_chests.register_chest(fromname, data)
chestdata[fromname] = data
--~ local mod, name = fromname:split":"
local name_left = fromname .. "_connected_left"
local name_right = fromname .. "_connected_right"
data.left = name_left
data.right = name_right
-- executed when connecting the chest
data.on_connect = function(pu, pa, par, metatable)
minetest.add_node(pu, {name=name_left, param2=par})
minetest.add_node(pa, {name=name_right, param2=par})
metatable.fields.formspec = data.get_formspec(metatable, pu)
metatable.fields.infotext = "Big " .. metatable.fields.infotext
local meta = minetest.get_meta(pu)
meta:from_table(metatable)
local inv = meta:get_inventory()
inv:set_size("main", 65)
end
-- override the original node to support connecting
local place_chest = minetest.registered_nodes[fromname].on_place
minetest.override_item(fromname, {
on_place = function(itemstack, placer, pointed_thing)
if not placer then
return return
end end
local x, z = unpack(param_tab2[oldnode.param2]) local pu, pa, par2 = get_pointed_info(pointed_thing, fromname)
local node_left = minetest.get_node{x=pos.x+x, y=pos.y, z=pos.z+z} if not pu
if node_left.name == "connected_chests:chest_left" or not placer:get_player_control().sneak then
and node_left.param2 == oldnode.param2 return place_chest(itemstack, placer, pointed_thing)
and minetest.get_node(pos).name == "air" then end
minetest.set_node(pos, oldnode) if minetest.is_protected(pa, placer:get_player_name()) then
return return
end end
tube_update(pos) connect_chests(pu, pa, par2, fromname)
end, if not minetest.setting_getbool"creative_mode" then
tube = tube_to_left, itemstack:take_item()
groups = tube_groups, return itemstack
end
end
}) })
minetest.register_node("connected_chests:chest_locked_right", {
tiles = {top_texture.."^[transformFX", top_texture.."^[transformFX", "default_chest_side.png", -- Adds the big chest nodes
"default_obsidian_glass.png", side_texture, side_texture.."^connected_chests_front.png^connected_chests_lock.png^[transformFX"},
-- the left one contains inventory
local chest = {}
local origdef = minetest.registered_nodes[fromname]
for i in pairs(origdef) do
chest[i] = rawget(origdef, i)
end
local top = chest.tiles[1]
local side = chest.tiles[4]
local top_texture = top .. "^([combine:16x16:5,0=" .. top .. "^connected_chests_frame.png^[makealpha:255,126,126)"
local side_texture = side .. "^([combine:16x16:5,0=" .. side .. "^connected_chests_frame.png^[makealpha:255,126,126)"
chest.description = "Big " .. chest.description
chest.groups = table.copy(chest.groups)
chest.groups.not_in_creative_inventory = 1
chest.legacy_facedir_simple = nil
chest.after_place_node = nil
chest.on_receive_fields = nil
if not data.front then
data.front = "connected_chests_front.png"
if data.lock then
data.front = data.front .. "^connected_chests_lock.png"
end
end
chest.tiles = {top_texture, top_texture, "default_obsidian_glass.png",
side, side_texture.."^[transformFX", side_texture.."^" .. data.front}
chest.drop = (chest.drop or fromname) .. " 2"
chest.selection_box = {
type = "fixed",
fixed = {
{-0.5, -0.5, -0.5, 1.5, 0.5, 0.5},
},
}
chest.on_construct = return_add_next(name_right)
chest.after_destruct = return_remove_next(name_right)
--~ minetest.register_node("connected_chests:chest_left", chest)
minetest.register_node(":" .. name_left, chest)
-- the right one is the deco one
local tiles = {top_texture.."^[transformFX", top_texture.."^[transformFX",
side, "default_obsidian_glass.png", side_texture, side_texture
.. "^" .. data.front .. "^[transformFX"}
--~ minetest.register_node(, {
minetest.register_node(":" .. name_right, {
tiles = tiles,
paramtype2 = "facedir", paramtype2 = "facedir",
drop = "", drop = "",
pointable = false, pointable = false,
@ -363,13 +234,12 @@ minetest.register_node("connected_chests:chest_locked_right", {
local node = minetest.get_node(pos) local node = minetest.get_node(pos)
if node.param2 > 3 then if node.param2 > 3 then
node.param2 = node.param2%4 node.param2 = node.param2%4
-- ↓ calls the on_construct from the beginning again
minetest.set_node(pos, node) minetest.set_node(pos, node)
return return
end end
local x, z = unpack(param_tab2[node.param2]) local x, z = unpack(param_tab2[node.param2])
local node_left = minetest.get_node{x=pos.x+x, y=pos.y, z=pos.z+z} local node_left = minetest.get_node{x=pos.x+x, y=pos.y, z=pos.z+z}
if node_left.name ~= "connected_chests:chest_locked_left" if node_left.name ~= name_left
or node_left.param2 ~= node.param2 then or node_left.param2 ~= node.param2 then
minetest.remove_node(pos) minetest.remove_node(pos)
return return
@ -382,7 +252,7 @@ minetest.register_node("connected_chests:chest_locked_right", {
end end
local x, z = unpack(param_tab2[oldnode.param2]) local x, z = unpack(param_tab2[oldnode.param2])
local node_left = minetest.get_node{x=pos.x+x, y=pos.y, z=pos.z+z} local node_left = minetest.get_node{x=pos.x+x, y=pos.y, z=pos.z+z}
if node_left.name == "connected_chests:chest_locked_left" if node_left.name == name_left
and node_left.param2 == oldnode.param2 and node_left.param2 == oldnode.param2
and minetest.get_node(pos).name == "air" then and minetest.get_node(pos).name == "air" then
minetest.set_node(pos, oldnode) minetest.set_node(pos, oldnode)
@ -390,14 +260,38 @@ minetest.register_node("connected_chests:chest_locked_right", {
end end
tube_update(pos) tube_update(pos)
end, end,
tube = tube_to_left_locked, tube = data.lock and tube_to_left_locked or tube_to_left,
groups = tube_groups, groups = tube_groups,
}) })
end
local big_formspec = "size[13,9]"..
"list[current_name;main;0,0;13,5;]"..
"list[current_player;main;2.5,5.2;8,4;]"..
"listring[current_name;main]"..
"listring[current_player;main]"
connected_chests.register_chest("default:chest", {
get_formspec = function()
return big_formspec
end
})
connected_chests.register_chest("default:chest_locked", {
get_formspec = function()
return big_formspec
end,
lock = true
})
-- abms to fix half chests -- abms to fix half chests
for _,i in pairs{"chest", "chest_locked"} do for _,i in pairs(chestdata) do
minetest.register_abm{ minetest.register_abm{
nodenames = {"connected_chests:"..i.."_right"}, nodenames = {i.right},
interval = 10, interval = 10,
chance = 1, chance = 1,
action = function(pos, node) action = function(pos, node)
@ -408,21 +302,50 @@ for _,i in pairs{"chest", "chest_locked"} do
end end
local x, z = unpack(param_tab2[node.param2]) local x, z = unpack(param_tab2[node.param2])
local left_node = minetest.get_node{x=pos.x+x, y=pos.y, z=pos.z+z} local left_node = minetest.get_node{x=pos.x+x, y=pos.y, z=pos.z+z}
if left_node.name ~= "connected_chests:"..i.."_left" if left_node.name ~= i.left
or left_node.param2 ~= node.param2 then or left_node.param2 ~= node.param2 then
minetest.remove_node(pos) minetest.remove_node(pos)
end end
end, end,
} }
minetest.register_abm{ minetest.register_abm{
nodenames = {"connected_chests:"..i.."_left"}, nodenames = {i.left},
interval = 3, interval = 3,
chance = 1, chance = 1,
action = return_add_next("connected_chests:"..i.."_right"), action = return_add_next(i.right),
} }
end end
-- legacy
minetest.register_alias("connected_chests:chest_left", "default:chest_connected_left")
minetest.register_alias("connected_chests:chest_right", "default:chest_connected_right")
minetest.register_alias("connected_chests:chest_left_locked", "default:chest_locked_connected_left")
minetest.register_alias("connected_chests:chest_right_locked", "default:chest_locked_connected_right")
--~ local function log_access(pos, player, text)
--~ minetest.log("action", player:get_player_name()..
--~ " moves stuff "..text.." at "..minetest.pos_to_string(pos))
--~ end
--~ chest_locked.on_rightclick = function(pos, _, clicker)
--~ local meta = minetest.get_meta(pos)
--~ local pname = clicker:get_player_name()
--~ if pname == meta:get_string"owner"
--~ or pname == minetest.setting_get"name" then
--~ minetest.show_formspec(
--~ pname,
--~ "connected_chests:chest_locked_left",
--~ "size[13,9]"..
--~ "list[nodemeta:".. pos.x .. "," .. pos.y .. "," ..pos.z .. ";main;0,0;13,5;]"..
--~ "list[current_player;main;2.5,5.2;8,4;]"
--~ )
--~ end
--~ end
local time = (minetest.get_us_time() - load_time_start) / 1000000 local time = (minetest.get_us_time() - load_time_start) / 1000000
local msg = "[connected_chests] loaded after ca. " .. time .. " seconds." local msg = "[connected_chests] loaded after ca. " .. time .. " seconds."
if time > 0.01 then if time > 0.01 then

Binary file not shown.

Before

Width:  |  Height:  |  Size: 74 B

Binary file not shown.

Before

Width:  |  Height:  |  Size: 74 B