Version 1.4
[minetest_playertools.git] / init.lua
blob88ac2ea7de3f84efe1201c466971a8385e939073
1 -- Boilerplate to support localized strings if intllib mod is installed.
2 local S
3 if minetest.get_modpath("intllib") then
4 S = intllib.Getter()
5 else
6 S = function(s,a,...)a={a,...}return s:gsub("@(%d+)",function(n)return a[tonumber(n)]end)end
7 end
9 --[[ privileges ]]
10 if(minetest.setting_getbool("enable_damage") == true) then
11 minetest.register_privilege("heal", {
12 description = S("Allows player to set own health and breath with /sethp, /sethealth and /setbreath"),
13 give_to_singleplayer = false
15 end
16 minetest.register_privilege("physics", {
17 description = S("Allows player to set own gravity, jump height and movement speed with /setgravity, /setjump and /setspeed, respectively"),
18 give_to_singleplayer = false
20 minetest.register_privilege("hotbar", {
21 description = S("Allows player to set the number of slots of the hotbar with /sethotbarsize"),
22 give_to_singleplayer = false
25 --[[ informational commands ]]
26 minetest.register_chatcommand("whoami", {
27 params = "",
28 description = S("Shows your player name"),
29 privs = {},
30 func = function(name)
31 minetest.chat_send_player(name, S("Your player name is @1.", name))
32 end,
35 minetest.register_chatcommand("ip", {
36 params = "",
37 description = S("Shows your IP address"),
38 privs = {},
39 func = function(name)
40 minetest.chat_send_player(name, S("Your IP address is @1.", minetest.get_player_ip(name)))
41 end
44 --[[ HUD commands ]]
45 minetest.register_chatcommand("sethotbarsize", {
46 params = S("<1...23>"),
47 description = S("Sets the size of your hotbar to the provided number of slots; the number must be between 1 and 23"),
48 privs = {hotbar=true},
49 func = function(name, slots)
50 if slots == "" then
51 minetest.chat_send_player(name, S("You did not specify the parameter."))
52 return
53 end
54 if type(tonumber(slots)) ~= "number" then
55 minetest.chat_send_player(name, S("This is not a number."))
56 return
57 end
58 if tonumber(slots) < 1 or tonumber(slots) > 23 then
59 minetest.chat_send_player(name, S("Number of slots is out of bounds. The number of slots must be between 1 and 23."))
60 return
61 end
62 local player = minetest.get_player_by_name(name)
63 player:hud_set_hotbar_itemcount(tonumber(slots))
64 end,
67 --[[ health and breath commands ]]
69 if(minetest.setting_getbool("enable_damage") == true) then
70 minetest.register_chatcommand("sethp", {
71 params = S("<hp>"),
72 description = S("Sets your health to <hp> HP (=half the number of “hearts”)"),
73 privs = {heal=true},
74 func = function(name, hp)
75 local player = minetest.get_player_by_name(name)
76 if not player then
77 return
78 end
79 if hp == "" then
80 minetest.chat_send_player(name, S("You did not specify the parameter."))
81 return
82 end
83 if type(tonumber(hp)) ~= "number" then
84 minetest.chat_send_player(name, S("This is not a number."))
85 return
86 end
87 hp = math.max(0, math.floor(hp+0.5)) -- round the number, ensure minimum value of 0
88 player:set_hp(tonumber(hp))
89 end,
92 minetest.register_chatcommand("sethealth", {
93 params = S("<hearts>"),
94 description = S("Sets your health to <hearts> hearts"),
95 privs = {heal=true},
96 func = function(name, hearts)
97 local player = minetest.get_player_by_name(name)
98 if not player then
99 return
101 if hearts == "" then
102 minetest.chat_send_player(name, S("You did not specify the parameter."))
103 return
105 if type(tonumber(hearts)) ~= "number" then
106 minetest.chat_send_player(name, S("This is not a number."))
107 return
109 local hp = tonumber(hearts) * 2
110 hp = math.max(0, math.floor(hp+0.5)) -- round the number, ensure minimum value of 0
111 player:set_hp(hp)
112 end,
115 minetest.register_chatcommand("setbreath", {
116 params = S("<breath>"),
117 description = S("Sets your breath to <breath> breath points"),
118 privs = {heal=true},
119 func = function(name, breath)
120 local player = minetest.get_player_by_name(name)
121 if not player then
122 return
124 if breath == "" then
125 minetest.chat_send_player(name, S("You did not specify the parameter."))
126 return
128 if type(tonumber(breath)) ~= "number" then
129 minetest.chat_send_player(name, S("This is not a number."))
130 return
132 local bp = math.max(0, tonumber(breath)) -- ensure minimum value of 0
133 player:set_breath(bp)
134 end,
137 minetest.register_chatcommand("killme", {
138 params = "",
139 description = S("Kills yourself"),
140 func = function(name, param)
141 local player = minetest.get_player_by_name(name)
142 if not player then
143 return
145 player:set_hp(0)
146 end,
150 --[[ Player physics commands ]]
152 -- speed
153 minetest.register_chatcommand("setspeed", {
154 params = S("[<speed>]"),
155 description = S("Sets your movement speed to <speed> (default: 1)"),
156 privs={physics=true},
157 func = function(name, speed)
158 local player = minetest.get_player_by_name(name)
159 if not player then
160 return
162 if speed == "" then
163 speed=1
165 if type(tonumber(speed)) ~= "number" then
166 minetest.chat_send_player(name, S("This is not a number."))
167 return
170 player:set_physics_override(tonumber(speed), nil, nil)
171 end,
174 -- gravity
175 minetest.register_chatcommand("setgravity", {
176 params = S("[<gravity>]"),
177 description = S("Sets your gravity to <gravity> (default: 1)"),
178 privs={physics=true},
179 func = function(name, gravity)
180 local player = minetest.get_player_by_name(name)
181 if not player then
182 return
184 if gravity == "" then
185 gravity=1
187 if type(tonumber(gravity)) ~= "number" then
188 minetest.chat_send_player(name, S("This is not a number."))
189 return
191 player:set_physics_override(nil, tonumber(gravity), nil)
192 end,
195 -- jump height
196 minetest.register_chatcommand("setjump", {
197 params = S("[<jump height>]"),
198 description = S("Sets your jump height to <jump height> (default: 1)"),
199 privs = {physics=true},
200 func = function(name, jump_height)
201 local player = minetest.get_player_by_name(name)
202 if not player then
203 return
205 if jump_height == "" then
206 jump_height=1
208 if type(tonumber(jump_height)) ~= "number" then
209 minetest.chat_send_player(name, S("This is not a number."))
210 return
212 player:set_physics_override(nil, nil, jump_height)
213 end,
216 minetest.register_chatcommand("pulverizeall", {
217 params = "",
218 description = S("Destroys all items in your player inventory and crafting grid"),
219 func = function(name, param)
220 local player = minetest.get_player_by_name(name)
221 if not player then
222 return
224 local inv = player:get_inventory()
225 inv:set_list("main", {})
226 inv:set_list("craft", {})
227 end,