init.lua 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. local function nextrange(x, max)
  2. x = x + 1
  3. if x > max then
  4. x = 0
  5. end
  6. return x
  7. end
  8. -- Handles rotation
  9. local function screwdriver_handler(itemstack, user, pointed_thing, mode)
  10. if pointed_thing.type ~= "node" then
  11. return
  12. end
  13. local pos = pointed_thing.under
  14. local keys = user:get_player_control()
  15. local player_name = user:get_player_name()
  16. if minetest.is_protected(pos, user:get_player_name()) then
  17. minetest.record_protection_violation(pos, user:get_player_name())
  18. return
  19. end
  20. local node = minetest.get_node(pos)
  21. local ndef = minetest.registered_nodes[node.name]
  22. if not ndef or not ndef.paramtype2 == "facedir" or
  23. (ndef.drawtype == "nodebox" and
  24. not ndef.node_box.type == "fixed") or
  25. node.param2 == nil then
  26. return
  27. end
  28. -- Set param2
  29. local n = node.param2
  30. local axisdir = math.floor(n / 4)
  31. local rotation = n - axisdir * 4
  32. if mode == 1 then
  33. n = axisdir * 4 + nextrange(rotation, 3)
  34. elseif mode == 3 then
  35. n = nextrange(axisdir, 5) * 4
  36. end
  37. node.param2 = n
  38. minetest.swap_node(pos, node)
  39. local item_wear = tonumber(itemstack:get_wear())
  40. item_wear = item_wear + 300 -- was 327
  41. if item_wear > 65535 then
  42. itemstack:clear()
  43. return itemstack
  44. end
  45. itemstack:set_wear(item_wear)
  46. return itemstack
  47. end
  48. -- Screwdriver
  49. minetest.register_tool("screwdriver:screwdriver", {
  50. description = "Screwdriver (left-click rotates face, right-click rotates axis)",
  51. inventory_image = "screwdriver.png",
  52. on_use = function(itemstack, user, pointed_thing)
  53. screwdriver_handler(itemstack, user, pointed_thing, 1)
  54. return itemstack
  55. end,
  56. on_place = function(itemstack, user, pointed_thing)
  57. screwdriver_handler(itemstack, user, pointed_thing, 3)
  58. return itemstack
  59. end,
  60. })
  61. minetest.register_craft({
  62. output = 'screwdriver:screwdriver',
  63. recipe = {
  64. {'', '', 'default:steel_ingot'},
  65. {'', 'default:steel_ingot', ''},
  66. {'group:stick', '', ''},
  67. }
  68. })
  69. -- Compatibility with original mod
  70. minetest.register_alias("screwdriver:screwdriver1", "screwdriver:screwdriver")
  71. minetest.register_alias("screwdriver:screwdriver2", "screwdriver:screwdriver")
  72. minetest.register_alias("screwdriver:screwdriver3", "screwdriver:screwdriver")
  73. minetest.register_alias("screwdriver:screwdriver4", "screwdriver:screwdriver")