args to hours, mins & seconds

Discussion in 'Spigot Plugin Development' started by Snowerfo, Aug 19, 2017.

  1. If one of my args in my command was <time>, how would I make it so if the player does this,

    /command 4h39m3s

    it would output

    hours = 4
    mins = 39
    seconds = 3

    but then if they did it in different orders like 39m4h3s it would output

    hours = 4
    mins = 39
    seconds = 3

    as well.

    Also, how could I make it so you dont have to put an hour, mins, or seconds,

    and I could just do /command 49m3s and it would output

    hours = 0
    mins = 49
    seconds = 3

    and I could also do stuff like just 5h

    hours = 5
    mins = 0
    seconds = 0

    thank you for your help :)
  2. You can use String#indexOf

    Code (Text):
    String args = "1h3s5m"
    int h = args[args.indexOf("h")-1]
    #2 Bimmr, Aug 19, 2017
    Last edited: Aug 20, 2017
  3. But doesnt that only take into account the first didget?

    What if I wanted 20m
  4. Then you do the same thing for the others
  5. But isnt the -1 just taking the 1 didget before?
  6. true(plus the code i gave you was wrong).

    My suggestion would be to still get the index of the letter, but then have a loop that goes thrrough and checks how far the number goes back.

    Code (Text):

    String args = "1h3s5m"

    int last = args.indexOf("h")
    int first;
    for(int i = last; i >= 0; i--){
        first = i;
    int h = args.substring(first, last);
    But you'd have to make the code to check if it's a number
    #6 Bimmr, Aug 19, 2017
    Last edited: Aug 19, 2017
  7. I reliased what I was talking about

    Code (Text):

    String time = args[1];
                    int h = time.indexOf("h")-1;
                    int m = time.indexOf("m")-1;
                    int s = time.indexOf("s")-1;
                    sender.sendMessage(h + " hours, " + m + " minutes, "+ s + " seconds.");
    However, this code for some reason doesnt work

    my args was 5h4m3s

    my message I got from it was : 0 hours, 2mins, 4secs. No idea why

    :/ @Bimmr
  8. The code I gave before would return the index not the value, you'd have to args[h], args[m], etc. Although as you said it would only work for single digits.

    the fact that you're allowing them to switch the hours,minutes, and seconds is what makes it a little tricky.
  9. Ok, how would I make it so its straight forward hours mins seconds, or should I seperate them so you would do it as '03:02:01' then seperate them?
  10. Yes, that'll be much easier. You can just then split it at ":" and youll get hrs, mins and sec respectively.
  11. Using the colons would probably make it the easiest.
  12. You could also use the h m s and but in the format 1h.2s so you split it at the "." and iterate through the string you get when split. Then you check to see which letter it ends in and do what u wanna do.
  13. You can try to use this method :
    Code (Text):
        public Date parseDate(String date){
            int weeks = 0;
            int days = 0;
            int hours = 0;
            int mins = 0;
            int secs = 0;
            String nums = "";
            for(int j = 0; j < date.length(); j++){
                String c = date.substring(j, j + 1);
                try {
                    nums = new StringBuilder().append(nums).append(c).toString();
                } catch (Exception e) {
                    int num = Integer.parseInt(nums);
                    if(c.equals("w"))weeks = num;
                    else if(c.equals("d"))days = num;
                    else if(c.equals("h"))hours = num;
                    else if(c.equals("m"))mins = num;
                    else if(c.equals("s"))secs = num;
                    else throw new IllegalArgumentException("Invalid time measurement: " + c);
                    nums = "";
            long time = 0;
            time += TimeUnit.SECONDS.toMillis(secs);
            time += TimeUnit.MINUTES.toMillis(mins);
            time += TimeUnit.HOURS.toMillis(hours);
            time += TimeUnit.DAYS.toMillis(days);
            time += TimeUnit.DAYS.toMillis(weeks*7);
            return new Date(time);
    Command you type :
    Code (Text):
    /timetest 5h2s3m
    Get the argument :
    Code (Text):
    String time = args[0];
    Parse it
    Code (Text):
    Date time = parseDate(time);
    and finally, you can use the following methods to parse the time with the Date object which was returned with the parse method.

    Code (Text):
    long timeMillis = date.getTime();

    int days = Math.ceil((int) TimeUnit.MILLISECONDS.toDays(timeMillis));
    int weeks = Math.ceil(days / 7);
    timeMillis -= TimeUnit.DAYS.toMillis(days);
    int hours = Math.ceil((int) TimeUnit.MILLISECONDS.toHours(timeMillis));
    timeMillis -= TimeUnit.HOURS.toMillis(hours);
    int minutes = Math.ceil((int) TimeUnit.MILLISECONDS.toMinutes(timeMillis));
    timeMillis -= TimeUnit.MINUTES.toMillis(minutes);
    int seconds = Math.ceil((int) TimeUnit.MILLISECONDS.toSeconds(timeMillis));
    Edit: Not tested yet, if it don't work, tell me!
  14. I honestly dont really understand that code, so Im jot going to use t. Ive got my sustem working with seperating it with :, so Ill stick with it for now. Tomorrow I need to just translate the seconds into hours minutes seconds again (cba rn 1am). Thanks for the help anyway!
  15. Well, it's just a utility method, so you don't REALLY have to understand it as long as you know the output. You can go over it later and take time to learn the algorithms (I don't always learn the algorithms myself but just find them on the internet, and sometimes I go back later).