[Solved] How to add a delay?

Discussion in 'Plugin Development' started by se1by, Oct 26, 2011.

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

    se1by

    I want to have a little time between two tasks.
    I tried to solve it with an scheduler, but I need variables from the code outside the scheduler.
    How can I add them?
    Here is my code:
    Code:
    public void onPlayerInteractEntity(PlayerInteractEntityEvent event) {
    
                Player admin = event.getPlayer();
                String adminname = admin.getName();
                Entity user = event.getRightClicked();
    
                if(plugin.hasEntry(admin)) {
    
                if (BanAxe.Active(admin)){
    
                if (admin.getItemInHand().getTypeId() == 258) {
    
                        if(user instanceof Player){
                            Player userplayer = ((Player) user);
    
                            admin.getWorld().strikeLightning(user.getLocation());
    
                            plugin.getServer().getScheduler().scheduleSyncDelayedTask(plugin, new Runnable() {
    
                                public void run() {
    
                                admin.sendMessage(ChatColor.DARK_BLUE + "[BanAxe] " + ChatColor.RED + "You banned " + userplayer);
                                userplayer.kickPlayer("You got banned by " + adminname);
                                admin.performCommand("ban " + userplayer);
                                System.out.println("[BanAxe] " + admin.getName() + " banned " + userplayer);
    
                                }
                            }, 60L);
    
                        }
                                else{
                         admin.sendMessage(ChatColor.DARK_BLUE + "[BanAxe] "+ ChatColor.RED + "Click on a user!");
    
                        }
                    }
                }
            }
    
        }
     
  2. Offline

    Windwaker

    Well to start your syntax is scary.
     
  3. Offline

    se1by

    I'm working on it :D
    But do you have a solution for my problem?
     
  4. Offline

    Evangon

    public static void wait (int n){
    long t0,t1;
    t0=System.currentTimeMillis();
    do{
    t1=System.currentTimeMillis();
    }
    while (t1-t0<1000);
    }

    Use wait(time).
    I used it, it works, but weirdly. Try to make use of it.
     
  5. Offline

    DDoS

    You'll need to assign the values you want to pass to your scheduler to some class variables. I guess you could try creating a new class BanEvent, put your three variables at the top as the class variables (and also your plugin, you'll need it for the scheduler), and have the scheduler code there. So you make a new BanEvent instance every time you have to ban someone.

    Like:

    Code:
    BanEvent ban = new BanEvent(admin, userPlayer, plugin);
    ban.run();
    And for BanEvent:

    Code:
    public class BanEvent {
    
    private Player admin;
    private Player user;
    private BanAxe plugin;
    
    public BanEvent(Player admin, Player user, BanAxe plugin) {
    
    this.admin = admin;
    this.user = user;
    this.plugin = plugin;
    
    }
    
    public void run() {
    
    plugin.getServer().getScheduler().scheduleAsyncDelayedTask(plugin, new Runnable() {
    
    @Override
    public void run() {
    
    //Your code here.
    // You should be able to access admin and player.
    
    }
    }, 60L);
    
    
     
  6. Offline

    se1by

    thx, I'll try it.
     
Thread Status:
Not open for further replies.

Share This Page