Remove a block after a certain about of time?

Discussion in 'Spigot Plugin Development' started by William029, Jun 1, 2017.

  1. title says it all how would I do that?
     
  2. If you want someone to spoon-feed you code, you're not gonna get it. I'll help you out a bit though. Have you tried creating a cooldown timer using an arrayList and/or a Bukkit runnable to remove a block from a saved location?
     
    • Agree Agree x 3
  3. ScarabCoder

    ScarabCoder Retired Resource Staff
    Retired

    You can use Bukkit Schedulers. Pretty much, create a Bukkit Scheduler runnable, then set it to run later, giving it ticks as a parameter.
    Learn more about Bukkit Schedulers here: http://bukkit.gamepedia.com/Scheduler_Programming#BukkitRunnable

    Looking at that wiki, it doesn't really show the cleanest way. In their examples, they create a class which extends BukkitRunnable. I like to (and it may be the proper way) create a Bukkit Runnable as a variable:
    Code (Text):
    BukkitRunnable r = new BukkitRunnable(){

        @Override
         public void run(){

         }

    }
     
    • Like Like x 1
  4. That's called an anonymous class. It's useful, but not always the best. To expand on your anonymous class example I'm going to show how to properly inject data to the class instead of abusing final.

    Code (Text):

    Block block = player.getLocation().getBlock().getRelative(DOWN);
    MyTask myTask = new MyTask(block);
    myTask.runTaskLater(plugin, 60L);

    public class MyTask extends BukkitRunnable {
        private Block block

        public MyTask(Block block {
            this.block = block;
        }

        @Override
         public void run(){
            block.setType(Material.AIR);
         }
    }
     
    This will remove the block under the player's location after 3 seconds, even if the player moves away from the location before it happens.
     
    • Useful Useful x 1