BlockIterator freeze

Discussion in 'Plugin Development' started by matejdro, Apr 13, 2012.

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

    matejdro

    This is supposed to damage all entities within 10 block range, closer entities are, higher is damage. But only thing it will do is freeze server as soon as fireball is about to hit land.

    Code:java
    1. @EventHandler()
    2. public void onEntityExplode(EntityExplodeEvent event) {
    3. if (event.getEntity() instanceof Fireball)
    4. {
    5. fireballExplode((Fireball) event.getEntity());
    6. event.setCancelled(true);
    7. return;
    8. }
    9.  
    10. }

    Code:java
    1. public void fireballExplode(Fireball ball)
    2. {
    3. List<LivingEntity> entities = new ArrayList<LivingEntity>();
    4. for (Entity e : ball.getNearbyEntities(10, 10, 10))
    5. if (e instanceof LivingEntity) entities.add((LivingEntity) e);
    6.  
    7. BlockIterator literator = new BlockIterator(ball.getLocation(), 10);
    8.  
    9. while (literator.hasNext())
    10. {
    11. Block b = literator.next();
    12. Location loc = b.getLocation();
    13. for (LivingEntity e : entities.toArray(new LivingEntity[0]))
    14. {
    15. Location l = e.getLocation();
    16. if (loc.getX() + 1.5 > l.getX() && loc.getX() - 0.5 < l.getX() &&
    17. loc.getY() + 2.5 > l.getY() && loc.getY() - 1.5 < l.getY() &&
    18. loc.getZ() + 1.5 > l.getZ() && loc.getZ() - 0.5 < l.getZ())
    19. {
    20. double range = ball.getLocation().distance(l);
    21. if (range > 10) continue;
    22.  
    23. e.damage((int) (10 - range), ball);
    24. entities.remove(e);
    25. }
    26. }
    27. }
    28.  
    29. }
    30.  
     
  2. Offline

    Sagacious_Zed Bukkit Docs

    You may be just trying to do too much work on your server all at once. Which may cause the server to freeze.
     
  3. Offline

    Father Of Time

    This is entirely too complicated for as simple of a task as you are trying to do:

    1) use the fireball entity to getnearbyentities( 20, 20, 20 ) // 10 block radius
    2) check the distance from the fireball entity to target
    3) Divide damage based on distance
    4) apply damage.

    This could be done in like 8 lines of code. Look at the GetNearbyEntities function, I think it will save you a lot of headaches.

    Good luck!
     
  4. Offline

    matejdro

    Um yeah, I see now how I completely failed on this concept (BlockIterator iterates only in one line). Why you did getNearbyEntities(20, 20, 20) if blockradius must be 10?
     
  5. Offline

    Father Of Time

    check the java docs, the input is:

    GetNearbyEntities( Diameter/2, Diameter/2, Diameter/2 )

    it's 20 Diameter, but 10 radius.

    XXXXXXXXXXOXXXXXXXXXX

    X = one block
    0 = Center
    it's 20 wide, but only 10 from the center.

    Hope this helps!
     
    matejdro likes this.
  6. Offline

    matejdro

    Oh, I see. Thanks.
     
    Father Of Time likes this.
  7. Offline

    Father Of Time

    My pleasure, I am happy to assist. :D
     
  8. Offline

    nisovin

    That's not how I interpret it. It says to pass 1/2 the size of the box, meaning you should pass the radius (10), not the diameter (20).
     
  9. Offline

    matejdro

    Yes, after reading javadocs I think nisovin is right.
     
  10. Offline

    Father Of Time

    Ahh, there you go; it had been awhile since I used it, I knew it wasn't simply the diameter. Apparently I had it backwards, so if you want it to have a diameter of 10 you must enter 5.

    Oh well, no one bats perfect... without steroids... :eek: *calls his doctor*
     
Thread Status:
Not open for further replies.

Share This Page