How to resume any BukkitRunnables after a reload

Discussion in 'Plugin Development' started by raunak114, Mar 26, 2017.

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

    raunak114

    Do i like save all the tasks in one of my configs and then after reload what do i do.. this is boiling my head xD
    Please help me
     
  2. Offline

    timtower Administrator Administrator Moderator

    @raunak114 Depends on what the tasks do.
    Many ways to tackle this.
     
  3. Offline

    raunak114

    @timtower
    So basically i have a bukkit runnable scheduled, that counts down an int ETA, in a right click event, and everytime the event is triggered, this code is ran (ETA is public)
    Code:
        Bukkit.getServer().getScheduler().scheduleAsyncDelayedTask(plugin, new Runnable(){@Override public void run(){
            ETA = ETA + (20 * 60);
            flylands.remove(p.getName());
        flylands.put(ETA, p.getName());
        plugin.getConfig().addDefault("FlyLands", flylands);
        plugin.saveConfig();}}, 1);
        if (ETA <= 1){
        new BukkitRunnable(){
             public int taskID = Bukkit.getScheduler().scheduleSyncRepeatingTask(plugin, this, 1L, 20L);
            
                @Override
                public void run(){
                    --ETA;
                    p.sendMessage(ETA + "");
                    if (ETA <0){
                        flylands.remove(p.getName());
                        plugin.getConfig().addDefault("FlyLands", flylands);
                        plugin.saveConfig();
                        Bukkit.getScheduler().cancelTask(taskID);
                    }
                }
            };
        }
     
  4. Offline

    timtower Administrator Administrator Moderator

    @raunak114 This looks like something that is fine to not rum when the server shuts down.
    Having eta public will make all players have the same timer.
    My suggestion would be a single runnable and a hasmah for the players.
     
  5. Offline

    raunak114

    @timtower What do you mean by fine to not run when the server shuts down? Oops sorry ETA wasnt public, it was declared as
    Code:
    int ETA = 1;
    There is only a single runnable and hashmap for the players??
    So do i make like a for loop for each Hashmap (name,ETA) and then start the bukkit runnable all over again? (In my on enable), If so, how?
     
  6. Offline

    timtower Administrator Administrator Moderator

    @raunak114 Based on your code: you might not even need a runnable at all.
    What are you trying to do?
     
  7. Offline

    raunak114

    @timtower So basically when a player right clicks and item, the value for eta increases, and the bukkit runnable starts (If it was not started alrady) and counts ETA down to 0, after that it stops, so after a restart, I want this bukkitrunnab le to keep on going if it was running before
     
  8. Offline

    timtower Administrator Administrator Moderator

  9. Offline

    raunak114

    You could say so, but it's a cool down of every only player (who has right clicked an item) and their ETA
     
  10. Offline

    timtower Administrator Administrator Moderator

    @raunak114 Then store the time that they used the item in a hashmap<UUID,long>
    If the time stored < currenttime-cooldowntime then don't allow.
    Else: allow the action, do make sure to check if they are in the map.
     
  11. Offline

    raunak114

    Actually, I have got what I needed to work :), but I want the bukkitrunnable to continue to repeat my task when the server reloads/restarts
     
  12. Offline

    timtower Administrator Administrator Moderator

    @raunak114 Then you need to save the time left in the config per player
     
  13. Offline

    raunak114

    I have done that, my question is, how do I start all he runnables again? (After a reload/restart)
     
  14. Offline

    timtower Administrator Administrator Moderator

    @raunak114 You just start them with the right eta
     
  15. Offline

    raunak114

    Ik, by I mean how do I just start all of the bukkit runnables on enable?
     
  16. Offline

    timtower Administrator Administrator Moderator

    @raunak114 What part do you need help with? Your code is already starting them, you just need to move that and do for each eta
     
  17. Offline

    raunak114

    Ik my code is working, but, when the server restarts, the bukkitrunnable () will stop after a reload/restart...
    And I want it to continue ;)
     
  18. Offline

    timtower Administrator Administrator Moderator

    @raunak114 Then you need to start it again in the onEnable.
     
  19. Offline

    raunak114

    How is what I am asking xD for each player
     
  20. Offline

    timtower Administrator Administrator Moderator

    @raunak114 Loop through the names in the config. Or where you might store the eta's.
     
  21. Offline

    raunak114

    I just don't know how to loop it...
     
  22. Offline

    timtower Administrator Administrator Moderator

  23. Offline

    raunak114

    It's like this:
    Code:
    Flylands
    <String playername>:<ETA>
    
    
    //For example
    raunak114:1200
    
     
  24. Offline

    timtower Administrator Administrator Moderator

    @raunak114 Please post the actual config, this is not working like this.
     
  25. Offline

    raunak114

    I can't I am not at home,
    In my config, I basically store a hashmap<String,Integer> and that hashmap is known has flylands (as you can see in the runnable)
     
  26. Offline

    timtower Administrator Administrator Moderator

    @raunak114 configurationSection.getKeys(false)
     
  27. Offline

    raunak114

    ? I have seen many posts with how to derive a hashmap, and in that I have seen that code, but I srsly don't know what that does
     
  28. Offline

    timtower Administrator Administrator Moderator

    @raunak114 Then play with it when you get home.
    It returns a set, you can loop through a set.
     
  29. Offline

    raunak114

    Ok ;)
    If I have a problem I'll ask you
     
  30. Offline

    raunak114

    Couldn't really figure this out
     
Thread Status:
Not open for further replies.

Share This Page