Repeating a message

Discussion in 'Plugin Development' started by Beno65Dev, May 26, 2014.

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

    Beno65Dev

    I make a plugin where you get a message about a minute I thought it was as yet not, I hope you can help

    here is my code

    The main class:

    Code:java
    1.  
    2. package me.beno65dev.website;
    3.  
    4. import org.bukkit.Bukkit;
    5. import org.bukkit.ChatColor;
    6. import org.bukkit.plugin.Plugin;
    7. import org.bukkit.plugin.java.JavaPlugin;
    8.  
    9. public class Main extends JavaPlugin{
    10.  
    11. static Plugin plugin;
    12.  
    13. @SuppressWarnings("deprecation")
    14. @Override
    15. public void onEnable() {
    16.  
    17. Bukkit.getScheduler().scheduleAsyncRepeatingTask(Main.getMain(), new Runnable() {
    18.  
    19. @Override
    20. public void run() {
    21. Bukkit.broadcastMessage(ChatColor.translateAlternateColorCodes('&', Config.getBroadcastMessage()));
    22. }
    23. }, 20 * 1, 0);
    24.  
    25. Main.plugin = this;
    26. Config.initConfig();
    27. }
    28.  
    29. public static Plugin getMain(){
    30. return plugin;
    31. }
    32.  
    33. public static Plugin getInstance(){
    34. return plugin;
    35. }
    36. }
    37.  


    and my Config:
    Code:java
    1. package me.beno65dev.website;
    2.  
    3. import org.bukkit.configuration.file.FileConfiguration;
    4.  
    5. public class Config {
    6. static FileConfiguration conf = Main.getInstance().getConfig();
    7.  
    8.  
    9. public static String getBroadcastMessage(){
    10. return getFileConfiguration().getString("Websitemessage.Default_message_message");
    11. }
    12.  
    13. public static FileConfiguration getFileConfiguration(){
    14. return conf;
    15. }
    16.  
    17. public static void initConfig() {
    18. if (!getFileConfiguration().contains("Websitemessage.Default_message_message")){
    19. getFileConfiguration().addDefault("Websitemessage.message", "You need to edit this");
    20. getFileConfiguration().options().copyDefaults(true);
    21. Main.getInstance().saveConfig();
    22. return;
    23. }
    24. }
    25. }
    26.  
     
  2. Offline

    xize

    Beno65Dev

    a few things I don't want to talk you down or something, but why are you using a async task in onEnable? async is actually designed for heavy tasks for example a blocking request when your plugin connects to a website to lookup things.

    but now back to your problem since you want a minute you can calculate it from milliseconds back to a minute:
    roughly I guess its 60*60*20 or 60*60*60*20 but I'm not to good with math though.

    however if you like to make it easier for yourself you could do this note that it may can work inacurate since set in the date object can return negative outputs not sure about the minutes and second though:

    Code:
         public Long getMinutes(int min) {
                  Date date = new Date();
                  date.setMinutes(date.getMinutes()+min);
                  long time = (date.getTime()-System.currentTimeMillies());
                  return time;
         }
    
     
  3. Offline

    TopTobster5

    Beno65Dev I would use a sync repeating task.
    Code:java
    1. public static int loop = 0;
    2.  
    3. loop = Bukkit.getScheduler().scheduleSyncRepeatingTask(this, new Runnable() {
    4. public void run() {
    5. //Code goes here
    6. }
    7. }, 60 * 20, 60 * 20);


    Simply put the code youw ant to use in the commented section (in your case, the broadcast message) and the plugin will broadcast it every 60 seconds.
     
  4. Offline

    Beno65Dev

  5. Offline

    TopTobster5

Thread Status:
Not open for further replies.

Share This Page