Brackets?

Discussion in 'Plugin Development' started by Luke_Lax, Jul 7, 2013.

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

    Luke_Lax

    Hello everyone, I'm making a new project in new java territory for me so please bare with me, I'm not stupid nor am I going to "Copy and paste", I just need a little extra help.

    here's my code:
    Show Spoiler

    package WorldBank;

    import java.text.DateFormat;
    import java.text.SimpleDateFormat;
    import java.util.Calendar;
    import java.util.Date;
    import java.util.HashMap;
    import java.util.logging.Logger;

    import org.bukkit.Bukkit;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandSender;
    import org.bukkit.plugin.Plugin;
    import org.bukkit.plugin.PluginDescriptionFile;
    import org.bukkit.plugin.java.JavaPlugin;
    import org.bukkit.scheduler.BukkitRunnable;

    public class WorldBankMain extends JavaPlugin {

    private static final String Chatcolor = null;
    public final Logger logger = Logger.getLogger("Minecraft");
    public static WorldBankMain plugin;

    @Override
    public void onEnable() {
    PluginDescriptionFile pdfFile = this.getDescription();
    this.logger.info(pdfFile.getName() + " Version " + pdfFile.getVersion() + " Has been Enabled!");
    getConfig().options().copyDefaults(true);
    saveConfig();
    }

    @Override
    public void onDisable() {
    PluginDescriptionFile pdfFile = this.getDescription();
    this.logger.info(pdfFile.getName() + " Version " + pdfFile.getVersion() + " Has been Disabled!");
    }



    Bukkit.getServer().getScheduler().scheduleSyncRepeatingTask(plugin, new BukkitRunnable() {
    public void run() {
    DateFormat dateFormat = new SimpleDateFormat("dd/mm/yyyy");
    Date date = new Date();
    //Any code here will run every 24 hours(Starting from when the plugin is loaded)
    if((dateFormat.format(date)) == getConfig().getString("Dates")) {
    Bukkit.broadcastMessage("The date is equal to: " + getConfig().getString("Dates"));
    //Calculations
    }
    }
    }, 100, 86400*20);
    }


    Here's my issue:
    On the onDisable method, it tells me to remove my ending bracket and insert it right at the bottom of the code, so that the onDisable method wraps around the whole scheduleSyncRepeatingTask - why is that and how can I fix it?

    Any help is appreciated, like I said, this is new for me so I will need a little extra explaining :)
     
  2. Offline

    SugarCraft

    Code:java
    1. package WorldBank;
    2.  
    3. import java.text.DateFormat;
    4. import java.text.SimpleDateFormat;
    5. import java.util.Calendar;
    6. import java.util.Date;
    7. import java.util.HashMap;
    8. import java.util.logging.Logger;
    9.  
    10. import org.bukkit.Bukkit;
    11. import org.bukkit.command.Command;
    12. import org.bukkit.command.CommandSender;
    13. import org.bukkit.plugin.Plugin;
    14. import org.bukkit.plugin.PluginDescriptionFile;
    15. import org.bukkit.plugin.java.JavaPlugin;
    16. import org.bukkit.scheduler.BukkitRunnable;
    17.  
    18. public class WorldBankMain extends JavaPlugin {
    19.  
    20. private static final String Chatcolor = null;
    21. public final Logger logger = Logger.getLogger("Minecraft");
    22. public static WorldBankMain plugin;
    23.  
    24. @Override
    25. public void onEnable() {
    26. PluginDescriptionFile pdfFile = this.getDescription();
    27. this.logger.info(pdfFile.getName() + " Version " + pdfFile.getVersion() + " Has been Enabled!");
    28. getConfig().options().copyDefaults(true);
    29. saveConfig();
    30. }
    31.  
    32. @Override
    33. public void onDisable() {
    34. PluginDescriptionFile pdfFile = this.getDescription();
    35. this.logger.info(pdfFile.getName() + " Version " + pdfFile.getVersion() + " Has been Disabled!");
    36. }
    37.  
    38.  
    39.  
    40. Bukkit.getServer().getScheduler().scheduleSyncRepeatingTask(plugin, new BukkitRunnable() {
    41. public void run() {
    42. DateFormat dateFormat = new SimpleDateFormat("dd/mm/yyyy");
    43. Date date = new Date();
    44. //Any code here will run every 24 hours(Starting from when the plugin is loaded)
    45. if((dateFormat.format(date)) == getConfig().getString("Dates")) {
    46. Bukkit.broadcastMessage("The date is equal to: " + getConfig().getString("Dates"));
    47. //Calculations
    48. }
    49. }
    50. }, 100, 86400*20);
    51. }[/snytax]
     
  3. Offline

    Devil0s

    Please use the code tags to post your code. It looks like you see much better and it's much easier to help you with your problem.
     
  4. Offline

    Luke_Lax

    Yes sorry I forgot to add them, I'm rushing to get this working on time but I can't get any further with this error :(
     
  5. Offline

    Rocoty

    You have to understand that in Java, nothing works outside of methods. Everything goes inside methods which are called from various other methods, and so forth and so on. So if you'd like to schedule that task when the plugin is enabled, why not just put it in the onEnable method?
     
  6. Offline

    Luke_Lax

    Because anything within the onEnable method only runs when the plugin is loaded, I need to check the date every 24 hours to check if the date is the same as what's in the config, if it is then do {something} if not then wait until the next day to check, does that make sense?
     
  7. Offline

    Rocoty

    Put it in onEnable()....yes, it only runs once when the plugin is enabled, but you're only scheduling the task once, and it will run indefinitely. But I will advice you, that since you use such a long delay between every execution of the task, you really should make it an async task.
     
  8. Offline

    Luke_Lax

    Rocoty I must stress this is new to me, please explain a little more clearer:

    Are you saying that the onEnable method will keep it running? I didn't know that, I'm sure I can get it to work with that knowledge, thank you very much. However, you mentioned "async task" I'm not sure what that means/entails, could you explain more please? :)

    Rocoty ^

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 3, 2016
  9. Offline

    Rocoty

    Hmm. To clarify: No, onEnable() is not what keeps it running. But when you schedule the tast, the scheduler puts the task in its memory, and runs it through whenever it's supposed to. It's all to do with threads, you'll understand it clearly one day.

    So, async tasks (hold tight, here comes threads again) are tasks run asynchronously, in other words: They run in another thread. Threads are basically small operations that run pseudo-parallel with eachother. So basically, making the task asynchronous will relieve the server's main thread of keeping the task running, and so reduce potential lag or slowdown.

    Did it make things clearer for you?
     
  10. Offline

    Luke_Lax

    Rocoty
    Yes that is clear! I know minecraft runs on one big thread that's why if you do a task which is big or extensive, it'll cause a slowdown in the main thread and possibly crash the server :p
    Do you think you could tell me how to make it Async?
     
  11. Offline

    Rocoty

    two solutions:
    scheduleAsyncRepeatingTask

    or

    new BukkitRunnable() {
    //stuff
    }.runTaskTimerAsynchronously();
     
Thread Status:
Not open for further replies.

Share This Page