Making Mob Spawn Near You?

Discussion in 'Plugin Development' started by Wantsome909, Aug 23, 2013.

Thread Status:
Not open for further replies.
  1. Offline

    Wantsome909

    I was seeing if any one knew how to spawn zombie near a player I have code for spawning a zombie at a player potion,But u don't see how to make them spawn around you?

    Here code:
    Code:java
    1. this.getServer().getScheduler().scheduleSyncRepeatingTask(this, new Runnable() {
    2. public void run() {
    3. Player[] players = main.this.getServer().getOnlinePlayers();
    4. for (Player p : players) {
    5. Location loc = p.getLocation();
    6. Zombie zombie = (Zombie) p.getWorld().spawnEntity(loc, EntityType.ZOMBIE);
    7. zombie.setCustomName(ChatColor.RED + "Zombie");
    8. zombie.setCustomNameVisible(true);
    9. Bukkit.broadcastMessage("Zombie Spawned!");
    10. }
    11. }
    12. }, 0, 6000);


    all help is appreciated :)
     
  2. Offline

    Lactem

    Zombie zombie =(Zombie) p.getWorld().spawnEntity(loc.add(5, 0, 5, EntityType.ZOMBIE);
     
  3. Offline

    FireBreath14

    Try increasing the X and Z values of your location. That way the zombies will spawn to the left/right or the front/back of the player.

    Hope that helped ya a bit ;)
     
  4. Offline

    Wantsome909

    Lactem not like that. i was thinking of spawning a zombie around the player.

    FireBreath14 how would you increase the X and Z values

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 4, 2016
  5. Wantsome909
    Add values to the location.
    Code:
    Location loc = p.getLocation().add(x, y, z);
    If you increase the 'y' by two, then it's going to spawn a zombie above the player.

    If you want to spawn zombies in a circle shape around the player, use a loop.
     
  6. Offline

    FireBreath14

    so you would want to do this:
    Code:
    Location loc = p.getLocation().add(2, 0, 2);
    Location loc = p.getLocation().add(-2, 0, -2);
    Location loc = p.getLocation().add(-2, 0, 2);
    Location loc = p.getLocation().add(2, 0, -2);
    to make a nice circle

    Kinda... I don't think THAT code will work but you get the idea

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 4, 2016
  7. Offline

    Wantsome909

  8. FireBreath14
    Actually the last one overrides the upper ones. Either combine the locations, or once again, use a loop. Also that wouldn't be a circle.

    Wantsome909
    Use a random number generator.

    Sorry for double post, but here's a nice circle generator.
    Code:
    public static List < Location > circle(Location loc, Integer r, Integer h, Boolean hollow, Boolean sphere, int plus_y) {
        List < Location > circleblocks = new ArrayList < Location > ();
        int cx = loc.getBlockX();
        int cy = loc.getBlockY();
        int cz = loc.getBlockZ();
        for (int x = cx - r; x <= cx + r; x++)
            for (int z = cz - r; z <= cz + r; z++)
                for (int y = (sphere ? cy - r : cy); y < (sphere ? cy + r : cy + h); y++) {
                    double dist = (cx - x) * (cx - x) + (cz - z) * (cz - z) + (sphere ? (cy - y) * (cy - y) : 0);
                    if (dist < r * r && !(hollow && dist < (r - 1) * (r - 1))) {
                        Location l = new Location(loc.getWorld(), x, y + plus_y, z);
                        circleblocks.add(l);
                    }
                }
     
        return circleblocks;
    }
    Usage:
    Code:
    Zombie zombie = (Zombie) p.getWorld().spawnEntity(Arrays.asList(circle(loc, 4, 1, true, true, 1)), EntityType.ZOMBIE);
    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 4, 2016
  9. Offline

    Wantsome909

    Assist & FireBreath14 would this work?

    Random r = new Random();
    int[] values = new int[2];

    for(int i = 0; i < 3; i++) {
    values = r.nextInt(299) + 1;
    }

    Location loc = new Location(p.getWorld(), values[0], values[1], values[2]);
     
  10. That looks oddly familiar. No, I'll write you something in a bit.
     
  11. Offline

    Wantsome909

    Assist ok i remember i found it on a forum
     
  12. Wantsome909
    I'm not sure if this is going to work, wrote it on my phone.
    Code:java
    1. public static List <Location> getRandom(Location loc, int radius, int chance) {
    2. List <Location> range = new ArrayList < > ();
    3.  
    4. World w = loc.getWorld();
    5.  
    6. Random rand = new Random();
    7.  
    8. int cx = loc.getBlockX();
    9. int cz = loc.getBlockZ();
    10. int rs = radius * radius;
    11.  
    12. for (int x = cx - radius; x <= cx + radius; x++) {
    13. for (int y = 0; y < 3; y++) {
    14. for (int z = cz - radius; z <= cz + radius; z++) {
    15. if ((cx - x) * (cx - x) + (cz - z) * (cz - z) <= rs) {
    16. range.add(new Location(w, x + rand.nextInt(chance), y + rand.nextInt(chance), z + rand.nextInt(chance)));
    17. }
    18. }
    19. }
    20. }
    21.  
    22. return range;
    23. }
    Usage:
    Code:java
    1. for(Location r : getRandom(loc, 5, 5)) {
    2. Zombie zombie = (Zombie) p.getWorld().spawn(r, Zombie.class);
    3. }

    The first '5' is the radius, the second '5' is the random integer added to the circle shape location.
     
  13. Offline

    Wantsome909

    Assist ok i got it but how can i stop making them spawn in the walls and in the air?
     
  14. Wantsome909
    Try this
    Code:java
    1. public static List <Location> getRandom(Location loc, int radius, int chance) {
    2. List <Location> range = new ArrayList <>();
    3.  
    4. World w = loc.getWorld();
    5. Random rand = new Random();
    6.  
    7. int cx = loc.getBlockX();
    8. int cz = loc.getBlockZ();
    9. int rs = radius * radius;
    10.  
    11. for (int x = cx - radius; x <= cx + radius; x++) {
    12. for (int z = cz - radius; z <= cz + radius; z++) {
    13. if ((cx - x) * (cx - x) + (cz - z) * (cz - z) <= rs) {
    14. Location rloc = new Location(w, x + rand.nextInt(chance), 1, z + rand.nextInt(chance));
    15.  
    16. if (!w.getBlockAt(rloc).getType().isSolid()) {
    17. range.add(rloc);
    18. }
    19. }
    20. }
    21. }
    22.  
    23. return range;
    24. }
     
  15. Offline

    Wantsome909

    Assist Thanks Assist! :) this help alot. but they will spawn in side walls and in the air... how could i stop that?
     
  16. Wantsome909
    That code above should've fixed that. Did you try it?
     
  17. Offline

    Wantsome909

    @assit i did'nt test it. lol ill try...

    Assist there alot of error's. here code

    this is in my main class.

    Code:java
    1. this.getServer().getScheduler().scheduleSyncRepeatingTask(this, new Runnable() {
    2. public void run() {
    3. Player[] players = main.this.getServer().getOnlinePlayers();
    4. for (Player p : players) {
    5. public static List <Location> getRandom (Location loc, int radius, int chance) {
    6. List <Location> range = new ArrayList <>();
    7.  
    8. World w = loc.getWorld();
    9. Random rand = new Random();
    10.  
    11. int cx = loc.getBlockX();
    12. int cz = loc.getBlockZ();
    13. int rs = radius * radius;
    14.  
    15. for (int x = cx - radius; x <= cx + radius; x++) {
    16. for (int z = cz - radius; z <= cz + radius; z++) {
    17. if ((cx - x) * (cx - x) + (cz - z) * (cz - z) <= rs) {
    18. Location rloc = new Location(w, x + rand.nextInt(chance), 1, z + rand.nextInt(chance));
    19.  
    20. if (!w.getBlockAt(rloc).getType().isSolid()) {
    21. range.add(rloc);
    22. }
    23. }
    24. }
    25. }
    26.  
    27. return range;
    28. }
    29. for(Location r : getRandom(loc, 5, 5)) {
    30. Zombie zombie = (Zombie) p.getWorld().spawn(r, Zombie.class);
    31. }
    32. }
    33. }
    34. }, 0, 3600);


    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 4, 2016
  18. Wantsome909
    Put my code outside your method, then just use the for(Location r : etc).
     
  19. Offline

    Wantsome909

    Assist i see what you mean. but in the for loop there a error with loc?
     
  20. Offline

    Wantsome909

    Assist
    Code:java
    1. //Tip-Spawning
    2. this.getServer().getScheduler().scheduleSyncRepeatingTask(this, new Runnable() {
    3. public void run() {
    4. Player[] players = main.this.getServer().getOnlinePlayers();
    5. for (Player p : players) {
    6. for(Location r : getRandom(p.getLocation(), 5, 5)) {
    7. Zombie zombie = (Zombie) p.getWorld().spawn(r, Zombie.class);
    8. zombie.addPotionEffect(new PotionEffect(PotionEffectType.SPEED, 9999, 1, false));
    9. zombie.addPotionEffect(new PotionEffect(PotionEffectType.INCREASE_DAMAGE, 9999, 1, false));
    10. Bukkit.broadcastMessage("hey");
    11. }
    12. }
    13. }
    14. }, 20, 20);


    You Code:

    Code:java
    1. public static List <Location> getRandom(Location loc, int radius, int chance) {
    2. List <Location> range = new ArrayList <>();
    3.  
    4. World w = loc.getWorld();
    5. Random rand = new Random();
    6.  
    7. int cx = loc.getBlockX();
    8. int cz = loc.getBlockZ();
    9. int rs = radius * radius;
    10.  
    11. for (int x = cx - radius; x <= cx + radius; x++) {
    12. for (int z = cz - radius; z <= cz + radius; z++) {
    13. if ((cx - x) * (cx - x) + (cz - z) * (cz - z) <= rs) {
    14. Location rloc = new Location(w, x + rand.nextInt(chance), 1, z + rand.nextInt(chance));
    15.  
    16. if (!w.getBlockAt(rloc).getType().isSolid()) {
    17. range.add(rloc);
    18. }
    19. }
    20. }
    21. }
    22.  
    23. return range;
    24. }
     
  21. Wantsome909
    Ok, where is the error, and what does it say?
     
  22. Offline

    Wantsome909

    the error is in the for loop and it the loc. it say can resolved variable Assist
     
  23. Wantsome909
    There's 4 for loops in that code. I put my code in eclipse, and got no errors, so either you've got wrong imports, you don't have the getRandom method in your class, or you messed up the brackets.

    Copy and paste the exact line that has the error, and tell me what part of it is red lined.
     
  24. Offline

    Wantsome909

    Assist
    Code:java
    1. for(Location r : getRandom(loc, 5, 5)) {


    it is loc

    can i see your code in eclipse?
     
  25. Wantsome909
    I used 'loc' as an example. That's the location where you want the zombies to randomly spawn.

    You already fixed that by replacing it with p.getLocation().
     
  26. Offline

    Wantsome909

    Assist wow i dumb, lol but when i test it it still didnt work?
     
  27. Wantsome909
    Any errors in the server console? If not, debug your code, and see why it stops running.
     
  28. Offline

    Wantsome909

    Assist no errors? and how do you debug? and plus does it work for u?

    Assist this is what the debug says

    1 issuer names found, serial number 20
    Matches original client x509 cert
    1 entity names found
    Matches original ca x509 cert
    valid not before: Fri Aug 23 12:06:17 MDT 2013
    valid not before: Fri Aug 23 12:07:57 MDT 2013
    cert has 1 attributes:
    OID: 2.5.24.72
    rolesyntax read from cert!

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 4, 2016
Thread Status:
Not open for further replies.

Share This Page