Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Worldedit Selections (optional) #15

Open
wants to merge 11 commits into
base: master
Choose a base branch
from
1 change: 1 addition & 0 deletions depends.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
worldedit?
81 changes: 59 additions & 22 deletions pos.lua
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,17 @@ areas.set_pos = {}
areas.pos1 = {}
areas.pos2 = {}

function areas.useWorldedit(playerName)
if nil ~= worldedit then
if nil == playerName then
return true
elseif minetest.check_player_privs(playerName, {worldedit = true}) then
return true
end
end
return false
end

minetest.register_chatcommand("select_area", {
params = "<ID>",
description = "Select a area by id.",
Expand Down Expand Up @@ -101,13 +112,13 @@ minetest.register_chatcommand("area_pos", {
return true, "Select position 2 by punching a node."
elseif param == "get" then -- Display current area positions
local pos1str, pos2str = "Position 1: ", "Position 2: "
if areas.pos1[name] then
pos1str = pos1str..minetest.pos_to_string(areas.pos1[name])
if nil ~= areas:getPos1(name) then
pos1str = pos1str..minetest.pos_to_string(areas:getPos1(name))
else
pos1str = pos1str.."<not set>"
end
if areas.pos2[name] then
pos2str = pos2str..minetest.pos_to_string(areas.pos2[name])
if nil ~= areas:getPos2(name) then
pos2str = pos2str..minetest.pos_to_string(areas:getPos2(name))
else
pos2str = pos2str.."<not set>"
end
Expand All @@ -117,26 +128,52 @@ minetest.register_chatcommand("area_pos", {
end
end,
})

function areas:getPos(playerName)
local pos1, pos2 = areas.pos1[playerName], areas.pos2[playerName]
if not (pos1 and pos2) then
return nil
end
-- Copy positions so that the area table doesn't contain multiple
-- references to the same position.
pos1, pos2 = vector.new(pos1), vector.new(pos2)
return areas:sortPos(pos1, pos2)
local pos1, pos2 = nil, nil
if areas.useWorldedit(playerName) then
pos1, pos2 = worldedit.pos1[playerName], worldedit.pos2[playerName]
else
pos1, pos2 = areas.pos1[playerName], areas.pos2[playerName]
end

if not (pos1 and pos2) then
return nil
end
-- Copy positions so that the area table doesn't contain multiple
-- references to the same position.
pos1, pos2 = vector.new(pos1), vector.new(pos2)
return areas:sortPos(pos1, pos2)
end

function areas:getPos1(playerName)
local pos1, pos2 = areas:getPos(playerName)
return pos1
end

function areas:getPos2(playerName)
local pos1, pos2 = areas:getPos(playerName)
return pos2
end

function areas:setPos1(playerName, pos)
areas.pos1[playerName] = pos
areas.markPos1(playerName)
if areas.useWorldedit(playerName) then
worldedit.pos1[playerName] = pos
worldedit.mark_pos1(playerName)
else
areas.pos1[playerName] = pos
areas.markPos1(playerName)
end
end

function areas:setPos2(playerName, pos)
areas.pos2[playerName] = pos
areas.markPos2(playerName)
if areas.useWorldedit(playerName) then
worldedit.pos2[playerName] = pos
worldedit.mark_pos2(playerName)
else
areas.pos2[playerName] = pos
areas.markPos2(playerName)
end
end


Expand All @@ -145,21 +182,21 @@ minetest.register_on_punchnode(function(pos, node, puncher)
-- Currently setting position
if name ~= "" and areas.set_pos[name] then
if areas.set_pos[name] == "pos1" then
areas.pos1[name] = pos
areas:setPos1(name, pos)
areas.markPos1(name)
areas.set_pos[name] = "pos2"
minetest.chat_send_player(name,
"Position 1 set to "
..minetest.pos_to_string(pos))
elseif areas.set_pos[name] == "pos1only" then
areas.pos1[name] = pos
areas:pos1(name, pos)
areas.markPos1(name)
areas.set_pos[name] = nil
minetest.chat_send_player(name,
"Position 1 set to "
..minetest.pos_to_string(pos))
elseif areas.set_pos[name] == "pos2" then
areas.pos2[name] = pos
areas:setPos2(name, pos)
areas.markPos2(name)
areas.set_pos[name] = nil
minetest.chat_send_player(name,
Expand Down Expand Up @@ -187,7 +224,7 @@ end

-- Marks area position 1
areas.markPos1 = function(name)
local pos = areas.pos1[name]
local pos = areas.getPos1(name)
if areas.marker1[name] ~= nil then -- Marker already exists
areas.marker1[name]:remove() -- Remove marker
areas.marker1[name] = nil
Expand All @@ -200,7 +237,7 @@ end

-- Marks area position 2
areas.markPos2 = function(name)
local pos = areas.pos2[name]
local pos = areas.getPos2(name)
if areas.marker2[name] ~= nil then -- Marker already exists
areas.marker2[name]:remove() -- Remove marker
areas.marker2[name] = nil
Expand Down