Skip to content

Commit

Permalink
Shared drawers: new feature
Browse files Browse the repository at this point in the history
  • Loading branch information
mazes-80 committed Feb 18, 2023
1 parent ef16d4c commit ead1eb5
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 6 deletions.
39 changes: 36 additions & 3 deletions lua/api.lua
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,8 @@ else
x_offset = 0.5
end

drawers.drawer_formspec = "size[9,7]" ..
local drawers_formspec_data = {}
local drawer_formspec = "size[9,7]" ..
"list[context;upgrades;2,0.5;5,1;]" ..
"list[current_player;main;" .. x_offset ..
",3;" .. formspec_size .. ";]" ..
Expand All @@ -56,6 +57,18 @@ drawers.drawer_formspec = "size[9,7]" ..
drawers.gui_slots ..
drawers.get_upgrade_slots_bg(2, 0.5)

drawers.get_drawer_formspec = function(pos)
local shared
if minetest.get_meta(pos):get_string("shared") == "true" then
shared = "true"
else
shared = "false"
end

return drawer_formspec ..
"checkbox[3.5,2;shared;" .. S("Shared") .. ";" .. shared .. "]"
end

-- construct drawer
function drawers.drawer_on_construct(pos)
local node = core.get_node(pos)
Expand Down Expand Up @@ -90,9 +103,18 @@ function drawers.drawer_on_construct(pos)

-- create drawer upgrade inventory
meta:get_inventory():set_size("upgrades", 5)
end

-- set the formspec
meta:set_string("formspec", drawers.drawer_formspec)
-- show formspec on right click
drawers.drawer_on_rightclick = function(pos, node, clicker, itemstack, pointed_thing)
if not clicker:is_player() then
return
end
local pname = clicker:get_player_name()
if not minetest.is_protected(pos, pname) then
drawers_formspec_data[pname] = pos
minetest.show_formspec(pname, "drawers:drawer", drawers.get_drawer_formspec(pos))
end
end

-- destruct drawer
Expand Down Expand Up @@ -303,6 +325,7 @@ function drawers.register_drawer(name, def)

-- events
def.on_construct = drawers.drawer_on_construct
def.on_rightclick = drawers.drawer_on_rightclick
def.on_destruct = drawers.drawer_on_destruct
def.on_dig = drawers.drawer_on_dig
def.allow_metadata_inventory_put = drawers.drawer_allow_metadata_inventory_put
Expand Down Expand Up @@ -436,3 +459,13 @@ function drawers.register_drawer_upgrade(name, def)
end
end

minetest.register_on_player_receive_fields(function(player, formname, fields)
if formname ~= "drawers:drawer" then return end
local pname = player:get_player_name()
local pos = drawers_formspec_data[pname]
if pos and fields.shared then
local meta = minetest.get_meta(pos)
meta:set_string("shared", fields.shared)
end
drawers_formspec_data[pname] = nil
end)
8 changes: 5 additions & 3 deletions lua/visual.lua
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,8 @@ core.register_entity("drawers:visual", {
end,

on_rightclick = function(self, clicker)
if core.is_protected(self.drawer_pos, clicker:get_player_name()) then
if core.is_protected(self.drawer_pos, clicker:get_player_name()) and
minetest.get_meta(self.drawer_pos):get_string("shared") ~= "true" then
core.record_protection_violation(self.drawer_pos, clicker:get_player_name())
return
end
Expand Down Expand Up @@ -214,7 +215,8 @@ core.register_entity("drawers:visual", {
return
end
local add_stack = not puncher:get_player_control().sneak
if core.is_protected(self.drawer_pos, puncher:get_player_name()) then
if core.is_protected(self.drawer_pos, puncher:get_player_name()) and
minetest.get_meta(self.drawer_pos):get_string("shared") ~= "true" then
core.record_protection_violation(self.drawer_pos, puncher:get_player_name())
return
end
Expand Down Expand Up @@ -437,7 +439,7 @@ core.register_lbm({
-- create drawer upgrade inventory
meta:get_inventory():set_size("upgrades", 5)
-- set the formspec
meta:set_string("formspec", drawers.drawer_formspec)
meta:set_string("formspec", "")

-- count the drawer visuals
local drawerType = core.registered_nodes[node.name].groups.drawer
Expand Down

0 comments on commit ead1eb5

Please sign in to comment.