Help: Spawn mobs in air ONLY

Discussion in 'Plugin Development' started by spacerocket, Jan 23, 2014.

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

    spacerocket

    Hi,
    I'm currently developing a plugin for spawning mobs around the player (at a set radius). Everything works in the plugin, except when a player is too close to a wall or the terrain rises: some mobs will spawn inside that wall (thus suffocating). Here's some code that I tried, but ended up crashing my main server Thread with (chat threads were OK, but after a few minutes the server would crash with an IO exception or Read Timeout):

    Code:java
    1. Location spawnloc = new Location(player.getWorld(), spawnX, centerY, spawnZ);
    2.  
    3. while(!isAir(spawnloc))
    4. {
    5. spawnloc.setY(spawnloc.getY() + 1);
    6.  
    7. if(isAir(spawnloc))
    8. {
    9. break;
    10. }
    11. }
    12.  
    13. player.getWorld().spawnEntity(spawnloc, type);
    14.  
    15. }
    16.  
    17. private boolean isAir(Location loc)
    18. {
    19. Block block = loc.getBlock();
    20. if(block == Material.AIR)
    21. {
    22. return true;
    23. }
    24. return false;
    25. }


    Of course this isn't the whole class, because everything else works (the spawn location / spawning works without the above code added).

    I think that I'm getting an infinite loop, but I'm not sure (console does not log any exceptions, so this is why I think so).

    Thanks for any suggestions!
     
  2. Offline

    calebbfmv

    Iterate through all the blocks around the player, check if they are air, then do whatever
     
  3. Offline

    Carnes

    Because you're in a while loop it may never escape if those conditions aren't met (for whatever reason). Try adding some limits.
    Don't let it look above world.getMaxHeight()
    And maybe don't let it look below world.getSeaLevel()

    Even still you may need to go up one more block if they are spawning half-way in the ground.
     
  4. Offline

    spacerocket


    Okay, thanks for the suggestion; I'll try that.
     
  5. Offline

    spacerocket

    For future coders, I figured out something that works:
    Code:java
    1. while(!(spawnloc.getBlock().getType() == Material.AIR) && spawnloc.getY() <= (int)world.getMaxHeight()-5) //don't spawn mobs in air
    2. {
    3. spawnloc.setY(spawnloc.getY()+1);
    4. player.sendMessage("y set to: " + Double.toString(spawnloc.getY()));
    5. }
    6. player.getWorld().spawnEntity(spawnloc, type);
     
Thread Status:
Not open for further replies.

Share This Page