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

Add option to enable PvP by default #3

Merged
merged 4 commits into from
May 19, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 7 additions & 3 deletions chatcommands.lua
Original file line number Diff line number Diff line change
Expand Up @@ -493,7 +493,7 @@ minetest.register_chatcommand("area_pvp", {
S("Area @1 does not exist or is not owned by you.", id)
end

local canPvP = areas.areas[id].canPvP
local canPvP = areas:canPvP(id)

if not canPvP then
local players = {}
Expand All @@ -513,8 +513,12 @@ minetest.register_chatcommand("area_pvp", {
end
end

-- Save false as nil to avoid inflating the DB.
areas.areas[id].canPvP = not canPvP or nil
if areas.config.pvp_by_default == not canPvP then
-- Save the default value as nil to avoid inflating the DB.
areas.areas[id].canPvP = nil
else
areas.areas[id].canPvP = not canPvP
end
areas:save()
return true, S("PvP is @1 in area @2.",
not canPvP and S("enabled") or S("disabled"), id)
Expand Down
7 changes: 6 additions & 1 deletion hud.lua
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,12 @@ local function createAreaString(area, id)
tinsert(parts, " [" .. S("Open") .. "]")
end

if area.canPvP and not creative_mode then
if areas.config.pvp_by_default then
-- Compare with false as nil = default
if area.canPvP == false and not creative_mode then
tinsert(parts, " [" .. S("PvP disabled") .. "]")
end
elseif area.canPvP and not creative_mode then
tinsert(parts, " [" .. S("PvP enabled") .. "]")
end

Expand Down
8 changes: 5 additions & 3 deletions interact.lua
Original file line number Diff line number Diff line change
Expand Up @@ -38,14 +38,16 @@ minetest.register_on_protection_violation(function(pos, name)
end)

local function can_pvp_at(pos)
local default = areas.config.pvp_by_default
for id in pairs(areas:getAreasAtPos(pos)) do
-- This uses areas:canPvP instead of area.canPvP in case areas:canPvP
-- is overridden
if areas:canPvP(id) then
return true
local value = areas:canPvP(id)
if value ~= default then
return value
end
end
return false
return default
end

minetest.register_on_punchplayer(function(player, hitter, time_from_last_punch)
Expand Down
7 changes: 3 additions & 4 deletions internal.lua
Original file line number Diff line number Diff line change
Expand Up @@ -167,11 +167,10 @@ end
-- @returns true or false (enabled or disabled)
function areas:canPvP(id)
local area = self.areas[id]
if not area then
return true
if not area or area.canPvP == nil then
return areas.config.pvp_by_default
end
-- canPvP is nil when false
return area.canPvP or false
return area.canPvP
end

-- Checks if a area between two points is entirely contained by another area.
Expand Down
1 change: 1 addition & 0 deletions settings.lua
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ end
--------------

setting("string", "filename", world_path.."/areas.dat")
setting("boolean", "pvp_by_default", false)

-- Allow players with a privilege create their own areas
-- within the maximum size and number.
Expand Down
3 changes: 3 additions & 0 deletions settingtypes.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
# Static paths do not work well with settings
#areas.filename (Configuration file path) string (world_path)/areas.dat

# When enabled, makes PvP opt-out rather than opt-in.
MoNTE48 marked this conversation as resolved.
Show resolved Hide resolved
areas.pvp_by_default (PvP by default) bool false

# Allow players with a privilege create their own areas using /protect
# within the specified size and amount limits.
areas.self_protection (Self protection) bool false
Expand Down
Loading