Sysy's
Sysy's

Reputation: 67

(Spigot) How to await in java

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 ;)

Upvotes: 0

Views: 1067

Answers (1)

Elikill58
Elikill58

Reputation: 4908

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.

Upvotes: 2

Related Questions