]> git.lizzy.rs Git - Crafter.git/blob - mods/mob/chatcommands.lua
Add in group_attack setting for mobs
[Crafter.git] / mods / mob / chatcommands.lua
1 minetest.register_chatcommand("spawn", {
2         params = "<mob>",
3         description = "Spawn x amount of a mob, used as /spawn 'mob' 10 or /spawn 'mob' for one",
4         privs = {server = true},
5         func = function( name, mob)
6                 --local vars
7                 local str = mob
8                 local amount = 1
9                 
10                 --checks if a player put a number of mobs
11                 local number_of_mobs = string.find(str, "%s%d+")
12                 
13                 
14                 --remove spaces from the string
15                 if number_of_mobs == nil then
16                         str:gsub("%s", "")
17                         str = "mob:"..mob
18                         --don't change amount
19                 else--or find values
20                         amount = tonumber(str:match("^.-%s(.*)"))
21                         str = "mob:"..str:match("(.*)%s")
22                 end
23                 --explain formatting
24                 if amount == nil or str == nil then
25                         minetest.chat_send_player(name, "Format as /spawn 'mob' 20  ...  or /spawn 'mob'")
26                 end
27                 
28                 --add amount of entities if registered
29                 if minetest.registered_entities[str] ~= nil then
30                         local pos = minetest.get_player_by_name(name):getpos()
31                         pos.y = pos.y + 1
32                         --add in amount through loop
33                         if amount > 1 then
34                                 for i = 1,amount do 
35                                         minetest.add_entity(pos,str)
36                                 end
37                         else --add single
38                                 minetest.add_entity(pos,str)
39                         end
40                 else --tell player the mob doesn't exist if not a registered entity
41                         minetest.chat_send_player(name, str:match("^.-:(.*)"):gsub("^%l", string.upper).." is not a mob.")
42                 end
43                 
44         end,
45 })