Archiviert
1
0

Merge pull request 'Add relative punishment times' (#439) from RelativePunishments into master

Reviewed-on: SteamWar/BungeeCore#439
Reviewed-by: Lixfel <lixfel@steamwar.de>
Dieser Commit ist enthalten in:
Lixfel 2022-10-28 10:10:12 +02:00
Commit 83839a292a

Datei anzeigen

@ -30,7 +30,10 @@ import java.sql.Timestamp;
import java.text.ParseException; import java.text.ParseException;
import java.text.SimpleDateFormat; import java.text.SimpleDateFormat;
import java.time.Instant; import java.time.Instant;
import java.time.temporal.ChronoUnit;
import java.util.Date; import java.util.Date;
import java.util.regex.MatchResult;
import java.util.regex.Pattern;
public class PunishmentCommand { public class PunishmentCommand {
@ -96,10 +99,39 @@ public class PunishmentCommand {
return target; return target;
} }
private static Pattern RELATIVE_PATTERN = Pattern.compile("([1-9]\\d*[hdwmy])+");
public static Timestamp parseTime(CommandSender sender, String arg) { public static Timestamp parseTime(CommandSender sender, String arg) {
if (arg.equalsIgnoreCase("perma")) { if (arg.equalsIgnoreCase("perma")) {
return Punishment.PERMA_TIME; return Punishment.PERMA_TIME;
} else { } else {
MatchResult matchResult = RELATIVE_PATTERN.matcher(arg).toMatchResult();
if (matchResult.groupCount() > 0) {
Instant instant = Instant.now();
for (int i = 1; i < matchResult.groupCount(); i++) {
String group = matchResult.group(i);
int amount = Integer.parseInt(group.substring(0, group.length() - 1));
char unit = Character.toLowerCase(group.charAt(group.length() - 1));
switch (unit) {
case 'h':
instant = instant.plus(amount, ChronoUnit.HOURS);
break;
case 'd':
instant = instant.plus(amount, ChronoUnit.DAYS);
break;
case 'w':
instant = instant.plus(amount, ChronoUnit.WEEKS);
break;
case 'm':
instant = instant.plus(amount, ChronoUnit.MONTHS);
break;
case 'y':
instant = instant.plus(amount, ChronoUnit.YEARS);
break;
}
}
return Timestamp.from(instant);
}
SimpleDateFormat dateFormat = new SimpleDateFormat("dd.MM.yyyy_HH:mm"); SimpleDateFormat dateFormat = new SimpleDateFormat("dd.MM.yyyy_HH:mm");
try { try {
Date parsedDate = dateFormat.parse(arg); Date parsedDate = dateFormat.parse(arg);