VoxeLibre/mods/ITEMS/mcl_barrels/init.lua

231 lines
7.5 KiB
Lua
Raw Permalink Normal View History

local S = minetest.get_translator("mcl_barrels")
2022-01-20 09:22:17 +01:00
local F = minetest.formspec_escape
local C = minetest.colorize
--TODO: fix barrel rotation placement
2022-01-20 09:22:17 +01:00
local open_barrels = {}
local drop_content = mcl_util.drop_items_from_meta_container("main")
2022-01-20 09:22:17 +01:00
2022-09-10 21:51:40 +02:00
---@param pos Vector
2022-01-20 09:22:17 +01:00
local function on_blast(pos)
local node = minetest.get_node(pos)
drop_content(pos, node)
minetest.remove_node(pos)
end
2022-03-25 13:28:33 +01:00
-- Simple protection checking functions
local function protection_check_move(pos, from_list, from_index, to_list, to_index, count, player)
local name = player:get_player_name()
if minetest.is_protected(pos, name) then
minetest.record_protection_violation(pos, name)
return 0
else
return count
end
end
local function protection_check_put_take(pos, listname, index, stack, player)
local name = player:get_player_name()
if minetest.is_protected(pos, name) then
minetest.record_protection_violation(pos, name)
return 0
else
return stack:get_count()
end
end
2022-01-20 09:22:17 +01:00
local function barrel_open(pos, node, clicker)
local name = minetest.get_meta(pos):get_string("name")
if name == "" then
name = S("Barrel")
end
local playername = clicker:get_player_name()
minetest.show_formspec(playername,
2022-09-10 21:51:40 +02:00
"mcl_barrels:barrel_" .. pos.x .. "_" .. pos.y .. "_" .. pos.z,
2022-01-20 09:22:17 +01:00
table.concat({
2022-09-10 21:51:40 +02:00
"formspec_version[4]",
"size[11.75,10.425]",
"label[0.375,0.375;" .. F(C(mcl_formspec.label_color, name)) .. "]",
mcl_formspec.get_itemslot_bg_v4(0.375, 0.75, 9, 3),
"list[nodemeta:" .. pos.x .. "," .. pos.y .. "," .. pos.z .. ";main;0.375,0.75;9,3;]",
"label[0.375,4.7;" .. F(C(mcl_formspec.label_color, S("Inventory"))) .. "]",
mcl_formspec.get_itemslot_bg_v4(0.375, 5.1, 9, 3),
"list[current_player;main;0.375,5.1;9,3;9]",
mcl_formspec.get_itemslot_bg_v4(0.375, 9.05, 9, 1),
"list[current_player;main;0.375,9.05;9,1;]",
"listring[nodemeta:" .. pos.x .. "," .. pos.y .. "," .. pos.z .. ";main]",
2022-01-20 09:22:17 +01:00
"listring[current_player;main]",
})
)
2022-09-10 21:51:40 +02:00
minetest.swap_node(pos, { name = "mcl_barrels:barrel_open", param2 = node.param2 })
2022-01-20 09:22:17 +01:00
open_barrels[playername] = pos
minetest.sound_play({name="mcl_barrels_default_barrel_open", gain=0.5}, {
pos = pos,
max_hear_distance = 16,
}, true)
2022-01-20 09:22:17 +01:00
end
2022-09-10 21:51:40 +02:00
---@param pos Vector
2022-01-20 09:22:17 +01:00
local function close_forms(pos)
local players = minetest.get_connected_players()
2022-09-10 21:51:40 +02:00
local formname = "mcl_barrels:barrel_" .. pos.x .. "_" .. pos.y .. "_" .. pos.z
2022-01-20 09:22:17 +01:00
for p = 1, #players do
if vector.distance(players[p]:get_pos(), pos) <= 30 then
minetest.close_formspec(players[p]:get_player_name(), formname)
end
end
end
2022-09-10 21:51:40 +02:00
---@param pos Vector
2022-01-20 09:22:17 +01:00
local function update_after_close(pos)
local node = minetest.get_node_or_nil(pos)
if not node then return end
if node.name == "mcl_barrels:barrel_open" then
2022-09-10 21:51:40 +02:00
minetest.swap_node(pos, { name = "mcl_barrels:barrel_closed", param2 = node.param2 })
minetest.sound_play({name="mcl_barrels_default_barrel_close", gain=0.5}, {
pos = pos,
max_hear_distance = 16,
}, true)
2022-01-20 09:22:17 +01:00
end
end
2022-09-10 21:51:40 +02:00
---@param player ObjectRef
2022-01-20 09:22:17 +01:00
local function close_barrel(player)
local name = player:get_player_name()
local open = open_barrels[name]
if open == nil then
return
end
update_after_close(open)
open_barrels[name] = nil
end
minetest.register_node("mcl_barrels:barrel_closed", {
2022-01-20 09:22:17 +01:00
description = S("Barrel"),
2022-02-06 22:55:09 +01:00
_tt_help = S("27 inventory slots"),
2022-02-06 19:04:13 +01:00
_doc_items_longdesc = S("Barrels are containers which provide 27 inventory slots."),
_doc_items_usagehelp = S("To access its inventory, rightclick it. When broken, the items will drop out."),
2022-09-10 21:51:40 +02:00
tiles = { "mcl_barrels_barrel_top.png^[transformR270", "mcl_barrels_barrel_bottom.png", "mcl_barrels_barrel_side.png" },
2022-01-20 09:22:17 +01:00
paramtype = "light",
paramtype2 = "facedir",
on_place = function(itemstack, placer, pointed_thing)
2022-09-10 21:51:40 +02:00
minetest.rotate_and_place(itemstack, placer, pointed_thing,
minetest.is_creative_enabled(placer:get_player_name()), {}
, false)
2022-01-20 09:22:17 +01:00
return itemstack
end,
stack_max = 64,
sounds = mcl_sounds.node_sound_wood_defaults(),
2022-09-10 21:51:40 +02:00
groups = { handy = 1, axey = 1, container = 2, material_wood = 1, flammable = -1, deco_block = 1 },
2022-01-20 09:22:17 +01:00
on_construct = function(pos)
local meta = minetest.get_meta(pos)
local inv = meta:get_inventory()
2022-09-10 21:51:40 +02:00
inv:set_size("main", 9 * 3)
2022-01-20 09:22:17 +01:00
end,
after_place_node = function(pos, placer, itemstack, pointed_thing)
minetest.get_meta(pos):set_string("name", itemstack:get_meta():get_string("name"))
end,
2022-03-25 13:28:33 +01:00
allow_metadata_inventory_move = protection_check_move,
allow_metadata_inventory_take = protection_check_put_take,
allow_metadata_inventory_put = protection_check_put_take,
on_metadata_inventory_move = function(pos, from_list, from_index, to_list, to_index, count, player)
2022-09-10 21:51:40 +02:00
minetest.log("action", player:get_player_name() ..
" moves stuff in barrel at " .. minetest.pos_to_string(pos))
2022-03-25 13:28:33 +01:00
end,
on_metadata_inventory_put = function(pos, listname, index, stack, player)
2022-09-10 21:51:40 +02:00
minetest.log("action", player:get_player_name() ..
" moves stuff to barrel at " .. minetest.pos_to_string(pos))
2022-03-25 13:28:33 +01:00
end,
on_metadata_inventory_take = function(pos, listname, index, stack, player)
2022-09-10 21:51:40 +02:00
minetest.log("action", player:get_player_name() ..
" takes stuff from barrel at " .. minetest.pos_to_string(pos))
2022-03-25 13:28:33 +01:00
end,
2022-01-20 09:22:17 +01:00
after_dig_node = drop_content,
on_blast = on_blast,
on_rightclick = barrel_open,
on_destruct = close_forms,
_mcl_blast_resistance = 2.5,
_mcl_hardness = 2.5,
})
minetest.register_node("mcl_barrels:barrel_open", {
2022-01-20 09:22:17 +01:00
description = S("Barrel Open"),
2022-02-06 22:55:09 +01:00
_tt_help = S("27 inventory slots"),
2022-02-06 19:04:13 +01:00
_doc_items_longdesc = S("Barrels are containers which provide 27 inventory slots."),
_doc_items_usagehelp = S("To access its inventory, rightclick it. When broken, the items will drop out."),
2022-01-20 09:22:17 +01:00
_doc_items_create_entry = false,
2022-09-10 21:51:40 +02:00
tiles = { "mcl_barrels_barrel_top_open.png", "mcl_barrels_barrel_bottom.png", "mcl_barrels_barrel_side.png" },
2022-01-20 09:22:17 +01:00
paramtype = "light",
paramtype2 = "facedir",
drop = "mcl_barrels:barrel_closed",
2022-01-20 09:22:17 +01:00
stack_max = 64,
sounds = mcl_sounds.node_sound_wood_defaults(),
2022-09-10 21:51:40 +02:00
groups = {
handy = 1,
axey = 1,
container = 2,
material_wood = 1,
flammable = -1,
deco_block = 1,
not_in_creative_inventory = 1
},
2022-03-25 13:28:33 +01:00
allow_metadata_inventory_move = protection_check_move,
allow_metadata_inventory_take = protection_check_put_take,
allow_metadata_inventory_put = protection_check_put_take,
on_metadata_inventory_move = function(pos, from_list, from_index, to_list, to_index, count, player)
2022-09-10 21:51:40 +02:00
minetest.log("action", player:get_player_name() ..
" moves stuff in barrel at " .. minetest.pos_to_string(pos))
2022-03-25 13:28:33 +01:00
end,
on_metadata_inventory_put = function(pos, listname, index, stack, player)
2022-09-10 21:51:40 +02:00
minetest.log("action", player:get_player_name() ..
" moves stuff to barrel at " .. minetest.pos_to_string(pos))
2022-03-25 13:28:33 +01:00
end,
on_metadata_inventory_take = function(pos, listname, index, stack, player)
2022-09-10 21:51:40 +02:00
minetest.log("action", player:get_player_name() ..
" takes stuff from barrel at " .. minetest.pos_to_string(pos))
2022-03-25 13:28:33 +01:00
end,
2022-01-20 09:22:17 +01:00
after_dig_node = drop_content,
on_blast = on_blast,
on_rightclick = barrel_open,
on_destruct = close_forms,
_mcl_blast_resistance = 2.5,
_mcl_hardness = 2.5,
})
minetest.register_on_player_receive_fields(function(player, formname, fields)
if formname:find("mcl_barrels:") == 1 and fields.quit then
2022-01-20 09:22:17 +01:00
close_barrel(player)
end
end)
minetest.register_on_leaveplayer(function(player)
close_barrel(player)
2022-01-20 09:40:15 +01:00
end)
--Minecraft Java Edition craft
minetest.register_craft({
output = "mcl_barrels:barrel_closed",
2022-01-20 09:40:15 +01:00
recipe = {
2022-09-10 21:51:40 +02:00
{ "group:wood", "group:wood_slab", "group:wood" },
{ "group:wood", "", "group:wood" },
{ "group:wood", "group:wood_slab", "group:wood" },
},
})
minetest.register_craft({
type = "fuel",
recipe = "mcl_barrels:barrel_closed",
burntime = 15,
})