stain.lua 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. local function set_wear(itemstack, level, max_level)
  2. local temp
  3. if level == 0 then
  4. temp = 0
  5. else
  6. temp = 65536 - math.floor(level / max_level * 65535)
  7. if temp > 65535 then temp = 65535 end
  8. if temp < 1 then temp = 1 end
  9. end
  10. itemstack:set_wear(temp)
  11. end
  12. local function get_wear(itemstack)
  13. if itemstack:get_metadata() == "" then
  14. return 30
  15. else
  16. return tonumber(itemstack:get_metadata())
  17. end
  18. end
  19. local stain_table = {
  20. {'Golden Oak', '1', 32, '#cc7431'},
  21. {'Country Pine', '2', 64, '#b76126'},
  22. {'Cinnamon', '3', 96, '#9c4a1b'},
  23. {'Cherry', '4', 128, '#873a14'},
  24. {'Mahogany', '5', 160, '#712b0d'},
  25. {'Walnut', '6', 192, '#5b1e07'},
  26. {'Black', '7', 224, '#461404'}
  27. }
  28. for i in ipairs (stain_table) do
  29. local desc = stain_table[i][1]
  30. local name = stain_table[i][2]
  31. local indx = stain_table[i][3]
  32. local colo = stain_table[i][4]
  33. minetest.register_tool('furniture:stain_brush'..name, {
  34. description = desc..' Stain brush',
  35. inventory_image = 'furniture_brush.png^(furniture_brush_overlay.png^[colorize:'..colo..':255)',
  36. stack_max = 1,
  37. wear_represents = 'content_level',
  38. groups = {stain_brush=1},
  39. on_use = function(itemstack, user, pointed_thing)
  40. if pointed_thing.type ~= 'node' then return end
  41. local player_name = user:get_player_name()
  42. local pos = minetest.get_pointed_thing_position(pointed_thing)
  43. if minetest.is_protected(pos, player_name) and not minetest.check_player_privs(user, 'protection_bypass') then
  44. return
  45. end
  46. local node = minetest.get_node(pos)
  47. if minetest.get_item_group(node.name, 'stainable') > 0 then
  48. local fdir = node.param2 % 32
  49. local stain = get_wear(itemstack)
  50. if stain == 0 then
  51. itemstack:set_name('furniture:brush')
  52. return itemstack end
  53. if node.param2 == (fdir + indx) then
  54. local player = user:get_player_name()
  55. minetest.chat_send_player(player, "Already stained this color.")
  56. return
  57. elseif node.param2 >= (fdir + indx) then
  58. local player = user:get_player_name()
  59. minetest.chat_send_player(player, "You can only stain things darker.")
  60. return end
  61. minetest.swap_node(pos, {name = node.name, param2 = fdir + indx})
  62. stain = stain - 1
  63. itemstack:set_metadata(tostring(stain))
  64. set_wear(itemstack, stain, 30)
  65. return itemstack
  66. else
  67. local player = user:get_player_name()
  68. minetest.chat_send_player(player, "You can't stain that item.")
  69. end
  70. end,
  71. })
  72. end
  73. minetest.register_tool('furniture:sanding', {
  74. description = 'Sanding pad',
  75. inventory_image = 'furniture_sanding.png',
  76. stack_max = 1,
  77. wear_represents = 'content_level',
  78. on_use = function(itemstack, user, pointed_thing)
  79. if pointed_thing.type ~= 'node' then return end
  80. local player_name = user:get_player_name()
  81. local pos = minetest.get_pointed_thing_position(pointed_thing)
  82. if minetest.is_protected(pos, player_name) and not minetest.check_player_privs(user, 'protection_bypass') then
  83. return
  84. end
  85. local node = minetest.get_node(pos)
  86. if minetest.get_item_group(node.name, 'stainable') > 0 then
  87. --local mod = string.sub(node.name, 1, 9)
  88. --if mod == 'furniture' then
  89. local fdir = node.param2 % 32
  90. if node.param2 == fdir then
  91. local player = user:get_player_name()
  92. minetest.chat_send_player(player, "Already sanded.")
  93. return end
  94. minetest.swap_node(pos, {name = node.name, param2 = fdir})
  95. itemstack:add_wear(65535 / 48)
  96. return itemstack
  97. else
  98. local player = user:get_player_name()
  99. minetest.chat_send_player(player, "You can't sand that item.")
  100. end
  101. end,
  102. })