Scheduler don't replace blocks

Discussion in 'Plugin Development' started by Sahee, Aug 31, 2014.

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

    Sahee

    Hello, im trying to paste 2 milions blocks from array using scheduler. Im getting all alerts without any expections but this dont replace blocks. Here is my code:
    Code:java
    1. @Override
    2. public void run() {
    3. Task = Bukkit.getScheduler().scheduleSyncRepeatingTask(Main.plugin,
    4. new Runnable() {
    5. @Override
    6. public void run() {
    7.  
    8. if(blockList.size() == 1){
    9. Bukkit.broadcastMessage("Task done!");
    10. Bukkit.getScheduler().cancelTask(Task);
    11. }
    12. //Changer class contains x,y,z and material type props
    13. Changer g = blockList.get(0);
    14. Location loc = new Location(world, g.x, g.y, g.z);
    15.  
    16. if(!loc.getChunk().isLoaded())
    17. loc.getChunk().load();
    18. loc.getBlock().setType(g.block); // this don't seems to work
    19. blockList.remove(0);
    20. Bukkit.broadcastMessage(blockList.size()+" X:" + g.x + " Z:" + g.z+ " Y:" + g.y );
    21. //Main.plugin.getServer().getPlayer("Sahee").teleport(loc);
    22. }
    23. }, 0, 1L);
    24. }
     
  2. Offline

    Flamedek

    Sahee
    Try using BlockStates.
    Perhaps when you 'saved' the blocks you want to replace, you just saved the reference to that block. Meaning the Type you have saved changes with the current Type of the block = no change.
     
  3. Offline

    Sahee

    Flamedek
    Can you write that for me? I don't understand. so hard lol
    just tried like:
    still dont work
    Code:java
    1. world.getBlockAt(loc).setType(g.block);
    2. //or
    3. BlockState old = (BlockState)loc.getBlock().getState();
    4. old.setType(g.block);
     
  4. Offline

    fireblast709

    Sahee debug the type as well. See if the type is actually different from the real block.
     
  5. Offline

    Totom3

    Sahee Ok wow! Just saying, if the List really contains 2 MILLIONS objects, running this code will annihilate your processor. By removing the first entry, you are forcing Java to copy the entire array, just to shift all elements by 1. So we get O(n) for 1 entry (replace n by 2 millions). In total the CPU will have to do (n^2 + n) / 2 operations which results in this case to 2,000,001,000,000.

    Maybe you should consider fetching the list backwards, or using a Set instead. Actually hintss is right, Set's are bad for iterations, don't use them for that
     
  6. Offline

    hintss

    Sets (well, hashsets at least) have a much slower iteration speed, fetching the list backwards is probably a better idea
     
  7. Offline

    Totom3

    hintss Right, changed it.
     
  8. Offline

    Sahee

    fireblast709 all types are replaced from air (cus of flat map) to other materials like wood, stone etc...
    Totom3 hintss My i7 working very well at that calculations. I will replace this with backward deleting things :)

    But what about setType of block becouse this mainly don't work.

    What im doing? Flat map from image thats 2000x2000 excluding water at 1st layer (60%) and other 14 layers down same size
    Now its look like:
    Code:java
    1. @Override
    2. public void run() {
    3. Task = Bukkit.getScheduler().scheduleSyncRepeatingTask(Main.plugin,
    4. new Runnable() {
    5. @Override
    6. public void run() {
    7.  
    8. if (blockList.size() == 0) {
    9. Bukkit.broadcastMessage("Task done!");
    10. Bukkit.getScheduler().cancelTask(Task);
    11. }
    12.  
    13. int size = blockList.size() - 1;
    14. Changer g = blockList.get(size);
    15. Location loc = new Location(world, g.x, g.y, g.z);
    16.  
    17. if (!loc.getChunk().isLoaded())
    18. loc.getChunk().load();
    19.  
    20. loc.getBlock().setType(g.block);
    21.  
    22. blockList.remove(size);
    23. Bukkit.broadcastMessage(blockList.size() + " X:" + g.x
    24. + " Z:" + g.z + " Y:" + g.y);
    25. Main.plugin.getServer().getPlayer("Sahee")
    26. .teleport(loc);
    27. }
    28. }, 0, 1L);
    29. }


    Im solved this problem, just misspeled coordinates z instead of y
     
Thread Status:
Not open for further replies.

Share This Page