connected_chests/init.lua

384 lines
11 KiB
Lua
Raw Normal View History

2016-07-07 11:55:29 +02:00
local load_time_start = minetest.get_us_time()
2014-06-02 20:38:58 +02:00
2016-07-07 11:55:29 +02:00
local creative_enabled = minetest.setting_getbool"creative_mode"
2014-06-03 20:38:52 +02:00
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]"
2014-06-03 20:38:52 +02:00
local chests = {
["default:chest"] = function(pu, pa, par, 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)
2014-06-03 20:38:52 +02:00
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)
2014-06-03 20:38:52 +02:00
meta:set_string("infotext", "Big Locked Chest (owned by "..
2016-07-07 11:55:29 +02:00
meta:get_string"owner"..")")
2014-06-03 20:38:52 +02:00
local inv = meta:get_inventory()
inv:set_size("main", 65)
inv:set_list("main", stuff)
end,
}
2016-07-07 11:55:29 +02:00
local function get_pointed_info(pt, name)
if not pt then
2014-06-02 20:38:58 +02:00
return
end
2016-07-07 11:55:29 +02:00
local pu = minetest.get_pointed_thing_position(pt)
local pa = minetest.get_pointed_thing_position(pt, true)
2015-09-19 09:40:14 +02:00
if not pu
or not pa
or pu.y ~= pa.y then
2014-06-02 20:38:58 +02:00
return
end
local nd_u = minetest.get_node(pu)
2014-06-03 20:38:52 +02:00
if nd_u.name ~= name then
2014-06-02 20:38:58 +02:00
return
end
return pu, pa, nd_u.param2
end
local param_tab = {
["-1 0"] = 0,
["1 0"] = 2,
["0 -1"] = 3,
["0 1"] = 1,
}
2014-06-04 18:37:17 +02:00
local param_tab2 = {}
for n,i in pairs(param_tab) do
2016-07-07 11:55:29 +02:00
param_tab2[i] = n:split" "
2014-06-04 18:37:17 +02:00
end
2014-06-02 20:38:58 +02:00
local pars = {[0]=2, 3, 0, 1}
2014-06-03 20:38:52 +02:00
local function connect_chests(pu, pa, old_param2, name)
local oldmeta = minetest.get_meta(pu)
2016-07-07 11:55:29 +02:00
local stuff = oldmeta:get_inventory():get_list"main"
local owner = oldmeta:get_string"owner"
2014-06-02 20:38:58 +02:00
local par = param_tab[pu.x-pa.x.." "..pu.z-pa.z]
2014-06-03 20:38:52 +02:00
local par_inverted = pars[par]
if old_param2 == par_inverted then
2014-06-02 20:38:58 +02:00
pu, pa = pa, pu
2014-06-03 20:38:52 +02:00
par = par_inverted
2014-06-02 20:38:58 +02:00
end
2014-06-03 20:38:52 +02:00
chests[name](pu, pa, par, stuff, name, owner)
2014-06-02 20:38:58 +02:00
end
2016-07-07 11:55:29 +02:00
for name in pairs(chests) do
2014-06-03 20:38:52 +02:00
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)
2015-09-19 09:40:14 +02:00
if not pu
or not placer:get_player_control().sneak then
2014-06-03 20:38:52 +02:00
return place_chest(itemstack, placer, pointed_thing)
end
2015-09-19 09:40:14 +02:00
if minetest.is_protected(pa, placer:get_player_name()) then
2014-06-03 20:38:52 +02:00
return
end
connect_chests(pu, pa, par2, name)
if not creative_enabled then
itemstack:take_item()
return itemstack
end
2014-06-02 20:38:58 +02:00
end
2014-06-03 20:38:52 +02:00
})
end
2014-06-02 20:38:58 +02:00
local function return_remove_next(allowed_name)
local function remove_next(pos, oldnode)
2015-04-18 11:51:06 +02:00
if oldnode.param2 > 3 then
return
end
2016-07-07 11:55:29 +02:00
local x, z = unpack(param_tab2[oldnode.param2])
pos.x = pos.x-x
pos.z = pos.z-z
if minetest.get_node(pos).name == allowed_name then
minetest.remove_node(pos)
2014-06-02 21:04:06 +02:00
end
end
return remove_next
2014-06-02 21:04:06 +02:00
end
2014-06-02 20:38:58 +02:00
2015-04-18 15:45:11 +02:00
local function return_add_next(right_name)
local function add_next(pos, node)
node = node or minetest.get_node(pos)
local par = node.param2
if par > 3 then
node.param2 = 0
minetest.set_node(pos, node)
return
end
2016-07-07 11:55:29 +02:00
local x, z = unpack(param_tab2[par])
2015-04-18 15:45:11 +02:00
pos.x = pos.x-x
pos.z = pos.z-z
if minetest.get_node(pos).name == "air" then
minetest.set_node(pos, {name=right_name, param2=par})
end
end
return add_next
end
2015-04-18 15:05:26 +02:00
2014-06-03 20:38:52 +02:00
local function log_access(pos, player, text)
minetest.log("action", player:get_player_name()..
" moves stuff "..text.." at "..minetest.pos_to_string(pos))
end
2015-04-18 15:05:26 +02:00
-- 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
2015-04-18 11:39:01 +02:00
chest.legacy_facedir_simple = nil
chest.after_place_node = nil
chest.on_receive_fields = nil
2015-04-18 15:05:26 +02:00
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},
2014-06-02 20:38:58 +02:00
},
}
2016-07-07 11:55:29 +02:00
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)
2014-06-02 20:38:58 +02:00
local chest_locked = {}
local origdef = minetest.registered_nodes["default:chest_locked"]
for i in pairs(origdef) do
chest_locked[i] = rawget(origdef, i)
end
2015-04-18 11:39:01 +02:00
chest_locked.description = nil
chest_locked.legacy_facedir_simple = nil
chest_locked.after_place_node = nil
chest_locked.on_construct = nil
chest_locked.on_receive_fields = nil
2015-04-18 15:05:26 +02:00
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"}
2015-04-18 11:39:01 +02:00
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},
},
}
2016-07-07 11:55:29 +02:00
chest_locked.on_construct = return_add_next"connected_chests:chest_locked_right"
chest_locked.after_destruct = return_remove_next"connected_chests:chest_locked_right"
2015-04-18 11:39:01 +02:00
chest_locked.on_metadata_inventory_move = function(pos, _, _, _, _, _, player)
log_access(pos, player, "in a big locked chest")
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)
2016-03-28 12:53:12 +02:00
local pname = clicker:get_player_name()
2016-07-07 11:55:29 +02:00
if pname == meta:get_string"owner"
or pname == minetest.setting_get"name" then
2015-04-18 11:39:01 +02:00
minetest.show_formspec(
2016-03-28 12:53:12 +02:00
pname,
2015-04-18 11:39:01 +02:00
"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;]"
)
2014-06-02 20:38:58 +02:00
end
end
2015-04-18 11:39:01 +02:00
minetest.register_node("connected_chests:chest_locked_left", chest_locked)
2014-06-02 21:04:06 +02:00
local tube_to_left, tube_to_left_locked, tube_update, tube_groups
2016-07-07 11:55:29 +02:00
if minetest.global_exists"pipeworks" then
tube_to_left_locked = {
insert_object = function(pos, node, stack)
2016-07-07 11:55:29 +02:00
local x, z = unpack(param_tab2[node.param2])
return minetest.get_meta{x=pos.x+x, y=pos.y, z=pos.z+z}:get_inventory():add_item("main", stack)
end,
can_insert = function(pos, node, stack)
2016-07-07 11:55:29 +02:00
local x, z = unpack(param_tab2[node.param2])
return minetest.get_meta{x=pos.x+x, y=pos.y, z=pos.z+z}:get_inventory():room_for_item("main", stack)
end,
connect_sides = {right = 1, back = 1, front = 1, bottom = 1, top = 1}
}
tube_to_left = table.copy(tube_to_left_locked)
tube_to_left.input_inventory = "main"
tube_update = pipeworks.scan_for_tube_objects
tube_groups = {tubedevice=1, tubedevice_receiver=1}
else
function tube_update() end
end
2014-06-02 21:04:06 +02:00
minetest.register_node("connected_chests:chest_right", {
2015-04-18 15:05:26 +02:00
tiles = {top_texture.."^[transformFX", top_texture.."^[transformFX", "default_chest_side.png",
"default_obsidian_glass.png", side_texture, side_texture.."^connected_chests_front.png^[transformFX"},
2014-06-02 21:04:06 +02:00
paramtype2 = "facedir",
drop = "",
pointable = false,
2015-04-16 20:42:17 +02:00
diggable = false,
2015-04-18 15:45:11 +02:00
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
2016-07-07 11:55:29 +02:00
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}
2015-04-18 15:45:11 +02:00
if node_left.name ~= "connected_chests:chest_left"
or node_left.param2 ~= node.param2 then
minetest.remove_node(pos)
return
2015-04-18 15:45:11 +02:00
end
tube_update(pos)
2015-04-18 15:45:11 +02:00
end,
after_destruct = function(pos, oldnode)
if oldnode.param2 > 3 then
return
end
2016-07-07 11:55:29 +02:00
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}
2015-04-18 15:45:11 +02:00
if node_left.name == "connected_chests:chest_left"
and node_left.param2 == oldnode.param2
and minetest.get_node(pos).name == "air" then
minetest.set_node(pos, oldnode)
return
2015-04-18 15:45:11 +02:00
end
tube_update(pos)
end,
tube = tube_to_left,
groups = tube_groups,
2014-06-02 21:04:06 +02:00
})
2014-06-02 20:38:58 +02:00
2014-06-02 21:04:06 +02:00
minetest.register_node("connected_chests:chest_locked_right", {
2015-04-18 15:05:26 +02:00
tiles = {top_texture.."^[transformFX", top_texture.."^[transformFX", "default_chest_side.png",
"default_obsidian_glass.png", side_texture, side_texture.."^connected_chests_front.png^connected_chests_lock.png^[transformFX"},
2014-06-02 21:04:06 +02:00
paramtype2 = "facedir",
drop = "",
pointable = false,
2015-04-16 20:42:17 +02:00
diggable = false,
2015-04-18 15:45:11 +02:00
on_construct = function(pos)
local node = minetest.get_node(pos)
if node.param2 > 3 then
node.param2 = node.param2%4
-- ↓ calls the on_construct from the beginning again
2015-04-18 15:45:11 +02:00
minetest.set_node(pos, node)
return
end
2016-07-07 11:55:29 +02:00
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}
2015-04-18 15:45:11 +02:00
if node_left.name ~= "connected_chests:chest_locked_left"
or node_left.param2 ~= node.param2 then
minetest.remove_node(pos)
return
2015-04-18 15:45:11 +02:00
end
tube_update(pos)
2015-04-18 15:45:11 +02:00
end,
after_destruct = function(pos, oldnode)
if oldnode.param2 > 3 then
return
end
2016-07-07 11:55:29 +02:00
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}
2015-04-18 15:45:11 +02:00
if node_left.name == "connected_chests:chest_locked_left"
and node_left.param2 == oldnode.param2
and minetest.get_node(pos).name == "air" then
minetest.set_node(pos, oldnode)
return
2015-04-18 15:45:11 +02:00
end
tube_update(pos)
end,
tube = tube_to_left_locked,
groups = tube_groups,
2014-06-02 21:04:06 +02:00
})
2014-06-02 20:38:58 +02:00
2015-04-18 15:05:26 +02:00
-- abms to fix half chests
2016-07-07 11:55:29 +02:00
for _,i in pairs{"chest", "chest_locked"} do
minetest.register_abm{
2014-06-04 18:37:17 +02:00
nodenames = {"connected_chests:"..i.."_right"},
2015-04-18 15:45:11 +02:00
interval = 10,
2014-06-04 18:37:17 +02:00
chance = 1,
2016-07-07 11:55:29 +02:00
action = function(pos, node)
if node.param2 > 3 then
2015-04-18 15:45:11 +02:00
node.param2 = node.param2%4
minetest.set_node(pos, node)
return
end
2016-07-07 11:55:29 +02:00
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}
2015-04-18 15:45:11 +02:00
if left_node.name ~= "connected_chests:"..i.."_left"
or left_node.param2 ~= node.param2 then
2014-06-04 18:37:17 +02:00
minetest.remove_node(pos)
end
end,
2016-07-07 11:55:29 +02:00
}
minetest.register_abm{
2014-06-04 18:37:17 +02:00
nodenames = {"connected_chests:"..i.."_left"},
interval = 3,
chance = 1,
2015-04-18 15:45:11 +02:00
action = return_add_next("connected_chests:"..i.."_right"),
2016-07-07 11:55:29 +02:00
}
2014-06-04 18:37:17 +02:00
end
2016-07-07 11:55:29 +02:00
local time = (minetest.get_us_time() - load_time_start) / 1000000
local msg = "[connected_chests] loaded after ca. " .. time .. " seconds."
if time > 0.01 then
2015-03-21 20:19:32 +01:00
print(msg)
else
minetest.log("info", msg)
end