areas/interact.lua

124 lines
3.0 KiB
Lua
Raw Normal View History

2021-04-24 03:55:20 -07:00
local S = areas.S
2020-03-15 18:08:31 -07:00
2021-05-19 01:42:16 -07:00
local enable_damage = minetest.settings:get_bool("enable_damage")
2013-12-17 17:31:11 -08:00
local old_is_protected = minetest.is_protected
2019-09-07 06:05:19 -07:00
local disallowed = {
2019-09-10 10:49:22 -07:00
["^[A-Za-z]+[0-9][0-9][0-9]"] = "You play using an unofficial client. Your actions are limited. "..
"Download \"MultiCraft ― Build and Mine!\" on Google Play / App Store to play ad-free!"
2019-09-07 06:05:19 -07:00
}
2020-02-14 08:04:09 -08:00
local function old_version(name)
local info = minetest.get_player_information(name)
if info and info.version_string and info.version_string < "0.4.16" then
return true
end
end
2019-09-07 06:05:19 -07:00
-- Disable some actions for Guests
function minetest.is_protected_action(pos, name)
2020-02-14 08:04:09 -08:00
for r, reason in pairs(disallowed) do
if name:lower():find(r) then
if old_version(name) then
minetest.chat_send_player(name, reason)
return true
end
2019-09-07 06:05:19 -07:00
end
end
if not areas:canInteract(pos, name) then
return true
end
return old_is_protected(pos, name)
end
--==--
2013-12-15 14:47:56 -08:00
function minetest.is_protected(pos, name)
if not areas:canInteract(pos, name) then
return true
end
2013-12-15 14:47:56 -08:00
return old_is_protected(pos, name)
end
2013-12-15 14:47:56 -08:00
minetest.register_on_protection_violation(function(pos, name)
if not areas:canInteract(pos, name) then
local owners = areas:getNodeOwners(pos)
minetest.chat_send_player(name,
2020-03-08 14:15:00 -07:00
S("@1 is protected by @2.",
2013-12-15 14:47:56 -08:00
minetest.pos_to_string(pos),
table.concat(owners, ", ")))
2020-03-15 18:07:49 -07:00
-- Little damage player
local player = minetest.get_player_by_name(name)
if player and player:is_player() then
2021-05-19 01:42:16 -07:00
if enable_damage then
local hp = player:get_hp()
if hp and hp > 2 then
player:set_hp(hp - 1)
end
end
local player_pos = player:get_pos()
if pos.y < player_pos.y then
player:set_pos({
x = player_pos.x,
y = player_pos.y + 1,
z = player_pos.z
})
2020-03-15 18:07:49 -07:00
end
end
2013-09-02 16:16:14 -07:00
end
2013-12-15 14:47:56 -08:00
end)
2021-05-19 01:42:16 -07:00
2021-06-27 08:59:23 -07:00
minetest.register_on_punchplayer(function(player, hitter, time_from_last_punch)
2021-05-19 01:42:16 -07:00
if not enable_damage then
2021-06-27 08:59:23 -07:00
return true
2021-05-19 01:42:16 -07:00
end
-- If it's a mob, deal damage as usual
if not hitter or not hitter:is_player() then
return false
end
-- It is possible to use cheats
if time_from_last_punch < 0.25 then
minetest.chat_send_player(hitter:get_player_name(), S("Wow, wow, take it easy!"))
return true
end
2021-06-27 08:59:23 -07:00
local hitterInPvP
-- Check if the hitter is in an area with allowed PvP
local hitterAreas = areas:getAreasAtPos(hitter:get_pos())
2021-05-19 01:42:16 -07:00
-- If the table is empty, PvP is not allowed
2021-06-27 08:59:23 -07:00
if not next(hitterAreas) then
2021-05-19 01:42:16 -07:00
return true
end
-- Do any of the areas have allowed PvP?
2021-06-27 08:59:23 -07:00
for id, area in pairs(hitterAreas) do
2021-05-19 01:42:16 -07:00
if area.canPvP then
2021-06-27 08:59:23 -07:00
hitterInPvP = true
break
end
end
if hitterInPvP then
-- Check if the victim is in an area with allowed PvP
local victimAreas = areas:getAreasAtPos(player:get_pos())
-- If the table is empty, PvP is not allowed
if not next(victimAreas) then
return true
end
-- Do any of the areas have allowed PvP?
for id, area in pairs(victimAreas) do
if area.canPvP then
return false
end
2021-05-19 01:42:16 -07:00
end
end
-- Otherwise, it doesn't do damage
minetest.chat_send_player(hitter:get_player_name(), S("PvP is not allowed in this area!"))
return true
end)