Solved Tempban String with units to seconds

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

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

    PlayWolfYT

    Hello to everyone who reads this:

    I'm currently making a tempban plugin and I don't know how to proceed.
    My Problem is, that i receive a String (from a config) which looks something like this: "30d 10h..."

    But I don't know how to convert these "30d" to the requested seconds, because I dont know how to handle this with the chars of the String to replace everything correctly.

    This is what I'll get as String to convert
    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"
    
    Code:
    package de.diewolfbuddies.bansystem.managers;
    
    import org.bukkit.Bukkit;
    import org.bukkit.entity.Player;
    import org.bukkit.event.Listener;
    
    import de.diewolfbuddies.bansystem.BanSystem;
    import de.diewolfbuddies.bansystem.mysql.Settings;
    import de.diewolfbuddies.bansystem.utils.Reason;
    
    public class BanManager implements Listener {
     
        public BanManager(BanSystem bs) {
            Bukkit.getPluginManager().registerEvents(this, bs);
        }
     
        public static void banPlayer(Player p, Reason reason) {
            String reasonname = reason.getName();
         
            // If ban is permanent
            if(reason.getTime().equalsIgnoreCase("permanent")) {
                new Settings(p.getUniqueId()).banPlayer(reasonname, 0);
                return;
            }
    
            // Otherwise
            long endAt = getEndAtAsMillis(reason.getTime());
         
         
            new Settings(p.getUniqueId()).banPlayer(reasonname, endAt);
         
        }
     
    
    
        private static long getEndAtAsMillis(String s) {
         
            long current = System.currentTimeMillis() / 1000;
         
            long time = 0;
         
         
            return current + time;
        }
     
    }
    Thanks already to anyone who can help!
     
    Last edited: Jan 3, 2019
  2. Offline

    torpkev

    Take the string from time, split it on " " and then loop through. Check the last character of each entry, then convert the rest of the string to an integer (don't forget to use error handling) - then do the math

    So for example -
    "30d 6h"

    Split it on " " gives you

    Position 0: 30d
    Position 1: 6h

    the last character of first entry is d, the rest of it is 30

    There are 60 seconds in a minute, 3,600 seconds in an hour, 86,400 in a day, so let's do the math here.. 30 * 86,400 = 2,592,000 seconds in 30 days

    Then the next one.. last character is h, so it's hours, the rest of it is 6, so..

    6 * 3,600 = 21, 600

    add them together = 2,613,600 seconds

    Like I say, be careful of your error handling, if the user can change this stuff in the config, assume they'll enter all kinds of nonsense.
     
Thread Status:
Not open for further replies.

Share This Page