1 minetest.register_chatcommand("spawn", {
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)
10 --checks if a player put a number of mobs
11 local number_of_mobs = string.find(str, "%s%d+")
14 --remove spaces from the string
15 if number_of_mobs == nil then
20 amount = tonumber(str:match("^.-%s(.*)"))
21 str = "mob:"..str:match("(.*)%s")
24 if amount == nil or str == nil then
25 minetest.chat_send_player(name, "Format as /spawn 'mob' 20 ... or /spawn 'mob'")
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()
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)
37 local intersection = ray:next()
38 if intersection and intersection.above then
39 casted_pos = intersection.above
44 --add in amount through loop
47 minetest.add_entity(casted_pos,str)
50 minetest.add_entity(casted_pos,str)
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.")