Fixing Thread.sleep delay.

Discussion in 'Plugin Development' started by mrdude123, Jul 15, 2015.

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

    mrdude123

    As we all know, Thread.Sleep is horrible for lagging the server. What can I use to set a delay where the Thread.sleep is?
    Code:
    if(args[1].equalsIgnoreCase("1")){
                        sender.sendMessage(ChatColor.GREEN + "You have a " + ChatColor.YELLOW + "16%" + ChatColor.GREEN + " chance to die.");
                        sender.sendMessage(ChatColor.RED + "Your hands are sweaty, you're feeling nauseous...");
                        player.addPotionEffect(new PotionEffect(PotionEffectType.CONFUSION, 100, 2));
                        try {
                            Thread.sleep(1000);
                        } catch (InterruptedException e1) {  // First delay
                            // TODO Auto-generated catch block
                            e1.printStackTrace();
                        }
                        sender.sendMessage(ChatColor.RED + "And you take a shot.");
                        try {
                            Thread.sleep(2000);
                        } catch (InterruptedException e) {
                            // TODO Auto-generated catch block //Second delay
                            e.printStackTrace();
                        }
                        Random r = new Random();
                        int num = r.nextInt(5);
                       
                        if(num == 0){
                            player.setHealth(0.0D);
                        }else{
                            sender.sendMessage(ChatColor.GREEN + "You got lucky.");
                        }
                       
                    }
                }
            }
           
            return false;
        }
    
    }
     
  2. Offline

    SuperOriginal

    Delayed tasks.
     
  3. Offline

    Zombie_Striker

    @mrdude123
    Thread.sleep means that nothing should happen for __ period. Since bukkit sends/receives packets every fraction of a second, this is why things are "laggy" when you use it (it's not really lag, it's just a short disconnection from the server)

    Use this :
    Code:
    getServer().getSchuduler().scheduleSyncDelayedTask( the plugin, new Runnable(){
    public void run(){
    //code
    }
    },the delay);
    
     
  4. Offline

    mrdude123

    Can one of you guys show me how you would use it in my code? I'm trying to delay the "Your hands are sweaty" to "You take a shot." for a dramatic, nerve-racking effect. Would I just put the bukkit scheduler right in between?
     
  5. Offline

    DoggyCode™

    I'm not a 100% sure, but you might want to use something like this:

    Code:
    getServer().getSchuduler().scheduleSyncDelayedTask( the plugin, new Runnable(){
    public void run(){
    sender.sendMessage("Please wait ca. 5 seconds");
    //Basically you can have any code here
    }
    },20 * 5  //20 ticks = 1 second times 5 = 5 seconds);
    sender.sendMessage("Wohoo! You have waited for 5 seconds
    
    I'm not so sure about this though, I have nowhere to actually test it at the moment. I'd suggest you just try different things, eventually it will work ;)
     
  6. Offline

    mrdude123

  7. That actually isn't right, you've mixed it up there. The code inside the run method is that, what happens after the delay. So you should switch the 2 messages and put the one saying please wait 5 seconds before the delayed task, although It's not necessary probably.
     
    DoggyCode™ likes this.
  8. Offline

    DoggyCode™

    @Lionhard Yeye, I kind of knew that xD I was just very sceptical :D.
     
    Lionhard likes this.
Thread Status:
Not open for further replies.

Share This Page