Tempban-Plugin hands out wrong time milliseconds

Discussion in 'Plugin Development' started by PlayWolfYT, Jan 3, 2019.

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

    PlayWolfYT

    Hello to everyone!

    I'm making a Tempban-Plugin but somehow when I want the Milliseconds of when the ban expires my plugin just gives me something else, but idk why.

    Here's the Config.yml where I get the 4th banreason from:
    Code:
    
    #timeusage
    # Y -> Year
    # M -> Month
    # w -> Weeks
    # d -> Days
    # h -> Hours
    # m -> Minutes
    # s -> Seconds
    
    BanReasons:
      1:
        Name: "Hacking"
        BanType: "Ban"
        Time: "Permanent"
      2:
        Name: "Advertisement - Bot"
        BanType: "Ban"
        Time: "30d 6h"
      3:
        Name: "Advertisement - Player"
        BanType: "Mute"
        Time: "1w 2d"
      4:
        Name: "Year"
        BanType: Ban
        Time: "1Y 6M 2w 5d 9h 15m 9s"
    
    
    And here's the code for the calculation I do:

    Code:
        private static long getExpireDate(String string) {
            long timemillis = 0;
            String[] array = string.split(" ");
          
            for(String s : array) {
                if(s.endsWith("Y")) {
                    System.err.println(s.replace("Y", "") + " Years");
                    timemillis += (Long.parseLong(s.replaceAll("Y", "")) * 12 * 4 * 7 * 24 * 60 * 60 * 1000);
                } else if(s.endsWith("M")) {
                    System.err.println(s.replace("M", "") + " Months");
                    timemillis += (Long.parseLong(s.replaceAll("M", "")) * 4 * 7 * 24 * 60 * 60 * 1000);
                } else if(s.endsWith("w")) {
                    System.err.println(s.replace("w", "") + " Weeks");
                    timemillis += (Long.parseLong(s.replaceAll("w", "")) * 7 * 24 * 60 * 60 * 1000);
                } else if(s.endsWith("d")) {
                    System.err.println(s.replace("d", "") + " Days");
                    timemillis += (Long.parseLong(s.replaceAll("d", "")) * 24 * 60 * 60 * 1000);
                } else if(s.endsWith("h")) {
                    System.err.println(s.replace("h", "") + " Hours");
                    timemillis += (Long.parseLong(s.replaceAll("h", "")) * 60 * 60 * 1000);
                } else if(s.endsWith("m")) {
                    System.err.println(s.replace("m", "") + " Minutes");
                    timemillis += (Long.parseLong(s.replaceAll("m", "")) * 60 * 1000);
                } else if(s.endsWith("s")) {
                    System.err.println(s.replace("s", "") + " Seconds");
                    timemillis += (Long.parseLong(s.replaceAll("s", "")) * 1000);
                }
            }
          
          
          
            long current = System.currentTimeMillis();
            return current + timemillis;
        }
    
    Thanks for any help!
     
    Last edited: Jan 4, 2019
  2. Offline

    Zombie_Striker

    @PlayWolfYT
    One of the issues may be that you are parsing using Integer instead of Long. Because of this, when you do the multiplications, it is still treating the number as though it is an int, and you may be creating a stack overflow.

    Also:
    Its labeled "Year", yet it looks like the bantime is for over a year and a half.
    Was this what you wanted?
     
  3. Offline

    PlayWolfYT

    @Zombie_Striker
    Actually I saw that I should use a Long instead of an Integer and fixed this in my actual code, but I still get a wrong output.
    And yes it was intended to have a bantime of more than one and a half years, i just put the name "Year" in it for testing reasons. (Now that I'm thinking of it was a stupid idea xD)
     
Thread Status:
Not open for further replies.

Share This Page