-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathpaint.lua
More file actions
213 lines (200 loc) · 5.47 KB
/
paint.lua
File metadata and controls
213 lines (200 loc) · 5.47 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
202
203
204
205
206
207
208
209
210
211
212
local BRUSH_USES = 3
local CAN_USES = 100
function check_paintcan(pos, node)
local name = node.name
if string.sub(name, 1, 14) ~= "mypaint:paint_" then
return
end
local color = string.sub(name, 15)
local meta = minetest.get_meta(pos)
stack = ItemStack("mypaint:brush_"..color)
if minetest.settings:get_bool("creative_mode") then
return stack
end
local uses = meta:get_int("mypaint:uses") - 1
meta:set_int("mypaint:uses", uses)
if uses <= 0 then
minetest.dig_node(pos)
else
local info = meta:get_string("infotext")
info = string.gsub(info, "%(.*%)", "("..uses.." uses)")
meta:set_string("infotext", info)
end
return stack
end
function paint_node(pos, node, col, itemstack)
local s, e
local nname = node.name
s, e = string.find(nname, "_[^_]+$")
local color
if s and e then
color = string.sub(nname, s + 1, e)
if mypaint.colors[color] then
nname = string.sub(nname, 1, s - 1)
if color == col then
return
end
end
end
for name, colors in pairs(mypaint.paintables) do
if (nname == name) then
if not col then
if color then
minetest.set_node(pos, {name = name, param2 = node.param2})
end
return
end
if not colors[col] then
return
end
minetest.set_node(pos, {name = name.."_"..col, param2 = node.param2})
if not minetest.settings:get_bool("creative_mode") then
local wear = itemstack:get_wear() + 65535 / BRUSH_USES
if wear < 65535 then
itemstack:set_wear(wear)
else
itemstack = ItemStack("mypaint:brush")
end
return itemstack
end
end
end
end
minetest.register_tool("mypaint:brush", {
description = "Paint Brush",
inventory_image = "mypaint_brush.png",
on_use = function(itemstack, user, pointed_thing)
if pointed_thing.type ~= "node" then
return
end
local pos = pointed_thing.under
local node = minetest.get_node(pos)
if string.sub(node.name, 1, 8) ~= "mypaint:" then
return
end
return check_paintcan(pos, node)
end
})
minetest.register_tool("mypaint:scraper", {
description = "Paint Scraper",
inventory_image = "mypaint_scraper.png",
on_use = function(itemstack, user, pointed_thing)
if pointed_thing.type ~= "node" then
return
end
local pos = pointed_thing.under
local node = minetest.get_node(pos)
return paint_node(pos, node, nil, itemstack)
end
})
for color, entry in pairs(mypaint.colors) do
local desc = entry[1]
local cstring = entry[2]
minetest.register_tool("mypaint:brush_"..color, {
description = "Paint Brush ("..desc.." Paint)",
inventory_image = "mypaint_brush.png^(mypaint_brush_color.png^[colorize:#"..cstring..")",
on_use = function(itemstack, user, pointed_thing)
if pointed_thing.type ~= "node" then
return
end
local pos = pointed_thing.under
local node = minetest.get_node(pos)
local ret = check_paintcan(pos, node)
if ret then
return ret
end
return paint_node(pos, node, color, itemstack)
end,
})
minetest.register_node("mypaint:paint_"..color, {
description = desc.." Paint",
drawtype = "mesh",
paramtype = "light",
paramtype2 = "facedir",
mesh = "mypaint_can.obj",
tiles = {"(mypaint_can_color.png^[colorize:#"..cstring..")^mypaint_can_base.png"},
stack_max = 1,
drop = "",
groups = {oddly_breakable_by_hand = 3, dig_immediate = 3, not_in_creative_inventory = 1},
selection_box = {
type = "fixed",
fixed = {
{-0.25, -0.5, -0.25, 0.25, 0., 0.25},
}
},
after_dig_node = function(pos, oldnode, oldmetadata, digger)
if not digger then
return
end
local inv = digger:get_inventory()
if not inv then
return
end
local itemstack = ItemStack("mypaint:paintcan_"..color)
local uses = tonumber(oldmetadata.fields["mypaint:uses"])
if not uses then
uses = 100
end
if uses <= 0 then
return
end
itemstack:set_wear((CAN_USES - uses) * (65535 / CAN_USES))
if inv:room_for_item("main", itemstack) then
inv:add_item("main", itemstack)
else
minetest.add_item(pos, itemstack)
end
end
})
minetest.register_tool("mypaint:paintcan_"..color, {
description = desc.." Paint",
inventory_image = "mypaint_inv_can_base.png^(mypaint_inv_can_color.png^[colorize:#"..cstring..":alpha)",
on_place = function(itemstack, user, pointed_thing)
local pname = "mypaint:paint_"..color
local paint = ItemStack(pname)
paint = minetest.item_place_node(paint, user, pointed_thing)
if not minetest.settings:get_bool("creative_mode") then
if not paint or (paint:get_count() > 0) then
return
end
end
local pos = pointed_thing.under
local node = minetest.get_node(pos)
local meta = minetest.get_meta(pos)
if node.name ~= pname or (meta:get_int("mypaint:uses") > 0) then
pos = pointed_thing.above
node = minetest.get_node(pos)
meta = minetest.get_meta(pos)
if node.name ~= pname or (meta:get_int("mypaint:uses") > 0) then
return
end
end
local uses = math.floor(CAN_USES - itemstack:get_wear() / (65535 / CAN_USES))
meta:set_int("mypaint:uses", uses)
meta:set_string("infotext", desc.." Paint ("..uses.." uses)")
itemstack:take_item()
return itemstack
end
})
minetest.register_craft({
output = "mypaint:paintcan_"..color,
recipe = {
{"bucket:bucket_water","dye:"..color}
},
replacements = {{"bucket:bucket_water","bucket:bucket_empty"}},
})
end
minetest.register_craft({
output = 'mypaint:brush',
recipe = {
{'wool:white'},
{'group:stick'},
}
})
minetest.register_craft({
output = 'mypaint:scraper',
recipe = {
{'default:steel_ingot', ''},
{'', 'group:stick'},
}
})