From c0195c15928937333dabc1cffb0d471f2d62e421 Mon Sep 17 00:00:00 2001 From: yoyosource Date: Mon, 24 Oct 2022 18:03:01 +0200 Subject: [PATCH] Add relative punishment times --- .../commands/PunishmentCommand.java | 32 +++++++++++++++++++ 1 file changed, 32 insertions(+) diff --git a/src/de/steamwar/bungeecore/commands/PunishmentCommand.java b/src/de/steamwar/bungeecore/commands/PunishmentCommand.java index 888e43c..c147ba1 100644 --- a/src/de/steamwar/bungeecore/commands/PunishmentCommand.java +++ b/src/de/steamwar/bungeecore/commands/PunishmentCommand.java @@ -30,7 +30,10 @@ import java.sql.Timestamp; import java.text.ParseException; import java.text.SimpleDateFormat; import java.time.Instant; +import java.time.temporal.ChronoUnit; import java.util.Date; +import java.util.regex.MatchResult; +import java.util.regex.Pattern; public class PunishmentCommand { @@ -96,10 +99,39 @@ public class PunishmentCommand { return target; } + private static Pattern RELATIVE_PATTERN = Pattern.compile("([1-9]\\d*[hdwmy])+"); + public static Timestamp parseTime(CommandSender sender, String arg) { if (arg.equalsIgnoreCase("perma")) { return Punishment.PERMA_TIME; } 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"); try { Date parsedDate = dateFormat.parse(arg);