0

I need an integer per player. I created a countdown, but the integer is for all players. When I set a value, this applies to all players. Each player should get their own value.

Does anyone have an idea how to fix this?

My code:

private static final int COUNTDOWN_SECONDS = 6;
private int seconds;

public StartCountdown() {
    seconds = COUNTDOWN_SECONDS;
}

@Override
public void start(Player player) {

    taskID = Bukkit.getScheduler().scheduleSyncRepeatingTask(Challenge.getInstance(), () -> {

        switch(seconds) {
            case 5:
                player.sendTitle(ChatColor.DARK_RED + "5", "", 10, 20, 30);
                player.playSound(player.getLocation(), Sound.ENTITY_EXPERIENCE_ORB_PICKUP, 1, 1);
                break;
            case 4:
                player.sendTitle(ChatColor.RED + "4", "", 10, 20, 30);
                player.playSound(player.getLocation(), Sound.ENTITY_EXPERIENCE_ORB_PICKUP, 1, 1);
                break;
            case 3:
                player.sendTitle(ChatColor.GOLD + "3", "", 10, 20, 30);
                player.playSound(player.getLocation(), Sound.ENTITY_EXPERIENCE_ORB_PICKUP, 1, 1);
                break;

            case 2:
                player.sendTitle(ChatColor.YELLOW + "2", "", 10, 20, 30);
                player.playSound(player.getLocation(), Sound.ENTITY_EXPERIENCE_ORB_PICKUP, 1, 1);
                break;
            case 1:
                player.sendTitle(ChatColor.DARK_GREEN + "1", "", 10, 20, 30);
                player.playSound(player.getLocation(), Sound.ENTITY_EXPERIENCE_ORB_PICKUP, 1, 1);
                break;
            case 0:
                player.sendTitle(ChatColor.GREEN + "GO", "", 10, 20, 50);
                player.playSound(player.getLocation(), Sound.ENTITY_PLAYER_LEVELUP, 1, 1);
                seconds = COUNTDOWN_SECONDS;
                stop();
                break;

            default:
                break;
        }
        seconds--;
    }, 0, 20);
Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
Slyxx_
  • 31
  • 3

2 Answers2

2

You can create a timer for each players, but it's not the most optimized way and it doesn't seems to be what you are looking for.

The better solution for you seems to be the Map.

Do you this, you should change seconds to :

public HashMap<Player, Integer> seconds = new HashMap<>();

Then, you should put value in map at the begin, and manage value with map, like that:

@Override
public void start(Player player) {
   seconds.put(player, COUNTDOWN_SECONDS);
   taskID = Bukkit.getScheduler().scheduleSyncRepeatingTask(Challenge.getInstance(), () -> {
       int playerSecond = seconds.getOrDefault(player, 0); // get stored value
       if(playerSecond == 1) {
           // here do what you want
       }
       seconds.put(player, playerSecond - 1); // downgrade second value
   }, 20, 20);
}

PS: you will have the same problem with the taskId.

Elikill58
  • 4,050
  • 24
  • 23
  • 45
0

I would suggest doing what Elikill58 suggested in this answer: https://stackoverflow.com/a/71399135/12964643

However, you shouldn't need to store a copy of the taskID if you are just trying to cancel it after a certain number of iterations. Where you and Elikill58 have written:

() -> {
    // Code
}

It can be replaced with

task -> {
    // Code
}

This allows you to have an instance of the BukkitTask in order to cancel it.

KingsDev
  • 654
  • 5
  • 21