init.lua 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. local version = {
  2. major = 1,
  3. minor = 0,
  4. revison = 0
  5. }
  6. vm_lighting_wand = {}
  7. while not vm_lighting_wand.loaded do
  8. local MT = minetest
  9. local creative_mode_cache = MT.settings:get_bool("creative_mode")
  10. local modname = MT.get_current_modname()
  11. local modpath = MT.get_modpath(modname)
  12. local enable_command = MT.settings:get_bool(modname .. "_command", false)
  13. local give_free_wand = MT.settings:get_bool(modname .. "_give_free_wand", false)
  14. local give_free_power = tonumber(MT.settings:get(modname .. "_free_power") or 500)
  15. local log_lvls = { "none", "error", "warning", "action", "info", "verbose" }
  16. local logger = function(log, level)
  17. level = level or "action"
  18. if not log_lvls[level] then
  19. level = "action"
  20. end
  21. MT.log(level, "[" .. modname .. "] " .. log)
  22. end
  23. local include = function(name)
  24. return dofile(modpath .. "/" .. name .. ".lua")
  25. end
  26. logger("Hello minetest", "action")
  27. -- Mods Global table
  28. vm_lighting_wand = {
  29. version = version,
  30. locale = MT.get_translator(modname),
  31. modname = modname,
  32. logger = logger,
  33. fsbuilder = include("core/fsbuilder"),
  34. round = function(value, decimals)
  35. if not value or value <= 0 then
  36. return 0
  37. end
  38. if decimals and decimals > 0 then
  39. local mult = 10 ^ decimals
  40. return math.floor(value * mult + 0.5) / mult
  41. end
  42. return math.ceil(math.floor(value + 0.5))
  43. end,
  44. is_creative_mode = function(player)
  45. return creative_mode_cache or MT.check_player_privs(player:get_player_name(), {
  46. creative = true
  47. })
  48. end
  49. }
  50. -- Wand "features"
  51. include("core/wand") -- on_use()
  52. include("core/power") -- Power API
  53. include("core/levels") -- Configure Levels
  54. include("core/gui") -- on_use() "2nd"
  55. include("core/effects") -- sound/particles
  56. include("core/ignored") -- Add a Node,Group or Drawtype to the ignore lists
  57. include("core/pointed_node") -- get pointed_node in the world
  58. include("core/detached") -- detached inventory to add "power"
  59. include("core/crafting") -- means to add a crafting recipe from the config file
  60. -- Only load the cmd if its enabled
  61. if enable_command then
  62. include("core/commands")
  63. end
  64. -- Configure "features"
  65. include("config")
  66. -- if config didnt add levels then load the defaults
  67. if not vm_lighting_wand.get_first_level() then
  68. include("core/default-config")
  69. end
  70. -- add Nodes and wand tool
  71. include("tools")
  72. include("nodes")
  73. -- !config.lua can override this if set.
  74. -- try set a recipe if not already set
  75. if not vm_lighting_wand.has_valid_recipe() then
  76. -- Minetest game
  77. if MT.get_modpath("default") then
  78. vm_lighting_wand.set_crafting_items("default:meselamp", "default:obsidian")
  79. end
  80. -- Mineclone2
  81. if MT.get_modpath("mcl_core") and MT.get_modpath("mcl_nether") then
  82. vm_lighting_wand.set_crafting_items("mcl_nether:glowstone", "mcl_core:obsidian")
  83. end
  84. -- Hades Revisited
  85. if MT.get_modpath("hades_core") and MT.get_modpath("glowcrystals") then
  86. vm_lighting_wand.set_crafting_items("glowcrystals:glowcrystal_block", "hades_core:obsidian")
  87. end
  88. end
  89. -- try Register a crafting recipe if it was set
  90. vm_lighting_wand.add_crafting_recipe()
  91. -- a smarter version of wand "freebie" settings
  92. MT.register_on_joinplayer(function(player)
  93. -- allow all users some free power regardless if they are new or not
  94. if not give_free_power and not give_free_wand then
  95. return
  96. end
  97. local meta = player:get_meta()
  98. -- give all users free power if not received
  99. if give_free_power > 0 then
  100. local had_power = meta:get_int(modname .. "_had_power") or 0
  101. if had_power < give_free_power then
  102. local diff = give_free_power - had_power
  103. vm_lighting_wand.add_power(player, diff)
  104. meta:set_int(vm_lighting_wand.modname .. "_had_power", give_free_power)
  105. end
  106. end
  107. -- if the free wand isnt enabled then just return
  108. if not give_free_wand then
  109. return
  110. end
  111. -- give all users a wand if enabled, if a "old" player joins and this is now enabled then give the player a wand also
  112. local had_wand = meta:get_int(modname .. "_had_wand") or 0
  113. -- well now you have one
  114. if had_wand < 1 then
  115. local item = vm_lighting_wand.tool
  116. local inv = player:get_inventory()
  117. if inv:room_for_item("main", item) then
  118. inv:add_item("main", item)
  119. else
  120. MT.add_item(player:get_pos(), item)
  121. end
  122. -- mark it as given
  123. meta:set_int(vm_lighting_wand.modname .. "_had_wand", 1)
  124. end
  125. end)
  126. -- dont allow water to replace this nodes, this is only a test for so doesnt matter if the mods are installed or not
  127. vm_lighting_wand.add_ignored_onlighting("default:lava_source")
  128. vm_lighting_wand.add_ignored_onlighting("mcl_core:lava_source")
  129. vm_lighting_wand.add_ignored_onlighting("hades_core:lava_source")
  130. -- escape the loop
  131. vm_lighting_wand.loaded = true
  132. end