2017-01-29 15:12:02 -08:00
|
|
|
|
|
|
|
local S = homedecor_i18n.gettext
|
2015-08-19 07:14:17 -07:00
|
|
|
|
|
|
|
local default_can_dig = function(pos,player)
|
|
|
|
local meta = minetest.get_meta(pos)
|
|
|
|
return meta:get_inventory():is_empty("main")
|
|
|
|
end
|
|
|
|
|
|
|
|
local background = default.gui_bg .. default.gui_bg_img .. default.gui_slots
|
|
|
|
local default_inventory_formspecs = {
|
|
|
|
["4"]="size[8,6]".. background ..
|
2016-08-27 12:10:43 -07:00
|
|
|
"list[context;main;2,0;4,1;]" ..
|
|
|
|
"list[current_player;main;0,2;8,4;]" ..
|
|
|
|
"listring[]",
|
2015-08-19 07:14:17 -07:00
|
|
|
|
|
|
|
["6"]="size[8,6]".. background ..
|
|
|
|
"list[context;main;1,0;6,1;]"..
|
2016-08-27 12:10:43 -07:00
|
|
|
"list[current_player;main;0,2;8,4;]" ..
|
|
|
|
"listring[]",
|
2015-08-19 07:14:17 -07:00
|
|
|
|
|
|
|
["8"]="size[8,6]".. background ..
|
|
|
|
"list[context;main;0,0;8,1;]"..
|
2016-08-27 12:10:43 -07:00
|
|
|
"list[current_player;main;0,2;8,4;]" ..
|
|
|
|
"listring[]",
|
2015-08-19 07:14:17 -07:00
|
|
|
|
|
|
|
["12"]="size[8,7]".. background ..
|
|
|
|
"list[context;main;1,0;6,2;]"..
|
2016-08-27 12:10:43 -07:00
|
|
|
"list[current_player;main;0,3;8,4;]" ..
|
|
|
|
"listring[]",
|
2015-08-19 07:14:17 -07:00
|
|
|
|
|
|
|
["16"]="size[8,7]".. background ..
|
|
|
|
"list[context;main;0,0;8,2;]"..
|
2016-08-27 12:10:43 -07:00
|
|
|
"list[current_player;main;0,3;8,4;]" ..
|
|
|
|
"listring[]",
|
2015-08-19 07:14:17 -07:00
|
|
|
|
|
|
|
["24"]="size[8,8]".. background ..
|
|
|
|
"list[context;main;0,0;8,3;]"..
|
2016-08-27 12:10:43 -07:00
|
|
|
"list[current_player;main;0,4;8,4;]" ..
|
|
|
|
"listring[]",
|
2015-08-19 07:14:17 -07:00
|
|
|
|
|
|
|
["32"]="size[8,9]".. background ..
|
|
|
|
"list[context;main;0,0.3;8,4;]"..
|
|
|
|
"list[current_player;main;0,4.85;8,1;]"..
|
|
|
|
"list[current_player;main;0,6.08;8,3;8]"..
|
2016-08-27 12:10:43 -07:00
|
|
|
"listring[context;main]" ..
|
|
|
|
"listring[current_player;main]" ..
|
2015-08-19 07:14:17 -07:00
|
|
|
default.get_hotbar_bg(0,4.85),
|
|
|
|
|
|
|
|
["50"]="size[10,10]".. background ..
|
|
|
|
"list[context;main;0,0;10,5;]"..
|
2016-08-27 12:10:43 -07:00
|
|
|
"list[current_player;main;1,6;8,4;]" ..
|
|
|
|
"listring[]",
|
2015-08-19 07:14:17 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
local function get_formspec_by_size(size)
|
|
|
|
--TODO heuristic to use the "next best size"
|
|
|
|
local formspec = default_inventory_formspecs[tostring(size)]
|
|
|
|
return formspec or default_inventory_formspecs
|
|
|
|
end
|
|
|
|
|
|
|
|
----
|
|
|
|
-- handle inventory setting
|
|
|
|
-- inventory = {
|
2015-08-19 09:00:45 -07:00
|
|
|
-- size = 16,
|
|
|
|
-- formspec = …,
|
2015-08-19 10:30:03 -07:00
|
|
|
-- locked = false,
|
|
|
|
-- lockable = true,
|
2015-08-19 07:14:17 -07:00
|
|
|
-- }
|
|
|
|
--
|
2015-08-19 09:47:24 -07:00
|
|
|
function homedecor.handle_inventory(name, def, original_def)
|
2015-08-19 07:14:17 -07:00
|
|
|
local inventory = def.inventory
|
|
|
|
if not inventory then return end
|
|
|
|
def.inventory = nil
|
|
|
|
|
2015-08-19 13:02:04 -07:00
|
|
|
if inventory.size then
|
|
|
|
local on_construct = def.on_construct
|
|
|
|
def.on_construct = function(pos)
|
|
|
|
local size = inventory.size
|
|
|
|
local meta = minetest.get_meta(pos)
|
|
|
|
meta:get_inventory():set_size("main", size)
|
|
|
|
meta:set_string("formspec", inventory.formspec or get_formspec_by_size(size))
|
|
|
|
if on_construct then on_construct(pos) end
|
|
|
|
end
|
2015-08-19 07:14:17 -07:00
|
|
|
end
|
|
|
|
|
|
|
|
def.can_dig = def.can_dig or default_can_dig
|
|
|
|
def.on_metadata_inventory_move = def.on_metadata_inventory_move or function(pos, from_list, from_index, to_list, to_index, count, player)
|
2017-01-24 12:43:20 -08:00
|
|
|
minetest.log("action", S("@1 moves stuff in @2 at @3",
|
2015-08-19 07:14:17 -07:00
|
|
|
player:get_player_name(), name, minetest.pos_to_string(pos)
|
|
|
|
))
|
|
|
|
end
|
|
|
|
def.on_metadata_inventory_put = def.on_metadata_inventory_put or function(pos, listname, index, stack, player)
|
2017-01-24 12:43:20 -08:00
|
|
|
minetest.log("action", S("@1 moves @2 to @3 at @4",
|
2016-05-05 01:18:12 -07:00
|
|
|
player:get_player_name(), stack:get_name(), name, minetest.pos_to_string(pos)
|
2015-08-19 07:14:17 -07:00
|
|
|
))
|
|
|
|
end
|
|
|
|
def.on_metadata_inventory_take = def.on_metadata_inventory_take or function(pos, listname, index, stack, player)
|
2017-01-24 12:43:20 -08:00
|
|
|
minetest.log("action", S("@1 takes @2 from @3 at @4",
|
2016-05-05 01:18:12 -07:00
|
|
|
player:get_player_name(), stack:get_name(), name, minetest.pos_to_string(pos)
|
2015-08-19 07:14:17 -07:00
|
|
|
))
|
|
|
|
end
|
2015-08-19 09:00:45 -07:00
|
|
|
|
2015-08-19 10:30:03 -07:00
|
|
|
local locked = inventory.locked
|
2015-08-19 09:00:45 -07:00
|
|
|
if locked then
|
|
|
|
local after_place_node = def.after_place_node
|
|
|
|
def.after_place_node = function(pos, placer)
|
|
|
|
local meta = minetest.get_meta(pos)
|
|
|
|
local owner = placer:get_player_name() or ""
|
|
|
|
|
|
|
|
meta:set_string("owner", owner)
|
2017-01-24 12:43:20 -08:00
|
|
|
meta:set_string("infotext", S("@1 (owned by @2)", def.infotext or def.description, owner))
|
2015-08-19 09:00:45 -07:00
|
|
|
return after_place_node and after_place_node(pos, placer)
|
|
|
|
end
|
|
|
|
|
|
|
|
local allow_move = def.allow_metadata_inventory_move
|
|
|
|
def.allow_metadata_inventory_move = function(pos, from_list, from_index, to_list, to_index, count, player)
|
|
|
|
local meta = minetest.get_meta(pos)
|
|
|
|
local owner = meta:get_string("owner")
|
|
|
|
local playername = player:get_player_name()
|
|
|
|
|
2016-12-31 18:47:09 -08:00
|
|
|
if playername == owner or
|
2017-02-27 12:31:21 -08:00
|
|
|
minetest.check_player_privs(playername, "protection_bypass") then
|
2016-12-31 18:47:09 -08:00
|
|
|
return allow_move and
|
|
|
|
allow_move(pos, from_list, from_index, to_list, to_index, count, player) or
|
|
|
|
count
|
2015-08-19 09:00:45 -07:00
|
|
|
end
|
|
|
|
|
2017-01-24 12:43:20 -08:00
|
|
|
minetest.log("action", S("@1 tried to access a @2 belonging to @3 at @4",
|
2016-12-31 18:47:09 -08:00
|
|
|
playername, name, owner, minetest.pos_to_string(pos)
|
|
|
|
))
|
|
|
|
return 0
|
2015-08-19 09:00:45 -07:00
|
|
|
end
|
|
|
|
|
|
|
|
local allow_put = def.allow_metadata_inventory_put
|
|
|
|
def.allow_metadata_inventory_put = function(pos, listname, index, stack, player)
|
|
|
|
local meta = minetest.get_meta(pos)
|
|
|
|
local owner = meta:get_string("owner")
|
|
|
|
local playername = player:get_player_name()
|
|
|
|
|
2016-12-31 18:47:09 -08:00
|
|
|
if playername == owner or
|
2017-02-27 12:31:21 -08:00
|
|
|
minetest.check_player_privs(playername, "protection_bypass") then
|
2016-12-31 18:47:09 -08:00
|
|
|
return allow_put and allow_put(pos, listname, index, stack, player) or
|
|
|
|
stack:get_count()
|
2015-08-19 09:00:45 -07:00
|
|
|
end
|
2016-12-31 18:47:09 -08:00
|
|
|
|
2017-01-24 12:43:20 -08:00
|
|
|
minetest.log("action", S("@1 tried to access a @2 belonging to @3 at @4",
|
2016-12-31 18:47:09 -08:00
|
|
|
playername, name, owner, minetest.pos_to_string(pos)
|
|
|
|
))
|
|
|
|
return 0
|
2015-08-19 09:00:45 -07:00
|
|
|
end
|
|
|
|
|
|
|
|
local allow_take = def.allow_metadata_inventory_take
|
|
|
|
def.allow_metadata_inventory_take = function(pos, listname, index, stack, player)
|
|
|
|
local meta = minetest.get_meta(pos)
|
|
|
|
local owner = meta:get_string("owner")
|
|
|
|
local playername = player:get_player_name()
|
|
|
|
|
2016-12-31 18:47:09 -08:00
|
|
|
if playername == owner or
|
2017-02-27 12:31:21 -08:00
|
|
|
minetest.check_player_privs(playername, "protection_bypass") then
|
2016-12-31 18:47:09 -08:00
|
|
|
return allow_take and allow_take(pos, listname, index, stack, player) or
|
|
|
|
stack:get_count()
|
2015-08-19 09:00:45 -07:00
|
|
|
end
|
2016-12-31 18:47:09 -08:00
|
|
|
|
2017-01-24 12:43:20 -08:00
|
|
|
minetest.log("action", S("@1 tried to access a @2 belonging to @3 at @4",
|
2016-12-31 18:47:09 -08:00
|
|
|
playername, name, owner, minetest.pos_to_string(pos)
|
|
|
|
))
|
|
|
|
return 0
|
2015-08-19 09:00:45 -07:00
|
|
|
end
|
|
|
|
end
|
2015-08-19 10:30:03 -07:00
|
|
|
|
|
|
|
local lockable = inventory.lockable
|
|
|
|
if lockable then
|
|
|
|
local locked_def = table.copy(original_def)
|
2017-01-24 12:43:20 -08:00
|
|
|
locked_def.description = S("@1 (Locked)", def.description or name)
|
2015-08-19 10:30:03 -07:00
|
|
|
|
|
|
|
local locked_inventory = locked_def.inventory
|
|
|
|
locked_inventory.locked = true
|
|
|
|
locked_inventory.lockable = nil -- avoid loops of locked locked stuff
|
|
|
|
|
|
|
|
local locked_name = name .. "_locked"
|
|
|
|
homedecor.register(locked_name, locked_def)
|
|
|
|
minetest.register_craft({
|
|
|
|
type = "shapeless",
|
|
|
|
output = "homedecor:" .. locked_name,
|
|
|
|
recipe = { "homedecor:" .. name, "default:steel_ingot" }
|
|
|
|
})
|
|
|
|
end
|
|
|
|
|
2015-08-19 07:14:17 -07:00
|
|
|
end
|