Adding a Pause

Discussion in 'Plugin Development' started by CodeAX2, Apr 29, 2017.

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

    CodeAX2

    Hello there! In my plugin, I am wanting to add a pause in a section of code. To clarify, I want the code to run until it hits the point where the code is paused, then it will wait the duration of time set by the pause, then continue the code after the pause is done. I know a little about the bukkit scheduler, but not a whole lot, so my idea would be to make a a method that takes an int to set the delay. Any ideas on how I would do this? Thanks!
     
  2. Offline

    Sethburster

  3. Offline

    CodeAX2

    I somewhat have it working, however, a few things aren't exactly how i want them to be. I am able to schedule a task that runs, say 5 seconds, after the method is called. However, the next line of code after the call to the method runs immediately, rather than 5 seconds later. Here is my code:

    Code:
        @EventHandler
        public void rightClickEvent(PlayerInteractEvent e) {
            Action a = e.getAction();
            Material b = e.getClickedBlock().getType();
            Player player = e.getPlayer();
            timer(100); //Method I created that is simply a lone scheduler.
            //I want this line and all following ones to run after 5 seconds, instead of immediately after the timer method.
            if (a == Action.RIGHT_CLICK_BLOCK || a == Action.LEFT_CLICK_BLOCK) {
                if (b == Material.WALL_SIGN || b == Material.SIGN || b == Material.SIGN_POST) {
                    Sign sign = (Sign) e.getClickedBlock().getState();
                    if (sign.getLine(0)
                            .equals(ChatColor.AQUA + "[" + ChatColor.GREEN + "Egg Wars" + ChatColor.AQUA + "]")) {
                        if (sign.getLine(2).equals(ChatColor.AQUA + "" + ChatColor.BOLD + "Join!")) {
                            if (p > totalPlayers) {
                                playerJoinLobby(player);
                                p++;
                                sign.setLine(1, ChatColor.AQUA + "" + p + ChatColor.GREEN + "/" + ChatColor.AQUA
                                        + ChatColor.BOLD + "" + totalPlayers);
                            } else {
                                player.sendMessage(title + ChatColor.RED + "Game is full!");
                            }
                        }
                    }
                }
            }
        }
    Any ideas on how I could pause my code until the 5 seconds are over? Side note: I want to be able to use the timer(int) method in multiple places, possible at the same time.
     
  4. Offline

    Sethburster

    I don't think you really need the timer method. I would just put all of the code after the timer method call in the run method of your scheduler. Just use the scheduler to create a new delayed task where your timer(100); is and put all of the rest of your code in there.


    Sent from my iPhone using Tapatalk
     
  5. Offline

    CodeAX2

    The reason I want the timer method is so I can use it to very easily create pauses whenever I need to, without having to retype the entire scheduler, and paste all the code into there, with possible even having schedulers inside of schedulers. I also want to be able to create countdowns, which I know there is a way to do, but it is fairly complicated. With the timer method, I could simple create a for loop for the countdown. So, is there any possible way that I could create the timer method? Perhaps by using while loops and booleans or something like that?
     
  6. Offline

    timtower Administrator Administrator Moderator

    @CodeAX2 While loops and Thread.sleep will freeze the server.
    Timers are the only way to do this reliable.
     
Thread Status:
Not open for further replies.

Share This Page