-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinit.lua
More file actions
201 lines (182 loc) · 5.94 KB
/
Copy pathinit.lua
File metadata and controls
201 lines (182 loc) · 5.94 KB
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
local mod_storage = core.get_mod_storage()
local S = core.get_translator("toolranks")
toolranks = {}
-- list of registered tools
toolranks.registered = {}
toolranks.colors = {
grey = core.get_color_escape_sequence("#9d9d9d"),
green = core.get_color_escape_sequence("#1eff00"),
gold = core.get_color_escape_sequence("#ffdf00"),
white = core.get_color_escape_sequence("#ffffff")
}
toolranks.tool_strings = {
hammer = S("hammer"),
hoe = S("hoe"),
pickaxe = S("pickaxe"),
pick = S("pickaxe"),
axe = S("axe"),
shears = S("shears"),
shovel = S("shovel"),
spear = S("spear"),
sword = S("sword"),
trident = S("trident"),
mace = S("mace")
}
local max_speed = tonumber(core.settings:get("toolranks_speed_multiplier")) or 2.0
local max_use = tonumber(core.settings:get("toolranks_use_multiplier")) or 2.0
local max_level = tonumber(core.settings:get("toolranks_levels")) or 10
local level_digs = tonumber(core.settings:get("toolranks_level_digs")) or 500
local level_multiplier = 1 / max_level
function toolranks.get_tool_type(description)
if not description then
return S("tool")
else
local d = string.lower(description)
for name, text in pairs(toolranks.tool_strings) do
if string.find(d, name) then
return text
end
end
-- This only returns if the for loop doesn't
return S("tool")
end
end
function toolranks.get_level(uses)
if type(uses) == "number" and uses > 0 then
if max_level == 0 then
return 0
elseif level_digs == 0 then
return max_level
else
return math.min(max_level, math.floor(uses / level_digs))
end
end
return 0
end
function toolranks.create_description(name, uses)
local description = name
local tooltype = toolranks.get_tool_type(description)
local newdesc = S(
"@1@2\n@3Level @4 @5\n@6@Node dug: @7",
toolranks.colors.green,
description,
toolranks.colors.gold,
toolranks.get_level(uses),
tooltype,
toolranks.colors.grey,
(type(uses) == "number" and uses or 0)
)
return newdesc
end
function toolranks.new_afteruse(itemstack, user, _, digparams)
if not user or not digparams then
return itemstack
end
local itemmeta = itemstack:get_meta()
local itemdef = itemstack:get_definition()
local itemdesc = itemdef.original_description or ""
local dugnodes = tonumber(itemmeta:get_string("dug")) or 0
local lastlevel = tonumber(itemmeta:get_string("lastlevel")) or 0
local most_digs = mod_storage:get_int("most_digs") or 0
local most_digs_user = mod_storage:get_string("most_digs_user") or 0
local pname = user:get_player_name()
if not pname then return itemstack end -- nil check
if digparams.wear > 0 then -- Only count nodes that spend the tool
dugnodes = dugnodes + 1
itemmeta:set_string("dug", dugnodes)
end
if dugnodes > most_digs then
if most_digs_user ~= pname and core.settings:get_bool("toolranks_announce", true) then
core.chat_send_all(S(
"Most used tool is now a @1@2@3 owned by @4 with @5 uses.",
toolranks.colors.green,
itemdesc,
toolranks.colors.white,
pname,
dugnodes
))
end
mod_storage:set_int("most_digs", dugnodes)
mod_storage:set_string("most_digs_user", pname)
end
if itemstack:get_wear() > 60135 then
if core.settings:get_bool("toolranks_warn", true) then
core.chat_send_player(user:get_player_name(), S("Your tool is about to break!"))
end
if core.settings:get_bool("toolranks_warn_sound", true) then
core.sound_play("toolranks_break", {to_player = pname, gain = 2.0})
end
end
local level = toolranks.get_level(dugnodes)
if lastlevel < level then
local levelup_text = S(
"Your @1@2@3 just leveled up!",
toolranks.colors.green,
itemdesc,
toolranks.colors.white
)
core.chat_send_player(user:get_player_name(), levelup_text)
core.sound_play("toolranks_levelup", {to_player = pname, gain = 2.0})
-- Make tool better by modifying tool_capabilities (if defined)
if itemdef.tool_capabilities then
local speed_multiplier = 1 + (level * level_multiplier * (max_speed - 1))
local use_multiplier = 1 + (level * level_multiplier * (max_use - 1))
local caps = table.copy(itemdef.tool_capabilities)
caps.full_punch_interval = caps.full_punch_interval and (caps.full_punch_interval / speed_multiplier)
caps.punch_attack_uses = caps.punch_attack_uses and (caps.punch_attack_uses * use_multiplier)
if caps.groupcaps then
for _,c in pairs(caps.groupcaps) do
c.uses = c.uses * use_multiplier
for i, t in ipairs(c.times) do
c.times[i] = t / speed_multiplier
end
end
end
itemmeta:set_tool_capabilities(caps)
end
end
-- Old method for compatibility with tools without tool_capabilities defined
local wear = digparams.wear
if level > 0 and not itemdef.tool_capabilities then
local use_multiplier = 1 + (level * level_multiplier * (max_use - 1))
wear = wear / use_multiplier
end
itemmeta:set_string("lastlevel", level)
itemmeta:set_string("description", toolranks.create_description(itemdesc, dugnodes))
itemstack:add_wear(wear)
return itemstack
end
-- keep in global namespace for compatibility
function toolranks.add_tool(name)
if not toolranks.registered[name] then
local desc = ItemStack(name):get_definition().description
core.override_item(name, {
original_description = desc,
description = core.formspec_escape(toolranks.create_description(desc)),
after_use = toolranks.new_afteruse
})
toolranks.registered[name] = true
return true
elseif toolranks.registered[name] then
return true
else
return false
end
end
-- dynamically register tools
core.register_on_mods_loaded(function()
for name, def in pairs(core.registered_tools) do
local short_name = name:gsub("^.-:", "")
local desc = def.description
for str, _ in pairs(toolranks.tool_strings) do
if string.find(short_name, str, 1, true) or string.find(desc, str, 1, true) then
toolranks.add_tool(name)
break
end
end
end
end)
-- Throw error when unsupported mods are found
if core.get_modpath("toolranks_extras") then
core.log("error", "Toolranks: Please disable toolranks_extras, it is unsupported.")
end