Waiting & Minigames?!?!

Discussion in 'Plugin Development' started by rangersmash, Dec 28, 2013.

Thread Status:
Not open for further replies.
  1. Offline

    rangersmash

    Hi. So I have this Runnable (run()) code. However, when it executes Thread.sleep() it makes the whole server wait. So how would I wait 5 seconds without making the whole server do this?

    Code:
                @Override
                public void run() {
                    //Countdown
                    int fmTicks = 5*1000;
                    int countdownTime = main.getConfig().getInt("Settings.CountdownTime");
                    for(int x= countdownTime ; x > 0 ; x = x - 1) {
                        main.msg.sendPlayersMsg(playerArray, ChatColor.GREEN + "Game beginning in " + x + "...");
                        try {
                            Thread.sleep(1000);
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    }
                    // Begin!
                    main.gearHandle.clearInventory(playerArray);
                    main.gearHandle.giveGear(playerArray, 1); // Round 1
                    main.msg.sendPlayersMsg(playerArray, ChatColor.GREEN + "Round 1 has begun!");
                    try {
                        Thread.sleep(fmTicks);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    main.gearHandle.giveGear(playerArray, 2); //
                    main.msg.sendPlayersMsg(playerArray, ChatColor.GREEN + "Round 2 has begun!");
                }

    Any help would be appreciated.

    Thanks
    -rangersmash
     
  2. Offline

    Gater12

    rangersmash Yeah.... Sleeping the main thread is not a good idea....
    Instead use your friendly neighborhood scheduleSyncDelayedTask!
     
    Ivan likes this.
  3. Offline

    Ivan

  4. Offline

    rangersmash

    I've read that but I can't seem to get the hang of it? The code I have is actually a SchedleSyncDelayedTask. I fire it with -
    Code:java
    1. Bukkit.getScheduler().scheduleSyncRepeatingTask(main, main.scheduler.createGameRunnable(main.players, main.blocks), 0, -1);



    Full code -
    Code:java
    1. public Runnable createGameRunnable(final ArrayList<String> playerArray, ArrayList<Block> spawns) {
    2.  
    3. return new Runnable() {
    4.  
    5. @Override
    6. public void run() {
    7. //Countdown
    8. int fmTicks = 5*1000;
    9. int countdownTime = main.getConfig().getInt("Settings.CountdownTime");
    10. for(int x= countdownTime ; x > 0 ; x = x - 1) {
    11. main.msg.sendPlayersMsg(playerArray, ChatColor.GREEN + "Game beginning in " + x + "...");
    12. try {
    13. Thread.sleep(1000);
    14. } catch (InterruptedException e) {
    15. e.printStackTrace();
    16. }
    17. }
    18. // Begin!
    19. main.gearHandle.clearInventory(playerArray);
    20. main.gearHandle.giveGear(playerArray, 1); // Round 1
    21. main.msg.sendPlayersMsg(playerArray, ChatColor.GREEN + "Round 1 has begun!");
    22. try {
    23. Thread.sleep(fmTicks);
    24. } catch (InterruptedException e) {
    25. e.printStackTrace();
    26. }
    27. main.gearHandle.giveGear(playerArray, 2); //
    28. main.msg.sendPlayersMsg(playerArray, ChatColor.GREEN + "Round 2 has begun!");
    29. }
    30.  
    31. };
    32.  
    33. }



    Any help? Ivan Gater12

    Or do I not need to use a scheduler to do a minigame sort of thing, similar to hunger games??? I'm rather confused on how to do gamemodes involving several players and have countdowns and rounds in :/

    Going sleep now, any help is appreciated on how to do mini games with lobbies and countdowns.

    Bump? :(

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 6, 2016
  5. Offline

    TomTheDeveloper

  6. Offline

    rangersmash

    Thank you very much. I will have a good read through :)!
     
Thread Status:
Not open for further replies.

Share This Page