diff --git a/.drone.yml b/.drone.yml index dfa044a..f9e2d79 100644 --- a/.drone.yml +++ b/.drone.yml @@ -11,7 +11,8 @@ steps: pull: always image: maven:3-alpine commands: - - mvn install + - mvn install -DskipTests=true -B -V + - mvn test -B --- kind: pipeline diff --git a/pom.xml b/pom.xml index f2eff55..6ceb514 100644 --- a/pom.xml +++ b/pom.xml @@ -37,6 +37,18 @@ h2 1.4.200 + + org.junit.jupiter + junit-jupiter + RELEASE + test + + + org.junit.jupiter + junit-jupiter + RELEASE + test + @@ -56,6 +68,7 @@ jitpack.io https://jitpack.io + @@ -64,5 +77,4 @@ https://repo.etztech.xyz/releases - \ No newline at end of file diff --git a/src/main/java/xyz/etztech/core/command/TickDuration.java b/src/main/java/xyz/etztech/core/command/TickDuration.java new file mode 100644 index 0000000..3f22f8c --- /dev/null +++ b/src/main/java/xyz/etztech/core/command/TickDuration.java @@ -0,0 +1,60 @@ +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 = "(?:(?\\d+)h)?(?:(?\\d+)m)?(?:(?\\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); + } +} \ No newline at end of file diff --git a/src/test/java/xyz/etztech/core/command/TickDurationTest.java b/src/test/java/xyz/etztech/core/command/TickDurationTest.java new file mode 100644 index 0000000..3093533 --- /dev/null +++ b/src/test/java/xyz/etztech/core/command/TickDurationTest.java @@ -0,0 +1,35 @@ +package xyz.etztech.core.command; + +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.*; + +class TickDurationTest { + @Test + void parse() { + try { + TickDuration tickDuration = TickDuration.parse("1s"); + assertEquals(20, tickDuration.toTicks()); + + tickDuration = TickDuration.parse("1m"); + assertEquals(20*60, tickDuration.toTicks()); + + tickDuration = TickDuration.parse("1h"); + assertEquals(20*60*60, tickDuration.toTicks()); + + tickDuration = TickDuration.parse("5h1m30s"); + assertEquals(20*60*60*5 + 60*20 + 30*20, tickDuration.toTicks()); + } + catch (Exception e) { + fail("Unable to parse time string"); + } + + try { + TickDuration tickDuration = TickDuration.parse("not a time"); + fail(); + } + catch (Exception e) { + + } + } +} \ No newline at end of file