Disable PvP when Y < 0

This commit is contained in:
2026-04-11 15:04:08 -04:00
parent 750a51f38d
commit c94ab3e0dc
4 changed files with 19 additions and 121 deletions

1
.gitignore vendored Normal file
View File

@@ -0,0 +1 @@
*.swp

View File

@@ -1,24 +1,9 @@
# PVP Choice
# PVP Zones
PVP Choice empowers players with the ability to toggle Player vs Player (PVP) interactions, providing a customizable experience in player interactions. This tool is ideal for those who prefer a more collaborative and peaceful environment or for times when engaging in combat isn't desired.
## Features
- **PVP Toggle Command**: Players can use `/toggle_pvp` to switch their PVP setting on or off.
- **Flexible Player Interactions**: If any player involved in a conflict has PVP disabled, damage between them is prevented.
- **Intuitive Player Settings**: PVP preferences are stored as player metadata, maintaining individual choices across sessions.
## Commands
- `/toggle_pvp`: Enables or disables PVP for the player issuing the command. By default, PVP is disabled.
## Compatibility
- Designed to integrate seamlessly with existing player interaction systems.
- Compatible with various player and combat-related functionalities.
PVP Zones toggles Player vs Player (PVP) interactions based on Y position (enabled in negative, disabled in positive). Fork of [PvP Toggle](https://content.luanti.org/packages/Impulse/pvp_toggle/).
## License
This project is under the GPLv3-or-later license. For more information, see the [LICENSE](./LICENSE) file.
---
---

112
init.lua
View File

@@ -1,4 +1,4 @@
pvp_choice = {}
pvp_zones = {}
-- Function to remove nearby mcl_bows:arrow_entity around a player
local function remove_nearby_arrows(player, radius)
@@ -20,112 +20,24 @@ local function remove_nearby_arrows(player, radius)
end
end
-- Function to toggle PvP for a player
local function toggle_pvp(player)
local pvp_setting = player:get_meta():get_string("pvp_enabled")
if pvp_setting == "" or pvp_setting == "false" then
player:get_meta():set_string("pvp_enabled", "true")
minetest.chat_send_player(player:get_player_name(), "PvP is now enabled for you.")
else
player:get_meta():set_string("pvp_enabled", "false")
minetest.chat_send_player(player:get_player_name(), "PvP is now disabled for you.")
end
end
-- Function to check if PvP is enabled for a player
local function is_pvp_enabled(player)
local pvp_setting = player:get_meta():get_string("pvp_enabled")
return pvp_setting ~= "false"
end
-- Registering the /toggle_pvp command
minetest.register_chatcommand("toggle_pvp", {
description = "Toggle PvP on or off",
privs = {interact = true},
func = function(name)
local player = minetest.get_player_by_name(name)
if player then
toggle_pvp(player)
end
end,
})
-- Register the on punch player event
minetest.register_on_punchplayer(function(player, hitter, time_from_last_punch, tool_capabilities, dir, damage)
-- Check if both player and hitter are valid and are players
if not player or not hitter or not player:is_player() or not hitter:is_player() then
return
end
local player_pvp_setting = player:get_meta():get_string("pvp_enabled")
local hitter_pvp_setting = hitter:get_meta():get_string("pvp_enabled")
-- Check if PvP is disabled for either player
if player_pvp_setting == "false" or hitter_pvp_setting == "false" then
-- allow PvP only when either player's Y coordinate is below 0
local ppos = player:get_pos()
local hpos = hitter:get_pos()
if not (ppos and hpos) then
return
end
if not (ppos.y < 0 or hpos.y < 0) then
if minetest.get_modpath("mcl_burning") then
mcl_burning.extinguish(player) -- Extinguish the player if they are on fire
mcl_burning.extinguish(player)
end
return true -- Cancel the punch event
core.chat_send_player(hitter:get_player_name(), "No PvP in Y > 0")
return true -- cancel punch when neither is below y < 0
end
-- otherwise allow PvP (fall through)
end)
-- Initialize PvP setting for each player as they join
minetest.register_on_joinplayer(function(player)
-- Default to PvP off if not already set
if player:get_meta():get_string("pvp_enabled") == "" then
player:get_meta():set_string("pvp_enabled", "false")
end
end)
if minetest.get_modpath("mcl_inventory") then
-- Override the damage handling function
local original_damage_function = mcl_damage.run_modifiers
mcl_damage.run_modifiers = function(obj, damage, reason)
-- Check if obj and reason.source are valid and if the damage is caused by a projectile
if obj and obj:is_player() and reason.source and reason.source:is_player() then
-- Check PvP settings for both players
if not is_pvp_enabled(obj) or not is_pvp_enabled(reason.source) then
remove_nearby_arrows(obj, 5) -- Remove attached arrows to the player
mcl_hunger.stop_poison(obj) -- Stop poisoning the player
mcl_potions._reset_player_effects(obj) -- Remove all potion effects from the player
mcl_burning.extinguish(obj) -- Extinguish the player if they are on fire
return 0 -- No damage if PvP is disabled for either player
end
end
-- Call the original damage function for all other cases
return original_damage_function(obj, damage, reason)
end
minetest.log("action", "[PvP Mod] mcl_inventory modpath found. Registering PvP tab.")
mcl_inventory.register_survival_inventory_tab({
id = "pvp_toggle",
description = "PvP Toggle",
item_icon = "mcl_tools:sword_diamond", -- Replace with an appropriate icon
show_inventory = true,
build = function(player)
minetest.log("action", "[PvP Mod] Building PvP formspec for player " .. player:get_player_name())
local pvp_setting = player:get_meta():get_string("pvp_enabled")
local button_label = pvp_setting == "true" and "Disable PvP" or "Enable PvP"
return "label[1,1;PvP Settings]" ..
"button[2,2;3,1;toggle_pvp;" .. button_label .. "]"
end,
handle = function(player, fields)
minetest.log("action", "[PvP Mod] PvP tab pressed by" .. player:get_player_name())
if fields.toggle_pvp then
minetest.log("action", "[PvP Mod] PvP toggle button pressed by " .. player:get_player_name())
toggle_pvp(player)
mcl_inventory.update_inventory(player) -- Update inventory to refresh the tab
end
end,
})
minetest.log("action", "[PvP Mod] PvP tab registered.")
else
minetest.log("error", "[PvP Mod] mcl_inventory modpath not found. PvP tab not registered.")
end

View File

@@ -1,5 +1,5 @@
name = pvp_toggle
title = PVP Toggle
description = Enables you to choose weather you want to PVP or not.
name = pvp_zones
title = PVP Zones
description = Enables PvP in specific zones
supported_games = *
optional_depends = mcl_inventory, mcl_damage, mcl_burning, mcl_hunger, mcl_potions