tweak and tidy code

This commit is contained in:
tenplus1 2024-09-16 11:35:13 +01:00
parent f272486f4f
commit ce84ac68b2
6 changed files with 363 additions and 437 deletions

View File

@ -1,27 +1,30 @@
local S = protector.intllib
-- translation and default name vars
local S = minetest.get_translator("protector")
local removal_names = ""
local replace_names = ""
-- remove protection command
minetest.register_chatcommand("protector_remove", {
params = S("<names list>"),
description = S("Remove Protectors around players (separate names with spaces)"),
privs = {server = true},
func = function(name, param)
if not param or param == "" then
minetest.chat_send_player(name,
S("Protector Names to remove: @1",
removal_names))
S("Protector Names to remove: @1", removal_names))
return
end
if param == "-" then
minetest.chat_send_player(name,
S("Name List Reset"))
minetest.chat_send_player(name, S("Name List Reset"))
removal_names = ""
@ -32,11 +35,13 @@ minetest.register_chatcommand("protector_remove", {
end
})
-- replace protection command
minetest.register_chatcommand("protector_replace", {
params = S("<owner name> <name to replace with>"),
description = S("Replace Protector Owner with name provided"),
privs = {server = true},
func = function(name, param)
-- reset list to empty
@ -50,13 +55,11 @@ minetest.register_chatcommand("protector_replace", {
end
-- show name info
if param == ""
and replace_names ~= "" then
if param == "" and replace_names ~= "" then
local names = replace_names:split(" ")
minetest.chat_send_player(name,
S("Replacing Protector name @1 with @2",
minetest.chat_send_player(name, S("Replacing Protector name @1 with @2",
names[1] or "", names[2] or ""))
return
@ -66,18 +69,17 @@ minetest.register_chatcommand("protector_replace", {
end
})
-- Abm to remove or replace protectors within active player area
minetest.register_abm({
nodenames = {"protector:protect", "protector:protect2", "protector:protect_hidden"},
interval = 6,
chance = 1,
catch_up = false,
action = function(pos, node)
if removal_names == ""
and replace_names == "" then
return
end
if removal_names == "" and replace_names == "" then return end
local meta = minetest.get_meta(pos)
@ -90,9 +92,7 @@ minetest.register_abm({
local names = removal_names:split(" ")
for _, n in pairs(names) do
if n == owner then
minetest.set_node(pos, {name = "air"})
end
if n == owner then minetest.set_node(pos, {name = "air"}) end
end
end
@ -108,16 +108,19 @@ minetest.register_abm({
end
})
-- get protection radius
-- get protection radius (max 30)
local r = tonumber(minetest.settings:get("protector_radius")) or 5
if r > 30 then r = 30 end
-- show protection areas of nearby protectors owned by you (thanks agaran)
minetest.register_chatcommand("protector_show_area", {
params = "",
description = S("Show protected areas of your nearby protectors"),
privs = {},
func = function(name, param)
local player = minetest.get_player_by_name(name)
@ -125,9 +128,9 @@ minetest.register_chatcommand("protector_show_area", {
-- find the protector nodes
local pos = minetest.find_nodes_in_area(
{x = pos.x - r, y = pos.y - r, z = pos.z - r},
{x = pos.x + r, y = pos.y + r, z = pos.z + r},
{"protector:protect", "protector:protect2", "protector:protect_hidden"})
{x = pos.x - r, y = pos.y - r, z = pos.z - r},
{x = pos.x + r, y = pos.y + r, z = pos.z + r},
{"protector:protect", "protector:protect2", "protector:protect_hidden"})
local meta, owner
@ -145,8 +148,8 @@ minetest.register_chatcommand("protector_show_area", {
end
})
-- ability to hide protection blocks (borrowed from doors mod :)
minetest.register_node("protector:protect_hidden", {
description = "Hidden Protector",
drawtype = "airlike",
@ -165,11 +168,11 @@ minetest.register_node("protector:protect_hidden", {
on_blast = function() end,
-- 1px block inside door hinge near node top
collision_box = {
type = "fixed",
fixed = {-15/32, 13/32, -15/32, -13/32, 1/2, -13/32}
type = "fixed", fixed = {-15/32, 13/32, -15/32, -13/32, 1/2, -13/32}
}
})
-- make own protectors visible in area
minetest.register_chatcommand("protector_show", {
params = "",
@ -187,9 +190,9 @@ minetest.register_chatcommand("protector_show", {
local pos = player:get_pos()
local a = minetest.find_nodes_in_area(
{x = pos.x - r, y = pos.y - r, z = pos.z - r},
{x = pos.x + r, y = pos.y + r, z = pos.z + r},
{"protector:protect_hidden"})
{x = pos.x - r, y = pos.y - r, z = pos.z - r},
{x = pos.x + r, y = pos.y + r, z = pos.z + r},
{"protector:protect_hidden"})
local meta, owner
@ -206,6 +209,8 @@ minetest.register_chatcommand("protector_show", {
end
})
-- make own protectors invisible in area
minetest.register_chatcommand("protector_hide", {
params = "",
description = S("Hide your nearby protection blocks"),
@ -222,9 +227,9 @@ minetest.register_chatcommand("protector_hide", {
local pos = player:get_pos()
local a = minetest.find_nodes_in_area(
{x = pos.x - r, y = pos.y - r, z = pos.z - r},
{x = pos.x + r, y = pos.y + r, z = pos.z + r},
{"protector:protect", "protector:protect2"})
{x = pos.x - r, y = pos.y - r, z = pos.z - r},
{x = pos.x + r, y = pos.y + r, z = pos.z + r},
{"protector:protect", "protector:protect2"})
local meta, owner

View File

@ -1,20 +1,22 @@
-- Since the doors mod has changed in the latest daily builds I have taken the
-- WTFPL licenced code from the old doors mod and included an edited version
-- within this mod for local use.
-- doors code from an old client re-used
local S = protector.intllib
local S = minetest.get_translator("protector")
local F = minetest.formspec_escape
-- MineClone2 support
local mcl = minetest.get_modpath("mcl_core")
local mcf = minetest.get_modpath("mcl_formspec")
-- Are crafts enabled?
local protector_crafts = minetest.settings:get_bool("protector_crafts") ~= false
-- Registers a door
function register_door(name, def)
local function register_door(name, def)
def.groups.not_in_creative_inventory = 1
def.groups.handy = 1
@ -24,20 +26,20 @@ function register_door(name, def)
def.node_box_top = box
def.selection_box_bottom = box
def.selection_box_top = box
def.sound_close_door = "doors_door_close"
def.sound_open_door = "doors_door_open"
minetest.register_craftitem(name, {
description = def.description,
inventory_image = def.inventory_image,
on_place = function(itemstack, placer, pointed_thing)
if pointed_thing.type ~= "node" then
return itemstack
end
local ptu = pointed_thing.under
local nu = minetest.get_node(ptu)
if minetest.registered_nodes[nu.name]
and minetest.registered_nodes[nu.name].on_rightclick then
return minetest.registered_nodes[nu.name].on_rightclick(ptu, nu, placer, itemstack)
@ -45,24 +47,24 @@ function register_door(name, def)
local pt = pointed_thing.above
local pt2 = {x=pt.x, y=pt.y, z=pt.z}
pt2.y = pt2.y+1
if
not minetest.registered_nodes[minetest.get_node(pt).name].buildable_to or
not minetest.registered_nodes[minetest.get_node(pt2).name].buildable_to or
not placer or
not placer:is_player()
then
if not minetest.registered_nodes[minetest.get_node(pt).name].buildable_to
or not minetest.registered_nodes[minetest.get_node(pt2).name].buildable_to
or not placer or not placer:is_player() then
return itemstack
end
if minetest.is_protected(pt, placer:get_player_name()) or
minetest.is_protected(pt2, placer:get_player_name()) then
if minetest.is_protected(pt, placer:get_player_name())
or minetest.is_protected(pt2, placer:get_player_name()) then
minetest.record_protection_violation(pt, placer:get_player_name())
return itemstack
end
local p2 = minetest.dir_to_facedir(placer:get_look_dir())
local pt3 = {x=pt.x, y=pt.y, z=pt.z}
if p2 == 0 then
pt3.x = pt3.x-1
elseif p2 == 1 then
@ -72,12 +74,14 @@ function register_door(name, def)
elseif p2 == 3 then
pt3.z = pt3.z-1
end
if minetest.get_item_group(minetest.get_node(pt3).name, "door") == 0 then
minetest.set_node(pt, {name=name.."_b_1", param2=p2})
minetest.set_node(pt2, {name=name.."_t_1", param2=p2})
minetest.set_node(pt, {name = name.."_b_1", param2 = p2})
minetest.set_node(pt2, {name = name.."_t_1", param2 = p2})
else
minetest.set_node(pt, {name=name.."_b_2", param2=p2})
minetest.set_node(pt2, {name=name.."_t_2", param2=p2})
minetest.set_node(pt, {name = name.."_b_2", param2 = p2})
minetest.set_node(pt2, {name = name.."_t_2", param2 = p2})
minetest.get_meta(pt):set_int("right", 1)
minetest.get_meta(pt2):set_int("right", 1)
end
@ -85,74 +89,73 @@ function register_door(name, def)
if not minetest.settings:get_bool("creative_mode") then
itemstack:take_item()
end
return itemstack
end,
end
})
local tt = def.tiles_top
local tb = def.tiles_bottom
local function after_dig_node(pos, name, digger)
local node = minetest.get_node(pos)
if node.name == name then
minetest.node_dig(pos, node, digger)
end
end
local function on_rightclick(pos, dir, check_name, replace, replace_dir, params)
pos.y = pos.y+dir
if minetest.get_node(pos).name ~= check_name then
return
end
local p2 = minetest.get_node(pos).param2
p2 = params[p2+1]
minetest.swap_node(pos, {name=replace_dir, param2=p2})
pos.y = pos.y+dir
if minetest.get_node(pos).name ~= check_name then return end
local p2 = minetest.get_node(pos).param2
p2 = params[p2 + 1]
minetest.swap_node(pos, {name=replace_dir, param2 = p2})
pos.y = pos.y-dir
minetest.swap_node(pos, {name=replace, param2=p2})
local snd_1 = def.sound_close_door
local snd_2 = def.sound_open_door
if params[1] == 3 then
snd_1 = def.sound_open_door
snd_2 = def.sound_close_door
end
if minetest.get_meta(pos):get_int("right") ~= 0 then
minetest.sound_play(snd_1, {pos = pos, gain = 0.3, max_hear_distance = 10}, true)
else
minetest.sound_play(snd_2, {pos = pos, gain = 0.3, max_hear_distance = 10}, true)
end
minetest.sound_play("default_dug_node",
{pos = pos, gain = 0.3, max_hear_distance = 10}, true)
end
local function on_rotate(pos, node, dir, user, check_name, mode, new_param2)
if mode ~= screwdriver.ROTATE_FACE then
return false
end
if mode ~= screwdriver.ROTATE_FACE then return false end
pos.y = pos.y + dir
if minetest.get_node(pos).name ~= check_name then
return false
end
if minetest.get_node(pos).name ~= check_name then return false end
if minetest.is_protected(pos, user:get_player_name()) then
minetest.record_protection_violation(pos, user:get_player_name())
return false
end
local node2 = minetest.get_node(pos)
node2.param2 = (node2.param2 + 1) % 4
minetest.swap_node(pos, node2)
pos.y = pos.y - dir
node.param2 = (node.param2 + 1) % 4
minetest.swap_node(pos, node)
return true
end
minetest.register_node(name.."_b_1", {
tiles = {tb[2], tb[2], tb[2], tb[2], tb[1], tb[1].."^[transformfx"},
minetest.register_node(name .. "_b_1", {
tiles = {tb[2], tb[2], tb[2], tb[2], tb[1], tb[1] .. "^[transformfx"},
paramtype = "light",
paramtype2 = "facedir",
use_texture_alpha = "clip",
@ -160,30 +163,29 @@ function register_door(name, def)
drop = name,
drawtype = "nodebox",
node_box = {
type = "fixed",
fixed = def.node_box_bottom
type = "fixed", fixed = def.node_box_bottom
},
selection_box = {
type = "fixed",
fixed = def.selection_box_bottom
type = "fixed", fixed = def.selection_box_bottom
},
groups = def.groups,
_mcl_hardness = 0.8,
_mcl_blast_resistance = 1,
after_dig_node = function(pos, oldnode, oldmetadata, digger)
pos.y = pos.y+1
pos.y = pos.y + 1
after_dig_node(pos, name.."_t_1", digger)
end,
on_rightclick = function(pos, node, clicker)
if not minetest.is_protected(pos, clicker:get_player_name()) then
on_rightclick(pos, 1, name.."_t_1", name.."_b_2", name.."_t_2", {1,2,3,0})
on_rightclick(pos, 1, name .. "_t_1", name .. "_b_2", name .. "_t_2", {1,2,3,0})
end
end,
on_rotate = function(pos, node, user, mode, new_param2)
return on_rotate(pos, node, 1, user, name.."_t_1", mode)
return on_rotate(pos, node, 1, user, name .. "_t_1", mode)
end,
sounds = def.sounds,
@ -191,8 +193,8 @@ function register_door(name, def)
on_blast = function() end,
})
minetest.register_node(name.."_t_1", {
tiles = {tt[2], tt[2], tt[2], tt[2], tt[1], tt[1].."^[transformfx"},
minetest.register_node(name .. "_t_1", {
tiles = {tt[2], tt[2], tt[2], tt[2], tt[1], tt[1] .. "^[transformfx"},
paramtype = "light",
paramtype2 = "facedir",
use_texture_alpha = "clip",
@ -200,12 +202,10 @@ function register_door(name, def)
drop = "",
drawtype = "nodebox",
node_box = {
type = "fixed",
fixed = def.node_box_top
type = "fixed", fixed = def.node_box_top
},
selection_box = {
type = "fixed",
fixed = def.selection_box_top
type = "fixed", fixed = def.selection_box_top
},
groups = def.groups,
_mcl_hardness = 0.8,
@ -213,17 +213,17 @@ function register_door(name, def)
after_dig_node = function(pos, oldnode, oldmetadata, digger)
pos.y = pos.y-1
after_dig_node(pos, name.."_b_1", digger)
after_dig_node(pos, name .. "_b_1", digger)
end,
on_rightclick = function(pos, node, clicker)
if not minetest.is_protected(pos, clicker:get_player_name()) then
on_rightclick(pos, -1, name.."_b_1", name.."_t_2", name.."_b_2", {1,2,3,0})
on_rightclick(pos, -1, name .. "_b_1", name .. "_t_2", name .. "_b_2", {1,2,3,0})
end
end,
on_rotate = function(pos, node, user, mode, new_param2)
return on_rotate(pos, node, -1, user, name.."_b_1", mode)
return on_rotate(pos, node, -1, user, name .. "_b_1", mode)
end,
sounds = def.sounds,
@ -232,7 +232,7 @@ function register_door(name, def)
})
minetest.register_node(name.."_b_2", {
tiles = {tb[2], tb[2], tb[2], tb[2], tb[1].."^[transformfx", tb[1]},
tiles = {tb[2], tb[2], tb[2], tb[2], tb[1] .. "^[transformfx", tb[1]},
paramtype = "light",
paramtype2 = "facedir",
use_texture_alpha = "clip",
@ -240,30 +240,28 @@ function register_door(name, def)
drop = name,
drawtype = "nodebox",
node_box = {
type = "fixed",
fixed = def.node_box_bottom
type = "fixed", fixed = def.node_box_bottom
},
selection_box = {
type = "fixed",
fixed = def.selection_box_bottom
type = "fixed", fixed = def.selection_box_bottom
},
groups = def.groups,
_mcl_hardness = 0.8,
_mcl_blast_resistance = 1,
after_dig_node = function(pos, oldnode, oldmetadata, digger)
pos.y = pos.y+1
after_dig_node(pos, name.."_t_2", digger)
pos.y = pos.y + 1
after_dig_node(pos, name .. "_t_2", digger)
end,
on_rightclick = function(pos, node, clicker)
if not minetest.is_protected(pos, clicker:get_player_name()) then
on_rightclick(pos, 1, name.."_t_2", name.."_b_1", name.."_t_1", {3,0,1,2})
on_rightclick(pos, 1, name .. "_t_2", name .. "_b_1", name .. "_t_1", {3,0,1,2})
end
end,
on_rotate = function(pos, node, user, mode, new_param2)
return on_rotate(pos, node, 1, user, name.."_t_2", mode)
return on_rotate(pos, node, 1, user, name .. "_t_2", mode)
end,
sounds = def.sounds,
@ -271,8 +269,8 @@ function register_door(name, def)
on_blast = function() end,
})
minetest.register_node(name.."_t_2", {
tiles = {tt[2], tt[2], tt[2], tt[2], tt[1].."^[transformfx", tt[1]},
minetest.register_node(name .. "_t_2", {
tiles = {tt[2], tt[2], tt[2], tt[2], tt[1] .. "^[transformfx", tt[1]},
paramtype = "light",
paramtype2 = "facedir",
use_texture_alpha = "clip",
@ -280,30 +278,28 @@ function register_door(name, def)
drop = "",
drawtype = "nodebox",
node_box = {
type = "fixed",
fixed = def.node_box_top
type = "fixed", fixed = def.node_box_top
},
selection_box = {
type = "fixed",
fixed = def.selection_box_top
type = "fixed", fixed = def.selection_box_top
},
groups = def.groups,
_mcl_hardness = 0.8,
_mcl_blast_resistance = 1,
after_dig_node = function(pos, oldnode, oldmetadata, digger)
pos.y = pos.y-1
after_dig_node(pos, name.."_b_2", digger)
pos.y = pos.y - 1
after_dig_node(pos, name .. "_b_2", digger)
end,
on_rightclick = function(pos, node, clicker)
if not minetest.is_protected(pos, clicker:get_player_name()) then
on_rightclick(pos, -1, name.."_b_2", name.."_t_1", name.."_b_1", {3,0,1,2})
on_rightclick(pos, -1, name .. "_b_2", name .. "_t_1", name .. "_b_1", {3,0,1,2})
end
end,
on_rotate = function(pos, node, user, mode, new_param2)
return on_rotate(pos, node, -1, user, name.."_b_2", mode)
return on_rotate(pos, node, -1, user, name .. "_b_2", mode)
end,
sounds = def.sounds,
@ -319,10 +315,7 @@ local name = "protector:door_wood"
register_door(name, {
description = S("Protected Wooden Door"),
inventory_image = "doors_wood.png^protector_logo.png",
groups = {
snappy = 1, choppy = 2, dig_immediate = 2,
unbreakable = 1, axey = 1, --door = 1
},
groups = {snappy = 1, choppy = 2, dig_immediate = 2, unbreakable = 1, axey = 1},
tiles_bottom = {"doors_wood_b.png^protector_logo.png", "doors_brown.png"},
tiles_top = {"doors_wood_a.png", "doors_brown.png"},
sounds = default.node_sound_wood_defaults(),
@ -330,29 +323,31 @@ register_door(name, {
})
if protector_crafts then
if mcl then
minetest.register_craft({
output = name,
recipe = {
{"mcl_doors:wooden_door", "mcl_core:gold_ingot"}
}
})
else
minetest.register_craft({
output = name,
recipe = {
{"group:wood", "group:wood"},
{"group:wood", "default:copper_ingot"},
{"group:wood", "group:wood"}
}
})
minetest.register_craft({
output = name,
recipe = {
{"doors:door_wood", "default:copper_ingot"}
}
})
if mcl then
minetest.register_craft({
output = name,
recipe = {
{"mcl_doors:wooden_door", "mcl_core:gold_ingot"}
}
})
else
minetest.register_craft({
output = name,
recipe = {
{"group:wood", "group:wood"},
{"group:wood", "default:copper_ingot"},
{"group:wood", "group:wood"}
}
})
minetest.register_craft({
output = name,
recipe = {
{"doors:door_wood", "default:copper_ingot"}
}
})
end
end
@ -365,7 +360,7 @@ register_door(name, {
inventory_image = "doors_steel.png^protector_logo.png",
groups = {
snappy = 1, bendy = 2, cracky = 1,
level = mcl and 0 or 2, pickaxey = 2, unbreakable = 1, -- door = 1
level = (mcl and 0 or 2), pickaxey = 2, unbreakable = 1
},
tiles_bottom = {"doors_steel_b.png^protector_logo.png", "doors_grey.png"},
tiles_top = {"doors_steel_a.png", "doors_grey.png"},
@ -404,21 +399,20 @@ end
----trapdoor----
function register_trapdoor(name, def)
local function register_trapdoor(name, def)
local name_closed = name
local name_opened = name.."_open"
local name_opened = name .. "_open"
def.on_rightclick = function (pos, node, clicker, itemstack, pointed_thing)
if minetest.is_protected(pos, clicker:get_player_name()) then
return
end
if minetest.is_protected(pos, clicker:get_player_name()) then return end
local newname = node.name == name_closed and name_opened or name_closed
local sound = false
if node.name == name_closed then sound = "doors_door_open" end
if node.name == name_opened then sound = "doors_door_close" end
if sound then
minetest.sound_play(sound, {pos = pos, gain = 0.3, max_hear_distance = 10}, true)
end
minetest.sound_play("default_dug_node",
{pos = pos, gain = 0.3, max_hear_distance = 10}, true)
minetest.swap_node(pos, {name = newname, param1 = node.param1, param2 = node.param2})
end
@ -433,23 +427,19 @@ function register_trapdoor(name, def)
local def_closed = table.copy(def)
def_closed.node_box = {
type = "fixed",
fixed = {-0.5, -0.5, -0.5, 0.5, -6/16, 0.5}
type = "fixed", fixed = {-0.5, -0.5, -0.5, 0.5, -6/16, 0.5}
}
def_closed.selection_box = {
type = "fixed",
fixed = {-0.5, -0.5, -0.5, 0.5, -6/16, 0.5}
type = "fixed", fixed = {-0.5, -0.5, -0.5, 0.5, -6/16, 0.5}
}
def_closed.tiles = { def.tile_front, def.tile_front, def.tile_side, def.tile_side,
def.tile_side, def.tile_side }
def_opened.node_box = {
type = "fixed",
fixed = {-0.5, -0.5, 6/16, 0.5, 0.5, 0.5}
type = "fixed", fixed = {-0.5, -0.5, 6/16, 0.5, 0.5, 0.5}
}
def_opened.selection_box = {
type = "fixed",
fixed = {-0.5, -0.5, 6/16, 0.5, 0.5, 0.5}
type = "fixed", fixed = {-0.5, -0.5, 6/16, 0.5, 0.5, 0.5}
}
def_opened.tiles = { def.tile_side, def.tile_side,
def.tile_side .. "^[transform3",
@ -471,38 +461,37 @@ register_trapdoor("protector:trapdoor", {
wield_image = "doors_trapdoor.png^protector_logo.png",
tile_front = "doors_trapdoor.png^protector_logo.png",
tile_side = "doors_trapdoor_side.png",
groups = {
snappy = 1, choppy = 2, dig_immediate = 2,
unbreakable = 1, axey = 1, --door = 1
},
groups = {snappy = 1, choppy = 2, dig_immediate = 2, unbreakable = 1, axey = 1},
_mcl_hardness = 0.8,
_mcl_blast_resistance = 1,
sounds = default.node_sound_wood_defaults(),
})
if protector_crafts then
if mcl then
minetest.register_craft({
output = "protector:trapdoor",
recipe = {
{"mcl_doors:trapdoor", "mcl_core:gold_ingot"}
}
})
else
minetest.register_craft({
output = "protector:trapdoor 2",
recipe = {
{"group:wood", "default:copper_ingot", "group:wood"},
{"group:wood", "group:wood", "group:wood"}
}
})
minetest.register_craft({
output = "protector:trapdoor",
recipe = {
{"doors:trapdoor", "default:copper_ingot"}
}
})
if mcl then
minetest.register_craft({
output = "protector:trapdoor",
recipe = {
{"mcl_doors:trapdoor", "mcl_core:gold_ingot"}
}
})
else
minetest.register_craft({
output = "protector:trapdoor 2",
recipe = {
{"group:wood", "default:copper_ingot", "group:wood"},
{"group:wood", "group:wood", "group:wood"}
}
})
minetest.register_craft({
output = "protector:trapdoor",
recipe = {
{"doors:trapdoor", "default:copper_ingot"}
}
})
end
end
@ -515,8 +504,8 @@ register_trapdoor("protector:trapdoor_steel", {
tile_front = "doors_trapdoor_steel.png^protector_logo.png",
tile_side = "doors_trapdoor_steel_side.png",
groups = {
snappy = 1, bendy = 2, cracky = 1, melty = 2, level = mcl and 0 or 2,
unbreakable = 1, pickaxey = 2, --door = 1
snappy = 1, bendy = 2, cracky = 1, melty = 2, level = (mcl and 0 or 2),
unbreakable = 1, pickaxey = 2
},
_mcl_hardness = 1,
_mcl_blast_resistance = 1,
@ -524,28 +513,29 @@ register_trapdoor("protector:trapdoor_steel", {
})
if protector_crafts then
if mcl then
minetest.register_craft({
output = "protector:trapdoor_steel",
recipe = {
{"mcl_doors:iron_trapdoor", "mcl_core:gold_ingot"}
}
})
else
minetest.register_craft({
output = "protector:trapdoor_steel",
recipe = {
{"default:copper_ingot", "default:steel_ingot"},
{"default:steel_ingot", "default:steel_ingot"}
}
})
minetest.register_craft({
output = "protector:trapdoor_steel",
recipe = {
{"doors:trapdoor_steel", "default:copper_ingot"}
}
})
if mcl then
minetest.register_craft({
output = "protector:trapdoor_steel",
recipe = {
{"mcl_doors:iron_trapdoor", "mcl_core:gold_ingot"}
}
})
else
minetest.register_craft({
output = "protector:trapdoor_steel",
recipe = {
{"default:copper_ingot", "default:steel_ingot"},
{"default:steel_ingot", "default:steel_ingot"}
}
})
minetest.register_craft({
output = "protector:trapdoor_steel",
recipe = {
{"doors:trapdoor_steel", "default:copper_ingot"}
}
})
end
end
@ -592,23 +582,20 @@ minetest.register_node("protector:chest", {
on_metadata_inventory_put = function(pos, listname, index, stack, player)
minetest.log("action", player:get_player_name() ..
" moves stuff to protected chest at " ..
minetest.pos_to_string(pos))
" moves stuff to protected chest at " .. minetest.pos_to_string(pos))
end,
on_metadata_inventory_take = function(pos, listname, index, stack, player)
minetest.log("action", player:get_player_name() ..
" takes stuff from protected chest at " ..
minetest.pos_to_string(pos))
" takes stuff from protected chest at " .. minetest.pos_to_string(pos))
end,
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 inside protected chest at " ..
minetest.pos_to_string(pos))
" moves stuff inside protected chest at " .. minetest.pos_to_string(pos))
end,
allow_metadata_inventory_put = function(pos, listname, index, stack, player)
@ -645,11 +632,7 @@ minetest.register_node("protector:chest", {
return
end
local meta = minetest.get_meta(pos)
if not meta then
return
end
local meta = minetest.get_meta(pos) ; if not meta then return end
local spos = pos.x .. "," .. pos.y .. "," ..pos.z
local formspec
@ -701,7 +684,8 @@ minetest.register_node("protector:chest", {
})
-- Container transfer helper
local to_from = function(src, dst)
local function to_from(src, dst)
local stack, item, leftover
local size = dst:get_size("main")
@ -728,16 +712,12 @@ end
minetest.register_on_player_receive_fields(function(player, formname, fields)
if string.sub(formname, 0, 16) ~= "protector:chest_" then
return
end
if string.sub(formname, 0, 16) ~= "protector:chest_" then return end
local pos_s = string.sub(formname, 17)
local pos = minetest.string_to_pos(pos_s)
if minetest.is_protected(pos, player:get_player_name()) then
return
end
if minetest.is_protected(pos, player:get_player_name()) then return end
local meta = minetest.get_meta(pos) ; if not meta then return end
local chest_inv = meta:get_inventory() ; if not chest_inv then return end

21
hud.lua
View File

@ -1,23 +1,27 @@
local S = protector.intllib
-- translation and protector radius
local S = minetest.get_translator("protector")
local radius = (tonumber(minetest.settings:get("protector_radius")) or 5)
-- radius limiter (minetest cannot handle node volume of more than 4096000)
if radius > 30 then radius = 30 end
-- hud settings
local hud = {}
local hud_timer = 0
local hud_interval = (tonumber(minetest.settings:get("protector_hud_interval")) or 5)
local hud_style = minetest.has_feature("hud_def_type_field")
if hud_interval > 0 then
minetest.register_globalstep(function(dtime)
-- every 5 seconds
hud_timer = hud_timer + dtime
if hud_timer < hud_interval then
return
end
if hud_timer < hud_interval then return end
hud_timer = 0
for _, player in pairs(minetest.get_connected_players()) do
@ -27,11 +31,12 @@ minetest.register_globalstep(function(dtime)
local hud_text = ""
local protectors = minetest.find_nodes_in_area(
{x = pos.x - radius , y = pos.y - radius , z = pos.z - radius},
{x = pos.x + radius , y = pos.y + radius , z = pos.z + radius},
{"protector:protect","protector:protect2", "protector:protect_hidden"})
{x = pos.x - radius , y = pos.y - radius , z = pos.z - radius},
{x = pos.x + radius , y = pos.y + radius , z = pos.z + radius},
{"protector:protect","protector:protect2", "protector:protect_hidden"})
if #protectors > 0 then
local npos = protectors[1]
local meta = minetest.get_meta(npos)
local nodeowner = meta:get_string("owner")
@ -72,4 +77,4 @@ minetest.register_on_leaveplayer(function(player)
hud[player:get_player_name()] = nil
end)
end
end -- END hud_interval > 0

298
init.lua
View File

@ -1,12 +1,14 @@
-- default support (for use with MineClone2 and other [games]
default = default or {
node_sound_stone_defaults = function(table) end,
node_sound_wood_defaults = function(table) end,
gui_bg = "",
gui_bg_img = "",
gui_slots = ""
}
if not minetest.global_exists("default") then
default = {
node_sound_stone_defaults = function(table) end,
node_sound_wood_defaults = function(table) end,
gui_bg = "", gui_bg_img = "", gui_slots = ""
}
end
if minetest.get_modpath("mcl_sounds") then
default.node_sound_stone_defaults = mcl_sounds.node_sound_stone_defaults
@ -14,23 +16,20 @@ if minetest.get_modpath("mcl_sounds") then
default.node_sound_metal_defaults = mcl_sounds.node_sound_metal_defaults
end
-- modpath, formspec helper and translator
local MP = minetest.get_modpath(minetest.get_current_modname())
local F = minetest.formspec_escape
-- Translation support
local S = minetest.get_translator("protector")
-- Load support for factions
-- global table
protector = { mod = "redo", modpath = MP }
-- settings
local factions_available = minetest.global_exists("factions")
protector = {
mod = "redo",
modpath = MP,
intllib = S
}
local protector_max_share_count = 12
-- get minetest.conf settings
local protector_radius = tonumber(minetest.settings:get("protector_radius")) or 5
local protector_flip = minetest.settings:get_bool("protector_flip") or false
local protector_hurt = tonumber(minetest.settings:get("protector_hurt")) or 0
@ -41,40 +40,40 @@ local protector_recipe = minetest.settings:get_bool("protector_recipe") ~= false
local protector_msg = minetest.settings:get_bool("protector_msg") ~= false
-- radius limiter (minetest cannot handle node volume of more than 4096000)
if protector_radius > 30 then protector_radius = 30 end
-- get static spawn position
local statspawn = minetest.string_to_pos(minetest.settings:get("static_spawnpoint"))
or {x = 0, y = 2, z = 0}
-- return list of members as a table
local get_member_list = function(meta)
local function get_member_list(meta)
return meta:get_string("members"):split(" ")
end
-- write member list table in protector meta as string
local set_member_list = function(meta, list)
local function set_member_list(meta, list)
meta:set_string("members", table.concat(list, " "))
end
-- check for owner name
local is_owner = function(meta, name)
local function is_owner(meta, name)
return name == meta:get_string("owner")
end
-- check for member name
local is_member = function(meta, name)
if factions_available
and meta:get_int("faction_members") == 1 then
local function is_member(meta, name)
if factions_available and meta:get_int("faction_members") == 1 then
if factions.version == nil then
@ -103,48 +102,37 @@ local is_member = function(meta, name)
for _, n in pairs(get_member_list(meta)) do
if n == name then
return true
end
if n == name then return true end
end
return false
end
-- add player name to table as member
local add_member = function(meta, name)
local function add_member(meta, name)
-- Validate player name for MT compliance
if name ~= string.match(name, "[%w_-]+") then
return
end
if name ~= string.match(name, "[%w_-]+") then return end
-- Constant (20) defined by player.h
if name:len() > 25 then
return
end
if name:len() > 25 then return end
-- does name already exist?
if is_owner(meta, name)
or is_member(meta, name) then
return
end
if is_owner(meta, name) or is_member(meta, name) then return end
local list = get_member_list(meta)
if #list >= protector_max_share_count then
return
end
if #list >= protector_max_share_count then return end
table.insert(list, name)
set_member_list(meta, list)
end
-- remove player name from table
local del_member = function(meta, name)
local function del_member(meta, name)
local list = get_member_list(meta)
@ -159,9 +147,9 @@ local del_member = function(meta, name)
set_member_list(meta, list)
end
-- protector interface
local protector_formspec = function(meta)
local function protector_formspec(meta)
local formspec = "size[8,7]"
.. default.gui_bg
@ -201,9 +189,7 @@ local protector_formspec = function(meta)
.. ";" .. (meta:get_int("faction_members") == 1 and
"true" or "false") .. "]"
if npp > 8 then
npp = 8
end
if npp > 8 then npp = 8 end
end
for n = 1, #members do
@ -240,35 +226,26 @@ local protector_formspec = function(meta)
return formspec
end
-- check if pos is inside a protected spawn area
local inside_spawn = function(pos, radius)
if protector_spawn <= 0 then
return false
end
local function inside_spawn(pos, radius)
if pos.x < statspawn.x + radius
and pos.x > statspawn.x - radius
and pos.y < statspawn.y + radius
and pos.y > statspawn.y - radius
and pos.z < statspawn.z + radius
and pos.z > statspawn.z - radius then
if protector_spawn <= 0 then return false end
if pos.x < statspawn.x + radius and pos.x > statspawn.x - radius
and pos.y < statspawn.y + radius and pos.y > statspawn.y - radius
and pos.z < statspawn.z + radius and pos.z > statspawn.z - radius then
return true
end
return false
end
-- show protection message if enabled
local show_msg = function(player, msg)
local function show_msg(player, msg)
-- if messages disabled or no player name provided
if protector_msg == false or not player or player == "" then
return
end
if protector_msg == false or not player or player == "" then return end
minetest.chat_send_player(player, msg)
end
@ -280,11 +257,9 @@ end
-- 2 for "This area is owned by <owner>.
-- 3 for checking protector overlaps
protector.can_dig = function(r, pos, digger, onlyowner, infolevel)
function protector.can_dig(r, pos, digger, onlyowner, infolevel)
if not digger or not pos then
return false
end
if not digger or not pos then return false end
-- protector_bypass privileged users can override protection
if infolevel == 1
@ -298,8 +273,7 @@ protector.can_dig = function(r, pos, digger, onlyowner, infolevel)
-- is spawn area protected ?
if inside_spawn(pos, protector_spawn) then
show_msg(digger,
S("Spawn @1 has been protected up to a @2 block radius.",
show_msg(digger, S("Spawn @1 has been protected up to a @2 block radius.",
minetest.pos_to_string(statspawn), protector_spawn))
return false
@ -307,9 +281,9 @@ protector.can_dig = function(r, pos, digger, onlyowner, infolevel)
-- find the protector nodes
local pos = minetest.find_nodes_in_area(
{x = pos.x - r, y = pos.y - r, z = pos.z - r},
{x = pos.x + r, y = pos.y + r, z = pos.z + r},
{"protector:protect", "protector:protect2", "protector:protect_hidden"})
{x = pos.x - r, y = pos.y - r, z = pos.z - r},
{x = pos.x + r, y = pos.y + r, z = pos.z + r},
{"protector:protect", "protector:protect2", "protector:protect_hidden"})
local meta, owner, members
@ -325,8 +299,7 @@ protector.can_dig = function(r, pos, digger, onlyowner, infolevel)
-- and you aren't on the member list
if onlyowner or not is_member(meta, digger) then
show_msg(digger,
S("This area is owned by @1", owner) .. "!")
show_msg(digger, S("This area is owned by @1", owner) .. "!")
return false
end
@ -336,13 +309,12 @@ protector.can_dig = function(r, pos, digger, onlyowner, infolevel)
if infolevel == 2 then
minetest.chat_send_player(digger,
S("This area is owned by @1", owner) .. ".")
S("This area is owned by @1", owner) .. ".")
minetest.chat_send_player(digger,
S("Protection located at: @1", minetest.pos_to_string(pos[n])))
S("Protection located at: @1", minetest.pos_to_string(pos[n])))
if members ~= "" then
minetest.chat_send_player(digger, S("Members: @1.", members))
end
@ -355,7 +327,6 @@ protector.can_dig = function(r, pos, digger, onlyowner, infolevel)
if infolevel == 2 then
if #pos < 1 then
minetest.chat_send_player(digger, S("This area is not protected."))
end
@ -365,8 +336,8 @@ protector.can_dig = function(r, pos, digger, onlyowner, infolevel)
return true
end
-- add protector hurt and flip to protection violation function
minetest.register_on_protection_violation(function(pos, name)
local player = minetest.get_player_by_name(name)
@ -401,21 +372,18 @@ minetest.register_on_protection_violation(function(pos, name)
local pla_pos = player:get_pos()
if pos.y < pla_pos.y then
player:set_pos({
x = pla_pos.x,
y = pla_pos.y + 0.8,
z = pla_pos.z
})
player:set_pos({x = pla_pos.x, y = pla_pos.y + 0.8, z = pla_pos.z})
end
end
end
end)
-- backup old is_protected function
local old_is_protected = minetest.is_protected
-- check for protected area, return true if protected and digger isn't on list
function minetest.is_protected(pos, digger)
digger = digger or "" -- nil check
@ -429,13 +397,11 @@ function minetest.is_protected(pos, digger)
return old_is_protected(pos, digger)
end
-- make sure protection block doesn't overlap another protector's area
local check_overlap = function(itemstack, placer, pointed_thing)
if pointed_thing.type ~= "node" then
return itemstack
end
local function check_overlap(itemstack, placer, pointed_thing)
if pointed_thing.type ~= "node" then return itemstack end
local pos = pointed_thing.above
local name = placer:get_player_name()
@ -444,8 +410,8 @@ local check_overlap = function(itemstack, placer, pointed_thing)
if inside_spawn(pos, protector_spawn + protector_radius) then
minetest.chat_send_player(name,
S("Spawn @1 has been protected up to a @2 block radius.",
minetest.pos_to_string(statspawn), protector_spawn))
S("Spawn @1 has been protected up to a @2 block radius.",
minetest.pos_to_string(statspawn), protector_spawn))
return itemstack
end
@ -454,34 +420,34 @@ local check_overlap = function(itemstack, placer, pointed_thing)
if not protector.can_dig(protector_radius * 2, pos, name, true, 3) then
minetest.chat_send_player(name,
S("Overlaps into above players protected area"))
S("Overlaps into above players protected area"))
return itemstack
end
return minetest.item_place(itemstack, placer, pointed_thing)
end
-- remove protector display entities
local del_display = function(pos)
local function del_display(pos)
local objects = minetest.get_objects_inside_radius(pos, 0.5)
for _, v in ipairs(objects) do
if v and v:get_luaentity()
and v:get_luaentity().name == "protector:display" then
if v and v:get_luaentity() and v:get_luaentity().name == "protector:display" then
v:remove()
end
end
end
-- temporary pos store
-- temporary position store
local player_pos = {}
-- protection node
minetest.register_node("protector:protect", {
description = S("Protection Block") .. " (" .. S("USE for area check") .. ")",
drawtype = "nodebox",
@ -497,10 +463,7 @@ minetest.register_node("protector:protect", {
light_source = 4,
node_box = {
type = "fixed",
fixed = {
{-0.499 ,-0.499, -0.499, 0.499, 0.499, 0.499}
}
type = "fixed", fixed = {{-0.499 ,-0.499, -0.499, 0.499, 0.499, 0.499}}
},
on_place = check_overlap,
@ -517,9 +480,7 @@ minetest.register_node("protector:protect", {
on_use = function(itemstack, user, pointed_thing)
if pointed_thing.type ~= "node" then
return
end
if pointed_thing.type ~= "node" then return end
protector.can_dig(protector_radius, pointed_thing.under,
user:get_player_name(), false, 2)
@ -530,8 +491,7 @@ minetest.register_node("protector:protect", {
local meta = minetest.get_meta(pos)
local name = clicker:get_player_name()
if meta
and protector.can_dig(1, pos, name, true, 1) then
if meta and protector.can_dig(1, pos, name, true, 1) then
player_pos[name] = pos
@ -541,9 +501,7 @@ minetest.register_node("protector:protect", {
on_punch = function(pos, node, puncher)
if minetest.is_protected(pos, puncher:get_player_name()) then
return
end
if minetest.is_protected(pos, puncher:get_player_name()) then return end
minetest.add_entity(pos, "protector:display")
end,
@ -559,32 +517,29 @@ minetest.register_node("protector:protect", {
})
-- default recipe and alternative for MineClone2
if protector_recipe then
if minetest.registered_items["default:stone"] then
local item_gold = "default:gold_ingot"
local item_stone = "default:stone"
minetest.register_craft({
output = "protector:protect",
recipe = {
{"default:stone", "default:stone", "default:stone"},
{"default:stone", "default:gold_ingot", "default:stone"},
{"default:stone", "default:stone", "default:stone"}
}
})
else
minetest.register_craft({
output = "protector:protect",
recipe = {
{"mcl_core:stone", "mcl_core:stone", "mcl_core:stone"},
{"mcl_core:stone", "mcl_core:gold_ingot", "mcl_core:stone"},
{"mcl_core:stone", "mcl_core:stone", "mcl_core:stone"}
}
})
if minetest.get_modpath("mcl_core") then
item_gold = "mcl_core:gold_ingot"
item_stone = "mcl_core:stone"
end
minetest.register_craft({
output = "protector:protect",
recipe = {
{item_stone, item_stone, item_stone},
{item_stone, item_gold, item_stone},
{item_stone, item_stone, item_stone}
}
})
end
-- protection logo
minetest.register_node("protector:protect2", {
description = S("Protection Logo") .. " (" .. S("USE for area check") .. ")",
tiles = {"protector_logo.png"},
@ -623,9 +578,7 @@ minetest.register_node("protector:protect2", {
on_use = function(itemstack, user, pointed_thing)
if pointed_thing.type ~= "node" then
return
end
if pointed_thing.type ~= "node" then return end
protector.can_dig(protector_radius, pointed_thing.under,
user:get_player_name(), false, 2)
@ -636,8 +589,7 @@ minetest.register_node("protector:protect2", {
local meta = minetest.get_meta(pos)
local name = clicker:get_player_name()
if meta
and protector.can_dig(1, pos, name, true, 1) then
if meta and protector.can_dig(1, pos, name, true, 1) then
player_pos[name] = pos
@ -647,9 +599,7 @@ minetest.register_node("protector:protect2", {
on_punch = function(pos, node, puncher)
if minetest.is_protected(pos, puncher:get_player_name()) then
return
end
if minetest.is_protected(pos, puncher:get_player_name()) then return end
minetest.add_entity(pos, "protector:display")
end,
@ -665,6 +615,7 @@ minetest.register_node("protector:protect2", {
})
-- recipes to switch between protectors
minetest.register_craft({
output = "protector:protect",
recipe = {{"protector:protect2"}}
@ -675,20 +626,16 @@ minetest.register_craft({
recipe = {{"protector:protect"}}
})
-- check formspec buttons or when name entered
minetest.register_on_player_receive_fields(function(player, formname, fields)
if formname ~= "protector:node" then
return
end
if formname ~= "protector:node" then return end
local name = player:get_player_name()
local pos = player_pos[name]
if not name or not pos then
return
end
if not name or not pos then return end
local add_member_input = fields.protector_add_member
@ -707,17 +654,12 @@ minetest.register_on_player_receive_fields(function(player, formname, fields)
-- are we adding member to a protection node ? (csm protection)
local nod = minetest.get_node(pos).name
if nod ~= "protector:protect"
and nod ~= "protector:protect2" then
if nod ~= "protector:protect" and nod ~= "protector:protect2" then
player_pos[name] = nil
return
end
local meta = minetest.get_meta(pos)
if not meta then
return
end
local meta = minetest.get_meta(pos) ; if not meta then return end
-- add faction members
if factions_available and fields.faction_members ~= nil then
@ -736,22 +678,21 @@ minetest.register_on_player_receive_fields(function(player, formname, fields)
for field, value in pairs(fields) do
if string.sub(field, 0,
string.len("protector_del_member_")) == "protector_del_member_" then
string.len("protector_del_member_")) == "protector_del_member_" then
del_member(meta,
string.sub(field,string.len("protector_del_member_") + 1))
string.sub(field,string.len("protector_del_member_") + 1))
end
end
minetest.show_formspec(name, formname, protector_formspec(meta))
end)
-- display entity shown when protector node is punched
minetest.register_entity("protector:display", {
initial_properties = {
physical = false,
collisionbox = {0, 0, 0, 0, 0, 0},
visual = "wielditem",
@ -768,17 +709,15 @@ minetest.register_entity("protector:display", {
self.timer = self.timer + dtime
-- remove after set number of seconds
if self.timer > protector_show then
self.object:remove()
end
if self.timer > protector_show then self.object:remove() end
end
})
-- Display-zone node, Do NOT place the display as a node,
-- it is made to be used as an entity (see above)
local x = protector_radius
minetest.register_node("protector:display_node", {
tiles = {"protector_display.png"},
use_texture_alpha = "clip",
@ -787,27 +726,22 @@ minetest.register_node("protector:display_node", {
node_box = {
type = "fixed",
fixed = {
-- sides
{-(x+.55), -(x+.55), -(x+.55), -(x+.45), (x+.55), (x+.55)},
{-(x+.55), -(x+.55), -(x+.55), -(x+.45), (x+.55), (x+.55)}, -- sides
{-(x+.55), -(x+.55), (x+.45), (x+.55), (x+.55), (x+.55)},
{(x+.45), -(x+.55), -(x+.55), (x+.55), (x+.55), (x+.55)},
{-(x+.55), -(x+.55), -(x+.55), (x+.55), (x+.55), -(x+.45)},
-- top
{-(x+.55), (x+.45), -(x+.55), (x+.55), (x+.55), (x+.55)},
-- bottom
{-(x+.55), -(x+.55), -(x+.55), (x+.55), -(x+.45), (x+.55)},
-- middle (surround protector)
{-.55,-.55,-.55, .55,.55,.55}
{-(x+.55), (x+.45), -(x+.55), (x+.55), (x+.55), (x+.55)}, -- top
{-(x+.55), -(x+.55), -(x+.55), (x+.55), -(x+.45), (x+.55)}, -- bottom
{-.55,-.55,-.55, .55,.55,.55} -- middle (surrounding protector)
}
},
selection_box = {
type = "regular",
},
selection_box = {type = "regular"},
paramtype = "light",
groups = {dig_immediate = 3, not_in_creative_inventory = 1},
drop = ""
})
-- load mod sections
dofile(MP .. "/doors_chest.lua")
dofile(MP .. "/pvp.lua")
@ -819,13 +753,13 @@ if minetest.get_modpath("lucky_block") then
dofile(MP .. "/lucky_block.lua")
end
-- stop mesecon pistons from pushing protectors
if minetest.get_modpath("mesecons_mvps") then
mesecon.register_mvps_stopper("protector:protect")
mesecon.register_mvps_stopper("protector:protect2")
mesecon.register_mvps_stopper("protector:protect_hidden")
mesecon.register_mvps_stopper("protector:chest")
end
print ("[MOD] Protector Redo loaded")

16
pvp.lua
View File

@ -1,18 +1,20 @@
local S = protector.intllib
-- get static spawn position
local statspawn = minetest.string_to_pos(minetest.settings:get("static_spawnpoint"))
or {x = 0, y = 2, z = 0}
-- is spawn protected
local protector_spawn = tonumber(minetest.settings:get("protector_spawn")
or minetest.settings:get("protector_pvp_spawn")) or 0
or minetest.settings:get("protector_pvp_spawn")) or 0
-- is night-only pvp enabled
local protector_night_pvp = minetest.settings:get_bool("protector_night_pvp")
-- disables PVP in your own protected areas
if minetest.settings:get_bool("enable_pvp")
and minetest.settings:get_bool("protector_pvp") then
@ -21,15 +23,13 @@ and minetest.settings:get_bool("protector_pvp") then
minetest.register_on_punchplayer(function(player, hitter,
time_from_last_punch, tool_capabilities, dir, damage)
if not player
or not hitter then
if not player or not hitter then
print("[MOD] Protector - on_punchplayer called with nil objects")
end
if not hitter:is_player() then
return false
end
if not hitter:is_player() then return false end
-- no pvp at spawn area
local pos = player:get_pos()

View File

@ -1,14 +1,18 @@
-- protector placement tool (thanks to Shara for code and idea)
local S = protector.intllib
local S = minetest.get_translator("protector")
-- get protection radius
local r = tonumber(minetest.settings:get("protector_radius")) or 5
-- radius limiter (minetest cannot handle node volume of more than 4096000)
if r > 30 then r = 30 end
-- protector placement tool
minetest.register_craftitem("protector:tool", {
description = S("Protector Placer Tool (stand near protector, face direction and use)"),
inventory_image = "protector_tool.png",
@ -21,8 +25,8 @@ minetest.register_craftitem("protector:tool", {
-- check for protector near player (2 block radius)
local pos = user:get_pos()
local pp = minetest.find_nodes_in_area(
vector.subtract(pos, 2), vector.add(pos, 2),
{"protector:protect", "protector:protect2", "protector:protect_hidden"})
vector.subtract(pos, 2), vector.add(pos, 2),
{"protector:protect", "protector:protect2", "protector:protect_hidden"})
if #pp == 0 then return end -- none found
@ -62,16 +66,15 @@ minetest.register_craftitem("protector:tool", {
if not protector.can_dig(r * 2, pos, user:get_player_name(), true, 3) then
minetest.chat_send_player(name,
S("Overlaps into above players protected area"))
S("Overlaps into above players protected area"))
return
end
-- does a protector already exist ?
if #minetest.find_nodes_in_area(
vector.subtract(pos, 1), vector.add(pos, 1),
{"protector:protect", "protector:protect2",
"protector:protect_hidden"}) > 0 then
if #minetest.find_nodes_in_area(vector.subtract(pos, 1), vector.add(pos, 1),
{"protector:protect", "protector:protect2",
"protector:protect_hidden"}) > 0 then
minetest.chat_send_player(name, S("Protector already in place!"))
@ -114,17 +117,17 @@ minetest.register_craftitem("protector:tool", {
local inv = minetest.get_inventory({type = "node", pos = pos})
if inv then
minetest.chat_send_player(name,
S("Cannot place protector, container at") ..
minetest.chat_send_player(name, S("Cannot place protector, container at") ..
" " .. minetest.pos_to_string(pos))
return
end
-- protection check for other mods like Areas
if minetest.is_protected(pos, name) then
minetest.chat_send_player(name,
S("Cannot place protector, already protected at") ..
" " .. minetest.pos_to_string(pos))
S("Cannot place protector, already protected at")
.. " " .. minetest.pos_to_string(pos))
return
end
@ -147,18 +150,17 @@ minetest.register_craftitem("protector:tool", {
minetest.chat_send_player(name,
S("Protector placed at") ..
" " .. minetest.pos_to_string(pos))
end,
end
})
-- tool recipe
local df = "default:steel_ingot"
if not minetest.registered_items[df] then
if minetest.get_modpath("mcl_core") then
df = "mcl_core:iron_ingot"
end
minetest.register_craft({
output = "protector:tool",
recipe = {