pvp_choice/init.lua

64 lines
2.5 KiB
Lua
Raw Normal View History

2018-02-14 07:32:20 -08:00
-- mods/pvp_choice/init.lua
-- =================
2019-06-29 06:36:30 -07:00
-- See README.md for licensing and other information.
2018-02-14 07:32:20 -08:00
2019-06-29 04:22:10 -07:00
local function form()
return "size[4.7,1.3]" ..
"label[0,0;Would you like to be vulnerable and attack others this month?*]" ..
"label[0.7,0.2;*This choice can't be changed afterwards]" ..
"button_exit[0.7,1;1.2,0.1;pvp_yes;Yes]" ..
"button_exit[2.5,1;1.2,0.1;pvp_no;No]"
end
2018-02-14 07:32:20 -08:00
minetest.register_on_joinplayer(function(player)
2019-06-29 04:22:10 -07:00
minetest.after(1, function(name)
local player = minetest.get_player_by_name(name)
2018-04-05 06:40:25 -07:00
if not player then
return
end
2019-06-29 04:22:10 -07:00
if minetest.get_player_privs(name).interact and (not player:get_attribute("pvp_choice_time") or os.time() >= tonumber(player:get_attribute("pvp_choice_time"))) then
minetest.show_formspec(name, "pvp_choice:main", form())
2018-04-05 06:40:25 -07:00
end
2019-06-29 04:22:10 -07:00
end, player:get_player_name())
2018-02-14 07:32:20 -08:00
end)
minetest.register_on_player_receive_fields(function(player, formname, fields)
2018-04-05 06:40:25 -07:00
if formname ~= "pvp_choice:main" then
2018-02-14 07:32:20 -08:00
return
end
2019-06-29 04:22:10 -07:00
local name = player:get_player_name()
if fields.quit and not (fields.pvp_yes or fields.pvp_no) then
minetest.after(1, function(name)
minetest.show_formspec(name, "pvp_choice:main", form())
end, name)
return
end
2018-02-14 07:32:20 -08:00
if fields.pvp_yes then
2018-04-05 06:40:25 -07:00
player:set_attribute("pvp_choice", 1)
player:set_attribute("pvp_choice_time", os.time() + (60 * 60 * 24 * 31))
2019-06-29 04:22:10 -07:00
minetest.chat_send_player(name, "[Server]: You are now vulnerable and can attack others this month")
2018-02-14 07:32:20 -08:00
elseif fields.pvp_no then
2018-04-05 06:40:25 -07:00
player:set_attribute("pvp_choice", 0)
player:set_attribute("pvp_choice_time", os.time() + (60 * 60 * 24 * 31))
2019-06-29 04:22:10 -07:00
minetest.chat_send_player(name, "[Server]: You can't be attacked and can't attack others this month")
2018-02-14 07:32:20 -08:00
end
end)
minetest.register_on_punchplayer(function(player, hitter, time_from_last_punch, tool_capabilities, dir, damage)
if not hitter:is_player() then
return
end
local hitter_name = hitter:get_player_name()
2018-04-05 06:40:25 -07:00
local player_name = player:get_player_name()
if tonumber(player:get_attribute("pvp_choice")) == 0 or tonumber(hitter:get_attribute("pvp_choice")) == 0 then
if tonumber(player:get_attribute("pvp_choice")) == 0 then
2019-06-29 04:22:10 -07:00
minetest.chat_send_player(hitter_name, "[Server]: You can't attack ".. player_name .." because he has decide against PVP this month")
2018-02-14 07:32:20 -08:00
else
2019-06-29 04:22:10 -07:00
minetest.chat_send_player(hitter_name, "[Server]: You can't attack ".. player_name .." because you have decide against PVP this month")
2018-02-14 07:32:20 -08:00
end
return true
end
return
end)
2019-06-29 04:22:10 -07:00