localise protector functions and tidy code

This commit is contained in:
TenPlus1 2017-09-03 10:53:49 +01:00
parent ba70325bbe
commit e36b8a7128
2 changed files with 42 additions and 37 deletions

@ -50,6 +50,7 @@ Change log:
players can hurt one another even inside protected areas (not spawn protected) players can hurt one another even inside protected areas (not spawn protected)
2.2 - Updated protector tool so that player only needs to stand nearby (2 block radius) 2.2 - Updated protector tool so that player only needs to stand nearby (2 block radius)
It can also place vertically (up and down) as well. New protector recipe added. It can also place vertically (up and down) as well. New protector recipe added.
2.3 - Localise many of the protector functions and tidy code.
Lucky Blocks: 10 Lucky Blocks: 10
@ -108,3 +109,10 @@ protector_hurt = 2
protector_flip = true protector_flip = true
- When true players who dig inside a protected area will flipped around to stop them using lag to grief into someone else's build - When true players who dig inside a protected area will flipped around to stop them using lag to grief into someone else's build
Protector Tool
Can be crafted with a protector surrounded by steel ingots and is used to place new protectors at a set distance of protector.radius in all directions including up and down.
Use by standing near an existing protector and using as a tool, hold sneak/shift to place new protector containing member list from inside nearest one.

