Solved Handling Reloads?

Discussion in 'Plugin Development' started by jthort, Nov 2, 2013.

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

    jthort

    Hey I'm having some trouble getting this plugin to work after the server is reloaded. Once you do /reload, it continues to run. Then if you do the command to start the plugin, it will run 2 sets of the same plugin (I hope that makes sense). Any Help?
    Code:java
    1. package me.Ian.AutoMessager;
    2.  
    3. import java.util.Timer;
    4. import java.util.TimerTask;
    5. import java.util.logging.Logger;
    6.  
    7. import org.bukkit.Bukkit;
    8. import org.bukkit.ChatColor;
    9. import org.bukkit.command.Command;
    10. import org.bukkit.command.CommandSender;
    11. import org.bukkit.entity.Player;
    12. import org.bukkit.event.Listener;
    13. import org.bukkit.plugin.PluginManager;
    14. import org.bukkit.plugin.java.JavaPlugin;
    15.  
    16. public class AutoMessager extends JavaPlugin implements Listener {
    17. public final Logger logger = Logger.getLogger("Minecraft");
    18. public static AutoMessager plugin;
    19. public static int nums = 0;
    20. public static int counter = 0;
    21.  
    22.  
    23. protected String Chatcolor;
    24. final String message = ("Visit our website at [url="http://www.economicallycrafted.enjin.com"]www.economicallycrafted.enjin.com[/url]");
    25. final String message2 = ("Don't forget to vote at [url="http://www.economicallycrafted.enjin.com"]www.economicallycrafted.enjin.com[/url]");
    26. final String message3 = ("Don't forget to check both our RPG, and Survival part of the server!");
    27.  
    28.  
    29. @Override
    30. public void onEnable(){
    31. logger.info("Automessage Enabled!");
    32. PluginManager pm = this.getServer().getPluginManager();
    33. pm.registerEvents(this, this);
    34.  
    35.  
    36.  
    37. }
    38. @Override
    39. public void onDisable(){
    40. logger.info("Ian's Automessager has been Disabled");
    41. }
    42.  
    43.  
    44. @Override
    45. public boolean onCommand(final CommandSender sender, Command cmd, final String commandLabel, String[] args){
    46.  
    47. Player player = (Player) sender;
    48. if (Global.counter == 0){
    49. if(commandLabel.equalsIgnoreCase("AmStart")){
    50.  
    51.  
    52.  
    53.  
    54. player.sendMessage(ChatColor.BLUE + "Now running the auto message " + ChatColor.DARK_GRAY + message);
    55. Global.counter++;
    56. if (Global.counter > 0 && Global.nums < 3){
    57.  
    58.  
    59. final Timer timer = new Timer ();
    60. TimerTask hourlyTask = new TimerTask () {
    61.  
    62.  
    63. @Override
    64. public void run () {
    65.  
    66. if (Global.nums == 0){
    67. Bukkit.broadcastMessage(ChatColor.BLUE + "[AutoMessager] " + ChatColor.GRAY + message);
    68. }else if (Global.nums == 1){
    69. Bukkit.broadcastMessage(ChatColor.BLUE + "[AutoMessager] " + ChatColor.GRAY + message2);
    70. }else if (Global.nums == 2){
    71. Bukkit.broadcastMessage(ChatColor.BLUE + "[AutoMessager] " + ChatColor.GRAY + message3);
    72. }
    73. Global.nums++;
    74. if (Global.nums == 3){
    75. Global.nums = 0;
    76. }
    77. }
    78.  
    79.  
    80.  
    81.  
    82.  
    83.  
    84. };
    85.  
    86. timer.schedule (hourlyTask, 10000, 10000);
    87. }else if (Global.counter > 1){
    88. player.sendMessage(ChatColor.RED + "You are already running AutoMessager!" );
    89. player.sendMessage(ChatColor.RED + "If AutoMessager is not running, conatact Ian!");
    90. }else{
    91. player.sendMessage(ChatColor.RED + "Error with plugin code 2, contact Ian!");
    92. }
    93.  
    94.  
    95.  
    96. }
    97.  
    98.  
    99. };
    100.  
    101. return true;
    102.  
    103.  
    104.  
    105.  
    106.  
    107.  
    108.  
    109.  
    110.  
    111.  
    112.  
    113.  
    114. }
    115.  
    116.  
    117. }




    I just started coding plugins, so any other help would be great. Thanks!
     
  2. Offline

    Shzylo

    The reload command should automatically handle for disabling the plugin..
     
  3. Offline

    jthort

    The reload command works fine, it says disabled plugin, enabled plugin. But the plugin continues to run.

    Before the reload, If I do /amstart multiple times it doesn't break.

    But If I reload the plugin and do /amstart, it starts another loop of messages.
     
  4. Offline

    CubieX

    /reload will not automaticly remove everything of your plugin from memory.
    You have to clean up yourself.
    Set all instances your plugin creates to "null" in the onDisable(),
    cancel all scheduled tasks of your plugin,
    and I'm sure some other things have to be done to properly reset your plugin.

    Generally: A /reload is a "worst case" scenario for a bukkit plugin.
    If I were you, I would never ever use it. Just do a proper restart to avoid memory leaks and other crazy problems.
    Each of your plugins (and especially plugins of other authors you use) can act up upon reload,
    because only a minority of them is really designed to hande reloads 100% correct.
    Just as a heads-up.
     
    jthort likes this.
  5. use the Bucket Scheduler instead of the timer class in java, the bukkit scheduler handles reload automatically (and is thread safe)
     
    jthort likes this.
  6. Offline

    jthort

    Okay thank's both of you and I'll try to put in the Bukkit Scheduler.
     
Thread Status:
Not open for further replies.

Share This Page