-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathpvp.lua
158 lines (132 loc) · 4.5 KB
/
pvp.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
-- Private table
local pvptable = {}
-- Public table, containing global functions
pvpplus = {}
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)
if state then
pvptable[player_name].pvpenabled = 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 enabled for you!"),
number = 0xFF0000 -- Red
})
pvptable[player_name].pvppic = player:hud_add({
hud_elem_type = "image",
position = {x = 1, y = 0},
offset = {x=-210, y = 20},
scale = {x = 1, y = 1},
text = "pvp.png"
})
else
pvptable[player_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
})
pvptable[player_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"
})
end
return true
end
function pvpplus.pvp_enable(player_name)
return pvpplus.pvp_set(player_name, true)
end
function pvpplus.pvp_disable(player_name)
return pvpplus.pvp_set(player_name, false)
end
function pvpplus.pvp_toggle(playername)
if pvptable[playername].state then
return pvpplus.pvp_disable(playername)
else
return pvpplus.pvp_enable(playername)
end
end
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
return pvptable[playername].state or false
end
dofile(minetest.get_modpath(minetest.get_current_modname()).."/pvp_commands.lua")
------ Load tournaments ------
dofile(minetest.get_modpath(minetest.get_current_modname()).."/tournament.lua")
------------------------------
-- Make these functions private
local tournament_on_punchplayer = pvpplus.tournament_on_punchplayer
pvpplus.tournament_on_punchplayer = nil
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
})
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)
local localname = player:get_player_name()
local hittername = hitter:get_player_name()
if localname == hittername then
return false
end
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
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))
return true
end
return false
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