forked from mtcontrib/connected_chests
update big locked chest
This commit is contained in:
parent
4a46667e28
commit
0162d972a7
@ -1,4 +1,3 @@
|
|||||||
TODO:
|
TODO:
|
||||||
— finish adding a big locked chest
|
|
||||||
— maybe use [combine for front and lock
|
— maybe use [combine for front and lock
|
||||||
— get more stuff from the default chests
|
— get more stuff from the default chests (minetest.registered_nodes)
|
||||||
|
169
init.lua
169
init.lua
@ -1,6 +1,45 @@
|
|||||||
local load_time_start = os.clock()
|
local load_time_start = os.clock()
|
||||||
|
|
||||||
local function get_pointed_info(pointed_thing)
|
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)
|
||||||
if not pointed_thing then
|
if not pointed_thing then
|
||||||
return
|
return
|
||||||
end
|
end
|
||||||
@ -13,17 +52,12 @@ local function get_pointed_info(pointed_thing)
|
|||||||
return
|
return
|
||||||
end
|
end
|
||||||
local nd_u = minetest.get_node(pu)
|
local nd_u = minetest.get_node(pu)
|
||||||
if nd_u.name ~= "default:chest" then
|
if nd_u.name ~= name then
|
||||||
return
|
return
|
||||||
end
|
end
|
||||||
return pu, pa, nd_u.param2
|
return pu, pa, nd_u.param2
|
||||||
end
|
end
|
||||||
|
|
||||||
local big_chest_formspec =
|
|
||||||
"size[13,9]"..
|
|
||||||
"list[current_name;main;0,0;13,5;]"..
|
|
||||||
"list[current_player;main;2.5,5.2;8,4;]"
|
|
||||||
|
|
||||||
local param_tab = {
|
local param_tab = {
|
||||||
["-1 0"] = 0,
|
["-1 0"] = 0,
|
||||||
["1 0"] = 2,
|
["1 0"] = 2,
|
||||||
@ -33,44 +67,44 @@ local param_tab = {
|
|||||||
|
|
||||||
local pars = {[0]=2, 3, 0, 1}
|
local pars = {[0]=2, 3, 0, 1}
|
||||||
|
|
||||||
local function connect_chests(pu, pa, old_param2)
|
local function connect_chests(pu, pa, old_param2, name)
|
||||||
local stuff = minetest.get_meta(pu):get_inventory():get_list("main")
|
local oldmeta = minetest.get_meta(pu)
|
||||||
|
local stuff = oldmeta:get_inventory():get_list("main")
|
||||||
|
local owner = oldmeta:get_string("owner")
|
||||||
|
|
||||||
local par = param_tab[pu.x-pa.x.." "..pu.z-pa.z]
|
local par = param_tab[pu.x-pa.x.." "..pu.z-pa.z]
|
||||||
if param_tab[pa.x-pu.x.." "..pa.z-pu.z] == old_param2 then
|
local par_inverted = pars[par]
|
||||||
|
if old_param2 == par_inverted then
|
||||||
pu, pa = pa, pu
|
pu, pa = pa, pu
|
||||||
par = pars[par]
|
par = par_inverted
|
||||||
end
|
|
||||||
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_chest_formspec)
|
|
||||||
meta:set_string("infotext", "Big Chest")
|
|
||||||
local inv = meta:get_inventory()
|
|
||||||
inv:set_size("main", 65)
|
|
||||||
inv:set_list("main", stuff)
|
|
||||||
end
|
end
|
||||||
|
|
||||||
local place_chest = minetest.registered_nodes["default:chest"].on_place
|
chests[name](pu, pa, par, stuff, name, owner)
|
||||||
minetest.override_item("default:chest", {
|
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)
|
on_place = function(itemstack, placer, pointed_thing)
|
||||||
if not placer then
|
if not placer then
|
||||||
return
|
return
|
||||||
end
|
end
|
||||||
local pu, pa, nd_u = get_pointed_info(pointed_thing)
|
local pu, pa, par2 = get_pointed_info(pointed_thing, name)
|
||||||
if not pu then
|
if not (pu and placer:get_player_control().sneak) then
|
||||||
return place_chest(itemstack, placer, pointed_thing)
|
return place_chest(itemstack, placer, pointed_thing)
|
||||||
end
|
end
|
||||||
local protected = minetest.is_protected(pa, placer:get_player_name())
|
local protected = minetest.is_protected(pa, placer:get_player_name())
|
||||||
if protected then
|
if protected then
|
||||||
return
|
return
|
||||||
end
|
end
|
||||||
connect_chests(pu, pa, nd_u)
|
connect_chests(pu, pa, par2, name)
|
||||||
|
if not creative_enabled then
|
||||||
itemstack:take_item()
|
itemstack:take_item()
|
||||||
return itemstack
|
return itemstack
|
||||||
end
|
end
|
||||||
|
end
|
||||||
})
|
})
|
||||||
|
end
|
||||||
|
|
||||||
local function remove_next(pos, oldnode)
|
local function remove_next(pos, oldnode)
|
||||||
local p1 = oldnode.param2
|
local p1 = oldnode.param2
|
||||||
@ -86,6 +120,11 @@ local function remove_next(pos, oldnode)
|
|||||||
minetest.remove_node(pos)
|
minetest.remove_node(pos)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
local function log_access(pos, player, text)
|
||||||
|
minetest.log("action", player:get_player_name()..
|
||||||
|
" moves stuff "..text.." at "..minetest.pos_to_string(pos))
|
||||||
|
end
|
||||||
|
|
||||||
minetest.register_node("connected_chests:chest_left", {
|
minetest.register_node("connected_chests:chest_left", {
|
||||||
tiles = {"connected_chests_top.png", "connected_chests_top.png", "default_obsidian_glass.png",
|
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"},
|
"default_chest_side.png", "connected_chests_side.png^[transformFX", "connected_chests_side.png^connected_chests_front.png"},
|
||||||
@ -107,16 +146,13 @@ minetest.register_node("connected_chests:chest_left", {
|
|||||||
end,
|
end,
|
||||||
after_dig_node = remove_next,
|
after_dig_node = remove_next,
|
||||||
on_metadata_inventory_move = function(pos, _, _, _, _, _, player)
|
on_metadata_inventory_move = function(pos, _, _, _, _, _, player)
|
||||||
minetest.log("action", player:get_player_name()..
|
log_access(pos, player, "in a big chest")
|
||||||
" moves stuff in big chest at "..minetest.pos_to_string(pos))
|
|
||||||
end,
|
end,
|
||||||
on_metadata_inventory_put = function(pos, _, _, _, player)
|
on_metadata_inventory_put = function(pos, _, _, _, player)
|
||||||
minetest.log("action", player:get_player_name()..
|
log_access(pos, player, "to a big chest")
|
||||||
" moves stuff to big chest at "..minetest.pos_to_string(pos))
|
|
||||||
end,
|
end,
|
||||||
on_metadata_inventory_take = function(pos, _, _, _, player)
|
on_metadata_inventory_take = function(pos, _, _, _, player)
|
||||||
minetest.log("action", player:get_player_name()..
|
log_access(pos, player, "from a big chest")
|
||||||
" takes stuff from big chest at "..minetest.pos_to_string(pos))
|
|
||||||
end,
|
end,
|
||||||
})
|
})
|
||||||
|
|
||||||
@ -128,6 +164,18 @@ local function has_locked_chest_privilege(meta, player)
|
|||||||
return true
|
return true
|
||||||
end
|
end
|
||||||
|
|
||||||
|
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
|
||||||
|
|
||||||
minetest.register_node("connected_chests:chest_locked_left", {
|
minetest.register_node("connected_chests:chest_locked_left", {
|
||||||
tiles = {"connected_chests_top.png", "connected_chests_top.png", "default_obsidian_glass.png",
|
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"},
|
"default_chest_side.png", "connected_chests_side.png^[transformFX", "connected_chests_side.png^connected_chests_lock.png"},
|
||||||
@ -142,13 +190,13 @@ minetest.register_node("connected_chests:chest_locked_left", {
|
|||||||
{-0.5, -0.5, -0.5, 1.5, 0.5, 0.5},
|
{-0.5, -0.5, -0.5, 1.5, 0.5, 0.5},
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
after_place_node = function(pos, placer)
|
--[[after_place_node = function(pos, placer)
|
||||||
local meta = minetest.get_meta(pos)
|
local meta = minetest.get_meta(pos)
|
||||||
meta:set_string("owner", placer:get_player_name() or "")
|
meta:set_string("owner", placer:get_player_name() or "")
|
||||||
meta:set_string("infotext", "Locked Chest (owned by "..
|
meta:set_string("infotext", "Locked Chest (owned by "..
|
||||||
meta:get_string("owner")..")")
|
meta:get_string("owner")..")")
|
||||||
end,
|
end,
|
||||||
--[[on_construct = function(pos)
|
on_construct = function(pos)
|
||||||
local meta = minetest.get_meta(pos)
|
local meta = minetest.get_meta(pos)
|
||||||
meta:set_string("infotext", "Locked Chest")
|
meta:set_string("infotext", "Locked Chest")
|
||||||
meta:set_string("owner", "")
|
meta:set_string("owner", "")
|
||||||
@ -161,58 +209,33 @@ minetest.register_node("connected_chests:chest_locked_left", {
|
|||||||
return inv:is_empty("main") and has_locked_chest_privilege(meta, player)
|
return inv:is_empty("main") and has_locked_chest_privilege(meta, player)
|
||||||
end,
|
end,
|
||||||
after_dig_node = remove_next,
|
after_dig_node = remove_next,
|
||||||
allow_metadata_inventory_move = function(pos, from_list, from_index, to_list, to_index, count, player)
|
allow_metadata_inventory_move = function(pos, _, _, _, _, count, player)
|
||||||
local meta = minetest.get_meta(pos)
|
return access_allowed(pos, player, count)
|
||||||
if not has_locked_chest_privilege(meta, player) then
|
|
||||||
minetest.log("action", player:get_player_name()..
|
|
||||||
" tried to access a locked chest belonging to "..
|
|
||||||
meta:get_string("owner").." at "..
|
|
||||||
minetest.pos_to_string(pos))
|
|
||||||
return 0
|
|
||||||
end
|
|
||||||
return count
|
|
||||||
end,
|
end,
|
||||||
allow_metadata_inventory_put = function(pos, listname, index, stack, player)
|
allow_metadata_inventory_put = function(pos, _, _, stack, player)
|
||||||
local meta = minetest.get_meta(pos)
|
return access_allowed(pos, player, stack:get_count())
|
||||||
if not has_locked_chest_privilege(meta, player) then
|
|
||||||
minetest.log("action", player:get_player_name()..
|
|
||||||
" tried to access a locked chest belonging to "..
|
|
||||||
meta:get_string("owner").." at "..
|
|
||||||
minetest.pos_to_string(pos))
|
|
||||||
return 0
|
|
||||||
end
|
|
||||||
return stack:get_count()
|
|
||||||
end,
|
end,
|
||||||
allow_metadata_inventory_take = function(pos, listname, index, stack, player)
|
allow_metadata_inventory_take = function(pos, _, _, stack, player)
|
||||||
local meta = minetest.get_meta(pos)
|
return access_allowed(pos, player, stack:get_count())
|
||||||
if not has_locked_chest_privilege(meta, player) then
|
|
||||||
minetest.log("action", player:get_player_name()..
|
|
||||||
" tried to access a locked chest belonging to "..
|
|
||||||
meta:get_string("owner").." at "..
|
|
||||||
minetest.pos_to_string(pos))
|
|
||||||
return 0
|
|
||||||
end
|
|
||||||
return stack:get_count()
|
|
||||||
end,
|
end,
|
||||||
on_metadata_inventory_move = function(pos, _, _, _, _, _, player)
|
on_metadata_inventory_move = function(pos, _, _, _, _, _, player)
|
||||||
minetest.log("action", player:get_player_name()..
|
log_access(pos, player, "in a big locked chest")
|
||||||
" moves stuff in big locked chest at "..minetest.pos_to_string(pos))
|
|
||||||
end,
|
end,
|
||||||
on_metadata_inventory_put = function(pos, _, _, _, player)
|
on_metadata_inventory_put = function(pos, _, _, _, player)
|
||||||
minetest.log("action", player:get_player_name()..
|
log_access(pos, player, "to a big locked chest")
|
||||||
" moves stuff to big locked chest at "..minetest.pos_to_string(pos))
|
|
||||||
end,
|
end,
|
||||||
on_metadata_inventory_take = function(pos, _, _, _, player)
|
on_metadata_inventory_take = function(pos, _, _, _, player)
|
||||||
minetest.log("action", player:get_player_name()..
|
log_access(pos, player, "from a big locked chest")
|
||||||
" takes stuff from big locked chest at "..minetest.pos_to_string(pos))
|
|
||||||
end,
|
end,
|
||||||
on_rightclick = function(pos, node, clicker)
|
on_rightclick = function(pos, _, clicker)
|
||||||
local meta = minetest.get_meta(pos)
|
local meta = minetest.get_meta(pos)
|
||||||
if has_locked_chest_privilege(meta, clicker) then
|
if has_locked_chest_privilege(meta, clicker) then
|
||||||
minetest.show_formspec(
|
minetest.show_formspec(
|
||||||
clicker:get_player_name(),
|
clicker:get_player_name(),
|
||||||
"default:chest_locked",
|
"connected_chests:chest_locked_left",
|
||||||
default.get_locked_chest_formspec(pos)
|
"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
|
||||||
end,
|
end,
|
||||||
|
Loading…
Reference in New Issue
Block a user