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