Problem with scheduler

Discussion in 'Spigot Plugin Development' started by RiotShielder, May 10, 2015.

  1. Hey all!

    I'm trying to create a plugin that will send messages periodically from a list of configurable messages, this was really easy until I wanted to allow the amount of lists to be configurable as well. I'm having trouble getting this to actually work, I'm not sure what's wrong.

    My code is probably pretty sloppy, I don't have a ton of experience with Java quite yet.
    Code (Text):
            List<String> announcements = this.getConfig().getStringList("announcements");
            BukkitScheduler scheduler = Bukkit.getServer().getScheduler();
            for (int i = 0; i < announcements.size(); i++) {
                int counter = i;
                scheduler.scheduleSyncRepeatingTask(this, new Runnable() {
                    @Override
                    public void run() {
                        List<String> motds = getConfig().getStringList("announcements." + announcements.get(counter) + ".messages");
                        final Random randommotd = new Random();
                        final String motdraw = motds.get(randommotd.nextInt(motds.size()));
                        String motdfinal = motdraw.replaceAll("&", "§").replaceAll("§§", "&").replaceAll("%newline%", "\n");
                        String prefix = parseConfig("announcements." + announcements.get(counter) + ".prefix");
                        for (Player player : getServer().getOnlinePlayers()) {
                            if (getConfig().getBoolean("preferences." + player.getName() + announcements.get(counter)) == true) {
                                player.sendMessage(prefix + motdfinal);
                            } else {

                            }
                        }
                    }
                }, 0L, getConfig().getInt("announcements." + announcements.get(counter) + ".interval"));
            }
    Example config
    Code (Text):
    announcements:
        list1:
            prefix: "Prefix"
            interval: 1
            delay: 0
            messages:
                - "Some Message"
        list2:
            prefix: "Prefix"
            interval: 1
            delay: 0
            messages:
                - "Some Message"
        list3:
            prefix: "Prefix"
            interval: 1
            delay: 0
            messages:
                - "Some Message"
     
    I appreciate any help! Thanks!
     
  2. I just coded :
    Code (Text):
    public void sendAutoMsg(){
            final List<String> announcements = this.getConfig().getStringList("announcements");
            final int i = 0;
            final int task = Bukkit.getScheduler().scheduleSyncRepeatingTask(yourplugininstance, new Runnable() {
             
                @Override
                public void run() {
                 
                    if(i != announcements.size()){
                        i++;
                        List<String> motds = getConfig().getStringList("announcements." + announcements.get(counter) + ".messages");
                        final Random randommotd = new Random();
                        final String motdraw = motds.get(randommotd.nextInt(motds.size()));
                        String motdfinal = motdraw.replaceAll("&", "§").replaceAll("§§", "&").replaceAll("%newline%", "\n");
                        String prefix = parseConfig("announcements." + announcements.get(counter) + ".prefix");
                        for (Player player : Bukkit.getServer().getOnlinePlayers()) {
                            if (getConfig().getBoolean("preferences." + player.getName() + announcements.get(counter)) == true) {
                                player.sendMessage(prefix + motdfinal);
                            } else {

                            }
                        }
                    }else{
                        Bukkit.getScheduler().cancelTask(task);
                    }
                }
            }, 0, 20);  // this will do very 1 sec, replace 20 by your config number
        }
    not tested..
     
  3. A few things that i noticed. Also when you are asking a question like this try and be more specific as to whats not working. Because code that compiles "works" but it doesnt necessarily work for what you need it to do.

    Code (Text):
    motdraw.replaceAll("&", "§").replaceAll("§§", "&")
    Should be done with
    Code (Text):
    ChatColor.translateAlternateColorCodes ("&", motdraw)
    Please save a reference to a Random instace outside of each task. No need to make a new one each time you send a message.

    Dont check if a boolean is true. Just use the boolean. If true == true then the first boolean must already be true.
     
  4. I'm actually really unsure about what isn't working here. It does compile fine, but the announcements never actually send.

    What's the benefit of doing it with translateAlternateColorCodes? I do it with replaceAll so that if I need to put an & sign, I just use &&

    And yea, I didn't realize that, thanks!

    How would I do that?
     
  5. Code (Text):
    if (getConfig().getBoolean("preferences." + player.getName() + announcements.get(counter)) == true) {
                                player.sendMessage(prefix + motdfinal);
                            } else {

                            }
    Notice that you only send the message if the players name is in the config section and is marked true. Also a possible bug is that you are actually checking the config for
    Code (Text):
    Preferences:
        <playerName><announcementName> : true
    (you need another ".").

    Lastly about the boolean comment
    Code (Text):
    boolean myBool = true;
    If (myBool == true) ...
    //is the same as
    If (myBool) ...