Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

how do i loop an if statement on a timer in java?

so im trying to make a minecraft plugin that sends a string of text in chat on random intervals. now im trying to make it start from line 1 every 5 minutes. i just dont know how to do this Ps: im new to java.

Random rand = new Random();
int n = rand.nextInt(3);

if (n == 1) {
    Bukkit.broadcastMessage("yeet");
}

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

A quick search should lead you to this:
https://bukkit.org/threads/creating-a-loop.119088/
https://www.codegrepper.com/code-examples/java/bukkit+repeating+task+every+minute+spigot

The code, without testing it:

Bukkit.getScheduler().scheduleSyncRepeatingTask(plugin, new Runnable() {
    @Override
    public void run() {
        Bukkit.broadcastMessage("This message is shown immediately and then repeated every second");
    }
}, 0L, 20L); //0 Tick initial delay, 20 Tick (1 Second) between repeats

It seems like in Minecraft, 20 Ticks equals to one second. So you should calculate 20 x 60 x 5 to get the amount of ticks needed for 5 minutes.

Try 6000.

I also recommend to you to read the Javadoc of the Bukkit project.

https://hub.spigotmc.org/javadocs/bukkit/org/bukkit/scheduler/BukkitScheduler.html#scheduleSyncRepeatingTask(org.bukkit.plugin.Plugin,org.bukkit.scheduler.BukkitRunnable,long,long)

This should lead you to the current function that is used.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading