From ef11620e2f1f73c525654582ec552cff830c309d Mon Sep 17 00:00:00 2001 From: yoyosource Date: Tue, 1 Nov 2022 23:22:24 +0100 Subject: [PATCH] Hotfix PunishmentCommand.parseTime --- .../commands/PunishmentCommand.java | 27 +++++++++++-------- 1 file changed, 16 insertions(+), 11 deletions(-) diff --git a/src/de/steamwar/bungeecore/commands/PunishmentCommand.java b/src/de/steamwar/bungeecore/commands/PunishmentCommand.java index 40d39fd..62e9831 100644 --- a/src/de/steamwar/bungeecore/commands/PunishmentCommand.java +++ b/src/de/steamwar/bungeecore/commands/PunishmentCommand.java @@ -29,10 +29,12 @@ import net.md_5.bungee.api.connection.ProxiedPlayer; import java.sql.Timestamp; import java.text.ParseException; import java.text.SimpleDateFormat; +import java.time.Duration; import java.time.Instant; import java.time.temporal.ChronoUnit; import java.util.Date; import java.util.regex.MatchResult; +import java.util.regex.Matcher; import java.util.regex.Pattern; public class PunishmentCommand { @@ -105,15 +107,18 @@ public class PunishmentCommand { if (arg.equalsIgnoreCase("perma")) { return Punishment.PERMA_TIME; } else { - MatchResult matchResult = RELATIVE_PATTERN.matcher(arg).toMatchResult(); - if (matchResult.groupCount() > 0) { + if (RELATIVE_PATTERN.matcher(arg).matches()) { 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)); - System.out.println("Amount: " + amount + " Unit: " + unit); - switch (unit) { + StringBuilder st = new StringBuilder(); + for (int i = 0; i < arg.length(); i++) { + char c = arg.charAt(i); + if (Character.isDigit(c)) { + st.append(c); + continue; + } + int amount = Integer.parseInt(st.toString()); + st = new StringBuilder(); + switch (c) { case 'h': instant = instant.plus(amount, ChronoUnit.HOURS); break; @@ -121,13 +126,13 @@ public class PunishmentCommand { instant = instant.plus(amount, ChronoUnit.DAYS); break; case 'w': - instant = instant.plus(amount, ChronoUnit.WEEKS); + instant = instant.plus(Duration.ofSeconds(amount * ChronoUnit.WEEKS.getDuration().getSeconds())); break; case 'm': - instant = instant.plus(amount, ChronoUnit.MONTHS); + instant = instant.plus(Duration.ofSeconds(amount * ChronoUnit.MONTHS.getDuration().getSeconds())); break; case 'y': - instant = instant.plus(amount, ChronoUnit.YEARS); + instant = instant.plus(Duration.ofSeconds(amount * ChronoUnit.YEARS.getDuration().getSeconds())); break; } }