sama/container.lua

174 lines
5.4 KiB
Lua

local modpath, S = ...
sama.container = {}
sama.container.open_containers = {}
function sama.container.get_container_formspec(pos)
local spos = pos.x .. "," .. pos.y .. "," .. pos.z
local meta = minetest.get_meta(pos)
local christmas_msg = meta:get_string("christmas_msg")
if not(christmas_msg) or christmas_msg == "" then
christmas_msg = S("Merry Christmas")
end
local formspec =
"size[8,7]" ..
"image[0,0;1,1;sama_christmas_container_inv.png]"..
"label[1,0;"..christmas_msg.."]"..
"list[nodemeta:" .. spos .. ";main;2,1.3;4,1;]" ..
"list[current_player;main;0,2.85;8,1;]" ..
"list[current_player;main;0,4.08;8,3;8]" ..
"listring[nodemeta:" .. spos .. ";main]" ..
"listring[current_player;main]" ..
default.get_hotbar_bg(0,2.85)
return formspec
end
function sama.container.container_lid_close(pn)
local container_open_info = sama.container.open_containers[pn]
local pos = container_open_info.pos
local sound = container_open_info.sound
local swap = container_open_info.swap
sama.container.open_containers[pn] = nil
for k, v in pairs(sama.container.open_containers) do
if v.pos.x == pos.x and v.pos.y == pos.y and v.pos.z == pos.z then
return true
end
end
local node = minetest.get_node(pos)
minetest.after(0.2, minetest.swap_node, pos, { name = "sama:" .. swap,
param2 = node.param2 })
minetest.sound_play(sound, {gain = 0.3, pos = pos, max_hear_distance = 10})
end
minetest.register_on_leaveplayer(function(player)
local pn = player:get_player_name()
if sama.container.open_containers[pn] then
sama.container.container_lid_close(pn)
end
end)
function sama.container.container_lid_obstructed(pos, direction)
if direction = "above" then
pos = {x = pos.x, y = pos.y + 1, z = pos.z}
end
local def = minetest.registered_nodes[minetest.get_node(pos).name]
-- allow ladders, signs, wallmounted things and torches to not obstruct
if def and
(def.drawtype == "airlike" or
def.drawtype == "signlike" or
def.drawtype == "torchlike" or
(def.drawtype == "nodebox" and def.paramtype2 == "wallmounted")) then
return false
end
return true
end
minetest.register_on_player_receive_fields(function(player, formname, fields)
if formname ~= "sama:container" then
return
end
if not player or not fields.quit then
return
end
local pn = player:get_player_name()
if not sama.container.open_containers[pn] then
return
end
sama.container.container_lid_close(pn)
return true
end)
function sama.register_container(name, d)
local def = table.copy(d)
def.drawtype = "mesh"
def.visual = "mesh"
def.paramtype = "light"
def.paramtype2 = "facedir"
def.legacy_facedir_simple = true
def.is_ground_content = false
def.on_construct = function(pos)
local meta = minetest.get_meta(pos)
meta:set_string("infotext", d.description)
local inv = meta:get_inventory()
inv:set_size("main", 4*1)
end
def.can_dig = function(pos,player)
local meta = minetest.get_meta(pos);
local inv = meta:get_inventory()
return inv:is_empty("main")
end
def.on_rightclick = function(pos, node, clicker)
minetest.sound_play(def.sound_open, {gain = 0.3, pos = pos,
max_hear_distance = 10})
if not sama.container.container_lid_obstructed(pos, "above") then
minetest.swap_node(pos, {
name = "sama:" .. name .. "_open",
param2 = node.param2 })
end
minetest.after(0.2, minetest.show_formspec,
clicker:get_player_name(),
"sama:container", sama.container.get_container_formspec(pos))
sama.container.open_containers[clicker:get_player_name()] = { pos = pos, sound = def.sound_close, swap = name }
end
def.on_blast = function(pos)
local drops = {}
default.get_inventory_drops(pos, "main", drops)
drops[#drops+1] = "sama:" .. name
minetest.remove_node(pos)
return drops
end
def.on_metadata_inventory_move = function(pos, from_list, from_index,
to_list, to_index, count, player)
minetest.log("action", player:get_player_name() ..
" moves stuff in container at " .. minetest.pos_to_string(pos))
end
def.on_metadata_inventory_put = function(pos, listname, index, stack, player)
minetest.log("action", player:get_player_name() ..
" moves " .. stack:get_name() ..
" to container at " .. minetest.pos_to_string(pos))
end
def.on_metadata_inventory_take = function(pos, listname, index, stack, player)
minetest.log("action", player:get_player_name() ..
" takes " .. stack:get_name() ..
" from container at " .. minetest.pos_to_string(pos))
end
local def_opened = table.copy(def)
local def_closed = table.copy(def)
def_opened.mesh = "container_open.obj"
for i = 1, #def_opened.tiles do
if type(def_opened.tiles[i]) == "string" then
def_opened.tiles[i] = {name = def_opened.tiles[i], backface_culling = true}
elseif def_opened.tiles[i].backface_culling == nil then
def_opened.tiles[i].backface_culling = true
end
end
def_opened.drop = "sama:" .. name
def_opened.groups.not_in_creative_inventory = 1
def_opened.selection_box = {
type = "fixed",
fixed = { -1/2, -1/2, -1/2, 1/2, 3/16, 1/2 },
}
def_opened.can_dig = function()
return false
end
def_opened.on_blast = function() end
def_closed.mesh = nil
def_closed.drawtype = nil
def_closed.tiles[6] = def.tiles[5] -- swap textures around for "normal"
def_closed.tiles[5] = def.tiles[3] -- drawtype to make them match the mesh
def_closed.tiles[3] = def.tiles[3].."^[transformFX"
minetest.register_node("sama:" .. name, def_closed)
minetest.register_node("sama:" .. name .. "_open", def_opened)
end