1
0

init.lua 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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. })