Solved How do I make sure my mobs don't suffocate when spawned?

Discussion in 'Plugin Development' started by WhosDaMan, Jan 8, 2014.

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

    WhosDaMan

    Using this code, I can spawn a few wolves around a target player

    Code:java
    1. if (Integer.valueOf(args[1]) <= configValInt("max-wolves")) {
    2. Player target = Bukkit.getServer().getPlayer(args[0]);
    3. Location targetLoc = target.getLocation();
    4.  
    5. Random r = new Random();
    6.  
    7. for (int i = 0; i < configValInt("max-wolves"); i++) {
    8. // Random x and z value within a 10x10 square
    9. int xr = r.nextInt(10) + 1;
    10. int zr = r.nextInt(10) + 1;
    11. int direction = r.nextInt(2) + 1;
    12.  
    13. if (direction == 1) {
    14. target.getWorld().spawnEntity(targetLoc.subtract(xr, 0, zr), EntityType.WOLF);
    15. } else if (direction == 2) {
    16. target.getWorld().spawnEntity(targetLoc.add(xr, 0, zr), EntityType.WOLF);
    17. }
    18. }
    19. }


    The only problem is that in hilly/mountainous environments and tight spaces the wolves, more often-than-not, spawn inside the terrain... and suffocate. How can I figure out a safe place for my wolves to spawn?

    Also, how can I change the state of the spawned wolves from passive to angry?
     
  2. Offline

    Jake6177

    Use
    Code:
    getHighestBlockYAt(XCoord, ZCoord);
    to get the highest block so that they can't spawn into terrain. They might spawn in a cave still though, but they won't suffocate.
     
  3. Offline

    WhosDaMan

    I did find that function last night, but when I implemented it
    Code:java
    1. int safeY = target.getWorld().getHighestBlockYAt((int) (targetLoc.getX()), (int) (targetLoc.getZ()));
    2.  
    3. if (direction == 1) {
    4. target.getWorld().spawnEntity(targetLoc.subtract(xr, 0, zr).add(0, safeY, 0), EntityType.WOLF);
    5. } else if (direction == 2) {
    6. target.getWorld().spawnEntity(targetLoc.add(xr, safeY, zr), EntityType.WOLF);
    7. }

    my wolves fell from the sky and died. Also only one spawned instead of the amount (1-5) specified in the command.
     
  4. Offline

    Jake6177

    WhosDaMan Odd, I don't have Java on my PC atm, so I can't test anything, might want to wait a bit for someone else. Sorry!
     
  5. Offline

    _Filip

    WhosDaMan
    I posted the solution in another thread.
    Start diggin'.
     
Thread Status:
Not open for further replies.

Share This Page