Protector block cleanup

master
MoNTE48 2020-02-13 19:32:37 +01:00
parent 030b27b826
commit 582db494e6
2 changed files with 20 additions and 48 deletions

View File

@ -1,4 +1,4 @@
local creative_mode = minetest.settings:get_bool("creative_mode") local radius = minetest.settings:get("areasprotector_radius") or 8
local function cyan(str) local function cyan(str)
return minetest.colorize("#00FFFF", str) return minetest.colorize("#00FFFF", str)
@ -8,15 +8,6 @@ local function red(str)
return minetest.colorize("#FF5555", str) return minetest.colorize("#FF5555", str)
end end
local radius = minetest.settings:get("areasprotector_radius") or 8
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
minetest.register_node(":areasprotector:protector", { minetest.register_node(":areasprotector:protector", {
description = "Protector Block", description = "Protector Block",
groups = {cracky = 1}, groups = {cracky = 1},
@ -35,7 +26,7 @@ minetest.register_node(":areasprotector:protector", {
on_place = function(itemstack, player, pointed) on_place = function(itemstack, player, pointed)
local pos = pointed.above local pos = pointed.above
local pos1 = vector.add(pos, vector.new(radius, radius, radius)) local pos1 = vector.add(pos, vector.new(radius, radius, radius))
local pos2 = vector.add(pos, vector.new(-1 * radius, -1 * radius, -1 * radius)) local pos2 = vector.add(pos, vector.new(-radius, -radius, -radius))
local name = player:get_player_name() local name = player:get_player_name()
if not minetest.is_protected_action(pos, name) then if not minetest.is_protected_action(pos, name) then
@ -44,14 +35,14 @@ minetest.register_node(":areasprotector:protector", {
minetest.chat_send_player(name, red("You are not allowed to protect that area: ") .. err) minetest.chat_send_player(name, red("You are not allowed to protect that area: ") .. err)
return itemstack return itemstack
end end
local id = areas:add(name, "Protected by Protector Block", pos1, pos2) local id = areas:add(name, "Protector Block", pos1, pos2)
areas:save() 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,
minetest.chat_send_player(name, msg) ("The area from %s to %s has been protected as #%s")
:format(cyan(minetest.pos_to_string(pos1)), cyan(minetest.pos_to_string(pos2)), cyan(id)))
minetest.set_node(pos, {name = "areasprotector:protector"}) minetest.set_node(pos, {name = "areasprotector:protector"})
local meta = minetest.get_meta(pos) local meta = minetest.get_meta(pos)
local infotext = string.format("Protecting area %d owned by %s", id, name) meta:set_string("infotext", ("Protecting area %d, Owned by %s"):format(id, name))
meta:set_string("infotext", infotext)
meta:set_int("area_id", id) meta:set_int("area_id", id)
meta:set_string("owner", name) meta:set_string("owner", name)
itemstack:take_item() itemstack:take_item()
@ -59,46 +50,29 @@ minetest.register_node(":areasprotector:protector", {
end end
end, end,
after_dig_node = function(pos, oldnode, oldmetadata, digger) after_dig_node = function(_, _, oldmetadata, digger)
if oldmetadata and oldmetadata.fields then if oldmetadata and oldmetadata.fields then
local owner = oldmetadata.fields.owner local owner = oldmetadata.fields.owner
local id = tonumber(oldmetadata.fields.area_id) local id = tonumber(oldmetadata.fields.area_id)
local playername = digger:get_player_name() local playername = digger:get_player_name()
if areas.areas[id] and areas:isAreaOwner(id, owner) then if areas.areas[id] and areas:isAreaOwner(id, owner) then
if digger:get_player_control().sneak then areas:remove(id)
local inv = digger:get_inventory() areas:save()
if not creative_mode then
if inv:room_for_item("main", "default:steel_ingot 6") then
inv:remove_item("main", "areasprotector: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", "areasprotector:protector 1")
end
else
areas:remove(id)
areas:save()
end
end end
end end
end, end,
on_punch = function(pos, node, puncher) on_punch = function(pos)
local objs = minetest.get_objects_inside_radius(pos, .5) -- a radius of .5 since the entity serialization seems to be not that precise 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 local displayed = false
for _, o in pairs(objs) do for _, o in pairs(objs) do
if (not o:is_player()) and o:get_luaentity().name == "areasprotector:display" then if not o:is_player() and o:get_luaentity().name == "areasprotector:display" then
o:remove() o:remove()
removed = true return
end end
end end
if not removed then -- nothing was removed: there wasn't the entity if not displayed then -- nothing was removed: there wasn't the entity
minetest.add_entity(pos, "areasprotector:display") minetest.add_entity(pos, "areasprotector:display")
minetest.after(4, remove_display, pos)
end end
end end
}) })
@ -107,14 +81,15 @@ minetest.register_node(":areasprotector:protector", {
minetest.register_entity(":areasprotector:display", { minetest.register_entity(":areasprotector:display", {
physical = false, physical = false,
collisionbox = {0, 0, 0, 0, 0, 0}, collisionbox = {0},
visual = "wielditem", 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 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 = {"areasprotector:display_node"}, textures = {"areasprotector:display_node"},
timer = 0,
on_step = function(self, dtime) on_step = function(self, dtime)
if minetest.get_node(self.object:getpos()).name ~= "areasprotector:protector" then self.timer = self.timer + dtime
if self.timer > 4 or minetest.get_node(self.object:getpos()).name ~= "areasprotector:protector" then
self.object:remove() self.object:remove()
return
end end
end end
}) })
@ -141,9 +116,7 @@ minetest.register_node(":areasprotector:display_node", {
{-.55, -.55, -.55, .55, .55, .55} {-.55, -.55, -.55, .55, .55, .55}
} }
}, },
selection_box = { selection_box = {type = "regular"},
type = "regular"
},
paramtype = "light", paramtype = "light",
groups = {dig_immediate = 3, not_in_creative_inventory = 1}, groups = {dig_immediate = 3, not_in_creative_inventory = 1},
drop = "" drop = ""

View File

@ -35,4 +35,3 @@ areas.self_protection_max_size_high (Maximal area size) v3f (512, 512, 512)
# Only enter positive whole numbers for the coordinate values or you'll mess up stuff. # Only enter positive whole numbers for the coordinate values or you'll mess up stuff.
# This setting applies for plyaers with the privilege 'areas_high_limit' # This setting applies for plyaers with the privilege 'areas_high_limit'
areas.self_protection_max_areas_high (Maximal area count) float 32 areas.self_protection_max_areas_high (Maximal area count) float 32