@ -29,30 +29,30 @@ protector.intllib = S
-- return list of members as a table -- return list of members as a table
protector.get_member_list = function(meta) local get_member_list = function(meta)
return meta:get_string("members"):split(" ") return meta:get_string("members"):split(" ")
end end
-- write member list table in protector meta as string -- write member list table in protector meta as string
protector.set_member_list = function(meta, list) local set_member_list = function(meta, list)
meta:set_string("members", table.concat(list, " ")) meta:set_string("members", table.concat(list, " "))
end end
-- check if name is owner -- check for owner name
protector.is_owner = function(meta, name) local is_owner = function(meta, name)
return name == meta:get_string("owner") return name == meta:get_string("owner")
end end
-- check if player name is a member -- check for member name
protector.is_member = function (meta, name) local is_member = function (meta, name)
for _, n in pairs(protector.get_member_list(meta)) do for _, n in pairs(get_member_list(meta)) do
if n == name then if n == name then
return true return true
@ -64,26 +64,26 @@ end
-- add player name to table as member -- add player name to table as member
protector.add_member = function(meta, name) local add_member = function(meta, name)
-- does name already exist? -- does name already exist?
if protector.is_owner(meta, name) if is_owner(meta, name)
or protector.is_member(meta, name) then or is_member(meta, name) then
return return
end end
local list = protector.get_member_list(meta) local list = get_member_list(meta)
table.insert(list, name) table.insert(list, name)
protector.set_member_list(meta, list) set_member_list(meta, list)
end end
-- remove player name from table -- remove player name from table
protector.del_member = function(meta, name) local del_member = function(meta, name)
local list = protector.get_member_list(meta) local list = get_member_list(meta)
for i, n in pairs(list) do for i, n in pairs(list) do
@ -93,12 +93,12 @@ protector.del_member = function(meta, name)
end end
end end
protector.set_member_list(meta, list) set_member_list(meta, list)
end end
-- protector interface -- protector interface
protector.generate_formspec = function(meta) local protector_formspec = function(meta)
local formspec = "size[8,7]" local formspec = "size[8,7]"
.. default.gui_bg .. default.gui_bg
@ -109,7 +109,7 @@ protector.generate_formspec = function(meta)
.. "label[0,2;" .. S("Members:") .. "]" .. "label[0,2;" .. S("Members:") .. "]"
.. "button_exit[2.5,6.2;3,0.5;close_me;" .. S("Close") .. "]" .. "button_exit[2.5,6.2;3,0.5;close_me;" .. S("Close") .. "]"
local members = protector.get_member_list(meta) local members = get_member_list(meta)
local npp = 12 -- max users added to protector list local npp = 12 -- max users added to protector list
local i = 0 local i = 0
@ -149,7 +149,7 @@ end
-- check if pos is inside a protected spawn area -- check if pos is inside a protected spawn area
local function inside_spawn(pos, radius) local inside_spawn = function(pos, radius)
if protector.spawn <= 0 then if protector.spawn <= 0 then
return false return false
@ -195,7 +195,7 @@ protector.can_dig = function(r, pos, digger, onlyowner, infolevel)
minetest.chat_send_player(digger, minetest.chat_send_player(digger,
S("Spawn @1 has been protected up to a @2 block radius.", S("Spawn @1 has been protected up to a @2 block radius.",
minetest.pos_to_string(statspawn), protector.spawn)) minetest.pos_to_string(statspawn), protector.spawn))
return false return false
end end
@ -218,7 +218,7 @@ protector.can_dig = function(r, pos, digger, onlyowner, infolevel)
if infolevel == 1 and owner ~= digger then if infolevel == 1 and owner ~= digger then
-- and you aren't on the member list -- and you aren't on the member list
if onlyowner or not protector.is_member(meta, digger) then if onlyowner or not is_member(meta, digger) then
minetest.chat_send_player(digger, minetest.chat_send_player(digger,
S("This area is owned by @1!", owner)) S("This area is owned by @1!", owner))
@ -230,16 +230,14 @@ protector.can_dig = function(r, pos, digger, onlyowner, infolevel)
-- when using protector as tool, show protector information -- when using protector as tool, show protector information
if infolevel == 2 then if infolevel == 2 then
minetest.chat_send_player(digger, 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, 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 if members ~= "" then
minetest.chat_send_player(digger, minetest.chat_send_player(digger, S("Members: @1.", members))
S("Members: @1.", members))
end end
return false return false
@ -252,8 +250,7 @@ protector.can_dig = function(r, pos, digger, onlyowner, infolevel)
if #pos < 1 then if #pos < 1 then
minetest.chat_send_player(digger, minetest.chat_send_player(digger, S("This area is not protected."))
S("This area is not protected."))
end end
minetest.chat_send_player(digger, S("You can build here.")) minetest.chat_send_player(digger, S("You can build here."))
@ -263,7 +260,7 @@ protector.can_dig = function(r, pos, digger, onlyowner, infolevel)
end end
protector.old_is_protected = minetest.is_protected local old_is_protected = minetest.is_protected
-- check for protected area, return true if protected and digger isn't on list -- check for protected area, return true if protected and digger isn't on list
function minetest.is_protected(pos, digger) function minetest.is_protected(pos, digger)
@ -317,12 +314,12 @@ function minetest.is_protected(pos, digger)
end end
-- otherwise can dig or place -- otherwise can dig or place
return protector.old_is_protected(pos, digger) return old_is_protected(pos, digger)
end end
-- make sure protection block doesn't overlap another protector's area -- make sure protection block doesn't overlap another protector's area
function protector.check_overlap(itemstack, placer, pointed_thing) local check_overlap = function(itemstack, placer, pointed_thing)
if pointed_thing.type ~= "node" then if pointed_thing.type ~= "node" then
return itemstack return itemstack
@ -377,7 +374,7 @@ minetest.register_node("protector:protect", {
} }
}, },
on_place = protector.check_overlap, on_place = check_overlap,
after_place_node = function(pos, placer) after_place_node = function(pos, placer)
@ -406,7 +403,7 @@ minetest.register_node("protector:protect", {
minetest.show_formspec(clicker:get_player_name(), minetest.show_formspec(clicker:get_player_name(),
"protector:node_" .. minetest.pos_to_string(pos), "protector:node_" .. minetest.pos_to_string(pos),
protector.generate_formspec(meta)) protector_formspec(meta))
end end
end, end,
@ -460,7 +457,7 @@ minetest.register_node("protector:protect2", {
}, },
selection_box = {type = "wallmounted"}, selection_box = {type = "wallmounted"},
on_place = protector.check_overlap, on_place = check_overlap,
after_place_node = function(pos, placer) after_place_node = function(pos, placer)
@ -487,7 +484,7 @@ minetest.register_node("protector:protect2", {
if protector.can_dig(1, pos, clicker:get_player_name(), true, 1) then if protector.can_dig(1, pos, clicker:get_player_name(), true, 1) then
minetest.show_formspec(clicker:get_player_name(), minetest.show_formspec(clicker:get_player_name(),
"protector:node_" .. minetest.pos_to_string(pos), protector.generate_formspec(meta)) "protector:node_" .. minetest.pos_to_string(pos), protector_formspec(meta))
end end
end, end,
@ -541,7 +538,7 @@ minetest.register_on_player_receive_fields(function(player, formname, fields)
if fields.protector_add_member then if fields.protector_add_member then
for _, i in pairs(fields.protector_add_member:split(" ")) do for _, i in pairs(fields.protector_add_member:split(" ")) do
protector.add_member(meta, i) add_member(meta, i)
end end
end end
@ -551,14 +548,14 @@ minetest.register_on_player_receive_fields(function(player, formname, fields)
if string.sub(field, 0, if string.sub(field, 0,
string.len("protector_del_member_")) == "protector_del_member_" then string.len("protector_del_member_")) == "protector_del_member_" then
protector.del_member(meta, del_member(meta,
string.sub(field,string.len("protector_del_member_") + 1)) string.sub(field,string.len("protector_del_member_") + 1))
end end
end end
-- reset formspec until close button pressed -- reset formspec until close button pressed
if not fields.close_me then if not fields.close_me then
minetest.show_formspec(player:get_player_name(), formname, protector.generate_formspec(meta)) minetest.show_formspec(player:get_player_name(), formname, protector_formspec(meta))
end end
end end
end) end)