2
0

init.lua 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. local S = core.get_translator("testitems")
  2. --
  3. -- Texture overlays for items
  4. --
  5. -- For the global overlay color test
  6. local GLOBAL_COLOR_ARG = "orange"
  7. -- Punch handler to set random color with "color" argument in item metadata
  8. local overlay_on_use = function(itemstack, user, pointed_thing)
  9. local meta = itemstack:get_meta()
  10. local color = math.random(0x0, 0xFFFFFF)
  11. local colorstr = string.format("#%06x", color)
  12. meta:set_string("color", colorstr)
  13. core.log("action", "[testitems] Color of "..itemstack:get_name().." changed to "..colorstr)
  14. return itemstack
  15. end
  16. -- Place handler to clear item metadata color
  17. local overlay_on_place = function(itemstack, user, pointed_thing)
  18. local meta = itemstack:get_meta()
  19. meta:set_string("color", "")
  20. return itemstack
  21. end
  22. core.register_craftitem("testitems:overlay_meta", {
  23. description = S("Texture Overlay Test Item, Meta Color") .. "\n" ..
  24. S("Image must be a square with rainbow cross (inventory and wield)") .. "\n" ..
  25. S("Item meta color must only change square color") .. "\n" ..
  26. S("Punch: Set random color") .. "\n" ..
  27. S("Place: Clear color"),
  28. -- Base texture: A grayscale square (can be colorized)
  29. inventory_image = "testitems_overlay_base.png",
  30. wield_image = "testitems_overlay_base.png",
  31. -- Overlay: A rainbow cross (NOT to be colorized!)
  32. inventory_overlay = "testitems_overlay_overlay.png",
  33. wield_overlay = "testitems_overlay_overlay.png",
  34. on_use = overlay_on_use,
  35. on_place = overlay_on_place,
  36. on_secondary_use = overlay_on_place,
  37. })
  38. core.register_craftitem("testitems:overlay_global", {
  39. description = S("Texture Overlay Test Item, Global Color") .. "\n" ..
  40. S("Image must be an orange square with rainbow cross (inventory and wield)"),
  41. -- Base texture: A grayscale square (to be colorized)
  42. inventory_image = "testitems_overlay_base.png",
  43. wield_image = "testitems_overlay_base.png",
  44. -- Overlay: A rainbow cross (NOT to be colorized!)
  45. inventory_overlay = "testitems_overlay_overlay.png",
  46. wield_overlay = "testitems_overlay_overlay.png",
  47. color = GLOBAL_COLOR_ARG,
  48. })
  49. core.register_craftitem("testitems:image_meta", {
  50. description = S("Image Override Meta Test Item"),
  51. inventory_image = "default_apple.png",
  52. wield_image = "basetools_icesword.png",
  53. on_use = function(itemstack, player)
  54. local meta = itemstack:get_meta()
  55. local state = meta:get_int("state")
  56. state = (state + 1) % 5
  57. meta:set_int("state", state)
  58. core.chat_send_player(player:get_player_name(), "State " .. state)
  59. if state == 0 then
  60. meta:set_string("inventory_image", "")
  61. meta:set_string("wield_image", "")
  62. meta:set_string("inventory_overlay", "")
  63. meta:set_string("wield_overlay", "")
  64. meta:set_string("wield_scale", "")
  65. elseif state == 1 then
  66. meta:set_string("inventory_image", "default_tree.png")
  67. meta:set_string("wield_image", "basetools_firesword.png")
  68. elseif state == 2 then
  69. meta:set_string("inventory_image", "default_apple.png^testitems_overridden.png")
  70. meta:set_string("wield_image", "basetools_icesword.png^testitems_overridden.png")
  71. elseif state == 3 then
  72. meta:set_string("inventory_image", "default_tree.png")
  73. meta:set_string("wield_image", "basetools_firesword.png")
  74. meta:set_string("inventory_overlay", "default_apple.png")
  75. meta:set_string("wield_overlay", "default_apple.png")
  76. elseif state == 4 then
  77. local scale = vector.new(0.5, 0.5, 0.5)
  78. meta:set_string("wield_scale", scale:to_string())
  79. end
  80. return itemstack
  81. end,
  82. })
  83. core.register_craftitem("testitems:telescope_stick", {
  84. description = S("Telescope Stick (Increases range on use.)"),
  85. inventory_image = "testitems_telescope_stick.png",
  86. on_use = function(itemstack, player)
  87. local meta = itemstack:get_meta()
  88. local range = meta:get_float("range") + 1.2
  89. if range > 10 then
  90. range = 0
  91. end
  92. meta:set_float("range", range)
  93. core.chat_send_player(player:get_player_name(), "Telescope Stick range set to "..range)
  94. return itemstack
  95. end,
  96. })
  97. -- Tree spawners
  98. local tree_def={
  99. axiom="Af",
  100. rules_a="TT[&GB][&+GB][&++GB][&+++GB]A",
  101. rules_b="[+GB]fB",
  102. trunk="basenodes:tree",
  103. leaves="basenodes:leaves",
  104. angle=90,
  105. iterations=4,
  106. trunk_type="single",
  107. thin_branches=true,
  108. }
  109. core.register_craftitem("testitems:tree_spawner", {
  110. description = S("Tree Spawner"),
  111. inventory_image = "testitems_tree_spawner.png",
  112. on_place = function(itemstack, placer, pointed_thing)
  113. if (not pointed_thing or pointed_thing.type ~= "node") then
  114. return
  115. end
  116. core.spawn_tree(pointed_thing.above, tree_def)
  117. end,
  118. })
  119. local vmanip_for_trees = {} -- per player
  120. core.register_craftitem("testitems:tree_spawner_vmanip", {
  121. description = S("Tree Spawner using VoxelManip"),
  122. inventory_image = "testitems_tree_spawner_vmanip.png",
  123. on_place = function(itemstack, placer, pointed_thing)
  124. if (not pointed_thing or pointed_thing.type ~= "node" or
  125. not core.is_player(placer)) then
  126. return
  127. end
  128. local name = placer:get_player_name()
  129. local vm = vmanip_for_trees[name]
  130. if not vm then
  131. vm = VoxelManip(vector.add(pointed_thing.above, 20),
  132. vector.subtract(pointed_thing.above, 20))
  133. vmanip_for_trees[name] = vm
  134. core.chat_send_player(name,
  135. "Tree in new VoxelManip spawned, left click to apply to map, "..
  136. "or right click to add more trees.")
  137. end
  138. core.spawn_tree_on_vmanip(vm, pointed_thing.above, tree_def)
  139. end,
  140. on_use = function(itemstack, user, pointed_thing)
  141. if not core.is_player(user) then
  142. return
  143. end
  144. local name = user:get_player_name()
  145. local vm = vmanip_for_trees[name]
  146. if vm then
  147. vm:write_to_map()
  148. vmanip_for_trees[name] = nil
  149. core.chat_send_player(name, "VoxelManip written to map.")
  150. end
  151. end,
  152. })
  153. core.register_on_leaveplayer(function(player, timed_out)
  154. vmanip_for_trees[player:get_player_name()] = nil
  155. end)