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

@ -201,7 +201,7 @@ public abstract class PlexCommand extends Command implements PluginIdentifiableC
}
}
catch (PlayerNotFoundException | CommandFailException | ConsoleOnlyException |
ConsoleMustDefinePlayerException | PlayerNotBannedException ex)
ConsoleMustDefinePlayerException | PlayerNotBannedException | NumberFormatException ex)
{
send(sender, PlexUtils.mmDeserialize(ex.getMessage()));
}

View File

@ -12,16 +12,19 @@ import dev.plex.punishment.Punishment;
import dev.plex.punishment.PunishmentType;
import dev.plex.rank.enums.Rank;
import dev.plex.util.PlexUtils;
import java.util.List;
import java.util.UUID;
import net.kyori.adventure.text.Component;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.math.NumberUtils;
import org.bukkit.Bukkit;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.time.LocalDateTime;
import java.util.List;
import java.util.UUID;
@CommandParameters(name = "tempban", usage = "/<command> <player> <time> [reason]", description = "Temporarily ban a player")
@CommandPermissions(level = Rank.ADMIN, permission = "plex.tempban", source = RequiredCommandSource.ANY)
@ -64,18 +67,17 @@ public class TempbanCMD extends PlexCommand
return messageComponent("playerBanned");
}
Punishment punishment = new Punishment(targetUUID, getUUID(sender));
punishment.setType(PunishmentType.BAN);
punishment.setType(PunishmentType.TEMPBAN);
if (args.length > 2)
{
reason = StringUtils.join(args, " ", 2, args.length);
punishment.setReason(reason);
}
else
} else
{
punishment.setReason("No reason provided.");
}
punishment.setPunishedUsername(plexPlayer.getName());
punishment.setEndDate(PlexUtils.parseDateOffset(args[1]));
punishment.setEndDate(PlexUtils.createDate(args[1]));
punishment.setCustomTime(false);
punishment.setActive(!isAdmin(plexPlayer));
if (player != null)

View File

@ -129,7 +129,7 @@ public class PunishmentManager implements PlexBase
{
PlexPlayer player = DataUtils.getPlayer(uuid);
player.loadPunishments();
return player.getPunishments().stream().anyMatch(punishment -> punishment.getType() == PunishmentType.BAN && punishment.isActive());
return player.getPunishments().stream().anyMatch(punishment -> (punishment.getType() == PunishmentType.BAN || punishment.getType() == PunishmentType.TEMPBAN) && punishment.isActive());
});
}
@ -139,7 +139,7 @@ public class PunishmentManager implements PlexBase
{
return false;
}*/
return DataUtils.getPlayer(uuid).getPunishments().stream().anyMatch(punishment -> punishment.getType() == PunishmentType.BAN && punishment.isActive());
return DataUtils.getPlayer(uuid).getPunishments().stream().anyMatch(punishment -> (punishment.getType() == PunishmentType.BAN || punishment.getType() == PunishmentType.TEMPBAN) && punishment.isActive());
}
public boolean isBanned(PlexPlayer player)
@ -154,7 +154,7 @@ public class PunishmentManager implements PlexBase
return CompletableFuture.supplyAsync(() ->
{
List<PlexPlayer> players = Plex.get().getMongoPlayerData().getPlayers();
return players.stream().map(PlexPlayer::getPunishments).flatMap(Collection::stream).filter(Punishment::isActive).filter(punishment -> punishment.getType() == PunishmentType.BAN).toList();
return players.stream().map(PlexPlayer::getPunishments).flatMap(Collection::stream).filter(Punishment::isActive).filter(punishment -> punishment.getType() == PunishmentType.BAN || punishment.getType() == PunishmentType.TEMPBAN).toList();
});
}
else
@ -164,7 +164,7 @@ public class PunishmentManager implements PlexBase
Plex.get().getSqlPunishment().getPunishments().whenComplete((punishments, throwable) ->
{
PlexLog.debug("Received Punishments");
List<Punishment> punishmentList = punishments.stream().filter(Punishment::isActive).filter(punishment -> punishment.getType() == PunishmentType.BAN).toList();
List<Punishment> punishmentList = punishments.stream().filter(Punishment::isActive).filter(punishment -> punishment.getType() == PunishmentType.BAN || punishment.getType() == PunishmentType.TEMPBAN).toList();
PlexLog.debug("Completing with {0} punishments", punishmentList.size());
future.complete(punishmentList);
});
@ -184,7 +184,7 @@ public class PunishmentManager implements PlexBase
return CompletableFuture.runAsync(() ->
{
PlexPlayer plexPlayer = DataUtils.getPlayer(uuid);
plexPlayer.setPunishments(plexPlayer.getPunishments().stream().filter(Punishment::isActive).filter(punishment -> punishment.getType() == PunishmentType.BAN)
plexPlayer.setPunishments(plexPlayer.getPunishments().stream().filter(Punishment::isActive).filter(punishment -> punishment.getType() == PunishmentType.BAN || punishment.getType() == PunishmentType.TEMPBAN)
.peek(punishment -> punishment.setActive(false)).collect(Collectors.toList()));
DataUtils.update(plexPlayer);
});

View File

@ -2,5 +2,5 @@ package dev.plex.punishment;
public enum PunishmentType
{
MUTE, FREEZE, BAN, KICK, SMITE
MUTE, FREEZE, BAN, KICK, SMITE, TEMPBAN
}

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))
{
switch (c)
{
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;
throw new NumberFormatException(messageString("unableToParseNumber", s));
}
break;
return Integer.parseInt(s);
}
public static LocalDateTime createDate(String arg)
{
LocalDateTime time = LocalDateTime.now();
for (String unit : PlexUtils.timeUnits)
{
if (arg.endsWith(unit))
{
int duration = parseInteger(arg.replace(unit, ""));
switch (unit)
{
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);
}
}
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;
}
}

View File

@ -174,3 +174,5 @@ sayMessage: "<blue>[Server: {0}] {1}"
# 0 - The command sender
# 1 - The message being said
consoleSayMessage: "<gray>[Console: {0}] <white>{1}"
# 0 - The number attempted to be parsed
unableToParseNumber: "<red>Unable to parse {0} as a number!"