local creative_mode = minetest.setting_getbool("creative_mode") local reg_alias = ocutil.safe_register_alias local function cyan(str) return minetest.colorize("#00FFFF", str) end local function red(str) return minetest.colorize("#FF5555", str) end local radius = minetest.setting_get ("protector_radius") or 10 local function remove_display(pos) local objs = minetest.get_objects_inside_radius(pos, 0.5) for _, o in pairs(objs) do o:remove() end end local xx = "protector_side.png" minetest.register_node(":areas:protector", { description = "Protector Block", groups = {cracky = 1}, tiles = { xx , xx, xx .. "^protector_stone.png" }, paramtype = "light", drawtype = "nodebox", node_box = { type = "fixed", fixed = {-0.5, -0.5, -0.5, 0.5, 0.5, 0.5} }, on_place = function(itemstack, player, pointed) local pos = pointed.above local pos1 = vector.add(pos, vector.new(radius, radius, radius)) local pos2 = vector.add(pos, vector.new(-1 * radius, -1 * radius, -1 * radius)) local name = player:get_player_name() if not minetest.is_protected_action(pos, name) then local perm, err = areas:canPlayerAddArea(pos1, pos2, name) if not perm then minetest.chat_send_player(name, red("You are not allowed to protect that area: ") .. err) return itemstack end local id = areas:add(name, "Protected by Protector Block", pos1, pos2) areas:save() local msg = string.format("The area from %s to %s has been protected as #%s", cyan(minetest.pos_to_string(pos1)), cyan(minetest.pos_to_string(pos2)), cyan(id)) minetest.chat_send_player(name, msg) minetest.set_node(pos, {name = "areas:protector"}) local meta = minetest.get_meta(pos) local infotext = string.format("Protecting area %d owned by %s", id, name) meta:set_string("infotext", infotext) meta:set_int("area_id", id) meta:set_string("owner", name) itemstack:take_item() return itemstack end end, after_dig_node = function(pos, oldnode, oldmetadata, digger) if oldmetadata and oldmetadata.fields then local owner = oldmetadata.fields.owner local id = tonumber(oldmetadata.fields.area_id) local playername = digger:get_player_name() if areas.areas[id] and areas:isAreaOwner(id, owner) then if digger:get_player_control().sneak then local inv = digger:get_inventory() if not creative_mode then if inv:room_for_item("main", "default:steel_ingot 6") then inv:remove_item("main", "areas:protector 1") inv:add_item("main", "default:steel_ingot 6") else minetest.chat_send_player(playername, "No room for the replacement ingots, just digging the protector and deleting the area normally.") areas:remove(id) areas:save() end else inv:remove_item("main", "areas:protector 1") end else areas:remove(id) areas:save() end end end end, on_punch = function(pos, node, puncher) local objs = minetest.get_objects_inside_radius(pos, .5) -- a radius of .5 since the entity serialization seems to be not that precise local removed = false for _, o in pairs(objs) do if (not o:is_player()) and o:get_luaentity().name == "areas:display" then o:remove() removed = true end end if not removed then -- nothing was removed: there wasn't the entity minetest.add_entity(pos, "areas:display") minetest.after(4, remove_display, pos) end end }) -- entities code below (and above) mostly copied-pasted from Zeg9's protector mod minetest.register_entity(":areas:display", { physical = false, collisionbox = {0, 0, 0, 0, 0, 0}, visual = "wielditem", visual_size = {x = 1.0 / 1.5, y = 1.0 / 1.5}, -- wielditem seems to be scaled to 1.5 times original node size textures = {"areas:display_node"}, on_step = function(self, dtime) if minetest.get_node(self.object:getpos()).name ~= "areas:protector" then self.object:remove() return end end }) local nb_radius = radius + 0.55 minetest.register_node(":areas:display_node", { tiles = {"protector_display.png"}, walkable = false, drawtype = "nodebox", node_box = { type = "fixed", fixed = { -- sides {-nb_radius, -nb_radius, -nb_radius, -nb_radius, nb_radius, nb_radius}, {-nb_radius, -nb_radius, nb_radius, nb_radius, nb_radius, nb_radius}, {nb_radius, -nb_radius, -nb_radius, nb_radius, nb_radius, nb_radius}, {-nb_radius, -nb_radius, -nb_radius, nb_radius, nb_radius, -nb_radius}, -- top {-nb_radius, nb_radius, -nb_radius, nb_radius, nb_radius, nb_radius}, -- bottom {-nb_radius, -nb_radius, -nb_radius, nb_radius, -nb_radius, nb_radius}, -- middle (surround protector) {-.55, -.55, -.55, .55, .55, .55} } }, selection_box = { type = "regular" }, paramtype = "light", groups = {dig_immediate = 3, not_in_creative_inventory = 1}, drop = "" }) local s1 = "default:stonebrick" local s2 = "default:mese" minetest.register_craft({ output = "areas:protector", type = "shapeless", recipe = { s1, s1, s1, s1, s2, s1, s1, s1, s1, } }) -- =================================================================== if ocutil.bool_setting ("enable_convert_protector") then minetest.register_lbm ({ -- For LBM version: name = ":areas:convert" , label = ":areas:convert" , run_at_every_load = true , -- For ABM version: catch_up = false , chance = 1 , interval = 3 , nodenames = { "protector:mff_block_15" , "protector:protect" , "protector:protect2" , } , action = function (pos, node) local name = node.name local meta = minetest.get_meta (pos) local pos_str = ocutil.pos_to_str (pos) ocutil.log ("areas: Found a " .. name .. " at " .. pos_str) if meta == nil then ocutil.log ("areas: " .. name .. " is missing meta") return end local owner = meta:get_string ("owner") if ocutil.str_empty (owner) then ocutil.log ("areas: " .. name .. " is missing owner") return end local p_range = 6 local m_range = -1 * p_range if ocutil.str_contains (name, "_15") then range = 15 end local pos1 = vector.add (pos, vector.new (p_range, p_range, p_range)) local pos2 = vector.add (pos, vector.new (m_range, m_range, m_range)) local id = areas:add (owner, areas.legacy_label01, pos1, pos2) areas:save() minetest.set_node (pos, { name = "areas:protector" }) ocutil.log ("areas: converted " .. name .. " at " .. pos_str .. " to area #" .. id) end , }) end