commit ca2b20c7665e6ee1d6f966e9a7c8bd43ac45c245 Author: Gaƫl de Sailly Date: Thu Feb 21 17:35:41 2019 +0100 Initial commit Allows to enable/disable placement of certain nodes on world layers diff --git a/api.lua b/api.lua new file mode 100644 index 0000000..6e90450 --- /dev/null +++ b/api.lua @@ -0,0 +1,70 @@ +worldlayers = {} + +local layers = {} -- localize to make use simpler and faster in the code +worldlayers.registered_layers = layers + +function worldlayers.register_layer(layer) + local size = layer.ymax - layer.ymin + for i, old_layer in ipairs(layers) do + if old_layer.ymax - old_layer.ymin > size then + table.insert(layers, i, layer) + return + end + end + table.insert(layers, layer) +end + +function worldlayers.iter(pos) + local y = pos.y + local n = 1 + return function() + for i=n, #layers do + local layer = layers[i] + if y >= layer.ymin and y <= layer.ymax then + n = i+1 + return i, layer + end + end + end +end + +function worldlayers.is_action_allowed(pos, node, action) + for _, layer in worldlayers.iter(pos) do + local restriction_table = layer.restrictions + if restriction_table then + local restriction = restriction_table[action] + if type(restriction) == "table" then + if restriction[node] ~= nil then + return restriction[node] + elseif restriction.general ~= nil then + return restriction.general + end + elseif type(restriction) == "boolean" then + return restriction + end + end + end + return true +end + +local old_is_protected = minetest.is_protected +function minetest.is_protected(pos, name) + local old_node = minetest.get_node(pos).name + local node, action + if old_node ~= "air" then + node = old_node + action = "dig" + else + local player = minetest.get_player_by_name(name) + if not player then + return false + end + local new_node = player:get_wielded_item():get_name() + if node ~= "" then + node = new_node + action = "place" + end + end + local is_protected = not worldlayers.is_action_allowed(pos, node, action) + return is_protected or old_is_protected(pos, name) +end diff --git a/init.lua b/init.lua new file mode 100644 index 0000000..33ab272 --- /dev/null +++ b/init.lua @@ -0,0 +1,13 @@ +local modpath = minetest.get_modpath("worldlayers") +dofile(modpath .. "/api.lua") + +worldlayers.register_layer({ + ymin = -31000, + ymax = -100, + restrictions = { + place = { + ["default:chest"] = false, + ["default:chest_locked"] = false, + }, + }, +})