Mob spawn areas [SOLVED]

Discussion in 'Spigot Plugin Development' started by dark4rr0w, Jul 1, 2015.

  1. I want to make mob spawn areas, I know how to spawn them, but I have no idea how to contain them in cuboid or at least in specific distance so they could only stay there. Any ideas? I would want it least laggy as possible.
     
  2. Your best bet is getting two Locations and then iterating every coordinate location inside of there as a possible location to spawn them. You can also do other checks as well.

    Location selected, min, max;

    if (selected.getX() > min && selected.getX() < max) //Inside The X Cuboid
    if (selected.getZ() > min && selected.getZ() < max) //Inside The Z Cuboid
    if (selected.getY() > min && selected.getY() < max) //Inside the Y Cuboid

    If all of these are true, then you can do some more checks on the location you're trying to spawn them in.
     
  3. I know how to check if it's in cuboid, but there aren't any move events for all entities so where should I put this? Wouldn't puting it in timer be laggy?
     
  4. You can make a 2d area and on EntitySpawnEvent or something like that you can check if the mob location is in the 2d area, if not, despawn him.
     
  5. Thanks all, but I will probably just use timers instead of events. Also I wonder: would it delay server ticks when I loop trough cuboid without doing anything?
     
  6. What I would suggest is looping through the cuboid only once and saving all the locations in an ArrayList or whatever you enjoy, then in the spawn code pick a random location from that List. It'll be a lot more efficient than looping through the cuboid every single time.
     
  7. Thanks, but I did it like this:
    Code (Text):

    public void spawnMobs(){
            List<String> l=new ArrayList<>(mobs.getConfigurationSection("").getKeys(false));
            Location l1,l2;
            int radius,x,z,hp,dmg,lvl,lvlmin;
            for(String n:l){
                String[] loc=mobs.getString(n+".Location").split(",");
                String[] level=mobs.getString(n+".Level").split("-");
                radius=mobs.getInt(n+".Radius");
                x=r.nextInt(radius*2)-radius;
                z=r.nextInt(radius*2)-radius;
                lvl=Integer.parseInt(level[1]);
                lvlmin=Integer.parseInt(level[0]);
                hp=mobs.getInt(n+".HP");
                dmg=mobs.getInt(n+".Damage");
                l1=new Location(Bukkit.getWorld(loc[0]),Double.parseDouble(loc[1]),80,Double.parseDouble(loc[2]));
                l2=l1.getWorld().getHighestBlockAt(new Location(Bukkit.getWorld(loc[0]),Double.parseDouble(loc[1])+x,80,Double.parseDouble(loc[2])+z)).getLocation();
                LivingEntity le = (LivingEntity) l1.getWorld().spawnEntity(l1, EntityType.valueOf(mobs.getString(n+".Type").toUpperCase().replace(" ", "_")));
                le.setCustomName(n+ChatColor.GREEN+" Level "+r.nextInt(lvl-lvlmin)+lvlmin);
                le.setMaxHealth(hp);
                le.setHealth(hp);
                Mobs.put(le, dmg);
                Mobs1.put(le, l1);
                System.out.print(l1.getWorld().getHighestBlockAt(new Location(Bukkit.getWorld(loc[0]),Double.parseDouble(loc[1])+x,80,Double.parseDouble(loc[2])+z)));
            }
        }
     
    The mobs are stored in config for easy editing. And yes, I know that my code is hard to understand. I will have to work on that...
    This almost works. Though I am having this spammed my cmd:
    Code (Text):
    Filtered out large getEntities call 11,88 2,88
    anyone knows the fix? I only have my plugin and I checked all getNearbyEntities methods which were running, i deleted them and cmd was still being spammed.
    EDIT
    Fixed it. Though it's a bug, but there were actually too many mobs.
     
    #7 dark4rr0w, Jul 1, 2015
    Last edited: Jul 1, 2015