Solved RunTaskTimer

Discussion in 'Spigot Plugin Development' started by HockeyFreak062, Jul 12, 2021.

  1. I'm trying to make an own auto broadcaster for our discord server. (I know, there are plugins already that do this stuff) But when i activate the plugin it first sends the message 1 time, then 2 then 3 etc etc. How can i fix this? This is my code:

    Code (Java):

    public static int i = 0;

    public static void onMessage() {

        new BukkitRunnable() {
            @Override
            public void run() {
                if (i == 0) {
                    Bukkit.getServer().broadcastMessage(Main.cc("&fHave you joined our discord already?"));
                    Bukkit.getServer().broadcastMessage(Main.cc("&9https://discord.gg/j3vvtayA"));
                    i++;
                    onMessage();
                    return;
                } else if (i == 1) {
                    Bukkit.getServer().broadcastMessage(Main.cc("&fHave you joined our discord already?"));
                    Bukkit.getServer().broadcastMessage(Main.cc("&9DISCORD LINK"));
                    i = 0;
                    onMessage();
                    return;
                }
            }
        }.runTaskTimer(Main.main, 20 * 5, 20 * 5);
    }
     

  2. Hi,

    in each period you start a new timer. That's why there appears the same text multiple times.

    I don't know what's your goal but try to remove these onMessage(); because the timer is already doing it himself.
     
    • Useful Useful x 1
  3. This was the problem.... Thanks!
     
  4. No problem!