QoL/src/main/java/xyz/etztech/qol/other/ShadowMuteTime.java

113 lines
2.9 KiB
Java

package xyz.etztech.qol.other;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class ShadowMuteTime {
private int hours;
private int minutes;
private int seconds;
public ShadowMuteTime() {
hours = 1;
minutes = 0;
seconds = 0;
}
public int getHours() {
return hours;
}
public void setHours(int hours) {
this.hours = hours;
}
public void addHours(int hours) {
this.hours += hours;
}
public int getMinutes() {
return minutes;
}
public void setMinutes(int minutes) {
this.minutes = minutes;
}
public void addMinutes(int minutes) {
this.minutes += minutes;
}
public int getSeconds() {
return seconds;
}
public void setSeconds(int seconds) {
this.seconds = seconds;
}
public void addSeconds(int seconds) {
this.seconds += seconds;
}
public String toString() {
return getHours() + " hours, " + getMinutes() + " minutes, and " + getSeconds() + " seconds";
}
public long toTicks() {
int seconds = getSeconds();
seconds += getMinutes()*60;
seconds += getHours()*60*60;
return seconds*20;
}
public static ShadowMuteTime parse(String time) throws Exception {
ShadowMuteTime smt = new ShadowMuteTime();
smt.setHours(0); // Defaults to 1 hour
String timePattern = "(?:(?<hours>\\d+)h)?(?:(?<minutes>\\d+)m)?(?:(?<seconds>\\d+)s)?";
Pattern pattern = Pattern.compile(timePattern);
Matcher match = pattern.matcher(time);
if (!match.matches()) {
throw new Exception("Time format does not match, defaulting to 1 hour.");
}
String hourString = match.group("hours");
if (hourString != null) {
int hours = Integer.parseInt(hourString);
smt.addHours(hours);
}
String minuteString = match.group("minutes");
if (minuteString != null) {
int minutes = Integer.parseInt(minuteString);
if (minutes >= 60) {
int toHours = minutes / 60;
minutes %= 60;
smt.addHours(toHours);
}
smt.addMinutes(minutes);
}
String secondString = match.group("seconds");
if (secondString != null) {
int seconds = Integer.parseInt(secondString);
if (seconds >= 60) {
int toMinutes = seconds / 60;
seconds %= 60;
int minutes = smt.getMinutes() + toMinutes;
if (minutes >= 60) {
int toHours = minutes / 60;
minutes %= 60;
smt.addHours(toHours);
}
smt.addMinutes(minutes);
}
smt.addSeconds(seconds);
}
return smt;
}
}