forked from Minecraft/PluginAPI
60 lines
1.7 KiB
Java
60 lines
1.7 KiB
Java
package xyz.etztech.core.command;
|
|
|
|
import java.time.Duration;
|
|
import java.util.regex.Matcher;
|
|
import java.util.regex.Pattern;
|
|
|
|
|
|
public class TickDuration {
|
|
|
|
private Duration duration;
|
|
|
|
public TickDuration(Duration duration) {
|
|
this.duration = duration;
|
|
}
|
|
|
|
public long toTicks() {
|
|
return duration.getSeconds()*20;
|
|
}
|
|
|
|
public Duration getDuration() {
|
|
return duration;
|
|
}
|
|
|
|
/**
|
|
* Converts a time string (e.g. 10h1m3s) into a TickDuration
|
|
* @param time time string
|
|
* @return TickDuration
|
|
* @throws Exception on invalid time string
|
|
*/
|
|
public static TickDuration parse(String time) throws Exception {
|
|
String timePattern = "(?:(?<hours>\\d+)h)?(?:(?<minutes>\\d+)m)?(?:(?<seconds>\\d+)s)?";
|
|
Pattern pattern = Pattern.compile(timePattern);
|
|
Matcher match = pattern.matcher(time);
|
|
Duration duration = Duration.ZERO;
|
|
|
|
if (!match.matches()) {
|
|
throw new Exception("Time format does not match");
|
|
}
|
|
|
|
String hourString = match.group("hours");
|
|
if (hourString != null) {
|
|
int hours = Integer.parseInt(hourString);
|
|
duration = duration.plusHours(hours);
|
|
}
|
|
|
|
String minuteString = match.group("minutes");
|
|
if (minuteString != null) {
|
|
int minutes = Integer.parseInt(minuteString);
|
|
duration = duration.plusMinutes(minutes);
|
|
}
|
|
|
|
String secondString = match.group("seconds");
|
|
if (secondString != null) {
|
|
int seconds = Integer.parseInt(secondString);
|
|
duration = duration.plusSeconds(seconds);
|
|
}
|
|
|
|
return new TickDuration(duration);
|
|
}
|
|
} |