Restoring Blocks

Discussion in 'Plugin Development' started by danthonywalker, May 21, 2013.

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

    danthonywalker

    I'm trying to restore blocks after a certain amount of time. Basically a terrain that will regenerate itself over time. However, my code doesn't work.

    Code:java
    1. @EventHandler
    2. public void blockRestore(EntityExplodeEvent event)
    3. {
    4. short loopDelayCount = 0;
    5.  
    6. for(final Block block : event.blockList())
    7. {
    8.  
    9. plugin.getServer().getScheduler().scheduleSyncDelayedTask(plugin, new Runnable()
    10. {
    11.  
    12. public void run()
    13. {
    14. if(block.getTypeId() == 2 || block.getTypeId() == 18) block.getWorld().playSound(block.getLocation(), Sound.DIG_GRASS, 1, 1);
    15. else if(block.getTypeId() == 3 || block.getTypeId() == 13) block.getWorld().playSound(block.getLocation(), Sound.DIG_GRAVEL, 1, 1);
    16. else if(block.getTypeId() == 12 || block.getTypeId() == 88) block.getWorld().playSound(block.getLocation(), Sound.DIG_SAND, 1, 1);
    17. else if(block.getTypeId() == 78 || block.getTypeId() == 80) block.getWorld().playSound(block.getLocation(), Sound.DIG_SNOW, 1, 1);
    18. else if(block.getTypeId() == 5 || block.getTypeId() == 17) block.getWorld().playSound(block.getLocation(), Sound.DIG_WOOD, 1, 1);
    19. else if(block.getTypeId() == 35 || block.getTypeId() == 92) block.getWorld().playSound(block.getLocation(), Sound.DIG_WOOL, 1, 1);
    20. else block.getWorld().playSound(block.getLocation(), Sound.DIG_STONE, 1, 1);
    21. block.getLocation().getBlock().setTypeId(block.getTypeId());
    22. }
    23.  
    24. }, loopDelayCount);
    25.  
    26. loopDelayCount += 10;
    27. }
    28.  
    29. }


    It is making a scheduled task, as it does play sounds, however, it does not restore the block, nor does it always confirm it as the right block, because always it's playing a stone sound and never any other sounds when it should. Can someone tell me how to do this?
     
  2. Bocks do not save the data from the time you access it. You need to use the BlockState instead. If you are done checking it's type. you can just use the blockState.update(true); method to ensure that the block gets restored.
     
  3. Offline

    danthonywalker

    Never used BlockState, mind showing an example of how I would implement that into my code?
     
Thread Status:
Not open for further replies.

Share This Page