0

I am making a minecraft mini game plugin, i need to make a loop that do the minigame, if the game is ended, stop it, but else continue, for making that i just created a boolean that is false and i put a :

while(isEnded) {
    //my code
}

But in my code, there is async fuction, so it's repeat, but the async function don't have the time to finish before an other loop start, so all my game is glitched. Any solution to await the async function ? ( i am using Bukkit.getServer().getScheduler().runTaskTimer(Main.plugin, new Runnable() { )

thanks for the help ;)

Elikill58
  • 4,050
  • 24
  • 23
  • 45
Sysy's
  • 67
  • 3
  • I don't understand, can you show more code/ change how you explain your issue ? – Elikill58 Feb 15 '22 at 17:34
  • I am making async method to wait before do things and for don't lock the game, so the other part of the code continue, so, when i do a loop to repeat this part of the game ( need to be repeat all time to the end ), it's just spam the function before the end, so i need to make an await function, but i can't find how in java – Sysy's Feb 15 '22 at 17:38

1 Answers1

2

I don't really understand where you are stuck, but I will give you some way to do what you are looking for.

  1. Run method from the end of mini-games.

For example, when the game when, you are running a method:

public void endGame() {
   // do something
   callMethod();
}
  1. Use for another variable.

You can just set a variable, then run a task like that :

public static boolean isEnd = false;

public void runTask() {
   Bukkit.getScheduler().runTaskTimer(myPlugin, () -> {
       if(isEnd) {
           // do something
       }
   }, 20, 20);
}

Finally, set the variable when it's fine with just MyClass.isEnd = true;

It will run each 20 ticks (so each second, because 20 ticks = 1 second).

  1. If you know the time to wait, you can use the same scheduler as you are using and as I explain in #2 option.
Elikill58
  • 4,050
  • 24
  • 23
  • 45