Bukkit Scheduler

Discussion in 'Plugin Development' started by Kermit_23, Feb 17, 2017.

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

    Kermit_23

    What i'm trying to do is to count player joins and if there are too many players logging very fast, run a scheduler for x amount of secs and see if player joins are not fast. What i have tried is to run a runTaskTimerAsynchronously for 100 times that checks the player joins each 5 secs passed using an hashmap. Although, before it detects the "fast joins" there will be so many joins that the server would not respond before it could even detect the joins. How can i improve it?
     
  2. Offline

    MaxFireIce

    @Kermit_23 If I understand you correctly this might help you. Get the current player joins and save that, do a scheduleSyncDelayedTask with delay of 100 (for 5 secs) and get the current amount of joins in there and save that, then compare the 2
     
  3. Offline

    Kermit_23

    @MaxFireIce Did that, but the problem is that i want the scheduler to run until the server stops. I don't know if it is going to affect the performance if i do it for 3 seconds.
     
  4. Offline

    ipodtouch0218

    Just scheduleSyncRepeatingTask then.
     
  5. Use a bukkit runnable not the schedular.
     
  6. Offline

    ipodtouch0218

    @TheEnderman
    Why?
     
  7. @ipodtouch0218
    It doesn't really matter if you use Bukkit.getScheduler() or the BukkitRunnable class to schedule your tasks, but it's generally agreed that the BukkitRunnable class is easier to work with because you can start and stop the task inside the task itself without having to keep track of a bunch of task IDs. See how this,
    Code:java
    1. void startTask() {
    2. new BukkitRunnable() {
    3. @Override
    4. public void run() {
    5. // See how easy it is to cancel?
    6. cancel();
    7. }
    8. }.runTask(myPlugin);
    9. }
    is way easier than this:
    Code:java
    1. int id;
    2.  
    3. void startTask() {
    4. id = Bukkit.getScheduler().scheduleSyncDelayedTask(myPlugin, new Runnable() {
    5. @Override
    6. public void run() {
    7. cancelTask();
    8. }
    9. });
    10. }
    11.  
    12. void cancelTask() {
    13. Bukkit.getScheduler().cancelTask(id);
    14. }
    Although one thing I can give the scheduler way credit for is that it uses Runnable, so you can use lambda expressions!
    Code:java
    1. Bukkit.getScheduler().scheduleSyncDelayedTask(myPlugin, () -> cancelTask());
    Or even a straight method reference!
    Code:java
    1. Bukkit.getScheduler().scheduleSyncDelayedTask(myPlugin, this::cancelTask);
     
    ipodtouch0218 likes this.
  8. Offline

    ipodtouch0218

    @AlvinB
    He wants it to run every 3 seconds from the start of the server, so its just easier to use RepeatingTasks than to schedule new BukkitRunnables every 3 seconds.
     
  9. @ipodtouch0218
    Yeah well, BukkitRunnables can be repeating too, I just used the runTask() as an example.
    Code:java
    1. new BukkitRunnable() {
    2. @Override
    3. public void run() {
    4.  
    5. }
    6. }.runTaskTimer(myPlugin, 0L, 60L);
    And this is where the easy cancelling really shines, as you more often need to cancel a repeating task than a non-repeating one.
     
    ipodtouch0218 likes this.
  10. Offline

    Kermit_23

    @AlvinB
    But what if i run another 2-3 schedulers inside it? Will it degrade the performance? also, i'm using lambda expressions in my plugin :)
     
  11. Offline

    timtower Administrator Administrator Moderator

    @Kermit_23 Depends on what they do, how long they run, how often they get started.
     
  12. Offline

    Kermit_23

    @timtower I will come with an edit after i leave the school
    EDIT:
    PHP:
    {
           
    Bukkit.getScheduler().runTaskTimerAsynchronously(AntiBotUltra.getInstance(), () -> {
                if (
    Hbot.get("LoginCount") < getConfig().getInt("AntiBotUltra.Sensibility")) {
                    
    Hbot.put("LoginCount"0);
                } else if (
    Hbot.get("LoginCount") > getConfig().getInt("AntiBotUltra.Sensibility") && !whitelistEnabled) {
                    
    whitelistEnabled true;
                    
    //debug("Attack detected!");
                  
    Bukkit.getScheduler().runTaskLater(AntiBotUltra.getInstance(), () -> {
                        
    Hbot.put("LoginCount"0);
                        
    //debug("Analyzing attack..")'
                    
    }, 2000L);
                   
    Bukkit.getScheduler().runTaskLater(AntiBotUltra.getInstance(), () -> {
                        if (
    Hbot.get("LoginCount") < getConfig().getInt("AntiBotUltra.Sensibility")) {
                            
    whitelistEnabled false;
                            
    //debug("Bot attack stopped, whitelist turned off");
                        
    } else {
                            
    //debug("Bot attack persists, restarting scheduler");
                        
    }
                    }, 
    6000L);
                }
            },
    100L100L);
        }
    Also, how can i make it to restart the scheduler from "//debug("Bot attack persists, restarting scheduler");"
     
    Last edited: Feb 23, 2017
  13. Offline

    Kermit_23

  14. Offline

    Kermit_23

  15. Offline

    Zombie_Striker

    @Kermit_23
    I would recommend moving the runnable to a new class. Once you get to that debug message, call this runnable again.
     
  16. Offline

    Kermit_23

    @Zombie_Striker It is in a new class :)
    Could you give me an example on how to call it? (i'm not asking for spoon feed)
     
  17. Offline

    Zombie_Striker

    @Kermit_23
    I meant the lambda expression should be in a seperate class, that way we can just use:
    Code:
    Bukkit.getScheduler().runTaskTimerAsynchronously(AntiBotUltra.getInstance(),  RUNNABLE_CLASS_INSTANCE,100l,100l);
     
  18. Offline

    Kermit_23

    @Zombie_Striker ok, but if i assign the scheduler, will i be able to call it?
     
Thread Status:
Not open for further replies.

Share This Page