[NOT SOLVED]Setting cube of blocks

Discussion in 'Plugin Development' started by Tomasko, May 28, 2014.

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

    Tomasko

    Hello,
    I tried to clean area of blocks, but unfortunately it will lag server and server will run out of memory.
    Class that clean blocks
    Code:java
    1. public class Map {
    2. int x = 0;
    3. int y = 0;
    4. int z = 0;
    5. int tx = 0;
    6. int ty = 0;
    7. int tz = 0;
    8.  
    9. public void clearMapArea(Player p, int size, int x, int y, int z) {
    10. this.x = x;
    11. this.y = y;
    12. this.z = z;
    13. int startx = x;
    14. int starty = y;
    15. int startz = z;
    16. int endx = x+size;
    17. int endy = y+size;
    18. int endz = z+size;
    19.  
    20. for(int ix = startx; ix < endx; ix++) {
    21. for(int iy = starty; ix < endy; iy++) {
    22. for(int iz = startz; ix < endz; iz++) {
    23.  
    24. Bukkit.getWorld(p.getWorld().getName()).getBlockAt(ix, iy, iz).setType(Material.AIR);
    25.  
    26.  
    27.  
    28. }
    29. }
    30. }
    31.  
    32. }
    33. }

    And event that start "cleaning"
    Code:java
    1. @EventHandler
    2. public void playerClick(PlayerInteractEvent e) {
    3.  
    4. if(e.getPlayer() != null && e.getPlayer().getInventory() != null && e.getPlayer().getItemInHand() != null) {
    5.  
    6. if(e.getPlayer().getItemInHand().getType() == Material.STICK && e.getPlayer().hasPermission("esp.generate")) {
    7.  
    8. if(e.getClickedBlock() != null) {
    9. if(e.getClickedBlock().getType() == Material.GOLD_BLOCK) {
    10. e.getPlayer().sendMessage(ChatColor.GREEN + ""+ChatColor.BOLD +"[ESPACE]" + ChatColor.RESET+ ""+ChatColor.GOLD +"Creating gold temple");
    11. Map m = new Map();
    12. m.clearMapArea(e.getPlayer(), 22,e.getClickedBlock().getLocation().getBlockX(),
    13. e.getClickedBlock().getLocation().getBlockY(),
    14. e.getClickedBlock().getLocation().getBlockZ());
    15. }
    16.  
    17. }
    18. }
    19. }
    20. }

    Event is trowed good and all data sended to Map() are good too. If someone know, how to fix it, please write it here :)
    Thanks for your time :)

    //EDIT: worldedit will set 1000000 block with small lag, but without running server out of memory
     
  2. Offline

    Tomasko

    bump...
     
  3. Offline

    renaud444

    I've done this before, using Math.min and such. Use two Locations, get the BlockX, BlockY, and BlockZ of each, then figure out for each the x, y, and z, which of the two is smaller, then use that as the starting value in the for loop.

    This is off the top of my head :p
    Code:java
    1. public void setCuboidToMaterial(Location l1, Location l2, Material material){
    2. int x1 = l1.getBlockX();
    3. int x2 = l2.getBlockX();
    4.  
    5. int y1 = l1.getBlockY();
    6. int y2 = l2.getBlockY();
    7.  
    8. int z1 = l1.getBlockZ();
    9. int z2 = l1.getBlockZ();
    10.  
    11. minX = Math.min(x1, x2);
    12. maxX = Math.max(x1, x2);
    13.  
    14. minY = Math.min(y1, y2);
    15. maxY = Math.max(y1, y2);
    16.  
    17. minZ = Math.min(z1, z2);
    18. maxZ = Math.min(z1, z2);
    19.  
    20. for(int x = minX; minX <= x && x <= maxX; x++){
    21. for(int y = minY; minY <= y && y <= maxY; y++){
    22. for(int z = minZ; minZ <= z && x <= maxZ; z++){
    23. Location loc = new Location(l1.getWorld(), x, y, z);
    24. loc.getBlock().setType(material);
    25. }
    26. }
    27. }
    28.  
    29. }


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

Share This Page