-
Notifications
You must be signed in to change notification settings - Fork 0
/
api.lua
98 lines (77 loc) · 2.75 KB
/
api.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
aurora_tech = {}
local function register_tool(name, defs, cb)
defs.drawtype = "mesh"
defs.node_placement_prediction = "air"
defs.on_place = cb
defs.on_secondary_use = cb
defs.stack_max = defs.stack_max or 1
minetest.register_node(name, defs)
end
local function register_tool_durability(name, defs, cb, dura, auto, cb_dead)
local count = math.min(dura, 16)
if auto then
local old_cb = cb
cb = function(itemstack, placer, pt)
old_cb(itemstack, placer, pt)
return aurora_tech.damage_tool(itemstack)
end
end
for i = 1, count do
local cb = cb
if i == 1 then cb = cb_dead end
local factor = (i - 1) / (count - 1)
local pixels = math.floor(factor * 16)
local opac = 255 - math.floor(factor * 255)
local defs = table.copy(defs)
local groups = table.copy(defs.groups or {})
if i ~= count then groups["not_in_creative_inventory"] = 1 end
defs._aurora_tech_dura = dura
defs._aurora_tech_name = name
defs.groups = groups
defs.inventory_image =
defs.inventory_image .. "^((aurora_tech_ui_durability_full.png^[resize:" .. pixels .. "x16)^(aurora_tech_ui_durability_empty.png^[resize:" .. pixels .. "x16^[opacity:" .. opac .. ")^[mask:aurora_tech_ui_durability_mask.png)"
register_tool(name .. "_" .. i, defs, cb)
end
end
aurora_tech.register_tool_3d = function(name, defs, cb, dura, auto, cb_dead)
if dura == nil then register_tool(name, defs, cb)
else register_tool_durability(name, defs, cb, dura, auto, cb_dead) end
end
aurora_tech.register_repair = function(tool, material, amount, return_tool)
if return_tool == nil then return_tool = tool end
local tool_dura = math.min(minetest.registered_nodes[tool .. "_1"]._aurora_tech_dura, 16)
for i = 1, tool_dura - 1 do
for j = 1, 8 do
local recipe = { tool .. "_" .. i }
local res = i + amount * j
for k = 1, j do
table.insert(recipe, material)
end
minetest.register_craft({
type = "shapeless",
output = return_tool .. "_" .. math.min(res, tool_dura),
recipe = recipe
})
if res >= tool_dura then
break
end
end
end
end
aurora_tech.damage_tool = function(itemstack)
if not itemstack or not minetest.registered_nodes[itemstack:get_name()] then return itemstack end
local dura = minetest.registered_nodes[itemstack:get_name()]._aurora_tech_dura
local name = minetest.registered_nodes[itemstack:get_name()]._aurora_tech_name
if dura <= 16 then
itemstack:replace(name .. "_" .. (itemstack:get_name():sub(name:len() + 2) - 1))
return itemstack
else
local max_uses = math.floor(dura / 16)
local meta = itemstack:get_meta()
meta:set_int("uses", meta:get_int("uses", 0) + 1)
if meta:get_int("uses", 0) >= max_uses then
itemstack:replace(name .. "_" .. (itemstack:get_name():sub(name:len() + 2) - 1))
end
return itemstack
end
end