Regenerating Blocks

Discussion in 'Plugin Development' started by Kilovice, Nov 28, 2014.

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

    Kilovice

    I am currently attempting to regenerate blocks one-by-one one entity explode event. I am already aware that using:
    Code:java
    1. @EventHandler
    2. public void onExp(EntityExplodeEvent event){
    3. for(Block b : event.blockList()){
    4. final BlockState bs = b.getState();
    5. b.setType(Material.AIR);
    6.  
    7. int delay = 20;
    8.  
    9. if((b.getType() == Material.SAND) || (b.getType() == Material.GRAVEL) || (b.getType() == Material.ANVIL)){
    10. delay += 1;
    11. }
    12.  
    13. Bukkit.getServer().getScheduler().scheduleSyncDelayedTask(Main.getPlugin(), new Runnable(){
    14. public void run(){
    15. bs.update(true, false);
    16. }
    17. }, delay);
    18. }
    19. }

    That works, however it regenerates the blocks all at once, not one by one. So what I've thought is that using a repeating task, check if a list is empty then get a random block from that list, get the state of it, set it to air so there are no drops, then restore it and remove it from the list. (And check if the list is empty then cancel the task) To do this, I tried using:
    Code:java
    1. @EventHandler
    2. public void onExp(EntityExplodeEvent event){
    3. final List<Block> lb = event.blockList();
    4.  
    5. Random rnd = new Random();
    6. final int bid = rnd.nextInt(lb.size());
    7. final Block b = lb.get(bid);
    8. final BlockState bs = b.getState();
    9. b.setType(Material.AIR);
    10.  
    11. int delay = 20;
    12. if((b.getType() == Material.SAND) || (b.getType() == Material.GRAVEL) || (b.getType() == Material.ANVIL)){
    13. delay += 1;
    14. }
    15.  
    16. Bukkit.getServer().getScheduler().scheduleSyncRepeatingTask(Main.getPlugin(), new Runnable(){
    17. public void run(){
    18. bs.update(true, false);
    19. lb.remove(b);
    20. }
    21. }, delay, 20);
    22.  
    23. }

    (The check if it is empty was not added, any help on this?
    EDIT: I was also thinking it may be possible using a ListIterator but I'm not sure on that one..
     
  2. Here is what I did. I first added the exploded blocks to a list of BlockStates, like you did. Then I started a bukkit runnable with a delay to regen the blocks.

    Code:java
    1. @EventHandler
    2. public void explode(EntityExplodeEvent e) {
    3. if (!e.blockList().isEmpty() && (e.isCancelled() == false)) {
    4. final List<BlockState> blocks = new ArrayList<BlockState>();
    5. for (Block b : e.blockList()) {
    6. if (b.getType() != Material.AIR) {
    7. if (!blocks.contains(b.getState())) {
    8. blocks.add(b.getState());
    9. b.setType(Material.AIR);
    10. }
    11. }
    12. }
    13. new BukkitRunnable() {
    14. public void run() {
    15. regen(blocks, true, plugin.config.blockRegenSpeed);
    16. this.cancel();
    17. }
    18. }.runTaskTimer(plugin, plugin.config.blockRegenDelay * 20L, plugin.config.blockRegenDelay * 20L);
    19. }
    20. }


    Then I just simply created another bukkit runnable for the regen method.

    Code:java
    1. public void regen(final List<BlockState> blocks, final boolean effect,
    2. final int speed) {
    3.  
    4. new BukkitRunnable() {
    5. int i = -1;
    6.  
    7. @SuppressWarnings("deprecation")
    8. public void run() {
    9. if (i != blocks.size() - 1) {
    10. i++;
    11. BlockState bs = blocks.get(i);
    12. bs.getBlock().setType(bs.getType());
    13. bs.getBlock().setData(bs.getBlock().getData());
    14. if (effect) bs.getBlock().getWorld().playEffect(bs.getLocation(), Effect.STEP_SOUND, bs.getBlock().getType());
    15. } else {
    16. for (BlockState bs : blocks) {
    17. bs.getBlock().setType(bs.getType());
    18. bs.getBlock().setData(bs.getBlock().getData());
    19. }
    20. blocks.clear();
    21. this.cancel();
    22. }
    23. }
    24. }.runTaskTimer(plugin, speed, speed);
    25. }


    Here is my whole class:

    http://hastebin.com/zalosipico.avrasm

    This is kinda spoon feeding you, which I normally don't do, but I didn't understand it when I first attempted it either. Good luck!
     
    Kilovice likes this.
  3. Offline

    Kilovice

    HeyAwesomePeople This works entirely except it doesn't account for multiple explosions is there anything able to be done about that?
     
Thread Status:
Not open for further replies.

Share This Page