Auto Reload Please Help

Discussion in 'Plugin Development' started by VenamousV, Jul 19, 2013.

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

    VenamousV

    I am trying to make a plugin that after so many minutes will reload itself, or after so many minutes do something, and I have no idea where to start so if someone could point me in the right direction that would be great!
     
  2. Offline

    ChrisixStudios

    To reload your plugin you could create a method like this...

    Code:java
    1. public void reload(){
    2. onDisable();
    3. //set all variables to default.
    4. onEnable();
    5. }
     
  3. Offline

    VenamousV

  4. Offline

    ChrisixStudios


    Well for the every minute thing You need to create a thread that will loop until the disable. It would count every tick and once you get up to a minute then use the reload method.
     
  5. Offline

    CubieX

    You mean reload the config of the plugin?

    Use a timer. A sync repeating task for example "runTaskTimer()", set it to the time you want (see Bukkit API -> schedulers)
    And in the timers run() method, you do "plugin.reloadConfig()", where "plugin" must be a reference to your main class.
     
  6. Offline

    VenamousV

    No I mean reload the whole plugin, so Basically like he was telling me to do where I would put the Variables that I want to be executed. So basically what I want to do is have my plugin do something after every so many minutes. ChrisixStudios CubieX
     
  7. Offline

    CubieX

    Then my last post is good for you.
    Create a timer task and put all code in the run() method of the task.
    It will be executed in the given interval.

    But forget my "config" stuff. This is not what you want.

    I can't post helpful links, as I am on my mobile device currently.
     
  8. Offline

    VenamousV

    So with the runTaskTimer() how do I set it to run after say 30 minutes? CubieX
     
  9. Offline

    ChrisixStudios


    My way you would do.

    Code:java
    1. public class Reloader implements Runnable{
    2.  
    3. private <Your Plugin> plugin;
    4. private boolean running = true;
    5. private long time = 600000, lastTime, cumTime;
    6.  
    7. public Reloader(<Your Plugin> plugin){
    8. this.plugin = plugin;
    9. lastTime = new Date().getTime();
    10. }
    11.  
    12. @Override
    13. public void run(){
    14. while(running){
    15. long t = new Date().getTime();
    16. cumTime += t - lastTime;
    17. lastTime = t;
    18. if(cumTime >= time){
    19. cumTime = cumTime - time;
    20. Bukkit.getScheduler().runTask(plugin, new Runnable(){
    21.  
    22. @Override
    23. public void run(){
    24. plugin.reload();
    25. }
    26.  
    27. });
    28. }
    29. Thread.sleep(50);
    30. }
    31. }
    32.  
    33. }


    This will make sure it happens every minute. or whatever you set time to.
     
  10. Offline

    ZeusAllMighty11


    woah
     
  11. Offline

    VenamousV

    So the thread.sleep(); is where I define how long till the command is executed again? ChrisixStudios
     
  12. Offline

    CubieX

    Erm, why using your own time calculations, when a repeating task would do all of this?
    RunTaskTimer() set to 0 ticks delay and 20*60*30L cycle time will do the trick with less hassle.
     
  13. Offline

    Compressions

    ChrisixStudios Unless you are writing some erotic mathematics program, I would recommend you use different variable names.
     
    krazytraynz likes this.
  14. Offline

    VenamousV

    CubieX Do you think you could give me an example?
     
  15. Offline

    ChrisixStudios


    lol that's just the way I learned it when I was watching thenewboston and I don't feel like writing out cumulativeTime.
     
  16. Offline

    VenamousV

    ChrisixStudios thread.sleep(); is where I set how long till it does everything above it again right?
     
  17. Offline

    ChrisixStudios


    Basicly the Thread.sleep will cause the thread that you are on to sleep for 50 milliseconds then go through the while loop again. This will make it so the thread doesn't take up a lot of memory.
     
  18. Offline

    VenamousV

    Okay so if I wanted to throw a broadcast message in there so it is displayed every time all my code loops over again where would I put that? and also It is telling me to put a Try statement around my thread.sleep();
    ChrisixStudios
     
  19. Offline

    ChrisixStudios



    You need the try statement and you put your other code in the in the if statement or in the other thread.
     
Thread Status:
Not open for further replies.

Share This Page