forked from Minecraft/PluginAPI
Added TickDuration (#10)
Added tests to .drone.yml + Added `mvn test` step to compliance Merge branch 'master' of https://git.etztech.xyz/Minecraft/PluginAPI into time_parser Added tests and renamed BukkitDuration to TickDuration Added BukkitDuration + Designed for parsing time arguments easily Co-authored-by: Joey Hines <joey@ahines.net> Reviewed-on: https://git.etztech.xyz/Minecraft/PluginAPI/pulls/10 Reviewed-by: Etzelia <etzelia@hotmail.com>gradle
parent
6e90ba9587
commit
f62b9a4ac4
|
@ -11,7 +11,8 @@ steps:
|
||||||
pull: always
|
pull: always
|
||||||
image: maven:3-alpine
|
image: maven:3-alpine
|
||||||
commands:
|
commands:
|
||||||
- mvn install
|
- mvn install -DskipTests=true -B -V
|
||||||
|
- mvn test -B
|
||||||
|
|
||||||
---
|
---
|
||||||
kind: pipeline
|
kind: pipeline
|
||||||
|
|
14
pom.xml
14
pom.xml
|
@ -37,6 +37,18 @@
|
||||||
<artifactId>h2</artifactId>
|
<artifactId>h2</artifactId>
|
||||||
<version>1.4.200</version>
|
<version>1.4.200</version>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.junit.jupiter</groupId>
|
||||||
|
<artifactId>junit-jupiter</artifactId>
|
||||||
|
<version>RELEASE</version>
|
||||||
|
<scope>test</scope>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.junit.jupiter</groupId>
|
||||||
|
<artifactId>junit-jupiter</artifactId>
|
||||||
|
<version>RELEASE</version>
|
||||||
|
<scope>test</scope>
|
||||||
|
</dependency>
|
||||||
</dependencies>
|
</dependencies>
|
||||||
|
|
||||||
<repositories>
|
<repositories>
|
||||||
|
@ -56,6 +68,7 @@
|
||||||
<id>jitpack.io</id>
|
<id>jitpack.io</id>
|
||||||
<url>https://jitpack.io</url>
|
<url>https://jitpack.io</url>
|
||||||
</repository>
|
</repository>
|
||||||
|
|
||||||
</repositories>
|
</repositories>
|
||||||
|
|
||||||
<distributionManagement>
|
<distributionManagement>
|
||||||
|
@ -64,5 +77,4 @@
|
||||||
<url>https://repo.etztech.xyz/releases</url>
|
<url>https://repo.etztech.xyz/releases</url>
|
||||||
</repository>
|
</repository>
|
||||||
</distributionManagement>
|
</distributionManagement>
|
||||||
|
|
||||||
</project>
|
</project>
|
|
@ -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 = "(?:(?<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);
|
||||||
|
}
|
||||||
|
}
|
|
@ -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) {
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue