init.lua 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. local modname = assert(core.get_current_modname())
  2. local modstorage = core.get_mod_storage()
  3. local mod_channel
  4. dofile(core.get_modpath(modname) .. "example.lua")
  5. core.register_on_shutdown(function()
  6. print("[PREVIEW] shutdown client")
  7. end)
  8. local id = nil
  9. do
  10. local server_info = core.get_server_info()
  11. print("Server version: " .. server_info.protocol_version)
  12. print("Server ip: " .. server_info.ip)
  13. print("Server address: " .. server_info.address)
  14. print("Server port: " .. server_info.port)
  15. print("CSM restrictions: " .. dump(core.get_csm_restrictions()))
  16. local l1, l2 = core.get_language()
  17. print("Configured language: " .. l1 .. " / " .. l2)
  18. end
  19. mod_channel = core.mod_channel_join("experimental_preview")
  20. core.after(4, function()
  21. if mod_channel:is_writeable() then
  22. mod_channel:send_all("preview talk to experimental")
  23. end
  24. end)
  25. core.after(1, function()
  26. print("armor: " .. dump(core.localplayer:get_armor_groups()))
  27. id = core.localplayer:hud_add({
  28. type = "text",
  29. name = "example",
  30. number = 0xff0000,
  31. position = {x=0, y=1},
  32. offset = {x=8, y=-8},
  33. text = "You are using the preview mod",
  34. scale = {x=200, y=60},
  35. alignment = {x=1, y=-1},
  36. })
  37. end)
  38. core.register_on_modchannel_message(function(channel, sender, message)
  39. print("[PREVIEW][modchannels] Received message `" .. message .. "` on channel `"
  40. .. channel .. "` from sender `" .. sender .. "`")
  41. core.after(1, function()
  42. mod_channel:send_all("CSM preview received " .. message)
  43. end)
  44. end)
  45. core.register_on_modchannel_signal(function(channel, signal)
  46. print("[PREVIEW][modchannels] Received signal id `" .. signal .. "` on channel `"
  47. .. channel)
  48. end)
  49. core.register_on_inventory_open(function(inventory)
  50. print("INVENTORY OPEN")
  51. print(dump(inventory))
  52. return false
  53. end)
  54. core.register_on_placenode(function(pointed_thing, node)
  55. print("The local player place a node!")
  56. print("pointed_thing :" .. dump(pointed_thing))
  57. print("node placed :" .. dump(node))
  58. return false
  59. end)
  60. core.register_on_item_use(function(itemstack, pointed_thing)
  61. print("The local player used an item!")
  62. print("pointed_thing :" .. dump(pointed_thing))
  63. print("item = " .. itemstack:get_name())
  64. if not itemstack:is_empty() then
  65. return false
  66. end
  67. local pos = core.camera:get_pos()
  68. local pos2 = vector.add(pos, vector.multiply(core.camera:get_look_dir(), 100))
  69. local rc = core.raycast(pos, pos2)
  70. local i = rc:next()
  71. print("[PREVIEW] raycast next: " .. dump(i))
  72. if i then
  73. print("[PREVIEW] line of sight: " .. (core.line_of_sight(pos, i.above) and "yes" or "no"))
  74. local n1 = core.find_nodes_in_area(pos, i.under, {"default:stone"})
  75. local n2 = core.find_nodes_in_area_under_air(pos, i.under, {"default:stone"})
  76. print(("[PREVIEW] found %s nodes, %s nodes under air"):format(
  77. n1 and #n1 or "?", n2 and #n2 or "?"))
  78. end
  79. return false
  80. end)
  81. -- This is an example function to ensure it's working properly, should be removed before merge
  82. core.register_on_receiving_chat_message(function(message)
  83. print("[PREVIEW] Received message " .. message)
  84. return false
  85. end)
  86. -- This is an example function to ensure it's working properly, should be removed before merge
  87. core.register_on_sending_chat_message(function(message)
  88. print("[PREVIEW] Sending message " .. message)
  89. return false
  90. end)
  91. core.register_on_chatcommand(function(command, params)
  92. print("[PREVIEW] caught command '"..command.."'. Parameters: '"..params.."'")
  93. end)
  94. -- This is an example function to ensure it's working properly, should be removed before merge
  95. core.register_on_hp_modification(function(hp)
  96. print("[PREVIEW] HP modified " .. hp)
  97. end)
  98. -- This is an example function to ensure it's working properly, should be removed before merge
  99. core.register_on_damage_taken(function(hp)
  100. print("[PREVIEW] Damage taken " .. hp)
  101. end)
  102. -- This is an example function to ensure it's working properly, should be removed before merge
  103. core.register_chatcommand("dump", {
  104. func = function(param)
  105. return true, dump(_G)
  106. end,
  107. })
  108. local function preview_minimap()
  109. local minimap = core.ui.minimap
  110. if not minimap then
  111. print("[PREVIEW] Minimap is disabled. Skipping.")
  112. return
  113. end
  114. minimap:set_mode(4)
  115. minimap:show()
  116. minimap:set_pos({x=5, y=50, z=5})
  117. minimap:set_shape(math.random(0, 1))
  118. print("[PREVIEW] Minimap: mode => " .. dump(minimap:get_mode()) ..
  119. " position => " .. dump(minimap:get_pos()) ..
  120. " angle => " .. dump(minimap:get_angle()))
  121. end
  122. core.after(2, function()
  123. print("[PREVIEW] loaded " .. modname .. " mod")
  124. modstorage:set_string("current_mod", modname)
  125. assert(modstorage:get_string("current_mod") == modname)
  126. preview_minimap()
  127. end)
  128. core.after(5, function()
  129. if core.ui.minimap then
  130. core.ui.minimap:show()
  131. end
  132. print("[PREVIEW] Time of day " .. core.get_timeofday())
  133. print("[PREVIEW] Node level: " .. core.get_node_level({x=0, y=20, z=0}) ..
  134. " max level " .. core.get_node_max_level({x=0, y=20, z=0}))
  135. print("[PREVIEW] Find node near: " .. dump(core.find_node_near({x=0, y=20, z=0}, 10,
  136. {"group:tree", "default:dirt", "default:stone"})))
  137. print("[PREVIEW] Settings: preview_csm_test_setting = " ..
  138. tostring(core.settings:get_bool("preview_csm_test_setting", false)))
  139. end)
  140. core.register_on_dignode(function(pos, node)
  141. print("The local player dug a node!")
  142. print("pos:" .. dump(pos))
  143. print("node:" .. dump(node))
  144. return false
  145. end)
  146. core.register_on_punchnode(function(pos, node)
  147. print("The local player punched a node!")
  148. local itemstack = core.localplayer:get_wielded_item()
  149. print(dump(itemstack:to_table()))
  150. print("pos:" .. dump(pos))
  151. print("node:" .. dump(node))
  152. local meta = core.get_meta(pos)
  153. print("punched meta: " .. (meta and dump(meta:to_table()) or "(missing)"))
  154. return false
  155. end)
  156. core.register_chatcommand("privs", {
  157. func = function(param)
  158. return true, core.privs_to_string(minetest.get_privilege_list())
  159. end,
  160. })
  161. core.register_chatcommand("text", {
  162. func = function(param)
  163. return core.localplayer:hud_change(id, "text", param)
  164. end,
  165. })
  166. core.register_on_mods_loaded(function()
  167. core.log("Yeah preview mod is loaded with other CSM mods.")
  168. end)