Solved Countdown

Discussion in 'Plugin Development' started by minekam20, May 20, 2014.

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

    minekam20

    Okay so i'm making a warp plugin so when i type /warp [name] it would start a countdown to tp.When the countdown its finished it tp me to the warp location. I made a config.yml file to change the countdown time.I'm really bad with countdowns and i need help to get my plugin finished. :)


    Code:
    Code:
    public class Warp extends JavaPlugin
      implements Listener
    {
        public Main plugin;
      SettingsManager settings = SettingsManager.getInstance();
     
      public Warp(Main instance) {
        this.plugin = instance;
      }
      List<String> Tp = new ArrayList<String>();
      List<String> tp = new ArrayList<String>();
      private int task;
      private int countdown = this.settings.getConfig().getInt("cooldown");
      private boolean running = false;
     
     
      public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args)
      {
        if (!(sender instanceof Player)) {
          sender.sendMessage(ChatColor.RED + "This plugin is for players only!");
          return true;
        }
        final Player p = (Player)sender;
        if(cmd.getName().equalsIgnoreCase("warp")){
            if (args.length == 0) {
        if(p.hasPermission("EasyWarp.Warp")){
       
              p.sendMessage(ChatColor.RED + "Please specify a name!");
              showWarps(p);
       
          }
          }
            if (!this.running)
            {
              if (sender.hasPermission("warp")) {
           
                this.Tp.add(p.getName());
     
                if (this.Tp.contains(p.getName())) {
                  this.running = true;
                  this.task = Bukkit.getScheduler().scheduleSyncDelayedTask(this, new Runnable() {
                    public void run() {
               
                        p.sendMessage(ChatColor.RED + "Teleporting to " + ChatColor.WHITE + "Spawn" + ChatColor.RED + " in " + countdown + " seconds.");
                 
     
                      if (countdown == 1)
                      {
                        p.sendMessage(ChatColor.RED + "Teleporting to " + ChatColor.WHITE + "Spawn" + ChatColor.RED + " in 1 second.");
                      }
     
                      if (countdown == 0)
                      {
                 
                        p.sendMessage(ChatColor.RED + "Teleported to " + ChatColor.WHITE + "Spawn.");
                        Tp.remove(p.getName());
                        Bukkit.getScheduler().cancelTask(task);
                        running = false;
                 
                      }
                      countdown -= 1;
                    }
                  }
                  , countdown);
                }
              }
     
            }
            else
            {
              sender.sendMessage("The countdown is already in progress");
         
             
              }
             
              }
        return running;
          }
     
        private void showWarps(Player p)
          {
         
              if ((!p.hasPermission("ez.warplist")) && (p.hasPermission("ez.*"))) {
                  return;
                 
                  }
         
            if (this.settings.getData().contains("warps"))
            {
              Set<?> warps = this.settings.getData().getConfigurationSection("warps.").getKeys(false);
              String[] warpList = (String[])warps.toArray(new String[warps.size()]);
              p.sendMessage(ChatColor.GOLD + "Warps: " + ChatColor.GRAY + StringUtils.join(warpList, ',', 0, warpList.length));
            }
           
       
            else {
              p.sendMessage(ChatColor.translateAlternateColorCodes('&', "&6Warps: &7") + "[]");
            }
     
       
       
           
            }
    }
     
  2. Offline

    Trevor1134

    minekam20 schedule aSyncRepeatingTask rather than delayed, and count down in that, and once it is 1, cancel the task, you can do this by assagning an int to the task (task = Bukkit.getScheduler()....) and then creating a private method to cancel the task, and run it when you actually want to stop the count.
     
  3. Offline

    minekam20

    Trevor1134 I can't seem to even make the scheduler i get errors on it
     
  4. Offline

    Trevor1134

    minekam20 And the errors are...? Also, please don't tag and quote me, as it notifies me twice.
     
  5. Offline

    minekam20

    Sorry,I get errors at }countdown);

    Code:
      task = Bukkit.getScheduler().scheduleSyncDelayedTask(this, new Runnable() {
                   
                   
                    public void run() {
                   
                        p.sendMessage("test");
                       
                           
                        }
                    }
                }countdown);
     
  6. Offline

    Trevor1134

    minekam20 hover over it(assuming your on eclipse) and tell me the error it says.
     
  7. Offline

    minekam20


    syntax error on token "}",, excepted
     
  8. Offline

    Sabersamus

    you have one too many }, delete one
     
  9. Offline

    minekam20

    That fix that but now i at the end of "}" i get syntax error,insert "}" to complete methodbody & on cooldown says delete this token

    I still have 2 more errors "cooldown" delete token.... and the last } says sintax error,insert "}" to complete method body

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

    Sabersamus

    maybe you're missing your class ending }, id need to see the whole class
     
  11. Offline

    minekam20


    Code:java
    1. public class Warping extends JavaPlugin
    2. implements Listener
    3. {
    4. public Main plugin;
    5. SettingsManager settings = SettingsManager.getInstance();
    6.  
    7. public Warping(Main instance) {
    8. this.plugin = instance;
    9. }
    10. List<String> Tp = new ArrayList<String>();
    11. List<String> tp = new ArrayList<String>();
    12. private int task;
    13. private int countdown = this.settings.getConfig().getInt("cooldown");
    14. private boolean running = false;
    15.  
    16.  
    17. public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args)
    18. {
    19. if (!(sender instanceof Player)) {
    20. sender.sendMessage(ChatColor.RED + "This plugin is for players only!");
    21. return true;
    22. }
    23. final Player p = (Player)sender;
    24. if(cmd.getName().equalsIgnoreCase("debug")){
    25. if (args.length == 0) {
    26. if(p.hasPermission("EasyWarp.Warp")){
    27.  
    28. p.sendMessage(ChatColor.RED + "Please specify a name!");
    29. showWarps(p);
    30.  
    31. }
    32. }
    33. if (!this.running)
    34. {
    35. if (sender.hasPermission("spawn")) {
    36.  
    37. this.Tp.add(p.getName());
    38.  
    39. task = Bukkit.getScheduler().scheduleSyncDelayedTask(this, new Runnable() {
    40.  
    41.  
    42. public void run() {
    43.  
    44. p.sendMessage("test");
    45.  
    46.  
    47. }
    48. }countdown);
    49.  
    50.  
    51.  
    52.  
    53.  
    54.  
    55.  
    56.  
    57.  
    58.  
    59. private void showWarps(Player p)
    60. {
    61.  
    62. if ((!p.hasPermission("ez.warplist")) && (p.hasPermission("ez.*"))) {
    63. return;
    64.  
    65. }
    66.  
    67. if (this.settings.getData().contains("warps"))
    68. {
    69. Set<?> warps = this.settings.getData().getConfigurationSection("warps.").getKeys(false);
    70. String[] warpList = (String[])warps.toArray(new String[warps.size()]);
    71. p.sendMessage(ChatColor.GOLD + "Warps: " + ChatColor.GRAY + StringUtils.join(warpList, ',', 0, warpList.length));
    72. }
    73.  
    74.  
    75. else {
    76. p.sendMessage(ChatColor.translateAlternateColorCodes('&', "&6Warps: &7") + "[]");
    77. }
    78.  
    79.  
    80.  
    81.  
    82. }
    83. }
     
  12. Offline

    Sabersamus

    Code:
    public class Warping extends JavaPlugin implements Listener {
        public Main plugin;
        SettingsManager settings = SettingsManager.getInstance();
    
        public Warping(Main instance) {
            this.plugin = instance;
        }
    
        List<String> Tp = new ArrayList<String>();
        List<String> tp = new ArrayList<String>();
        private int task;
        private int countdown = this.settings.getConfig().getInt("cooldown");
        private boolean running = false;
    
        public boolean onCommand(CommandSender sender, Command cmd,
                String commandLabel, String[] args) {
            if (!(sender instanceof Player)) {
                sender.sendMessage(ChatColor.RED
                        + "This plugin is for players only!");
                return true;
            }
            final Player p = (Player) sender;
            if (cmd.getName().equalsIgnoreCase("debug")) {
                if (args.length == 0) {
                    if (p.hasPermission("EasyWarp.Warp")) {
    
                        p.sendMessage(ChatColor.RED + "Please specify a name!");
                        showWarps(p);
    
                    }
                }
                if (!this.running) {
                    if (sender.hasPermission("spawn")) {
    
                        this.Tp.add(p.getName());
    
                        task = Bukkit.getScheduler().scheduleSyncDelayedTask(this,
                                new Runnable() {
                                    public void run() {
                                        p.sendMessage("test");
                                    }
                                }, countdown);
                    }//missing
                }//missing
            }//missing
        }//missing? i dont remember 
    
        private void showWarps(Player p) {
    
            if ((!p.hasPermission("ez.warplist")) && (p.hasPermission("ez.*"))) {
                return;
    
            }
    
            if (this.settings.getData().contains("warps")) {
                Set<?> warps = this.settings.getData()
                        .getConfigurationSection("warps.").getKeys(false);
                String[] warpList = (String[]) warps.toArray(new String[warps
                        .size()]);
                p.sendMessage(ChatColor.GOLD + "Warps: " + ChatColor.GRAY
                        + StringUtils.join(warpList, ',', 0, warpList.length));
            }
    
            else {
                p.sendMessage(ChatColor.translateAlternateColorCodes('&',
                        "&6Warps: &7") + "[]");
            }
    
        }
    }

    some format might be messed up, but you were missing about 3 "}", after the }, countdown);
     
  13. Offline

    Garris0n

    Code:
        List<String> Tp = new ArrayList<String>();
        List<String> tp = new ArrayList<String>();
    
    Don't just violate the naming conventions so you can kind-of have two lists with the same name. Name them more specifically. If you are not familiar with the conventions, read this.

    Also, that was just about the worst way to implement a self-cancelling task. Here is a good way to do it if you want to use anonymous classes.
     
    minekam20 and Sabersamus like this.
  14. Offline

    minekam20


    Code:java
    1. The method runTaskTimer(Plugin, long, long) in the type BukkitRunnable is not applicable for the arguments (Warp, int, int)
     
  15. Offline

    Garris0n

  16. Offline

    minekam20


    Code:java
    1. package me.minekam.easywarp.events;
    2.  
    3. import java.util.ArrayList;
    4. import java.util.List;
    5.  
    6. import me.minekam.easywarp.Main;
    7. import me.minekam.easywarp.SettingsManager;
    8.  
    9. import org.bukkit.ChatColor;
    10. import org.bukkit.command.CommandExecutor;
    11. import org.bukkit.entity.Player;
    12. import org.bukkit.event.Listener;
    13. import org.bukkit.plugin.Plugin;
    14. import org.bukkit.scheduler.BukkitRunnable;
    15.  
    16. public class Warp {
    17. public Main plugin;
    18. SettingsManager settings = SettingsManager.getInstance();
    19.  
    20. public Warp(Main instance) {
    21. this.plugin = instance;
    22. }
    23. private int time = this.settings.getConfig().getInt("cooldown");
    24.  
    25.  
    26.  
    27.  
    28.  
    29. public void countdown(final Player player){ //A method
    30.  
    31. new BukkitRunnable(){ //BukkitRunnable, not Runnable
    32.  
    33. int countdown = time; //Instance variable in our anonymous class to easily hold the countdown value
    34.  
    35. @Override
    36. public void run(){
    37.  
    38. if(countdown <= 0 || !player.isOnline()){ //countdown is over or player left the server, just two example reasons to exit
    39.  
    40. this.cancel(); //cancel the repeating task
    41. return; //exit the method
    42.  
    43. }
    44.  
    45. player.sendMessage(ChatColor.RED + "There are " + countdown + " seconds until timtower gets moderator powers."); //Example usage
    46. countdown--; //decrement
    47.  
    48. }
    49.  
    50.  
    51. }.runTaskTimer(this, 0, 20); //Repeating task with 0 ticks initial delay, run once per 20 ticks (one second)
    52.  
    53. }
    54.  
    55.  
    56.  
    57.  
    58. }
    59.  

    Then i get
    1. The method runTaskTimer(Plugin, long, long) in the type BukkitRunnable is not applicable for the arguments (Warp, int, int)
    at runtasktimer
     
  17. Offline

    xTigerRebornx

    minekam20 Simply change 'this' to a valid instance of your plugin?
     
    minekam20 and Garris0n like this.
  18. Offline

    Garris0n

    Read what it says. It wants "Plugin, long, long". Your main class will, at some point up the hierarchy, implement Plugin.
     
    minekam20 and xTigerRebornx like this.
  19. Offline

    minekam20

    It worked thanks but how do i get Args on the cooldown i want that when cooldown its done it tps the player to the location of the warp.
     
  20. Offline

    Garris0n

    Store it in a variable (which will have to be final) and then just access it from the anonymous class.
     
  21. Offline

    minekam20

    I'm doing them finals but i can't it won't work
     
  22. Offline

    Garris0n

    Code?
     
  23. Offline

    minekam20

    This is at the command
    Code:java
    1.  
    2. final World w = Bukkit.getServer().getWorld(this.settings.getData().getString("warps." + args[0] + ".world"));
    3. final double x = this.settings.getData().getDouble("warps." + args[0] + ".x");
    4. final double y = this.settings.getData().getDouble("warps." + args[0] + ".y");
    5. final double z = this.settings.getData().getDouble("warps." + args[0] + ".z");
    6. p.teleport(new Location(w, x, y, z));
    7. p.sendMessage(ChatColor.GREEN + "Teleported to " + args[0] + "!");
    8.  



    And the cooldown:
    Code:java
    1. public void countdown(final Player player){ //A method
    2.  
    3. new BukkitRunnable(){ //BukkitRunnable, not Runnable
    4.  
    5. int countdown = time; //Instance variable in our anonymous class to easily hold the countdown value
    6.  
    7. @Override
    8. public void run(){
    9.  
    10. if(countdown <= 0 || !player.isOnline()){ //countdown is over or player left the server, just two example reasons to exit
    11.  
    12. this.cancel(); //cancel the repeating task
    13. return; //exit the method
    14.  
    15. }
    16.  
    17. player.sendMessage(ChatColor.RED + "There are " + countdown + " seconds until timtower gets moderator powers."); //Example usage
    18.  
    19.  
    20.  
    21. countdown--; //decrement
    22.  
    23. player.teleport(new Location(w, x, y, z));
    24.  
    25. }
    26.  
    27.  
    28. }.runTaskTimer(plugin, 1, 20); //Repeating task with 0 ticks initial delay, run once per 20 ticks (one second)
    29.  
    30.  
    31. }
    32.  



    I can't teleport says at w, y, x, z Cannot resolve variable
     
  24. Offline

    Garris0n

    The countdown method has no context of those variables because they're somewhere else.

    Also, you would have to teleport the player when the time runs out, not every time the countdown ticks...
     
  25. Offline

    minekam20

    It was a test :p And its all in the same class, Do you got any like example of how i make it a variable that can have args and run in runnable?
     
  26. Offline

    Garris0n

    What do you mean? Just pass it the location through the method or something, it's not rocket science...
     
  27. Offline

    minekam20

    I don't know how to put a location in a method cause it has args so idk
     
  28. Offline

    Gater12

    minekam20
    Not quite sure what you have a problem on...?

    You make it so the methods accept those parameters...
     
  29. Offline

    minekam20

    Yea but how lol i have no idea i'm lost
     
  30. Offline

    Gater12

    minekam20
    Umm... You make it so the methods accept those parameters.
    Code:java
    1. public void foobar(String foo, String bar){
    2. /* Method foobar needs two parameters to invoke it */
    3. }
     
Thread Status:
Not open for further replies.

Share This Page