Skip to content

Instantly share code, notes, and snippets.

@juhdanad
Created May 20, 2017 11:39
Show Gist options
  • Save juhdanad/433a0aa7094ae0dceaf00924d6fd1e3b to your computer and use it in GitHub Desktop.
Save juhdanad/433a0aa7094ae0dceaf00924d6fd1e3b to your computer and use it in GitHub Desktop.
Wool mod with autocolor
-- This uses a trick: you can first define the recipes using all of the base
-- colors, and then some recipes using more specific colors for a few non-base
-- colors available. When crafting, the last recipes will be checked first.
local dyes = {
{"white", "White", "basecolor_white"},
{"grey", "Grey", "basecolor_grey"},
{"black", "Black", "basecolor_black"},
{"red", "Red", "basecolor_red"},
{"yellow", "Yellow", "basecolor_yellow"},
{"green", "Green", "basecolor_green"},
{"cyan", "Cyan", "basecolor_cyan"},
{"blue", "Blue", "basecolor_blue"},
{"magenta", "Magenta", "basecolor_magenta"},
{"orange", "Orange", "excolor_orange"},
{"violet", "Violet", "excolor_violet"},
{"brown", "Brown", "unicolor_dark_orange"},
{"pink", "Pink", "unicolor_light_red"},
{"dark_grey", "Dark Grey", "unicolor_darkgrey"},
{"dark_green", "Dark Green", "unicolor_dark_green"},
}
minetest.register_node("wool:block", {
description = "Wool",
tiles = {"wool_block.png"},
paramtype2 = "color",
palette = "wool_palette.png",
is_ground_content = false,
groups = {snappy = 2, choppy = 2, oddly_breakable_by_hand = 3,
flammable = 3, wool = 1},
sounds = default.node_sound_defaults(),
})
minetest.register_craft({
output = "wool:block",
type = "shapeless",
recipe = {
"wool:block",
"group:dye",
},
})
minetest.register_on_craft(function(itemstack, player, old_craft_grid, craft_inv)
if itemstack:get_name() ~= "wool:block" then
return
end
local index = false
local dye
-- find dye color
for i = 1, player:get_inventory():get_size("craft") do
if old_craft_grid[i]:get_name():find("dye:") then
index = i
dye = old_craft_grid[i]:get_name():gsub("dye:", "")
end
end
if not index then
return
end
local cindex = false
for k, v in ipairs(dyes) do
if dye == v[1] then
cindex = k
end
end
if not cindex then
return
end
-- replace output with dyed itemstack
local meta = itemstack:get_meta()
meta:set_int("palette_index", cindex - 1)
-- do not add description, it would disappear when placing the node
-- meta:set_string("description", dyes[cindex][2] .. " wool")
return itemstack
end)
-- legacy
-- Backwards compatibility with jordach's 16-color wool mod
minetest.register_alias("wool:dark_blue", "wool:blue")
minetest.register_alias("wool:gold", "wool:yellow")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment