Want to change BossBar every 3 seconds, doesn't work :c

Hi!

I want to change the text of my BossBar every 3 seconds. This is my class called Bossbar.java:

public class Bossbar {
public static Integer i = 0;

public static ServerBossBar bar = ServerBossBar.builder()
        .color(BossBarColors.GREEN)
        .name(Text.of("Evil Boss Bar rawr!"))
        .overlay(BossBarOverlays.PROGRESS)
        .percent(0.75f)
        .build();


public static List<String> textPool = new ArrayList<>(Arrays.asList("Heute schon gevoted? /vote", "Komm auf unseren Discord! /discord", "Diskutiere mit anderen! /forum", "Mache dich mit den Regeln vertraut! /rules", "Gehe in einen Chat-Channel: /channel"));


public static void bossbarSchedule() {
    
    Random rand = new Random();
    
    Task.Builder taskBuilder = Task.builder();
    taskBuilder.execute(() -> {
        
        Integer randInt = rand.nextInt(textPool.size() - 1);
        i++;
        System.out.println(i);
        switch (i) {
            case 0:
                bar.setName(Text.of(textPool.get(randInt)));
                bar.setPercent(1f);
            case 1:
                bar.setPercent(0.75f);
            case 2:
                bar.setPercent(0.5f);
            case 3:
                bar.setPercent(0.25f);
            case 4:
                bar.setPercent(0f);
                i = 0;
        }
    }).delay(3, TimeUnit.SECONDS).submit(Sponge.getPluginManager().getPlugin("modhelper").get().getInstance().get());
}


}

I call it like this in the GameServerStartedEvent:
Bossbar.bossbarSchedule();

And it is applied to the players like this ( ClientConnectionEvent.Join)
Bossbar.bar.addPlayer(player);

It is applied correctly, but it doesnt change, like intended:
Capture

Any help is appreciated!

Replace delay with interval. Delay is to delay, interval is to repeat. :slight_smile:

2 Likes

Thank you so much!

Still doesn’t work :c

First add break statements to your switch-case (or don’t if you know what you are doing). Then rethink your logic about what values i is at which time. It seems to be impossible that i is 0 when the switch-case is entered due to the i++.

iirc the max for rand.nextInt is excluseive. that’d
make randInt = rnd.nextInt(textPool.size());

Also why not set i to 4, decreasing int and setting progress to i*0.25? would allow you to change text if i==0 and make the switch redundant.

Thanks! That worked!