1
0
mirror of https://github.com/HybridDog/connected_chests.git synced 2024-11-17 19:28:15 +01:00
connected_chests/init.lua

269 lines
8.1 KiB
Lua
Raw Normal View History

2014-06-02 20:38:58 +02:00
local load_time_start = os.clock()
2014-06-03 20:38:52 +02:00
local creative_enabled = minetest.setting_getbool("creative_mode")
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",
"size[13,9]"..
"list[current_name;main;0,0;13,5;]"..
"list[current_player;main;2.5,5.2;8,4;]"
)
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",
"size[13,9]"..
"list[current_name;main;0,0;13,5;]"..
"list[current_player;main;2.5,5.2;8,4;]"
)
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(pointed_thing, name)
2014-06-02 20:38:58 +02:00
if not pointed_thing then
return
end
local pu = minetest.get_pointed_thing_position(pointed_thing)
local pa = minetest.get_pointed_thing_position(pointed_thing, true)
if not (pu and pa) then
return
end
if pu.y ~= pa.y then
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,
}
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)
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
2014-06-03 20:38:52 +02:00
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 and placer:get_player_control().sneak) then
return place_chest(itemstack, placer, pointed_thing)
end
local protected = minetest.is_protected(pa, placer:get_player_name())
if protected then
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
2014-06-02 21:04:06 +02:00
local function remove_next(pos, oldnode)
local p1 = oldnode.param2
for p,param in pairs(param_tab) do
if param == p1 then
p1 = p
break
end
end
local x, z = unpack(string.split(p1, " "))
pos.x = pos.x-x
pos.z = pos.z-z
minetest.remove_node(pos)
end
2014-06-02 20:38:58 +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
2014-06-02 20:38:58 +02:00
minetest.register_node("connected_chests:chest_left", {
tiles = {"connected_chests_top.png", "connected_chests_top.png", "default_obsidian_glass.png",
"default_chest_side.png", "connected_chests_side.png^[transformFX", "connected_chests_side.png^connected_chests_front.png"},
paramtype2 = "facedir",
drop = "default:chest 2",
groups = {choppy=2,oddly_breakable_by_hand=2},
is_ground_content = false,
sounds = default.node_sound_wood_defaults(),
selection_box = {
type = "fixed",
fixed = {
{-0.5, -0.5, -0.5, 1.5, 0.5, 0.5},
},
},
can_dig = function(pos)
local meta = minetest.get_meta(pos);
local inv = meta:get_inventory()
return inv:is_empty("main")
end,
2014-06-02 21:04:06 +02:00
after_dig_node = remove_next,
2014-06-02 20:38:58 +02:00
on_metadata_inventory_move = function(pos, _, _, _, _, _, player)
2014-06-03 20:38:52 +02:00
log_access(pos, player, "in a big chest")
2014-06-02 20:38:58 +02:00
end,
on_metadata_inventory_put = function(pos, _, _, _, player)
2014-06-03 20:38:52 +02:00
log_access(pos, player, "to a big chest")
2014-06-02 20:38:58 +02:00
end,
on_metadata_inventory_take = function(pos, _, _, _, player)
2014-06-03 20:38:52 +02:00
log_access(pos, player, "from a big chest")
2014-06-02 20:38:58 +02:00
end,
})
local function has_locked_chest_privilege(meta, player)
if player:get_player_name() ~= meta:get_string("owner") then
return false
end
return true
end
2014-06-03 20:38:52 +02:00
local function access_allowed(pos, player, count)
local meta = minetest.get_meta(pos)
if not has_locked_chest_privilege(meta, player) then
minetest.log("action", player:get_player_name()..
" tried to access a big locked chest belonging to "..
meta:get_string("owner").." at "..
minetest.pos_to_string(pos))
return 0
end
return count
end
2014-06-02 21:04:06 +02:00
minetest.register_node("connected_chests:chest_locked_left", {
tiles = {"connected_chests_top.png", "connected_chests_top.png", "default_obsidian_glass.png",
"default_chest_side.png", "connected_chests_side.png^[transformFX", "connected_chests_side.png^connected_chests_lock.png"},
2014-06-02 20:38:58 +02:00
paramtype2 = "facedir",
2014-06-02 21:04:06 +02:00
drop = "default:chest_locked 2",
2014-06-02 20:38:58 +02:00
groups = {choppy=2,oddly_breakable_by_hand=2},
is_ground_content = false,
sounds = default.node_sound_wood_defaults(),
2014-06-02 21:04:06 +02:00
selection_box = {
type = "fixed",
fixed = {
{-0.5, -0.5, -0.5, 1.5, 0.5, 0.5},
},
},
2014-06-03 20:38:52 +02:00
--[[after_place_node = function(pos, placer)
2014-06-02 20:38:58 +02:00
local meta = minetest.get_meta(pos)
meta:set_string("owner", placer:get_player_name() or "")
meta:set_string("infotext", "Locked Chest (owned by "..
meta:get_string("owner")..")")
end,
2014-06-03 20:38:52 +02:00
on_construct = function(pos)
2014-06-02 20:38:58 +02:00
local meta = minetest.get_meta(pos)
meta:set_string("infotext", "Locked Chest")
meta:set_string("owner", "")
local inv = meta:get_inventory()
inv:set_size("main", 8*4)
2014-06-02 21:04:06 +02:00
end,]]
can_dig = function(pos, player)
2014-06-02 20:38:58 +02:00
local meta = minetest.get_meta(pos);
local inv = meta:get_inventory()
return inv:is_empty("main") and has_locked_chest_privilege(meta, player)
end,
2014-06-02 21:04:06 +02:00
after_dig_node = remove_next,
2014-06-03 20:38:52 +02:00
allow_metadata_inventory_move = function(pos, _, _, _, _, count, player)
return access_allowed(pos, player, count)
2014-06-02 20:38:58 +02:00
end,
2014-06-03 20:38:52 +02:00
allow_metadata_inventory_put = function(pos, _, _, stack, player)
return access_allowed(pos, player, stack:get_count())
2014-06-02 20:38:58 +02:00
end,
2014-06-03 20:38:52 +02:00
allow_metadata_inventory_take = function(pos, _, _, stack, player)
return access_allowed(pos, player, stack:get_count())
2014-06-02 20:38:58 +02:00
end,
2014-06-02 21:04:06 +02:00
on_metadata_inventory_move = function(pos, _, _, _, _, _, player)
2014-06-03 20:38:52 +02:00
log_access(pos, player, "in a big locked chest")
2014-06-02 20:38:58 +02:00
end,
2014-06-02 21:04:06 +02:00
on_metadata_inventory_put = function(pos, _, _, _, player)
2014-06-03 20:38:52 +02:00
log_access(pos, player, "to a big locked chest")
2014-06-02 20:38:58 +02:00
end,
2014-06-02 21:04:06 +02:00
on_metadata_inventory_take = function(pos, _, _, _, player)
2014-06-03 20:38:52 +02:00
log_access(pos, player, "from a big locked chest")
2014-06-02 20:38:58 +02:00
end,
2014-06-03 20:38:52 +02:00
on_rightclick = function(pos, _, clicker)
2014-06-02 20:38:58 +02:00
local meta = minetest.get_meta(pos)
if has_locked_chest_privilege(meta, clicker) then
minetest.show_formspec(
clicker:get_player_name(),
2014-06-03 20:38:52 +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,
2014-06-02 21:04:06 +02:00
})
minetest.register_node("connected_chests:chest_right", {
tiles = {"connected_chests_top.png^[transformFX", "connected_chests_top.png^[transformFX", "default_chest_side.png",
"default_obsidian_glass.png", "connected_chests_side.png", "connected_chests_side.png^connected_chests_front.png^[transformFX"},
paramtype2 = "facedir",
drop = "",
is_ground_content = false,
pointable = false,
can_dig = function()
return false
end,
})
2014-06-02 20:38:58 +02:00
2014-06-02 21:04:06 +02:00
minetest.register_node("connected_chests:chest_locked_right", {
tiles = {"connected_chests_top.png^[transformFX", "connected_chests_top.png^[transformFX", "default_chest_side.png",
"default_obsidian_glass.png", "connected_chests_side.png", "connected_chests_side.png^connected_chests_lock.png^[transformFX"},
paramtype2 = "facedir",
drop = "",
is_ground_content = false,
pointable = false,
can_dig = function()
return false
end,
})
2014-06-02 20:38:58 +02:00
print(string.format("[connected_chest] loaded after ca. %.2fs", os.clock() - load_time_start))