add tempban punishment system and redo the way tempbans parse duration

This commit is contained in:
Taah
2022-04-19 13:16:07 -07:00
parent 953f1813d3
commit 98c487c639
7 changed files with 43 additions and 64 deletions

View File

@ -63,7 +63,7 @@ import org.json.simple.parser.ParseException;
public class PlexUtils implements PlexBase
{
private static final Random RANDOM;
private static final List<String> regxList = new ArrayList<>()
public static final List<String> timeUnits = new ArrayList<>()
{{
add("y");
add("mo");
@ -290,7 +290,7 @@ public class PlexUtils implements PlexBase
{
StringBuilder sb = new StringBuilder();
regxList.forEach(obj ->
timeUnits.forEach(obj ->
{
if (parse.endsWith(obj))
{
@ -301,27 +301,36 @@ public class PlexUtils implements PlexBase
return Long.parseLong(sb.toString());
}
private static TimeUnit verify(String arg)
public static int parseInteger(String s) throws NumberFormatException
{
TimeUnit unit = null;
for (String c : regxList)
if (!NumberUtils.isNumber(s))
{
if (arg.endsWith(c))
throw new NumberFormatException(messageString("unableToParseNumber", s));
}
return Integer.parseInt(s);
}
public static LocalDateTime createDate(String arg)
{
LocalDateTime time = LocalDateTime.now();
for (String unit : PlexUtils.timeUnits)
{
if (arg.endsWith(unit))
{
switch (c)
int duration = parseInteger(arg.replace(unit, ""));
switch (unit)
{
case "y" -> unit = TimeUnit.YEAR;
case "mo" -> unit = TimeUnit.MONTH;
case "w" -> unit = TimeUnit.WEEK;
case "d" -> unit = TimeUnit.DAY;
case "h" -> unit = TimeUnit.HOUR;
case "m" -> unit = TimeUnit.MINUTE;
case "s" -> unit = TimeUnit.SECOND;
case "y" -> time = time.plusYears(duration);
case "mo" -> time = time.plusMonths(duration);
case "w" -> time = time.plusWeeks(duration);
case "d" -> time = time.plusDays(duration);
case "h" -> time = time.plusHours(duration);
case "m" -> time = time.plusMinutes(duration);
case "s" -> time = time.plusSeconds(duration);
}
break;
}
}
return (unit != null) ? unit : TimeUnit.DAY;
return time;
}
public static String useTimezone(LocalDateTime date)
@ -334,16 +343,6 @@ public class PlexUtils implements PlexBase
return DATE_FORMAT.withZone(ZoneId.of(TIMEZONE)).format(date);
}
public static LocalDateTime parseDateOffset(String... time)
{
Instant instant = Instant.now();
for (String arg : time)
{
instant = instant.plusSeconds(verify(arg).get() * a(arg));
}
return LocalDateTime.ofInstant(instant, ZoneId.systemDefault().getRules().getOffset(instant));
}
public static ChatColor getChatColorFromConfig(Config config, ChatColor def, String path)
{
ChatColor color;

View File

@ -1,24 +0,0 @@
package dev.plex.util;
public enum TimeUnit
{
SECOND(1L),
MINUTE(SECOND.get() * 60L),
HOUR(MINUTE.get() * 60L),
DAY(HOUR.get() * 24L),
WEEK(DAY.get() * 7L),
MONTH(DAY.get() * 30L),
YEAR(MONTH.get() * 12L);
private final long time;
TimeUnit(long time)
{
this.time = time;
}
public long get()
{
return time;
}
}