Create Small delay

Discussion in 'Plugin Development' started by Official_Spendrew, Mar 8, 2015.

Thread Status:
Not open for further replies.
  1. This may seem like a pretty basic question but I need an efficient way to create a delay. I am attempting to create my version of TNT Run, however when I use,
    Code:
                if(this.joined.contains(p) && (p.getLocation().getBlock().getRelative(BlockFace.DOWN).getType() == Material.TNT)){
                        p.getLocation().getBlock().getRelative(BlockFace.DOWN).setType(Material.AIR);
    
    the TNT deletes out from under the player to quickly. I tried using thread.sleep like so,

    Code:
                if(this.joined.contains(p) && (p.getLocation().getBlock().getRelative(BlockFace.DOWN).getType() == Material.TNT)){
                    try{
    
                        Thread.sleep(10);
                    }catch(Exception s)
                    {
                        System.out.println("Exception caught");
                    }
                        p.getLocation().getBlock().getRelative(BlockFace.DOWN).setType(Material.AIR);
                    }
    
    however, this caused lots of server lag when multiple people played. Any other ways I can create a small delay between getting the players location and removing the block from under them?
     
  2. Offline

    LegacyGaming

    @Official_Spendrew

    Like this?

    Code:
    new BukkitRunnable() {
        @Override
        public void run() {
            //code here
        }
    }.runTaskLater(Plugin, <delay in ticks>);
     
  3. Is Plugin a variable for something?

    Well I am assuming Plugin would just be "this"

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 13, 2016
  4. Offline

    LegacyGaming

  5. Like this?
    Code:
            if (gamestarted = true){
                if(this.joined.contains(p) && (p.getLocation().getBlock().getRelative(BlockFace.DOWN).getType() == Material.TNT)){
                    new BukkitRunnable() {
                        @Override
                        public void run() {
                            p.getLocation().getBlock().getRelative(BlockFace.DOWN).setType(Material.AIR);
                        }
                    }.runTaskLater(this, 20);
                    }
    
                }
    
     
  6. Offline

    LegacyGaming

    Yeah, it should work.
     
  7. Offline

    Skionz

    @Official_Spendrew It depends what 'this' represents. If you are doing this in your 'main' class then it would work.
     
  8. Got it to work :D
    Thanks for your suggestions. Here is my final code.
    Code:
    if (this.joined.contains(p) && (p.getLocation().getBlock().getRelative(BlockFace.DOWN).getType() == Material.TNT)) {
                    final Location plocation = p.getLocation();
                    new Timer().schedule(new TimerTask() {
                        @Override
                        public void run() {
                            plocation.getBlock().getRelative(BlockFace.DOWN).setType(Material.AIR);
                        }
                    }, 150);
    
     
  9. Offline

    CXdur

    @Official_Spendrew

    Nice! And in case you didn't know, you should always avoid using Thread.sleep inside plugins because it would make the main thread sleep and basically cause a lot of lag. I'm not really able to explain this properly, but it's definitely something you should refrain from doing!

    You should also take a look at this: http://wiki.bukkit.org/Scheduler_Programming
     
Thread Status:
Not open for further replies.

Share This Page