init.lua 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  1. minetest.register_chatcommand("hotbar", {
  2. params = "<size>",
  3. description = "Set hotbar size",
  4. func = function(name, param)
  5. local player = minetest.get_player_by_name(name)
  6. if not player then
  7. return false, "No player."
  8. end
  9. local size = tonumber(param)
  10. if not size then
  11. return false, "Missing or incorrect size parameter!"
  12. end
  13. local ok = player:hud_set_hotbar_itemcount(size)
  14. if ok then
  15. return true
  16. else
  17. return false, "Invalid item count!"
  18. end
  19. end,
  20. })
  21. minetest.register_chatcommand("hp", {
  22. params = "<hp>",
  23. description = "Set your health",
  24. func = function(name, param)
  25. local player = minetest.get_player_by_name(name)
  26. if not player then
  27. return false, "No player."
  28. end
  29. local hp = tonumber(param)
  30. if not hp then
  31. return false, "Missing or incorrect hp parameter!"
  32. end
  33. player:set_hp(hp)
  34. return true
  35. end,
  36. })
  37. minetest.register_on_joinplayer(function(player)
  38. player:set_properties({zoom_fov = 15})
  39. end)
  40. minetest.register_chatcommand("zoomfov", {
  41. params = "[<FOV>]",
  42. description = "Set or display your zoom_fov",
  43. func = function(name, param)
  44. local player = minetest.get_player_by_name(name)
  45. if not player then
  46. return false, "No player."
  47. end
  48. if param == "" then
  49. local fov = player:get_properties().zoom_fov
  50. return true, "zoom_fov = "..tostring(fov)
  51. end
  52. local fov = tonumber(param)
  53. if not fov then
  54. return false, "Missing or incorrect zoom_fov parameter!"
  55. end
  56. player:set_properties({zoom_fov = fov})
  57. fov = player:get_properties().zoom_fov
  58. return true, "zoom_fov = "..tostring(fov)
  59. end,
  60. })
  61. local s_infplace = minetest.settings:get("devtest_infplace")
  62. if s_infplace == "true" then
  63. infplace = true
  64. elseif s_infplace == "false" then
  65. infplace = false
  66. else
  67. infplace = minetest.is_creative_enabled("")
  68. end
  69. minetest.register_chatcommand("infplace", {
  70. params = "",
  71. description = "Toggle infinite node placement",
  72. func = function(name, param)
  73. infplace = not infplace
  74. if infplace then
  75. minetest.chat_send_all("Infinite node placement enabled!")
  76. minetest.log("action", "Infinite node placement enabled")
  77. else
  78. minetest.chat_send_all("Infinite node placement disabled!")
  79. minetest.log("action", "Infinite node placement disabled")
  80. end
  81. return true
  82. end,
  83. })
  84. minetest.register_chatcommand("detach", {
  85. params = "[<radius>]",
  86. description = "Detach all objects nearby",
  87. func = function(name, param)
  88. local radius = tonumber(param)
  89. if type(radius) ~= "number" then
  90. radius = 8
  91. end
  92. if radius < 1 then
  93. radius = 1
  94. end
  95. local player = minetest.get_player_by_name(name)
  96. if not player then
  97. return false, "No player."
  98. end
  99. local objs = minetest.get_objects_inside_radius(player:get_pos(), radius)
  100. local num = 0
  101. for o=1, #objs do
  102. if objs[o]:get_attach() then
  103. objs[o]:set_detach()
  104. num = num + 1
  105. end
  106. end
  107. return true, string.format("%d object(s) detached.", num)
  108. end,
  109. })
  110. minetest.register_chatcommand("use_tool", {
  111. params = "(dig <group> <leveldiff>) | (hit <damage_group> <time_from_last_punch>) [<uses>]",
  112. description = "Apply tool wear a number of times, as if it were used for digging",
  113. func = function(name, param)
  114. local player = minetest.get_player_by_name(name)
  115. if not player then
  116. return false, "No player."
  117. end
  118. local mode, group, level, uses = string.match(param, "([a-z]+) ([a-z0-9]+) (-?%d+) (%d+)")
  119. if not mode then
  120. mode, group, level = string.match(param, "([a-z]+) ([a-z0-9]+) (-?%d+)")
  121. uses = 1
  122. end
  123. if not mode or not group or not level then
  124. return false
  125. end
  126. if mode ~= "dig" and mode ~= "hit" then
  127. return false
  128. end
  129. local tool = player:get_wielded_item()
  130. local caps = tool:get_tool_capabilities()
  131. if not caps or tool:get_count() == 0 then
  132. return false, "No tool in hand."
  133. end
  134. local actual_uses = 0
  135. for u=1, uses do
  136. local wear = tool:get_wear()
  137. local dp
  138. if mode == "dig" then
  139. dp = minetest.get_dig_params({[group]=3, level=level}, caps, wear)
  140. else
  141. dp = minetest.get_hit_params({[group]=100}, caps, level, wear)
  142. end
  143. tool:add_wear(dp.wear)
  144. actual_uses = actual_uses + 1
  145. if tool:get_count() == 0 then
  146. break
  147. end
  148. end
  149. player:set_wielded_item(tool)
  150. if tool:get_count() == 0 then
  151. return true, string.format("Tool used %d time(s). "..
  152. "The tool broke after %d use(s).", uses, actual_uses)
  153. else
  154. local wear = tool:get_wear()
  155. return true, string.format("Tool used %d time(s). "..
  156. "Final wear=%d", uses, wear)
  157. end
  158. end,
  159. })
  160. -- Use this to test waypoint capabilities
  161. minetest.register_chatcommand("test_waypoints", {
  162. params = "[change_immediate]",
  163. description = "tests waypoint capabilities",
  164. func = function(name, params)
  165. local player = minetest.get_player_by_name(name)
  166. local regular = player:hud_add {
  167. hud_elem_type = "waypoint",
  168. name = "regular waypoint",
  169. text = "m",
  170. number = 0xFF0000,
  171. world_pos = vector.add(player:get_pos(), {x = 0, y = 1.5, z = 0})
  172. }
  173. local reduced_precision = player:hud_add {
  174. hud_elem_type = "waypoint",
  175. name = "better waypoint",
  176. text = "m (0.5 steps, precision = 2)",
  177. precision = 10,
  178. number = 0xFFFF00,
  179. world_pos = vector.add(player:get_pos(), {x = 0, y = 1, z = 0})
  180. }
  181. local function change()
  182. if regular then
  183. player:hud_change(regular, "world_pos", vector.add(player:get_pos(), {x = 0, y = 3, z = 0}))
  184. end
  185. if reduced_precision then
  186. player:hud_change(reduced_precision, "precision", 2)
  187. end
  188. end
  189. if params ~= "" then
  190. -- change immediate
  191. change()
  192. else
  193. minetest.after(0.5, change)
  194. end
  195. regular = regular or "error"
  196. reduced_precision = reduced_precision or "error"
  197. local hidden_distance = player:hud_add {
  198. hud_elem_type = "waypoint",
  199. name = "waypoint with hidden distance",
  200. text = "this text is hidden as well (precision = 0)",
  201. precision = 0,
  202. number = 0x0000FF,
  203. world_pos = vector.add(player:get_pos(), {x = 0, y = 0.5, z = 0})
  204. } or "error"
  205. local image_waypoint = player:hud_add {
  206. hud_elem_type = "image_waypoint",
  207. text = "wieldhand.png",
  208. world_pos = player:get_pos(),
  209. scale = {x = 10, y = 10},
  210. offset = {x = 0, y = -32}
  211. } or "error"
  212. minetest.chat_send_player(name, "Waypoint IDs: regular: " .. regular .. ", reduced precision: " .. reduced_precision ..
  213. ", hidden distance: " .. hidden_distance .. ", image waypoint: " .. image_waypoint)
  214. end
  215. })
  216. -- Unlimited node placement
  217. minetest.register_on_placenode(function(pos, newnode, placer, oldnode, itemstack)
  218. if placer and placer:is_player() then
  219. return infplace
  220. end
  221. end)
  222. -- Don't pick up if the item is already in the inventory
  223. local old_handle_node_drops = minetest.handle_node_drops
  224. function minetest.handle_node_drops(pos, drops, digger)
  225. if not digger or not digger:is_player() or not infplace then
  226. return old_handle_node_drops(pos, drops, digger)
  227. end
  228. local inv = digger:get_inventory()
  229. if inv then
  230. for _, item in ipairs(drops) do
  231. if not inv:contains_item("main", item, true) then
  232. inv:add_item("main", item)
  233. end
  234. end
  235. end
  236. end
  237. minetest.register_chatcommand("set_displayed_itemcount", {
  238. params = "(-s \"<string>\" [-c <color>]) | -a <alignment_num>",
  239. description = "Set the displayed itemcount of the wielded item",
  240. func = function(name, param)
  241. local player = minetest.get_player_by_name(name)
  242. local item = player:get_wielded_item()
  243. local meta = item:get_meta()
  244. local flag1 = param:sub(1, 2)
  245. if flag1 == "-s" then
  246. if param:sub(3, 4) ~= " \"" then
  247. return false, "Error: Space and string with \"s expected after -s."
  248. end
  249. local se = param:find("\"", 5, true)
  250. if not se then
  251. return false, "Error: String with two \"s expected after -s."
  252. end
  253. local s = param:sub(5, se - 1)
  254. if param:sub(se + 1, se + 4) == " -c " then
  255. s = minetest.colorize(param:sub(se + 5), s)
  256. end
  257. meta:set_string("count_meta", s)
  258. elseif flag1 == "-a" then
  259. local num = tonumber(param:sub(4))
  260. if not num then
  261. return false, "Error: Invalid number: "..param:sub(4)
  262. end
  263. meta:set_int("count_alignment", num)
  264. else
  265. return false
  266. end
  267. player:set_wielded_item(item)
  268. return true, "Displayed itemcount set."
  269. end,
  270. })
  271. minetest.register_chatcommand("dump_item", {
  272. params = "",
  273. description = "Prints a dump of the wielded item in table form",
  274. func = function(name, param)
  275. local player = minetest.get_player_by_name(name)
  276. local item = player:get_wielded_item()
  277. local str = dump(item:to_table())
  278. print(str)
  279. return true, str
  280. end,
  281. })
  282. -- shadow control
  283. minetest.register_on_joinplayer(function (player)
  284. player:set_lighting({shadows={intensity = 0.33}})
  285. end)
  286. core.register_chatcommand("set_shadow", {
  287. params = "<shadow_intensity>",
  288. description = "Set shadow parameters of current player.",
  289. func = function(player_name, param)
  290. local shadow_intensity = tonumber(param)
  291. minetest.get_player_by_name(player_name):set_lighting({shadows = { intensity = shadow_intensity} })
  292. end
  293. })