How can i broadcast a string of messages in order....

Discussion in 'Plugin Development' started by MrFluffyLion, Apr 16, 2014.

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

    MrFluffyLion

    How can i make this broadcast a list of messages within my config in order

    Code:java
    1. saveConfig();
    2. Bukkit.getServer().getScheduler().scheduleSyncRepeatingTask(this, new Runnable() {
    3. public void run() {
    4. Bukkit.broadcastMessage("Message");
    5. }
    6. }, 600, 1200);
    7.  


    Ended up doing this
    Code:java
    1. package Me.MrFluffyLion;
    2.  
    3. import org.bukkit.Bukkit;
    4. import org.bukkit.command.Command;
    5. import org.bukkit.command.CommandSender;
    6. import org.bukkit.event.Listener;
    7. import org.bukkit.plugin.java.JavaPlugin;
    8.  
    9. public class Main extends JavaPlugin implements Listener {
    10. int check = 1;
    11.  
    12.  
    13. public void onEnable() {
    14. Bukkit.getPluginManager().registerEvents(this, this);
    15. getConfig().options().copyDefaults(true);
    16. saveConfig();
    17. Bukkit.getServer().getScheduler().scheduleSyncRepeatingTask(this, new Runnable() {
    18. public void run() {
    19. if(check == 1) {
    20. ++check;
    21. Bukkit.broadcastMessage(getConfig().getString("1Message"));
    22. return;
    23. }
    24. if(check == 2) {
    25. ++check;
    26. Bukkit.broadcastMessage(getConfig().getString("2Message"));
    27. return;
    28. }
    29. if(check == 3) {
    30. check = 1;
    31. Bukkit.broadcastMessage(getConfig().getString("3Message"));
    32. return;
    33. }
    34.  
    35. }
    36. }, 0, 2400);
    37.  
    38. }

    But still wanna know how i could use a string list for this ):

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

    TheMcScavenger

    Make a string list in your configuration file, convert it to a String[], have an integer that tells you which one you recently broadcasted, broadcast the next one in the string set, and update the integer...?
     
  3. Offline

    DrEinsteinium

    I'm not sure if you want to make an indefinitely repeating series of messages... or what. I would use recursion to do it. Using the given list and the given index I can choose which messages I want to broadcast with the if/else statement in the run() method by changing it and calling the same task again.
    Code:
    public BroadcastTask extends Runnable
    {
        private int index;
        private String[] list;
     
        public BroadcastTask(String[] list, int index)
        {
            this.list = list;
            this.index = index;
        }
     
        public void run()
        {
            Bukkit.broadcastMessage(getConfig().getString(this.list[this.index]));
            if(this.index > 0) this.index--;
            else this.index = this.list.length;
     
            Bukkit.getServer().getScheduler().scheduleSyncRepeatingTask(this, new BroadcastTask(this.list, this.index), 0, 2400);
        }
    }
    
     
Thread Status:
Not open for further replies.

Share This Page