Timer manipulation

Discussion in 'Plugin Development' started by girardcome, Jun 19, 2019.

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

    girardcome

    Hi, here's the problem I'm having today.

    What I want: A countdown that lasts 10 minutes, and display as a percentage, for example: "10 minutes = 100%", "9min59 seconds = 99.9%", "9min58 = 99.8%"

    What I did:
    PHP:
        int time 600;
        
    int taskID;
        
    int generationTime;

        private 
    void setTimer() throws InterruptedException {
            
    taskID Bukkit.getServer().getScheduler().scheduleSyncRepeatingTask(Main.getInstance(), new Runnable() {

                @
    Override
                
    public void run() {
                    if(
    time == 600generationTime 100;
                    if(
    time == 599generationTime 99.9;
                    if(
    time == 598generationTime 99,8;
                    if(
    time == 597generationTime 99,7;
    Bukkit.broadcastMessage("Generation time: " generationTime "%");
                    if (
    time == 0) {
                        
    time--;
                        
    Bukkit.getScheduler().cancelTask(taskID);
                    }
                }

            }, 
    2020);
        }
    What I get: For the moment I'm getting a good result, but it's super long to do, it's pretty boring to measure each time and turn it into a second by second percentage.
     
  2. Offline

    timtower Moderator Moderator

  3. Offline

    girardcome

    PHP:
    taskID Bukkit.getServer().getScheduler().scheduleSyncRepeatingTask(Main.getInstance(), new Runnable() {

                @
    Override
                
    public void run() {
                    
    generationTime time*100;
                    if (
    time == 0) {
                        
    time--;
                        
    Bukkit.getScheduler().cancelTask(taskID);
                    }
                }

            }, 
    2020);
    When i'm returning generationTime it's giving me "60000"..
     
  4. Offline

    timtower Moderator Moderator

    @girardcome You are not dividing.
    time / 600 * 100
     
  5. Offline

    girardcome

    Okay it's working for a part, it's displyaing 100% that's cool, but how to the other tricks? like 99,9 when time = 9 minutes and 59 seconds ?

    and btw it's still displaying "100%" even if timer was started 3 minutes ago..

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
  6. Offline

    timtower Moderator Moderator

    @girardcome Yeah, that is because of this:
    Code:
     if (time == 0) {
    time--;
    Bukkit.getScheduler().cancelTask(taskID);
     }
    You are only decreasing the time when the time == 0.
     
  7. Offline

    girardcome

    Yes i just saw that, my bad

    That's what i did:
    PHP:
    taskID Bukkit.getServer().getScheduler().scheduleSyncRepeatingTask(Main.getInstance(), new Runnable() {

                @
    Override
                
    public void run() {
                    
    time--;
                    
    generationTime time 600 100;
                    if (
    time == 0) {
                       
                        
    Bukkit.getScheduler().cancelTask(taskID);
                    }
                }

            }, 
    2020);
    And now it's displaying only "0%"

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
  8. Offline

    timtower Moderator Moderator

    And where are you printing it then?
     
  9. Offline

    girardcome

    In a command class, so when a player types /status then i execute this line:
    PHP:
    p.sendMessage(ChatColor.RED "Time : " ChatColor.WHITE plugin.generationTime "%");
    PHP:
    public class Status implements CommandExecutor {

        
    Main plugin;

        public 
    Status(Main plugin) {
            
    this.plugin plugin;
            
    plugin.getCommand("status").setExecutor(this);
        }

        @
    Override
        
    public boolean onCommand(CommandSender senderCommand cmdString labelString[] args) {
            if (!(
    sender instanceof Player)) {
                
    sender.sendMessage("Only players may execute this command.");
                return 
    true;
            }
            
    Player p = (Playersender;
            if (
    plugin.getPlayersConfig().getStringList("Players." p.getName() + ".permissions.commands")
                    .
    contains("command_Status")) {
                if (
    args.length || args.length 1) {
                    
    p.sendMessage(ChatColor.translateAlternateColorCodes('&',
                            
    plugin.message("Messages""error""status_incorrect_syntax")));
                    return 
    true;
                } else if (
    args.length == 1) {
                    
    String result args[0];

                    
    Player player Bukkit.getPlayerExact(result);
                    if (
    player == null) {
                        
    p.sendMessage(ChatColor.translateAlternateColorCodes('&',
                                
    plugin.message("Messages""error""player_not_exist")));
                        return 
    true;
                    } else {
                        
    p.sendMessage(ChatColor.RED "Time : " ChatColor.WHITE plugin.generationTime "%");
                    }
                    return 
    false;
                }
            } else {
                
    p.sendMessage(plugin.message("Messages""error""unknown_command"));
            }

            return 
    false;
        }


    }
    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
  10. Offline

    timtower Moderator Moderator

    @girardcome You set the time, but do you also reset it to 600 again?
     
  11. Offline

    girardcome

    No, i didn't reset it
     
  12. Offline

    timtower Moderator Moderator

    In that case the time is still 0 when you start a new timer.
    Set it to 600 again when you start a timer.
     
  13. Offline

    girardcome

    The timer i wrote before, it's on "onEnable" method...
     
  14. Offline

    timtower Moderator Moderator

    Print the time as well in the runnable to see if you are fast enough to run the command.
     
  15. Offline

    girardcome

     

    Attached Files:

  16. Offline

    timtower Moderator Moderator

    Could you post the code where you start it?
     
  17. Offline

    girardcome

    Sure
    PHP:
        public int time 600;
        public 
    int taskID;
        public 
    int generationTime;
       
        @
    Override
        
    public void onEnable() {
            
    plugin this;

            
    taskID Bukkit.getServer().getScheduler().scheduleSyncRepeatingTask(Main.getInstance(), new Runnable() {

                @
    Override
                
    public void run() {
                   
                    
    generationTime time/600*100;
                   
                    
    Bukkit.broadcastMessage("Time: " generationTime);
                    if (
    time == 0) {
                      
                        
    Bukkit.getScheduler().cancelTask(taskID);
                    }
                    
    time--;
                }

            }, 
    2020);
           
        }
     
  18. Offline

    timtower Moderator Moderator

  19. Offline

    girardcome

  20. Offline

    timtower Moderator Moderator

    Then it might be rounding issues.
    Try this:
    Code:
    generationTime = (int) ((double)time/600*100);
    That casts the time to a double, then it applies the calculations (that will have double precision) then cast it back to int.
     
  21. Offline

    girardcome

    Okay, it's working a little bit.. it counting 10 minutes, but it's not writing "99,9%" then "99,98"
    upload_2019-6-19_14-36-15.png
     
  22. Offline

    timtower Moderator Moderator

    That image doesn't show that. Could you post your new code?
     
  23. Offline

    girardcome

    Sorry i missed up the image, the code works perfectly, but i forget something, i would like to start from 0 to 100% with this code :

    (I know that i must do time++; at Bukkit runnable, but how to count it now?)
    PHP:
        public int time 600;
        public 
    int taskID;
        public 
    int generationTime;
    PHP:
             taskID Bukkit.getServer().getScheduler().scheduleSyncRepeatingTask(Main.getInstance(), new Runnable() {

                    @
    Override
                    
    public void run() {
                      
                        
    generationTime = (int) ((double)time/600*100);
                     
                        if (
    time == 0) {
                         
                            
    Bukkit.getScheduler().cancelTask(taskID);
                        }
                        
    time--;
                    }

                }, 
    2020);
     
  24. Offline

    bwfcwalshy Retired Staff

    @girardcome Do this

    Code:java
    1. private final DecimalFormat df = new DecimalFormat("0.0#%");
    2.  
    3. // Task
    4. double generationTime = (double) time / 600 * 100;
    5.  
    6. Bukkit.broadcastMessage(df.format(generationTime));


    Edit: Missed what you now wanted to know. Counting up just do ++ until time == the time you need and then cancel.

    So, you'd need to have a global variable or store in config or something what you need to count to. Then just count up from a local variable and check.
     
  25. Offline

    girardcome

    I did this but doesn't work:

    PHP:
        public int time 600;
        public 
    int taskID;
        public 
    int generationTime;
        public 
    String timeProgress;
      
    public 
    void onEnable() {
          
    DecimalFormat df = new DecimalFormat("0.#");

             
    taskID Bukkit.getServer().getScheduler().scheduleSyncRepeatingTask(Main.getInstance(), new Runnable() {

                    @
    Override
                    
    public void run() {
                     

                          
    double generationTime = (double) time 600 100;
                          
    timeProgress df.format(generationTime);
                 
                        if (
    time == 600) {
                        
                            
    Bukkit.getScheduler().cancelTask(taskID);
                        }
                        
    time++;
                    }

                }, 
    2020);
    }
     
  26. Offline

    timtower Moderator Moderator

    @girardcome Hover your mouse over the red line. It is probably telling you to make df final.
    Do that or turn df into a private field.
     
Thread Status:
Not open for further replies.

Share This Page