-
Notifications
You must be signed in to change notification settings - Fork 2
/
init.lua
215 lines (206 loc) · 10.1 KB
/
init.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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
ranks = {}
local input, errstr = io.open(minetest.get_worldpath().."/ranks.dat", "r")
if input then
ranks = minetest.deserialize(input:read("*a") or {})
io.close(input)
else
print("[rank] "..minetest.get_worldpath().."/ranks.dat failed to load! ("..errstr..")")
end
function color(hex)
return core.get_color_escape_sequence(hex)
end
rank_colors = {
wood = "#60311b",
stone = "#7f7f7f",
coal = "#2e2e2e",
iron = "#baada3",
copper = "#b64100",
bronze = "#cd7f32",
gold = "#ffd700",
mese = "#a6a600",
diamond = "#0096ff",
obsidian = "#1a1a1a",
air = "#c8e7ff",
void = "#410041",
helper = "#2859d6",
admin = "#ff0000",
moderator = "#0000ff"
}
minetest.register_privilege("rank", {description = "Can change ranks.", give_to_singleplayer = false})
minetest.register_chatcommand("rank", {
description = "Set a player's rank",
params = "<name> wood|stone|coal|iron|copper|gold|mese|diamond|obsidian|air|void|helper|moderator|admin",
privs = {server = true, rank = true},
func = function(name, param)
local target = param:split(' ')[1]
local param = param:split(' ')[2]
if not param then return false, "Invalid Usage. See /help rank." end
param = param:lower()
if param == "wood" or param == "stone" or param == "coal" or param == "iron" or param == "copper" or param == "gold" or param == "mese"
or param == "diamond" or param == "obsidian" or param == "air" or param == "void" or param == "admin" or param == "moderator" or param == "bronze" or param == "helper" then
if minetest.get_player_by_name(target) then
minetest.get_player_by_name(target):set_nametag_attributes({text = "["..color(rank_colors[param])..param..color("#ffffff").."] "..target})
ranks[target] = param
minetest.chat_send_player(name, target.."'s rank has been changed to "..param..".")
end
else
minetest.chat_send_player(name, minetest.colorize("#FF0000", "Invalid Rank: "..param))
end
end
})
minetest.register_on_joinplayer(function(player)
minetest.after(0, function()
local name = player:get_player_name()
if ranks[name] and name ~= minetest.setting_get("name") then
player:set_nametag_attributes({text = "["..color(rank_colors[ranks[name]])..ranks[name]..color("#ffffff").."] "..name})
elseif name == minetest.setting_get("name") then
player:set_nametag_attributes({text = "["..color(rank_colors["admin"]).."admin"..color("#ffffff").."] "..name})
ranks[name] = "admin"
else
ranks[name] = "wood"
player:set_nametag_attributes({text = "["..color(rank_colors[ranks[name]])..ranks[name]..color("#ffffff").."] "..name})
end
end)
end)
minetest.register_on_shutdown(function()
print("[rank] Shutting down. Saving ranks.")
local stream, err = io.open(minetest.get_worldpath().."/ranks.dat", "w")
if stream then
stream:write(minetest.serialize(ranks))
io.close(stream)
else
print("[rank] "..minetest.get_worldpath().."/ranks.dat failed to load! ("..err..")")
end
end)
minetest.register_on_chat_message(function(name, message)
if message:sub(1,1) == "/" then
return false
end
local pname
if minetest.get_modpath("morecommands") then
if nicked_players[name] then
pname = "~" .. nicked_players[name]
else
pname = name
end
else
pname = name
end
minetest.chat_send_all("<["..minetest.colorize(rank_colors[ranks[name]], ranks[name]).."] "..pname.."> "..message)
return true
end)
minetest.register_chatcommand("rankup", {
description = "Increase your rank.",
privs = {interact = true},
params = "",
func = function(name, param)
local rank = ranks[name] or "wood"
local player = minetest.get_player_by_name(name)
local wielditem = player:get_wielded_item()
if rank == "wood" then
if wielditem:get_count() >= 40 and wielditem:get_name() == "default:stone" then
wielditem:set_count(wielditem:get_count() - 25)
player:set_wielded_item(wielditem)
ranks[name] = "stone"
player:set_nametag_attributes({text = "["..color(rank_colors[ranks[name]])..ranks[name]..color("#ffffff").."] "..name})
minetest.chat_send_all("Congratulations "..name.." for upgrading to the "..color(rank_colors.stone).."stone"..color("#ffffff").." rank!")
else
minetest.chat_send_player(name, minetest.colorize("#ff0000", "ERROR: You need to hold 40 stone to upgrade."))
end
elseif rank == "stone" then
if wielditem:get_count() >= 20 and wielditem:get_name() == "default:coalblock" then
wielditem:set_count(wielditem:get_count() - 15)
player:set_wielded_item(wielditem)
ranks[name] = "coal"
player:set_nametag_attributes({text = "["..color(rank_colors[ranks[name]])..ranks[name]..color("#ffffff").."] "..name})
minetest.chat_send_all("Congratulations "..name.." for upgrading to the "..color(rank_colors[ranks[name]])..ranks[name]..color("#ffffff").." rank!")
else
minetest.chat_send_player(name, minetest.colorize("#ff0000", "ERROR: You need to hold 20 coal blocks to upgrade."))
end
elseif rank == "coal" then
if wielditem:get_count() >= 20 and wielditem:get_name() == "default:steelblock" then
wielditem:set_count(wielditem:get_count() - 15)
player:set_wielded_item(wielditem)
ranks[name] = "iron"
player:set_nametag_attributes({text = "["..color(rank_colors[ranks[name]])..ranks[name]..color("#ffffff").."] "..name})
minetest.chat_send_all("Congratulations "..name.." for upgrading to the "..color(rank_colors[ranks[name]])..ranks[name]..color("#ffffff").." rank!")
else
minetest.chat_send_player(name, minetest.colorize("#ff0000", "ERROR: You need to hold 20 iron blocks to upgrade."))
end
elseif rank == "iron" then
if wielditem:get_count() >= 20 and wielditem:get_name() == "default:copperblock" then
wielditem:set_count(wielditem:get_count() - 15)
player:set_wielded_item(wielditem)
ranks[name] = "copper"
player:set_nametag_attributes({text = "["..color(rank_colors[ranks[name]])..ranks[name]..color("#ffffff").."] "..name})
minetest.chat_send_all("Congratulations "..name.." for upgrading to the "..color(rank_colors[ranks[name]])..ranks[name]..color("#ffffff").." rank!")
else
minetest.chat_send_player(name, minetest.colorize("#ff0000", "ERROR: You need to hold 20 copper blocks to upgrade."))
end
elseif rank == "copper" then
if wielditem:get_count() >= 20 and wielditem:get_name() == "default:bronzeblock" then
wielditem:set_count(wielditem:get_count() - 15)
player:set_wielded_item(wielditem)
ranks[name] = "bronze"
player:set_nametag_attributes({text = "["..color(rank_colors[ranks[name]])..ranks[name]..color("#ffffff").."] "..name})
minetest.chat_send_all("Congratulations "..name.." for upgrading to the "..color(rank_colors[ranks[name]])..ranks[name]..color("#ffffff").." rank!")
else
minetest.chat_send_player(name, minetest.colorize("#ff0000", "ERROR: You need to hold 20 bronze blocks to upgrade."))
end
elseif rank == "bronze" then
if wielditem:get_count() >= 20 and wielditem:get_name() == "default:goldblock" then
wielditem:set_count(wielditem:get_count() - 15)
player:set_wielded_item(wielditem)
ranks[name] = "gold"
player:set_nametag_attributes({text = "["..color(rank_colors[ranks[name]])..ranks[name]..color("#ffffff").."] "..name})
minetest.chat_send_all("Congratulations "..name.." for upgrading to the "..color(rank_colors[ranks[name]])..ranks[name]..color("#ffffff").." rank!")
else
minetest.chat_send_player(name, minetest.colorize("#ff0000", "ERROR: You need to hold 20 bronze blocks to upgrade."))
end
elseif rank == "gold" then
if wielditem:get_count() >= 20 and wielditem:get_name() == "default:mese" then
wielditem:set_count(wielditem:get_count() - 15)
player:set_wielded_item(wielditem)
ranks[name] = "mese"
player:set_nametag_attributes({text = "["..color(rank_colors[ranks[name]])..ranks[name]..color("#ffffff").."] "..name})
minetest.chat_send_all("Congratulations "..name.." for upgrading to the "..color(rank_colors[ranks[name]])..ranks[name]..color("#ffffff").." rank!")
else
minetest.chat_send_player(name, minetest.colorize("#ff0000", "ERROR: You need to hold 20 mese blocks to upgrade."))
end
elseif rank == "mese" then
if wielditem:get_count() >= 20 and wielditem:get_name() == "default:diamondblock" then
wielditem:set_count(wielditem:get_count() - 15)
player:set_wielded_item(wielditem)
ranks[name] = "diamond"
player:set_nametag_attributes({text = "["..color(rank_colors[ranks[name]])..ranks[name]..color("#ffffff").."] "..name})
minetest.chat_send_all("Congratulations "..name.." for upgrading to the "..color(rank_colors[ranks[name]])..ranks[name]..color("#ffffff").." rank!")
else
minetest.chat_send_player(name, minetest.colorize("#ff0000", "ERROR: You need to hold 20 mese blocks to upgrade."))
end
elseif rank == "diamond" then
if wielditem:get_count() >= 20 and wielditem:get_name() == "default:obsidian" then
wielditem:set_count(wielditem:get_count() - 15)
player:set_wielded_item(wielditem)
ranks[name] = "obsidian"
player:set_nametag_attributes({text = "["..color(rank_colors[ranks[name]])..ranks[name]..color("#ffffff").."] "..name})
minetest.chat_send_all("Congratulations "..name.." for upgrading to the "..color(rank_colors[ranks[name]])..ranks[name]..color("#ffffff").." rank!")
else
minetest.chat_send_player(name, minetest.colorize("#ff0000", "ERROR: You need to hold 20 obsidian blocks to upgrade."))
end
elseif rank == "obsidian" then
minetest.chat_send_player(name, "You can't upgrade any more unless you got a donor rank.")
elseif rank == "air" or rank == "void" then
minetest.chat_send_player(name, "You cannot upgrade a donor rank.")
elseif rank == "helper" then
minetest.chat_send_player(name, "You may not rankup from helper.")
elseif rank == "moderator" then
minetest.chat_send_player(name, "Ask the admin for an upgrade, not me.")
elseif rank == "admin" then
minetest.chat_send_player(name, "You are already an admin, what more do you want?")
else
minetest.chat_send_player(name, "You have an invalid rank.")
minetest.kick_player(name, "You have an invalid rank. If you rejoin, this will be fixed.")
if minetetest.setting_get("name") then minetest.chat_send_player(minetest.setting_get("name"), name.." has an invalid rank.") end
end
end
})