Making a Countdown Timer

Discussion in 'Plugin Development' started by Requadin, Aug 30, 2012.

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

    Requadin

    What I want to do is, when a player says /go a countdown timer which is set to 10 minutes starts. After 10 minutes the player is teleported to a specific coordinate.
    My question is how to make a timer and how to teleport a player.
    (I knew that on C # you're attaching a number value and when the trigger is sent, Time.deltaTime is decreased from that number. But I don't have any idea how to do that on Java, so i'm asking for the code, not the logic.)
    And i don't want p.performCommand("x"); for teleportation. I mean the players are not allowed to use /tp theirselves. The plugin should do that.
     
  2. Offline

    XbannisherX

    Countdown:
    this is a delayed scheduler, 20 ticks == 1 sec

    Code:
    this.getServer().getScheduler().scheduleSyncDelayedTask(this, new Runnable() {
     
      public void run() {
          YOURPLAYERVARIABLE.teleport(YOURTELEPORTLOCATION);
      }
    }, 60L);// 60 L == 3 sec, 20 ticks == 1 sec

    this should go into the command section
     
    SatproMC, Jomens235, wxwsk8er and 2 others like this.
  3. Offline

    Requadin

    Ok, that was my solution! But why do i add the teleport variables? I mean .teleport(x:100, y:50, z:100) like this?
    And am i adding the delay time after the code? Like this:

    Code:
    CODE{
    More code
    },60L);
     
  4. Offline

    XbannisherX

    do you know what variables are?

    the code you got , must be put in here:
    this.getServer().getScheduler().scheduleSyncDelayedTask(this, new Runnable() {

    public void run() {
    your code here
    }
    }, 60L);// 60 L == 3 sec, 20 ticks == 1 sec


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

    Requadin

    Actually, i'm going to add my "Players" arraylist as first variable but I'm not sure about the teleportation ones.
    And still not sure about that 60L. It's pretty weird. :eek:

    By the way, i think this register command should be added to onEnable part. However i want the countdown timer to start when a command is sent. How do i do that? I tried leaving register command withouth public void run but it gave some sort of errors.

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

    XbannisherX

    Code:
    public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args ){
    final Player player = (Player) sender; // here i define the player as the sender of the command
    if (cmd.getName().equalsIgnoreCase("yourcommand") && player.hasPermission("yourpermission.here")){//so if somebody does the command /yourcommand AND he got the permission for it
    final Location loc = player.getLocation()    ;
        this.getServer().getScheduler().scheduleSyncDelayedTask(this, new Runnable() {// the scheduler starts ticking, this is a delayed scheduler, which means : after a certain amount of time, he activates your code inside it
     
            public void run() {
            player.teleport(loc);
            }
            }, 12000L);// 60 L == 3 sec, 20 ticks == 1 sec
        //so, if a player does /yourcommand, he gets 3 sec before he gets teleported to the place where he is standing on
        //if you want some other place, you need to define it using a variable, a variable is a ''shortcut'' to the actual thing itself
        //if i want to teleport a player i can do this: player.teleport(player.getlocation);
        //but thats long and makes things complicated, so you use a variable.
        //if you want to use somthing in a scheduler, you need to make it final
        //60 L means: 3 sec, becaus 20 L means 1 sec 20*3=60
        //10 min == 12000 ticks
     
     
     
    }
    return false;
    }
    there ya go, it looks complicated but if you think logical its not.
    all the text with the // before it is a little explenation.
    dont mind my bad english :p

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 28, 2016
    BeatCycle and Requadin like this.
  7. Offline

    Requadin

    Ok, i think i understood very well! (Not a single problem about your english) Thank you so much, but I still have a question. You said use a shortcut and you got player's actual location as teleport destination. What if i just want a x,y,z coordinate? How can i do that?
     
  8. Offline

    XbannisherX

    just set the location you want your player to teleport to on the spot you want, like: a admin does /setlocationhere.
    (remember, variables, Location adminlocation = player.getLocation();
    if a player does /go
    player.teleport(adminlocation);
     
    Requadin likes this.
  9. Offline

    Requadin

    Ahh, noo! I'm not sure if you didn't understand me or i can't understand you. :D I just want to teleport the player a stable location. Just a x,y,z,. Not to an admin or something, just a stable location.
     
  10. Offline

    XbannisherX

    thought it was not possible , thats why i made that post ^^
    but i tinkered a bit with it and this is what i get
    Code:
    World w = player.getWorld();
    Location b = (Location) w.getBlockAt(1, 1, 1);
    player.teleport(b);
     
    Requadin likes this.
  11. Offline

    Requadin

    Oh, thank you so much for this! Thank you really much! If I get any errors, i'll tag you on this thread. :)
     
  12. Offline

    XbannisherX

    xD just helpin
     
  13. Offline

    Requadin

    @XbannisherX

    Ahh, I mixed everything :'( Actually what I want is this. I hope that you can help me with rebuilding that code.
    • Every player who types /ready is kept in Players list.
    • When a player types /ready player will check the amount of the Players ArrayList.
    • If that amount is equal to 10, every player in that Players list will be teleported to 10,10,10. And than a timer will start counting from 10 minutes.
    • When that timer reaches to 0, every player will be teleported to 50,50,50
    It shouldn't be that much hard but.. yeah, I'm not really good at Java. :oops: Here is my broken and uncompleted code:

    Code:
    public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args){       
     
            if(commandLabel.equalsIgnoreCase("ready")){
     
                Players.add(sender.getName());       
     
                sender.sendMessage("You're ready to join the game, please wait for the other players.");
     
           
     
            int playing = Players.size();
     
                if(playing == 10){
     
                    this.getServer().getScheduler().scheduleSyncDelayedTask(this, new Runnable() {
     
                       
     
                        public void run() {                       
     
                            Location b = (Location) w.getBlockAt(1, 1, 1);
     
                            player.teleport(b);    
    But, I have them in my Groups class. I register the codes on the main class so i can't use get.World and commands like this.
     
  14. Offline

    XbannisherX

    I dont know about the playerslist and arraylist, i have 0 knowledge on them :(
    but i altered your code a little, what it does now is: from the //here text, it teleports players to 10, 10, 10
    and after 10 min. it teleports them to 50, 50, 50

    Code:
    public boolean onCommand(final CommandSender sender, Command cmd, String commandLabel, String[] args){     
                if(commandLabel.equalsIgnoreCase("ready")){
                Player p = (Player)sender;
                Players.add(sender.getName()); 
                sender.sendMessage("You're ready to join the game, please wait for the other players.");
                }
                int playing = Players.size();
                if(playing == 10){
                    Player p = (Player)sender;
                    World w = p.getWorld();
                        Location b = (Location) w.getBlockAt(10, 10, 10);   
                        p.teleport(b);
                        this.getServer().getScheduler().scheduleSyncDelayedTask(this, new Runnable() {// the scheduler starts ticking, this is a delayed scheduler, which means : after a certain amount of time, he activates your code inside it
                           
                            public void run() {
                            Player p = (Player)sender;
                            World w = p.getWorld();
                            Location b = (Location) w.getBlockAt(50, 50, 50);   
                            p.teleport(b);
                            }
                            }, 12000L);
                }
                     
                   
                   
                   
                   
                   
                   
           
               
     
     
                 
               
               
               
                }
           
        }
     
  15. Offline

    comexpert

    Does
    Code:
    [/COLOR]
    [COLOR=#000000]Players.add(sender.getName());[/COLOR]
    [COLOR=#000000]

    work ?

    I prefer doing
    Code:
    [/COLOR]
    [COLOR=#000000]Players[Players.length]=sender.getName();[/COLOR]
    [COLOR=#000000]
    [/code][/COLOR]
     
  16. Offline

    kreashenz

    comexpert Come on, you bumped a year old post, and I'm pretty sure the code won't work anymore.
     
  17. Offline

    comexpert

    I'm sorry, I was reading this post, and I thought. Let's help them a bit.
     
  18. Offline

    xviniciox

    This can be more easy by typing this:
    Code:
    Location b = new Location(w, 1, 1, 1);
    Or...
    Location b = w.getBlockAt(1, 1, 1).getLocation();
     
  19. Offline

    ionre

    These TRAITORS!!! Have no idea how java works but think they're cool because they know c...
    (Just kidding, learning c myself right now)
     
Thread Status:
Not open for further replies.

Share This Page