pvpplus/pvp.lua

159 lines
4.5 KiB
Lua
Raw Normal View History

2017-02-05 12:10:29 -08:00
-- Private table
local pvptable = {}
-- Public table, containing global functions
pvpplus = {}
2019-02-11 08:50:31 -08:00
local MP = minetest.get_modpath(minetest.get_current_modname())
local S, NS = dofile(
MP .. "/intllib.lua"
)
minetest.register_privilege("pvp", S("Can change own PvP state"))
minetest.register_privilege("pvp_admin", S("Can change others PvP state"))
function pvpplus.pvp_set(player_name, state)
if pvpplus.is_playing_tournament(player_name) then
return false, S("PvP state cannot be changed while playing a tournament.")
end
if type(state) ~= "boolean" then
return false, S("The state parameter has to be a boolean.")
end
local player = minetest.get_player_by_name(player_name)
if not player then
return false, string.format(S("Player %s does not exist or is not currently connected."), player_name)
end
pvptable[player_name].state = state
minetest.chat_send_player(player_name, ((state and S("Your PvP has been enabled")) or S("Your PvP has been disabled")))
player:hud_remove((state and pvptable[player_name].pvpdisabled) or pvptable[player_name].pvpenabled)
player:hud_remove((state and pvptable[player_name].nopvppic) or pvptable[player_name].pvppic)
2017-03-19 08:45:14 -07:00
if state then
2017-04-01 02:46:46 -07:00
pvptable[player_name].pvpenabled = player:hud_add({
2017-03-19 08:45:14 -07:00
hud_elem_type = "text",
position = {x = 1, y = 0},
offset = {x=-125, y = 20},
scale = {x = 100, y = 100},
text = S("PvP is enabled for you!"),
2017-03-19 08:45:14 -07:00
number = 0xFF0000 -- Red
})
2017-04-01 02:46:46 -07:00
pvptable[player_name].pvppic = player:hud_add({
2017-03-19 08:45:14 -07:00
hud_elem_type = "image",
position = {x = 1, y = 0},
offset = {x=-210, y = 20},
scale = {x = 1, y = 1},
text = "pvp.png"
})
else
2017-04-01 02:46:46 -07:00
pvptable[player_name].pvpdisabled = player:hud_add({
2017-03-19 08:45:14 -07:00
hud_elem_type = "text",
position = {x = 1, y = 0},
offset = {x=-125, y = 20},
scale = {x = 100, y = 100},
text = S("PvP is disabled for you!"),
2017-03-19 08:45:14 -07:00
number = 0x7DC435
})
2017-04-01 02:46:46 -07:00
pvptable[player_name].nopvppic = player:hud_add({
2017-03-19 08:45:14 -07:00
hud_elem_type = "image",
position = {x = 1, y = 0},
offset = {x = -210, y = 20},
scale = {x = 1, y = 1},
text = "nopvp.png"
})
end
2017-02-05 12:10:29 -08:00
return true
end
2017-03-19 08:45:14 -07:00
function pvpplus.pvp_enable(player_name)
return pvpplus.pvp_set(player_name, true)
end
2017-02-05 12:10:29 -08:00
2017-03-19 08:45:14 -07:00
function pvpplus.pvp_disable(player_name)
return pvpplus.pvp_set(player_name, false)
2017-02-05 12:10:29 -08:00
end
function pvpplus.pvp_toggle(playername)
2017-04-01 02:46:46 -07:00
if pvptable[playername].state then
2017-02-05 12:10:29 -08:00
return pvpplus.pvp_disable(playername)
else
return pvpplus.pvp_enable(playername)
end
end
2017-03-19 08:45:14 -07:00
function pvpplus.is_pvp(playername)
if not pvptable[playername] then
return false, string.format(S("Player %s does not exist or is not currently connected."), playername)
end
2017-04-01 02:46:46 -07:00
return pvptable[playername].state or false
2017-03-19 08:45:14 -07:00
end
dofile(minetest.get_modpath(minetest.get_current_modname()).."/pvp_commands.lua")
2017-02-05 12:10:29 -08:00
2017-04-16 07:57:09 -07:00
------ Load tournaments ------
2017-02-05 12:10:29 -08:00
dofile(minetest.get_modpath(minetest.get_current_modname()).."/tournament.lua")
2017-04-16 07:57:09 -07:00
------------------------------
2017-02-05 12:10:29 -08:00
-- Make these functions private
local tournament_on_punchplayer = pvpplus.tournament_on_punchplayer
pvpplus.tournament_on_punchplayer = nil
2017-04-16 07:57:09 -07:00
2017-02-05 12:10:29 -08:00
minetest.register_on_joinplayer(function(player)
local name = player:get_player_name()
pvptable[name] = {state = false}
pvptable[name].nopvppic = player:hud_add({
hud_elem_type = "image",
position = {x = 1, y = 0},
offset = {x = -210, y = 20},
scale = {x = 1, y = 1},
text = "nopvp.png"
})
pvptable[name].pvpdisabled = player:hud_add({
hud_elem_type = "text",
position = {x = 1, y = 0},
offset = {x=-125, y = 20},
scale = {x = 100, y = 100},
text = S("PvP is disabled for you!"),
number = 0x7DC435
})
2017-02-05 12:10:29 -08:00
end)
minetest.register_on_punchplayer(function(player, hitter, time_from_last_punch, tool_capabilities, dir, damage)
if not hitter:is_player() then
return false
end
tournament_on_punchplayer(player, hitter, damage)
2017-02-05 12:10:29 -08:00
local localname = player:get_player_name()
local hittername = hitter:get_player_name()
if localname == hittername then
return false
end
2017-04-01 02:46:46 -07:00
if not pvptable[localname].state then
minetest.chat_send_player(hittername, string.format(S("You can't hit %s because their PvP is disabled."), localname))
return true
end
2017-04-01 02:46:46 -07:00
if not pvptable[hittername].state then
minetest.chat_send_player(hittername, string.format(S("You can't hit %s because your PvP is disabled."), localname))
2017-02-05 12:10:29 -08:00
return true
end
return false
2017-02-05 12:10:29 -08:00
end)
local old_calculate_knockback = minetest.calculate_knockback
function minetest.calculate_knockback(player, hitter, ...)
if not pvpplus.is_pvp(player:get_player_name()) or not pvpplus.is_pvp(hitter:get_player_name()) then
return 0
end
return old_calculate_knockback(player, hitter, ...)
end