use on_use instead of on_rightclick to apply dyes

also add some useful messages if the target was already
the same color as the dye, can't be colored, can't take
the requested color, etc.
This commit is contained in:
Vanessa Ezekowitz 2017-02-04 19:40:15 -05:00
parent 112ba73ed6
commit e981b29cd2

156
init.lua

@ -73,6 +73,24 @@ local HUES2 = {
"Red-violet" "Red-violet"
} }
local default_dyes = {
"black",
"blue",
"brown",
"cyan",
"dark_green",
"dark_grey",
"green",
"grey",
"magenta",
"orange",
"pink",
"red",
"violet",
"white",
"yellow"
}
-- code borrowed from homedecor -- code borrowed from homedecor
function unifieddyes.select_node(pointed_thing) function unifieddyes.select_node(pointed_thing)
@ -271,17 +289,49 @@ function unifieddyes.after_dig_node(pos, oldnode, oldmetadata, digger)
end end
end end
function unifieddyes.on_rightclick(pos, node, player, stack, pointed_thing, newnode, is_color_fdir) function unifieddyes.on_use(itemstack, player, pointed_thing)
local name = player:get_player_name()
if minetest.is_protected(pos,name) and not minetest.check_player_privs(name,{protection_bypass=true}) then
minetest.record_protection_violation(pos,name)
return stack
end
local name = stack:get_name()
local pos2 = unifieddyes.select_node(pointed_thing)
local paletteidx, hue = unifieddyes.getpaletteidx(name, is_color_fdir)
print(dump(paletteidx)) if not pointed_thing or pointed_thing.type == "nothing" then return end -- if "using" the dye on air
local pos = minetest.get_pointed_thing_position(pointed_thing)
local node = minetest.get_node(pos)
local nodedef = minetest.registered_nodes[node.name]
-- if the node has an on_punch defined, bail out and call that instead.
local onpunch = nodedef.on_punch(pos, node, player, pointed_thing)
if onpunch then
return onpunch
end
-- if the target is unknown, has no groups defined, or isn't UD-colorable, just bail out
if not (nodedef and nodedef.groups and nodedef.groups.ud_param2_colorable) then return end
local newnode = nodedef.ud_replacement_node
local is_color_fdir
if nodedef.paramtype2 == "color" then
is_color_fdir = false
elseif nodedef.paramtype2 == "colorfacedir"
then is_color_fdir = true
elseif nodedef.paramtype2 == "colorwallmounted"
then is_color_fdir = "wallmounted"
end
local playername = player:get_player_name()
if is_color_fdir == nil then -- target node doesn't support coloring at all.
minetest.chat_send_player(playername, "That node can't be colored.")
return
end
if minetest.is_protected(pos, playername) and not minetest.check_player_privs(playername, {protection_bypass=true}) then
minetest.record_protection_violation(pos, playername)
return
end
local stackname = itemstack:get_name()
local pos2 = unifieddyes.select_node(pointed_thing)
local paletteidx, hue = unifieddyes.getpaletteidx(stackname, is_color_fdir)
if paletteidx then if paletteidx then
@ -297,14 +347,19 @@ function unifieddyes.on_rightclick(pos, node, player, stack, pointed_thing, newn
end end
end end
meta:set_string("dye", name) meta:set_string("dye", stackname)
if not creative_mode and prevdye ~= name then
stack:take_item() if prevdye == stackname then
local a,b = string.find(stackname, ":")
minetest.chat_send_player(playername, "That node is already "..string.sub(stackname, a + 1).."." )
return
elseif not creative_mode then
itemstack:take_item()
end end
node.param2 = paletteidx node.param2 = paletteidx
local oldpaletteidx, oldhuenum = unifieddyes.getpaletteidx(prevdye, is_color_fdir) local oldpaletteidx, oldhuenum = unifieddyes.getpaletteidx(prevdye, is_color_fdir)
local oldnode = minetest.get_node(pos) local oldnode = minetest.get_node(pos)
local oldhue = nil local oldhue = nil
@ -343,6 +398,9 @@ function unifieddyes.on_rightclick(pos, node, player, stack, pointed_thing, newn
end end
node.name = newnode node.name = newnode
minetest.swap_node(pos, node) minetest.swap_node(pos, node)
if not creative_mode then
return itemstack
end
else -- this path is used when you're just painting an existing node, rather than replacing one. else -- this path is used when you're just painting an existing node, rather than replacing one.
newnode = oldnode -- note that here, newnode/oldnode are a full node, not just the name. newnode = oldnode -- note that here, newnode/oldnode are a full node, not just the name.
if is_color_fdir == "wallmounted" then if is_color_fdir == "wallmounted" then
@ -362,31 +420,24 @@ function unifieddyes.on_rightclick(pos, node, player, stack, pointed_thing, newn
newnode.param2 = paletteidx newnode.param2 = paletteidx
end end
minetest.swap_node(pos, newnode) minetest.swap_node(pos, newnode)
end
else -- here is where a node is just being placed, not something being colored
if unifieddyes.is_buildable_to(player:get_player_name(), pos2) and
minetest.registered_nodes[name] then
local placeable_node = minetest.registered_nodes[stack:get_name()]
local fdir = 0
if is_color_fdir == "wallmounted" then
local yaw = player:get_look_yaw()
local dir = minetest.yaw_to_dir(yaw-1.5)
fdir = minetest.dir_to_wallmounted(dir)
elseif is_color_fdir then
local yaw = player:get_look_yaw()
local dir = minetest.yaw_to_dir(yaw-1.5)
fdir = minetest.dir_to_facedir(dir)
end
minetest.set_node(pos2, { name = placeable_node.name, param2 = fdir })
if not creative_mode then if not creative_mode then
stack:take_item() return itemstack
end
return stack
end end
end end
else
local a,b = string.find(stackname, ":")
if a then
minetest.chat_send_player(playername, "That node can't be colored "..string.sub(stackname, a + 1).."." )
end
end
end
-- re-define default dyes slightly, to add on_use
for _, color in ipairs(default_dyes) do
minetest.override_item("dye:"..color, {
on_use = unifieddyes.on_use
})
end end
-- Items/recipes needed to generate the few base colors that are not -- Items/recipes needed to generate the few base colors that are not
@ -397,7 +448,8 @@ end
minetest.register_craftitem(":dye:lime", { minetest.register_craftitem(":dye:lime", {
description = S("Lime Dye"), description = S("Lime Dye"),
inventory_image = "unifieddyes_lime.png", inventory_image = "unifieddyes_lime.png",
groups = { dye=1, excolor_lime=1, unicolor_lime=1, not_in_creative_inventory=1 } groups = { dye=1, excolor_lime=1, unicolor_lime=1, not_in_creative_inventory=1 },
on_use = unifieddyes.on_use
}) })
minetest.register_craft( { minetest.register_craft( {
@ -414,7 +466,8 @@ minetest.register_craft( {
minetest.register_craftitem(":dye:aqua", { minetest.register_craftitem(":dye:aqua", {
description = S("Aqua Dye"), description = S("Aqua Dye"),
inventory_image = "unifieddyes_aqua.png", inventory_image = "unifieddyes_aqua.png",
groups = { dye=1, excolor_aqua=1, unicolor_aqua=1, not_in_creative_inventory=1 } groups = { dye=1, excolor_aqua=1, unicolor_aqua=1, not_in_creative_inventory=1 },
on_use = unifieddyes.on_use
}) })
minetest.register_craft( { minetest.register_craft( {
@ -431,7 +484,8 @@ minetest.register_craft( {
minetest.register_craftitem(":dye:skyblue", { minetest.register_craftitem(":dye:skyblue", {
description = S("Sky-blue Dye"), description = S("Sky-blue Dye"),
inventory_image = "unifieddyes_skyblue.png", inventory_image = "unifieddyes_skyblue.png",
groups = { dye=1, excolor_sky_blue=1, unicolor_sky_blue=1, not_in_creative_inventory=1 } groups = { dye=1, excolor_sky_blue=1, unicolor_sky_blue=1, not_in_creative_inventory=1 },
on_use = unifieddyes.on_use
}) })
minetest.register_craft( { minetest.register_craft( {
@ -448,7 +502,8 @@ minetest.register_craft( {
minetest.register_craftitem(":dye:redviolet", { minetest.register_craftitem(":dye:redviolet", {
description = S("Red-violet Dye"), description = S("Red-violet Dye"),
inventory_image = "unifieddyes_redviolet.png", inventory_image = "unifieddyes_redviolet.png",
groups = { dye=1, excolor_red_violet=1, unicolor_red_violet=1, not_in_creative_inventory=1 } groups = { dye=1, excolor_red_violet=1, unicolor_red_violet=1, not_in_creative_inventory=1 },
on_use = unifieddyes.on_use
}) })
minetest.register_craft( { minetest.register_craft( {
@ -466,7 +521,8 @@ minetest.register_craft( {
minetest.register_craftitem(":dye:light_grey", { minetest.register_craftitem(":dye:light_grey", {
description = S("Light Grey Dye"), description = S("Light Grey Dye"),
inventory_image = "unifieddyes_lightgrey.png", inventory_image = "unifieddyes_lightgrey.png",
groups = { dye=1, excolor_lightgrey=1, unicolor_light_grey=1, not_in_creative_inventory=1 } groups = { dye=1, excolor_lightgrey=1, unicolor_light_grey=1, not_in_creative_inventory=1 },
on_use = unifieddyes.on_use
}) })
minetest.register_craft( { minetest.register_craft( {
@ -643,40 +699,46 @@ for i = 1, 12 do
minetest.register_craftitem("unifieddyes:dark_" .. hue .. "_s50", { minetest.register_craftitem("unifieddyes:dark_" .. hue .. "_s50", {
description = S("Dark " .. hue2 .. " Dye (low saturation)"), description = S("Dark " .. hue2 .. " Dye (low saturation)"),
inventory_image = "unifieddyes_dark_" .. hue .. "_s50.png", inventory_image = "unifieddyes_dark_" .. hue .. "_s50.png",
groups = { dye=1, ["unicolor_dark_"..hue.."_s50"]=1, not_in_creative_inventory=1 } groups = { dye=1, ["unicolor_dark_"..hue.."_s50"]=1, not_in_creative_inventory=1 },
on_use = unifieddyes.on_use
}) })
if hue ~= "green" then if hue ~= "green" then
minetest.register_craftitem("unifieddyes:dark_" .. hue, { minetest.register_craftitem("unifieddyes:dark_" .. hue, {
description = S("Dark " .. hue2 .. " Dye"), description = S("Dark " .. hue2 .. " Dye"),
inventory_image = "unifieddyes_dark_" .. hue .. ".png", inventory_image = "unifieddyes_dark_" .. hue .. ".png",
groups = { dye=1, ["unicolor_dark_"..hue]=1, not_in_creative_inventory=1 } groups = { dye=1, ["unicolor_dark_"..hue]=1, not_in_creative_inventory=1 },
on_use = unifieddyes.on_use
}) })
end end
minetest.register_craftitem("unifieddyes:medium_" .. hue .. "_s50", { minetest.register_craftitem("unifieddyes:medium_" .. hue .. "_s50", {
description = S("Medium " .. hue2 .. " Dye (low saturation)"), description = S("Medium " .. hue2 .. " Dye (low saturation)"),
inventory_image = "unifieddyes_medium_" .. hue .. "_s50.png", inventory_image = "unifieddyes_medium_" .. hue .. "_s50.png",
groups = { dye=1, ["unicolor_medium_"..hue.."_s50"]=1, not_in_creative_inventory=1 } groups = { dye=1, ["unicolor_medium_"..hue.."_s50"]=1, not_in_creative_inventory=1 },
on_use = unifieddyes.on_use
}) })
minetest.register_craftitem("unifieddyes:medium_" .. hue, { minetest.register_craftitem("unifieddyes:medium_" .. hue, {
description = S("Medium " .. hue2 .. " Dye"), description = S("Medium " .. hue2 .. " Dye"),
inventory_image = "unifieddyes_medium_" .. hue .. ".png", inventory_image = "unifieddyes_medium_" .. hue .. ".png",
groups = { dye=1, ["unicolor_medium_"..hue]=1, not_in_creative_inventory=1 } groups = { dye=1, ["unicolor_medium_"..hue]=1, not_in_creative_inventory=1 },
on_use = unifieddyes.on_use
}) })
minetest.register_craftitem("unifieddyes:" .. hue .. "_s50", { minetest.register_craftitem("unifieddyes:" .. hue .. "_s50", {
description = S(hue2 .. " Dye (low saturation)"), description = S(hue2 .. " Dye (low saturation)"),
inventory_image = "unifieddyes_" .. hue .. "_s50.png", inventory_image = "unifieddyes_" .. hue .. "_s50.png",
groups = { dye=1, ["unicolor_"..hue.."_s50"]=1, not_in_creative_inventory=1 } groups = { dye=1, ["unicolor_"..hue.."_s50"]=1, not_in_creative_inventory=1 },
on_use = unifieddyes.on_use
}) })
if hue ~= "red" then if hue ~= "red" then
minetest.register_craftitem("unifieddyes:light_" .. hue, { minetest.register_craftitem("unifieddyes:light_" .. hue, {
description = S("Light " .. hue2 .. " Dye"), description = S("Light " .. hue2 .. " Dye"),
inventory_image = "unifieddyes_light_" .. hue .. ".png", inventory_image = "unifieddyes_light_" .. hue .. ".png",
groups = { dye=1, ["unicolor_light_"..hue]=1, not_in_creative_inventory=1 } groups = { dye=1, ["unicolor_light_"..hue]=1, not_in_creative_inventory=1 },
on_use = unifieddyes.on_use
}) })
end end
minetest.register_alias("unifieddyes:"..hue, "dye:"..hue) minetest.register_alias("unifieddyes:"..hue, "dye:"..hue)