Solved Performance/RAM cost while searching blocks

Discussion in 'Plugin Development' started by rfsantos1996, Mar 6, 2014.

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

    rfsantos1996

    Some of my plugin users are getting a lot of outOfMemoryMessages even with 3gb RAM being used (I think) @ 333x333 area.

    My block search code is:
    Code:java
    1. int blockxmin = Math.min(corner1.getBlockX(), corner2.getBlockX()),
    2. blockxmax = Math.max(corner1.getBlockX(), corner2.getBlockX()),
    3. blockymin = Math.min(corner1.getBlockY(), corner2.getBlockY()),
    4. blockymax = Math.max(corner1.getBlockY(), corner2.getBlockY()),
    5. blockzmin = Math.min(corner1.getBlockZ(), corner2.getBlockZ()),
    6. blockzmax = Math.max(corner1.getBlockZ(), corner2.getBlockZ());
    7. for (int x = blockxmin; x <= blockxmax; x++) {
    8. for (int y = blockymin; y <= blockymax; y++) {
    9. for (int z = blockzmin; z <= blockzmax; z++) {
    10. Block b = corner1.getWorld().getBlockAt(x, y, z);
    11. chunks.add(corner1.getWorld().getChunkAt(x, z)); // Yep, I noticed now that this is wrong
    12. if ((b.getState() instanceof Chest || b.getState() instanceof DoubleChest)) {
    13. if (!chests.containsKey(b.getLocation())) {
    14. chests.put(b.getLocation(), pl.defaultTier.getTier());
    15. }
    16. }
    17. }
    18. }
    19. }

    Do anyone know if theres a more effective way to do this?

    Also, can someone answer if doing this is more RAM efficient?
    Code:java
    1. int xmin = Math.min(corner1.getChunk().getX(), corner2.getChunk().getX()),
    2. xmax = Math.max(corner1.getChunk().getX(), corner2.getChunk().getX()),
    3. zmin = Math.min(corner1.getChunk().getZ(), corner2.getChunk().getZ()),
    4. zmax = Math.max(corner1.getChunk().getZ(), corner2.getChunk().getZ());
    5. for (int x = xmin; x <= xmax; x++) {
    6. for (int z = zmin; z <= zmax; z++) {
    7. chunks.add(corner1.getWorld().getChunkAt(x, z));
    8. // Search all chunk blocks here and check if block state is chest/doublechest
    9. }
    10. }
     
  2. rfsantos1996
    A chest is a tile entity. So instead of iterating over all blocks just iterate over all tile entities for the chunk.
     
    rfsantos1996 likes this.
  3. Offline

    rfsantos1996

    Wow, I didnt saw "getTileEntities" ;O

    Thank you so much!

    @EDIT: I used to use Regios from you, I remember fixing a bug that I needed to get fixed fast on GitHub but I never knew how to Pull request so I sent what you should add on your code to fix the issue xD One of my first Java work on Bukkit.
     
    Adamki11s likes this.
Thread Status:
Not open for further replies.

Share This Page