2
0

init.lua 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. core.register_chatcommand("hotbar", {
  2. params = "<size>",
  3. description = "Set hotbar size",
  4. func = function(name, param)
  5. local player = core.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. core.register_chatcommand("hp", {
  22. params = "<hp>",
  23. description = "Set your health",
  24. func = function(name, param)
  25. local player = core.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 or core.is_nan(hp) or hp < 0 or hp > 65535 then
  31. return false, "Missing or incorrect hp parameter!"
  32. end
  33. player:set_hp(hp)
  34. return true
  35. end,
  36. })
  37. local s_infplace = core.settings:get("devtest_infplace")
  38. if s_infplace == "true" then
  39. infplace = true
  40. elseif s_infplace == "false" then
  41. infplace = false
  42. else
  43. infplace = core.is_creative_enabled("")
  44. end
  45. core.register_chatcommand("infplace", {
  46. params = "",
  47. description = "Toggle infinite node placement",
  48. func = function(name, param)
  49. infplace = not infplace
  50. if infplace then
  51. core.chat_send_all("Infinite node placement enabled!")
  52. core.log("action", "Infinite node placement enabled")
  53. else
  54. core.chat_send_all("Infinite node placement disabled!")
  55. core.log("action", "Infinite node placement disabled")
  56. end
  57. return true
  58. end,
  59. })
  60. core.register_chatcommand("detach", {
  61. params = "[<radius>]",
  62. description = "Detach all objects nearby",
  63. func = function(name, param)
  64. local radius = tonumber(param)
  65. if type(radius) ~= "number" then
  66. radius = 8
  67. end
  68. if radius < 1 then
  69. radius = 1
  70. end
  71. local player = core.get_player_by_name(name)
  72. if not player then
  73. return false, "No player."
  74. end
  75. local objs = core.get_objects_inside_radius(player:get_pos(), radius)
  76. local num = 0
  77. for o=1, #objs do
  78. if objs[o]:get_attach() then
  79. objs[o]:set_detach()
  80. num = num + 1
  81. end
  82. end
  83. return true, string.format("%d object(s) detached.", num)
  84. end,
  85. })
  86. core.register_chatcommand("use_tool", {
  87. params = "(dig <group> <leveldiff>) | (hit <damage_group> <time_from_last_punch>) [<uses>]",
  88. description = "Apply tool wear a number of times, as if it were used for digging",
  89. func = function(name, param)
  90. local player = core.get_player_by_name(name)
  91. if not player then
  92. return false, "No player."
  93. end
  94. local mode, group, level, uses = string.match(param, "([a-z]+) ([a-z0-9]+) (-?%d+) (%d+)")
  95. if not mode then
  96. mode, group, level = string.match(param, "([a-z]+) ([a-z0-9]+) (-?%d+)")
  97. uses = 1
  98. end
  99. if not mode or not group or not level then
  100. return false
  101. end
  102. if mode ~= "dig" and mode ~= "hit" then
  103. return false
  104. end
  105. local tool = player:get_wielded_item()
  106. local caps = tool:get_tool_capabilities()
  107. if not caps or tool:get_count() == 0 then
  108. return false, "No tool in hand."
  109. end
  110. local actual_uses = 0
  111. for u=1, uses do
  112. local wear = tool:get_wear()
  113. local dp
  114. if mode == "dig" then
  115. dp = core.get_dig_params({[group]=3, level=level}, caps, wear)
  116. else
  117. dp = core.get_hit_params({[group]=100}, caps, level, wear)
  118. end
  119. tool:add_wear(dp.wear)
  120. actual_uses = actual_uses + 1
  121. if tool:get_count() == 0 then
  122. break
  123. end
  124. end
  125. player:set_wielded_item(tool)
  126. if tool:get_count() == 0 then
  127. return true, string.format("Tool used %d time(s). "..
  128. "The tool broke after %d use(s).", uses, actual_uses)
  129. else
  130. local wear = tool:get_wear()
  131. return true, string.format("Tool used %d time(s). "..
  132. "Final wear=%d", uses, wear)
  133. end
  134. end,
  135. })
  136. -- Unlimited node placement
  137. core.register_on_placenode(function(pos, newnode, placer, oldnode, itemstack)
  138. if placer and placer:is_player() then
  139. return infplace
  140. end
  141. end)
  142. -- Don't pick up if the item is already in the inventory
  143. local old_handle_node_drops = core.handle_node_drops
  144. function core.handle_node_drops(pos, drops, digger)
  145. if not digger or not digger:is_player() or not infplace then
  146. return old_handle_node_drops(pos, drops, digger)
  147. end
  148. local inv = digger:get_inventory()
  149. if inv then
  150. for _, item in ipairs(drops) do
  151. if not inv:contains_item("main", item, true) then
  152. inv:add_item("main", item)
  153. end
  154. end
  155. end
  156. end
  157. core.register_chatcommand("set_displayed_itemcount", {
  158. params = "(-s \"<string>\" [-c <color>]) | -a <alignment_num>",
  159. description = "Set the displayed itemcount of the wielded item",
  160. func = function(name, param)
  161. local player = core.get_player_by_name(name)
  162. local item = player:get_wielded_item()
  163. local meta = item:get_meta()
  164. local flag1 = param:sub(1, 2)
  165. if flag1 == "-s" then
  166. if param:sub(3, 4) ~= " \"" then
  167. return false, "Error: Space and string with \"s expected after -s."
  168. end
  169. local se = param:find("\"", 5, true)
  170. if not se then
  171. return false, "Error: String with two \"s expected after -s."
  172. end
  173. local s = param:sub(5, se - 1)
  174. if param:sub(se + 1, se + 4) == " -c " then
  175. s = core.colorize(param:sub(se + 5), s)
  176. end
  177. meta:set_string("count_meta", s)
  178. elseif flag1 == "-a" then
  179. local num = tonumber(param:sub(4))
  180. if not num then
  181. return false, "Error: Invalid number: "..param:sub(4)
  182. end
  183. meta:set_int("count_alignment", num)
  184. else
  185. return false
  186. end
  187. player:set_wielded_item(item)
  188. return true, "Displayed itemcount set."
  189. end,
  190. })
  191. core.register_chatcommand("dump_item", {
  192. params = "",
  193. description = "Prints a dump of the wielded item in table form",
  194. func = function(name, param)
  195. local player = core.get_player_by_name(name)
  196. local item = player:get_wielded_item()
  197. local str = dump(item:to_table())
  198. print(str)
  199. return true, str
  200. end,
  201. })
  202. core.register_chatcommand("dump_itemdef", {
  203. params = "",
  204. description = "Prints a dump of the wielded item's definition in table form",
  205. func = function(name, param)
  206. local player = core.get_player_by_name(name)
  207. local str = dump(player:get_wielded_item():get_definition())
  208. print(str)
  209. return true, str
  210. end,
  211. })
  212. core.register_chatcommand("dump_wear_bar", {
  213. params = "",
  214. description = "Prints a dump of the wielded item's wear bar parameters in table form",
  215. func = function(name, param)
  216. local player = core.get_player_by_name(name)
  217. local item = player:get_wielded_item()
  218. local str = dump(item:get_wear_bar_params())
  219. print(str)
  220. return true, str
  221. end,
  222. })
  223. core.register_chatcommand("set_saturation", {
  224. params = "<saturation>",
  225. description = "Set the saturation for current player.",
  226. func = function(player_name, param)
  227. local saturation = tonumber(param)
  228. core.get_player_by_name(player_name):set_lighting({saturation = saturation })
  229. end
  230. })