Finished leather armor colorizing

This commit is contained in:
the-real-herowl 2024-01-25 09:20:17 +01:00 committed by the-real-herowl
parent caa82c40c9
commit 3d799266c3

View file

@ -20,25 +20,36 @@ local colors = {
{ "white", "White", "mcl_dye:white", "#d1d7d8" }, { "white", "White", "mcl_dye:white", "#d1d7d8" },
} }
--local function get_color_rgb(color) -- #608b03 from #495d20
-- return tonumber(str.sub(first, 2, 3)), tonumber(str.sub(first, 4, 5)), tonumber(str.sub(first, 6, 7))
--end
local function color_string_to_table(colorstring)
return {
r = tonumber(colorstring:sub(2,3), 16), -- 16 as second parameter allows hexadecimal
g = tonumber(colorstring:sub(4,5), 16),
b = tonumber(colorstring:sub(6,7), 16),
}
end
local function av(a, b)
return (a + b)/2
end
local function calculate_color(first, last) local function calculate_color(first, last)
--local first_r = tonumber(str.sub(first, 2, 3)) return {
--local first_g = tonumber(str.sub(first, 4, 5)) r = av(first.r, last.r),
return tonumber(first)*tonumber(last) g = av(first.g, last.g),
b = av(first.b, last.b),
}
end end
local function get_texture_function(texture) local function get_texture_function(texture)
local function get_texture(_, itemstack) local function get_texture(_, itemstack)
local out local out
local color = itemstack:get_meta():get_string("color") local color = itemstack:get_meta():get_string("mcl_armor:color")
if color == "" or color == nil then if color == "" or color == nil then
out = texture out = texture
else else
out = texture.."^[multiply:"..color out = texture.."^[hsl:0:100:50^[multiply:"..color
end end
if mcl_enchanting.is_enchanted(itemstack:get_name()) then if mcl_enchanting.is_enchanted(itemstack:get_name()) then
@ -52,6 +63,36 @@ local function get_texture_function(texture)
return get_texture return get_texture
end end
function mcl_armor.colorize_leather_armor(itemstack, colorstring)
local color = color_string_to_table(colorstring)
local meta = itemstack:get_meta()
local old_color = meta:get_string("mcl_armor:color")
if old_color ~= "" then
color = calculate_color(
color_string_to_table(minetest.colorspec_to_colorstring(old_color)),
color
)
colorstring = minetest.colorspec_to_colorstring(color)
end
meta:set_string("mcl_armor:color", colorstring)
meta:set_string("inventory_image",
itemstack:get_definition().inventory_image .. "^[hsl:0:100:50^[multiply:" .. colorstring
)
tt.reload_itemstack_description(itemstack)
return itemstack
end
function mcl_armor.wash_leather_armor(itemstack)
if not itemstack or not itemstack:get_definition().groups.armor_leather == 1 then
return
end
local meta = itemstack:get_meta()
meta:set_string("mcl_armor:color", "")
meta:set_string("inventory_image", "")
tt.reload_itemstack_description(itemstack)
return itemstack
end
mcl_armor.register_set({ mcl_armor.register_set({
name = "leather", name = "leather",
description = "Leather", description = "Leather",
@ -81,30 +122,34 @@ tt.register_priority_snippet(function(_, _, itemstack)
if not itemstack or not itemstack:get_definition().groups.armor_leather == 1 then if not itemstack or not itemstack:get_definition().groups.armor_leather == 1 then
return return
end end
local color = itemstack:get_meta():get_string("color") local color = itemstack:get_meta():get_string("mcl_armor:color")
if color and color ~= "" then if color and color ~= "" then
--TODO: replace by just "Dyed"
local text = C(mcl_colors.GRAY, "Dyed: "..color) local text = C(mcl_colors.GRAY, "Dyed: "..color)
return text, false return text, false
end end
end) end)
-- This command is only temporary
minetest.register_chatcommand("color_leather", { minetest.register_chatcommand("color_leather", {
params = "<color>", params = "<color>",
description = "Colorize a leather armor", description = "Colorize a piece of leather armor, or wash it",
func = function(name, param) func = function(name, param)
local player = minetest.get_player_by_name(name) local player = minetest.get_player_by_name(name)
if player then if player then
local item = player:get_wielded_item() local item = player:get_wielded_item()
item:get_meta():set_string("color", param) if not item or not item:get_definition().groups.armor_leather == 1 then
tt.reload_itemstack_description(item) return false, "Not leather armor."
player:set_wielded_item(item) end
return true, "Done." if param == "wash" then
player:set_wielded_item(mcl_armor.wash_leather_armor(item))
return true, "Washed."
end
local colorstring = minetest.colorspec_to_colorstring(param)
if not colorstring then return false, "Invalid color" end
player:set_wielded_item(mcl_armor.colorize_leather_armor(item, colorstring))
return true, "Done: " .. colorstring
else else
return false, "Player isn't online" return false, "Player isn't online"
end end
end, end,
}) })