forked from MrRar/edit_skin
-
Notifications
You must be signed in to change notification settings - Fork 1
/
init.lua
669 lines (589 loc) · 19.7 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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
-- Edit Skin Mod
local S = minetest.get_translator("edit_skin")
local color_to_string = minetest.colorspec_to_colorstring
edit_skin = {
item_names = {"base", "footwear", "eye", "mouth", "bottom", "top", "hair", "headwear"},
tab_names = {"template", "base", "headwear", "hair", "eye", "mouth", "top", "bottom", "footwear"},
tab_descriptions = {
template = S("Templates"),
base = S("Bases"),
footwear = S("Footwears"),
eye = S("Eyes"),
mouth = S("Mouths"),
bottom = S("Bottoms"),
top = S("Tops"),
hair = S("Hairs"),
headwear = S("Headwears")
},
steve = {}, -- Stores skin values for Steve skin
alex = {}, -- Stores skin values for Alex skin
base = {}, -- List of base textures
-- Base color is separate to keep the number of junk nodes registered in check
base_color = {0xFFfbe3c8, 0xFFf6d2b1, 0xFFd09c6e, 0xFFd3a581, 0xFFb98664, 0xFFaa854f, 0xFFb57f52, 0xFF9d6444, 0xFF915a3c, 0xFF80513a, 0xFF674231, 0xFF42190b, 0xFF311304, 0xFF190c05, 0xFF775ebb, 0xFF3362d3, 0xFF3dc36c, 0xFFca5bc3},
color = {
0xff613915, -- 1 Dark brown Steve hair, Alex bottom
0xff97491b, -- 2 Medium brown
0xffb17050, -- 3 Light brown
0xffe2bc7b, -- 4 Beige
0xff706662, -- 5 Gray
0xff151515, -- 6 Black
0xffc21c1c, -- 7 Red
0xff178c32, -- 8 Green Alex top
0xffae2ad3, -- 9 Plum
0xffebe8e4, -- 10 White
0xffe3dd26, -- 11 Yellow
0xff449acc, -- 12 Light blue Steve top
0xff124d87, -- 13 Dark blue Steve bottom
0xfffc0eb3, -- 14 Pink
0xffd0672a, -- 15 Orange Alex hair
},
footwear = {},
mouth = {},
eye = {},
bottom = {},
top = {},
hair = {},
headwear = {},
masks = {},
preview_rotations = {},
ranks = {},
player_skins = {},
player_formspecs = {},
restricted_to_player = {},
restricted_to_admin = {},
}
minetest.register_privilege("edit_skin_admin", {
description = S("Allows access to restricted skin items."),
give_to_singleplayer = true,
give_to_admin = true,
})
function edit_skin.register_item(item)
assert(edit_skin[item.type], "Skin item type " .. item.type .. " does not exist.")
local texture = item.texture or "blank.png"
if item.steve then
edit_skin.steve[item.type] = texture
end
if item.alex then
edit_skin.alex[item.type] = texture
end
if item.restricted_to_admin then
edit_skin.restricted_to_admin[texture] = true
end
if item.for_player then
edit_skin.restricted_to_player[texture] = {}
if type(item.for_player) == "string" then
edit_skin.restricted_to_player[texture][item.for_player] = true
else
for i, name in pairs(item.for_player) do
edit_skin.restricted_to_player[texture][name] = true
end
end
end
table.insert(edit_skin[item.type], texture)
edit_skin.masks[texture] = item.mask
edit_skin.preview_rotations[texture] = item.preview_rotation
edit_skin.ranks[texture] = item.rank
end
function edit_skin.save(player)
if not player:is_player() then return end
local skin = edit_skin.player_skins[player]
if not skin then return end
player:get_meta():set_string("edit_skin:skin", minetest.serialize(skin))
end
minetest.register_chatcommand("skin", {
description = S("Open skin configuration screen."),
privs = {},
func = function(name, param) edit_skin.show_formspec(minetest.get_player_by_name(name)) end
})
function edit_skin.compile_skin(skin)
if not skin then return "blank.png" end
local ranks = {}
local layers = {}
for i, item in ipairs(edit_skin.item_names) do
local texture = skin[item]
local layer = ""
local rank = edit_skin.ranks[texture] or i * 10
if texture and texture ~= "blank.png" then
if skin[item .. "_color"] and edit_skin.masks[texture] then
local color = color_to_string(skin[item .. "_color"])
layer = "(" .. edit_skin.masks[texture] .. "^[colorize:" .. color .. ":alpha)"
end
if #layer > 0 then layer = layer .. "^" end
layer = layer .. texture
layers[rank] = layer
table.insert(ranks, rank)
end
end
table.sort(ranks)
local output = ""
for i, rank in ipairs(ranks) do
if #output > 0 then output = output .. "^" end
output = output .. layers[rank]
end
return output
end
function edit_skin.update_player_skin(player)
local output = edit_skin.compile_skin(edit_skin.player_skins[player])
player_api.set_texture(player, 1, output)
-- Set player first person hand node
local base = edit_skin.player_skins[player].base
local base_color = edit_skin.player_skins[player].base_color
local node_id = base:gsub(".png$", "") .. color_to_string(base_color):gsub("#", "")
player:get_inventory():set_stack("hand", 1, "edit_skin:" .. node_id)
for i = 1, #edit_skin.registered_on_set_skins do
edit_skin.registered_on_set_skins[i](player)
end
local name = player:get_player_name()
if
minetest.global_exists("armor") and
armor.textures and armor.textures[name]
then
armor.textures[name].skin = output
armor.update_player_visuals(armor, player)
end
if minetest.global_exists("i3") then i3.set_fs(player) end
end
minetest.register_on_joinplayer(function(player)
local function table_get_random(t)
return t[math.random(#t)]
end
local skin = player:get_meta():get_string("edit_skin:skin")
if skin then
skin = minetest.deserialize(skin)
end
if skin then
edit_skin.player_skins[player] = skin
else
if math.random() > 0.5 then
skin = table.copy(edit_skin.steve)
else
skin = table.copy(edit_skin.alex)
end
edit_skin.player_skins[player] = skin
edit_skin.save(player)
end
edit_skin.player_formspecs[player] = {
active_tab = "template",
page_num = 1,
has_admin_priv = minetest.check_player_privs(player, "edit_skin_admin"),
}
player:get_inventory():set_size("hand", 1)
edit_skin.update_player_skin(player)
if minetest.global_exists("inventory_plus") and inventory_plus.register_button then
inventory_plus.register_button(player, "edit_skin", S("Edit Skin"))
end
-- Needed for 3D Armor + sfinv
if minetest.global_exists("armor") then
minetest.after(0.01, function()
if player:is_player() then
edit_skin.update_player_skin(player)
end
end)
end
end)
minetest.register_on_leaveplayer(function(player)
player:get_inventory():set_size("hand", 0)
edit_skin.player_skins[player] = nil
edit_skin.player_formspecs[player] = nil
end)
minetest.register_on_shutdown(function()
for _, player in pairs(minetest.get_connected_players()) do
player:get_inventory():set_size("hand", 0)
end
end)
edit_skin.registered_on_set_skins = {}
function edit_skin.register_on_set_skin(func)
table.insert(edit_skin.registered_on_set_skins, func)
end
function edit_skin.show_formspec(player)
local formspec_data = edit_skin.player_formspecs[player]
local has_admin_priv = minetest.check_player_privs(player, "edit_skin_admin")
if has_admin_priv ~= formspec_data.has_admin_priv then
formspec_data.has_admin_priv = has_admin_priv
for i, name in pairs(edit_skin.item_names) do
formspec_data[name] = nil
end
end
local active_tab = formspec_data.active_tab
local page_num = formspec_data.page_num
local skin = edit_skin.player_skins[player]
local formspec = "formspec_version[3]size[14.2,11]"
for i, tab in pairs(edit_skin.tab_names) do
if tab == active_tab then
formspec = formspec ..
"style[" .. tab .. ";bgcolor=green]"
end
local y = 0.3 + (i - 1) * 0.8
formspec = formspec ..
"style[" .. tab .. ";content_offset=16,0]" ..
"button[0.3," .. y .. ";4,0.8;" .. tab .. ";" .. edit_skin.tab_descriptions[tab] .. "]" ..
"image[0.4," .. y + 0.1 .. ";0.6,0.6;edit_skin_icons.png^[verticalframe:9:" .. i - 1 .. "]"
end
local mesh = player:get_properties().mesh or ""
local textures = player_api.get_textures(player)
textures[2] = "blank.png" -- Clear out the armor
formspec = formspec ..
"model[11,0.3;3,7;player_mesh;" .. mesh .. ";" ..
table.concat(textures, ",") ..
";0,180;false;true;0,0]"
if active_tab == "template" then
formspec = formspec ..
"model[5,2;2,3;player_mesh;" .. mesh .. ";" ..
edit_skin.compile_skin(edit_skin.steve) ..
",blank.png,blank.png;0,180;false;true;0,0]" ..
"button[5,5.2;2,0.8;steve;" .. S("Select") .. "]" ..
"model[7.5,2;2,3;player_mesh;" .. mesh .. ";" ..
edit_skin.compile_skin(edit_skin.alex) ..
",blank.png,blank.png;0,180;false;true;0,0]" ..
"button[7.5,5.2;2,0.8;alex;" .. S("Select") .. "]"
else
formspec = formspec ..
"style_type[button,image_button;border=false;bgcolor=#00000000]"
if not formspec_data[active_tab] then edit_skin.filter_active_tab(player) end
local textures = formspec_data[active_tab]
local page_start = (page_num - 1) * 16 + 1
local page_end = math.min(page_start + 16 - 1, #textures)
for j = page_start, page_end do
local i = j - page_start + 1
local texture = textures[j]
local preview = edit_skin.masks[skin.base] .. "^[colorize:gray^" .. skin.base
local color = color_to_string(skin[active_tab .. "_color"])
local mask = edit_skin.masks[texture]
if color and mask then
preview = preview .. "^(" .. mask .. "^[colorize:" .. color .. ":alpha)"
end
preview = preview .. "^" .. texture
local mesh = "edit_skin_head.obj"
if active_tab == "top" then
mesh = "edit_skin_top.obj"
elseif active_tab == "bottom" or active_tab == "footwear" then
mesh = "edit_skin_bottom.obj"
end
local rot_x = -10
local rot_y = 20
if edit_skin.preview_rotations[texture] then
rot_x = edit_skin.preview_rotations[texture].x
rot_y = edit_skin.preview_rotations[texture].y
end
i = i - 1
local x = 4.5 + i % 4 * 1.6
local y = 0.3 + math.floor(i / 4) * 1.6
formspec = formspec ..
"model[" .. x .. "," .. y ..
";1.5,1.5;" .. mesh .. ";" .. mesh .. ";" ..
preview ..
";" .. rot_x .. "," .. rot_y .. ";false;false;0,0]"
if skin[active_tab] == texture then
formspec = formspec ..
"style[" .. texture ..
";bgcolor=;bgimg=edit_skin_select_overlay.png;" ..
"bgimg_pressed=edit_skin_select_overlay.png;bgimg_middle=14,14]"
end
formspec = formspec .. "button[" .. x .. "," .. y .. ";1.5,1.5;" .. texture .. ";]"
end
end
if skin[active_tab .. "_color"] then
local colors = edit_skin.color
if active_tab == "base" then colors = edit_skin.base_color end
local tab_color = active_tab .. "_color"
local selected_color = skin[tab_color]
for i, colorspec in pairs(colors) do
local color = color_to_string(colorspec)
i = i - 1
local x = 4.6 + i % 6 * 0.9
local y = 8 + math.floor(i / 6) * 0.9
formspec = formspec ..
"image_button[" .. x .. "," .. y ..
";0.8,0.8;blank.png^[noalpha^[colorize:" ..
color .. ":alpha;" .. colorspec .. ";]"
if selected_color == colorspec then
formspec = formspec ..
"style[" .. color ..
";bgcolor=;bgimg=edit_skin_select_overlay.png;bgimg_middle=14,14]" ..
"button[" .. x .. "," .. y .. ";0.8,0.8;" .. color .. ";]"
end
end
if not (active_tab == "base") then
-- Bitwise Operations !?!?!
local red = math.floor(selected_color / 0x10000) - 0xff00
local green = math.floor(selected_color / 0x100) - 0xff0000 - red * 0x100
local blue = selected_color - 0xff000000 - red * 0x10000 - green * 0x100
formspec = formspec ..
"container[10.2,8]" ..
"scrollbaroptions[min=0;max=255;smallstep=20]" ..
"box[0.4,0;2.49,0.38;red]" ..
"label[0.2,0.2;-]" ..
"scrollbar[0.4,0;2.5,0.4;horizontal;red;" .. red .."]" ..
"label[2.9,0.2;+]" ..
"box[0.4,0.6;2.49,0.38;green]" ..
"label[0.2,0.8;-]" ..
"scrollbar[0.4,0.6;2.5,0.4;horizontal;green;" .. green .."]" ..
"label[2.9,0.8;+]" ..
"box[0.4,1.2;2.49,0.38;blue]" ..
"label[0.2,1.4;-]" ..
"scrollbar[0.4,1.2;2.5,0.4;horizontal;blue;" .. blue .. "]" ..
"label[2.9,1.4;+]" ..
"container_end[]"
end
end
local page_count = 1
if edit_skin[active_tab] then
page_count = math.ceil(#formspec_data[active_tab] / 16)
end
if page_num > 1 then
formspec = formspec ..
"image_button[4.5,6.7;1,1;edit_skin_arrow.png^[transformFX;previous_page;]"
end
if page_num < page_count then
formspec = formspec ..
"image_button[9.8,6.7;1,1;edit_skin_arrow.png;next_page;]"
end
if page_count > 1 then
formspec = formspec ..
"label[7.3,7.2;" .. page_num .. " / " .. page_count .. "]"
end
minetest.show_formspec(player:get_player_name(), "edit_skin:edit_skin", formspec)
end
function edit_skin.filter_active_tab(player)
local formspec_data = edit_skin.player_formspecs[player]
local active_tab = formspec_data.active_tab
local admin_priv = formspec_data.has_admin_priv
local name = player:get_player_name()
formspec_data[active_tab] = {}
local textures = formspec_data[active_tab]
for i, texture in pairs(edit_skin[active_tab]) do
if admin_priv or not edit_skin.restricted_to_admin[texture] then
local restriction = edit_skin.restricted_to_player[texture]
if restriction then
if restriction[name] then
table.insert(textures, texture)
end
else
table.insert(textures, texture)
end
end
end
end
minetest.register_on_player_receive_fields(function(player, formname, fields)
if formname ~= "edit_skin:edit_skin" then return false end
local formspec_data = edit_skin.player_formspecs[player]
local active_tab = formspec_data.active_tab
-- Cancel formspec resend after scrollbar move
if formspec_data.form_send_job then
formspec_data.form_send_job:cancel()
end
if fields.quit then
edit_skin.save(player)
return true
end
if fields.alex then
edit_skin.player_skins[player] = table.copy(edit_skin.alex)
edit_skin.update_player_skin(player)
edit_skin.show_formspec(player)
return true
elseif fields.steve then
edit_skin.player_skins[player] = table.copy(edit_skin.steve)
edit_skin.update_player_skin(player)
edit_skin.show_formspec(player)
return true
end
for i, tab in pairs(edit_skin.tab_names) do
if fields[tab] then
formspec_data.active_tab = tab
formspec_data.page_num = 1
edit_skin.show_formspec(player)
return true
end
end
local skin = edit_skin.player_skins[player]
if not skin then return true end
if fields.next_page then
local page_num = formspec_data.page_num
page_num = page_num + 1
local page_count = math.ceil(#formspec_data[active_tab] / 16)
if page_num > page_count then
page_num = page_count
end
formspec_data.page_num = page_num
edit_skin.show_formspec(player)
return true
elseif fields.previous_page then
local page_num = formspec_data.page_num
page_num = page_num - 1
if page_num < 1 then page_num = 1 end
formspec_data.page_num = page_num
edit_skin.show_formspec(player)
return true
end
if
skin[active_tab .. "_color"] and (
fields.red and fields.red:find("^CHG") or
fields.green and fields.green:find("^CHG") or
fields.blue and fields.blue:find("^CHG")
)
then
local red = fields.red:gsub("%a%a%a:", "")
local green = fields.green:gsub("%a%a%a:", "")
local blue = fields.blue:gsub("%a%a%a:", "")
red = tonumber(red) or 0
green = tonumber(green) or 0
blue = tonumber(blue) or 0
local color = 0xff000000 + red * 0x10000 + green * 0x100 + blue
if color >= 0 and color <= 0xffffffff then
-- We delay resedning the form because otherwise it will break dragging scrollbars
formspec_data.form_send_job = minetest.after(0.2, function()
if player and player:is_player() then
skin[active_tab .. "_color"] = color
edit_skin.update_player_skin(player)
edit_skin.show_formspec(player)
formspec_data.form_send_job = nil
end
end)
return true
end
end
local field
for f, value in pairs(fields) do
if value == "" then
field = f
break
end
end
-- See if field is a texture
if field and edit_skin[active_tab] then
for i, texture in pairs(formspec_data[active_tab]) do
if texture == field then
skin[active_tab] = texture
edit_skin.update_player_skin(player)
edit_skin.show_formspec(player)
return true
end
end
end
-- See if field is a color
local number = tonumber(field)
if number and skin[active_tab .. "_color"] then
local color = math.floor(number)
if color and color >= 0 and color <= 0xffffffff then
skin[active_tab .. "_color"] = color
edit_skin.update_player_skin(player)
edit_skin.show_formspec(player)
return true
end
end
return true
end)
local function init()
local f = io.open(minetest.get_modpath("edit_skin") .. "/list.json")
assert(f, "Can't open the file list.json")
local data = f:read("*all")
assert(data, "Can't read data from list.json")
local json, error = minetest.parse_json(data)
assert(json, error)
f:close()
for _, item in pairs(json) do
edit_skin.register_item(item)
end
edit_skin.steve.base_color = edit_skin.base_color[1]
edit_skin.steve.hair_color = edit_skin.color[1]
edit_skin.steve.top_color = edit_skin.color[12]
edit_skin.steve.bottom_color = edit_skin.color[13]
edit_skin.alex.base_color = edit_skin.base_color[1]
edit_skin.alex.hair_color = edit_skin.color[15]
edit_skin.alex.top_color = edit_skin.color[8]
edit_skin.alex.bottom_color = edit_skin.color[1]
-- Register junk first person hand nodes
local function make_texture(base, colorspec)
local output = ""
if edit_skin.masks[base] then
output = edit_skin.masks[base] ..
"^[colorize:" .. color_to_string(colorspec) .. ":alpha"
end
if #output > 0 then output = output .. "^" end
output = output .. base
return output
end
local hand_def = minetest.registered_items[""]
local range = hand_def and hand_def.range
for _, base in pairs(edit_skin.base) do
for _, base_color in pairs(edit_skin.base_color) do
local id = base:gsub(".png$", "") .. color_to_string(base_color):gsub("#", "")
minetest.register_node("edit_skin:" .. id, {
drawtype = "mesh",
groups = { not_in_creative_inventory = 1 },
tiles = { make_texture(base, base_color) },
use_texture_alpha = "clip",
mesh = "edit_skin_hand.obj",
range = range,
})
end
end
if minetest.global_exists("i3") then
i3.new_tab("edit_skin", {
description = S("Edit Skin"),
--image = "edit_skin_button.png", -- Icon covers label
access = function(player, data) return true end,
formspec = function(player, data, fs) end,
fields = function(player, data, fields)
i3.set_tab(player, "inventory")
edit_skin.show_formspec(player)
end,
})
end
if minetest.global_exists("sfinv_buttons") then
sfinv_buttons.register_button("edit_skin", {
title = S("Edit Skin"),
action = function(player) edit_skin.show_formspec(player) end,
tooltip = S("Open skin configuration screen."),
image = "edit_skin_button.png",
})
elseif minetest.global_exists("sfinv") then
sfinv.register_page("edit_skin", {
title = S("Edit Skin"),
get = function(self, player, context) return "" end,
on_enter = function(self, player, context)
sfinv.contexts[player:get_player_name()].page = sfinv.get_homepage_name(player)
edit_skin.show_formspec(player)
end
})
end
if minetest.global_exists("unified_inventory") then
unified_inventory.register_button("edit_skin", {
type = "image",
image = "edit_skin_button.png",
tooltip = S("Edit Skin"),
action = function(player)
edit_skin.show_formspec(player)
end,
})
end
if minetest.global_exists("armor") and armor.get_player_skin then
armor.get_player_skin = function(armor, name)
return edit_skin.compile_skin(edit_skin.player_skins[minetest.get_player_by_name(name)])
end
end
if minetest.global_exists("inventory_plus") then
minetest.register_on_player_receive_fields(function(player, formname, fields)
if formname == "" and fields.edit_skin then
edit_skin.show_formspec(player)
return true
end
return false
end)
end
if minetest.global_exists("smart_inventory") then
smart_inventory.register_page({
name = "skin_edit",
icon = "edit_skin_button.png",
tooltip = S("Edit Skin"),
smartfs_callback = function(state) return end,
sequence = 100,
on_button_click = function(state)
local player = minetest.get_player_by_name(state.location.rootState.location.player)
edit_skin.show_formspec(player)
end,
is_visible_func = function(state) return true end,
})
end
end
init()