areas/protector.lua

146 lines
4.6 KiB
Lua
Raw Normal View History

2020-03-15 18:08:31 -07:00
local S = intllib.make_gettext_pair()
2020-02-13 10:32:37 -08:00
local radius = minetest.settings:get("areasprotector_radius") or 8
2019-09-08 12:20:32 -07:00
local function cyan(str)
return minetest.colorize("#00FFFF", str)
end
local function red(str)
return minetest.colorize("#FF5555", str)
end
2020-02-27 13:17:52 -08:00
minetest.register_node("areas:protector", {
2020-03-15 18:08:31 -07:00
description = S("Protector Block"),
2019-09-08 12:20:32 -07:00
groups = {cracky = 1},
tiles = {
"default_stonebrick_carved.png",
"default_stonebrick_carved.png",
2020-02-27 13:17:52 -08:00
"default_stonebrick_carved.png^areas_protector_stone.png"
2019-09-08 12:20:32 -07:00
},
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))
2020-02-13 10:32:37 -08:00
local pos2 = vector.add(pos, vector.new(-radius, -radius, -radius))
2019-09-08 12:20:32 -07:00
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
2020-03-15 18:08:31 -07:00
minetest.chat_send_player(name, red(S("You are not allowed to protect that area:") .. " ") .. err)
return itemstack
end
2020-02-13 10:32:37 -08:00
local id = areas:add(name, "Protector Block", pos1, pos2)
areas:save()
2020-02-13 10:32:37 -08:00
minetest.chat_send_player(name,
2020-08-15 11:46:09 -07:00
(S("The area from @1 to @1 has been protected as ID @1",
cyan(minetest.pos_to_string(pos1)), cyan(minetest.pos_to_string(pos2)), cyan(id))))
2020-02-27 13:17:52 -08:00
minetest.set_node(pos, {name = "areas:protector"})
local meta = minetest.get_meta(pos)
2020-08-15 11:46:09 -07:00
meta:set_string("infotext", (S("Protecting area @1, Owned by @1", id, name)))
meta:set_int("area_id", id)
meta:set_string("owner", name)
itemstack:take_item()
2019-09-08 12:20:32 -07:00
return itemstack
end
end,
2020-02-13 10:32:37 -08:00
after_dig_node = function(_, _, oldmetadata, digger)
2019-09-08 12:20:32 -07:00
if oldmetadata and oldmetadata.fields then
local owner = oldmetadata.fields.owner
local id = tonumber(oldmetadata.fields.area_id)
2020-08-15 11:46:09 -07:00
local name = digger:get_player_name()
2019-09-08 12:20:32 -07:00
if areas.areas[id] and areas:isAreaOwner(id, owner) then
2020-02-13 10:32:37 -08:00
areas:remove(id)
areas:save()
2020-08-15 11:46:09 -07:00
minetest.chat_send_player(name, (S("Removed area @1", cyan(id))))
2019-09-08 12:20:32 -07:00
end
end
end,
2020-02-13 10:32:37 -08:00
on_punch = function(pos)
2019-09-08 12:20:32 -07:00
local objs = minetest.get_objects_inside_radius(pos, .5) -- a radius of .5 since the entity serialization seems to be not that precise
2020-02-13 10:32:37 -08:00
local displayed = false
2019-09-08 12:20:32 -07:00
for _, o in pairs(objs) do
2020-02-27 13:17:52 -08:00
if not o:is_player() and o:get_luaentity().name == "areas:display" then
2019-09-08 12:20:32 -07:00
o:remove()
2020-02-13 10:32:37 -08:00
return
2019-09-08 12:20:32 -07:00
end
end
2020-02-13 10:32:37 -08:00
if not displayed then -- nothing was removed: there wasn't the entity
2020-02-27 13:17:52 -08:00
minetest.add_entity(pos, "areas:display")
2019-09-08 12:20:32 -07:00
end
end
})
-- entities code below (and above) mostly copied-pasted from Zeg9's protector mod
2020-02-27 13:17:52 -08:00
minetest.register_entity("areas:display", {
2019-09-08 12:20:32 -07:00
physical = false,
2020-02-13 10:32:37 -08:00
collisionbox = {0},
2019-09-08 12:20:32 -07:00
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
2020-02-27 13:17:52 -08:00
textures = {"areas:display_node"},
2020-02-13 10:32:37 -08:00
timer = 0,
2019-09-08 12:20:32 -07:00
on_step = function(self, dtime)
2020-02-13 10:32:37 -08:00
self.timer = self.timer + dtime
2020-03-23 04:31:10 -07:00
if self.timer > 4 or minetest.get_node(self.object:get_pos()).name ~= "areas:protector" then
2019-09-08 12:20:32 -07:00
self.object:remove()
end
end
})
local nb_radius = radius + 0.55
2020-02-27 13:17:52 -08:00
minetest.register_node("areas:display_node", {
tiles = {"areas_protector_display.png"},
2019-09-08 12:20:32 -07:00
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}
}
},
2020-02-13 10:32:37 -08:00
selection_box = {type = "regular"},
2019-09-08 12:20:32 -07:00
paramtype = "light",
groups = {dig_immediate = 3, not_in_creative_inventory = 1},
drop = ""
})
minetest.register_craft({
2020-02-27 13:17:52 -08:00
output = "areas:protector",
2019-09-08 12:20:32 -07:00
type = "shapeless",
recipe = {
"default:stonebrickcarved", "default:stonebrickcarved", "default:stonebrickcarved",
"default:stonebrickcarved", "mesecons:wire_00000000_off", "default:stonebrickcarved",
"default:stonebrickcarved", "default:stonebrickcarved", "default:stonebrickcarved"
}
})
2020-02-27 13:17:52 -08:00
2020-10-20 15:19:40 -07:00
-- MVPS stopper
if mesecon and mesecon.register_mvps_stopper then
mesecon.register_mvps_stopper("areas:protector")
end
2020-02-27 13:17:52 -08:00
-- Aliases
minetest.register_alias("areasprotector:protector", "areas:protector")
minetest.register_alias("areasprotector:display_node", "areas:display_node")