Skip to content

Commit

Permalink
Add check for tool_capabilities and fallback if not defined (#15)
Browse files Browse the repository at this point in the history
* add check for `tool_capabilities` and fallback if not defined

* fix whitespace
  • Loading branch information
OgelGames committed Jun 30, 2020
1 parent d000b4b commit 12af618
Showing 1 changed file with 23 additions and 14 deletions.
37 changes: 23 additions & 14 deletions init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -110,26 +110,35 @@ function toolranks.new_afteruse(itemstack, user, node, digparams)
to_player = user:get_player_name(),
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)

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
itemmeta:set_tool_capabilities(caps)
end
end

local speed_multiplier = 1 + (level * level_multiplier * (max_speed - 1))
-- 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))
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)

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
itemmeta:set_tool_capabilities(caps)
wear = wear / use_multiplier
end

itemmeta:set_string("lastlevel", level)
itemmeta:set_string("description", toolranks.create_description(itemdesc, dugnodes))
itemstack:add_wear(digparams.wear)
itemstack:add_wear(wear)
return itemstack
end

Expand Down

0 comments on commit 12af618

Please sign in to comment.