Ban service now works with JSON

This commit is contained in:
Telesphoreo 2022-04-03 17:59:16 -05:00
parent 894cda2ede
commit 511506348f

View File

@ -12,6 +12,7 @@ import dev.plex.util.PlexLog;
import dev.plex.util.PlexUtils; import dev.plex.util.PlexUtils;
import java.io.File; import java.io.File;
import java.io.FileInputStream; import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileWriter; import java.io.FileWriter;
import java.io.IOException; import java.io.IOException;
import java.nio.charset.StandardCharsets; import java.nio.charset.StandardCharsets;
@ -20,6 +21,7 @@ import java.time.temporal.ChronoUnit;
import java.util.Collection; import java.util.Collection;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.Objects;
import java.util.UUID; import java.util.UUID;
import java.util.stream.Collectors; import java.util.stream.Collectors;
import lombok.Data; import lombok.Data;
@ -183,28 +185,51 @@ public class PunishmentManager extends PlexBase
if (Plex.get().getRedisConnection().isEnabled()) if (Plex.get().getRedisConnection().isEnabled())
{ {
Jedis jedis = Plex.get().getRedisConnection().getJedis(); Jedis jedis = Plex.get().getRedisConnection().getJedis();
jedis.keys("*").forEach(key -> for (String key : jedis.keys("*"))
{ {
try try
{ {
UUID uuid = UUID.fromString(key); UUID uuid = UUID.fromString(key);
String jsonPunishmentString = jedis.get(uuid.toString()); String jsonPunishmentString = jedis.get(uuid.toString());
JSONObject object = new JSONObject(jsonPunishmentString); JSONObject object = new JSONObject(jsonPunishmentString);
object.getJSONObject(uuid.toString()).getJSONArray("punishments").forEach(json -> for (Object json : object.getJSONObject(uuid.toString()).getJSONArray("punishments"))
{ {
Punishment punishment = Punishment.fromJson(json.toString()); Punishment punishment = Punishment.fromJson(json.toString());
if (punishment.isActive() && punishment.getType() == PunishmentType.BAN) if (punishment.isActive() && punishment.getType() == PunishmentType.BAN)
{ {
punishments.add(punishment); punishments.add(punishment);
} }
}); }
} }
catch (IllegalArgumentException ignored) catch (IllegalArgumentException ignored)
{ {
} }
}
}
else
{
File fileDir = new File(plugin.getDataFolder() + File.separator + "punishments");
for (File file : Objects.requireNonNull(fileDir.listFiles()))
{
if (isNotEmpty(file))
{
try (FileInputStream fis = new FileInputStream(file))
{
JSONTokener tokener = new JSONTokener(fis);
JSONObject object = new JSONObject(tokener);
object.keySet().stream().findFirst().ifPresent(key ->
{
JSONObject obj = object.getJSONObject(key);
punishments.addAll(obj.getJSONArray("punishments").toList().stream().map(Object::toString).map(Punishment::fromJson).filter(punishment -> punishment.isActive() && punishment.getType() == PunishmentType.BAN).toList());
}); });
} }
catch (IOException e)
{
e.printStackTrace();
}
}
}
}
return punishments; return punishments;
} }