]> git.lizzy.rs Git - Crafter.git/blob - mods/mob/chatcommands.lua
remove server debug
[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 player = minetest.get_player_by_name(name)
31                         local pos = player:get_pos()
32                         pos.y = pos.y + 1.625
33                         local pos2 = vector.add(pos,vector.multiply(player:get_look_dir(),80))
34                         local ray = minetest.raycast(pos,pos2,false,false)
35                         local casted_pos = table.copy(pos)
36                         if ray then
37                                 local intersection = ray:next()
38                                 if intersection and intersection.above then
39                                         casted_pos = intersection.above
40                                 end
41                         end
42
43
44                         --add in amount through loop
45                         if amount > 1 then
46                                 for i = 1,amount do 
47                                         minetest.add_entity(casted_pos,str)
48                                 end
49                         else --add single
50                                 minetest.add_entity(casted_pos,str)
51                         end
52                 else --tell player the mob doesn't exist if not a registered entity
53                         minetest.chat_send_player(name, str:match("^.-:(.*)"):gsub("^%l", string.upper).." is not a mob.")
54                 end
55                 
56         end,
57 })