From 894cda2edec3e26039ec6b51e997e19dfa7197eb Mon Sep 17 00:00:00 2001 From: Telesphoreo Date: Sun, 3 Apr 2022 16:45:32 -0500 Subject: [PATCH] Remove some of this duplicate code --- .../java/dev/plex/punishment/Punishment.java | 2 - .../plex/punishment/PunishmentManager.java | 88 +++++++++---------- 2 files changed, 42 insertions(+), 48 deletions(-) diff --git a/src/main/java/dev/plex/punishment/Punishment.java b/src/main/java/dev/plex/punishment/Punishment.java index ae257c2..dd87200 100644 --- a/src/main/java/dev/plex/punishment/Punishment.java +++ b/src/main/java/dev/plex/punishment/Punishment.java @@ -1,6 +1,5 @@ package dev.plex.punishment; -import com.google.common.collect.Lists; import com.google.gson.GsonBuilder; import dev.plex.Plex; import dev.plex.util.MojangUtils; @@ -9,7 +8,6 @@ import dev.plex.util.adapter.LocalDateTimeDeserializer; import dev.plex.util.adapter.LocalDateTimeSerializer; import java.time.LocalDateTime; import java.time.format.DateTimeFormatter; -import java.util.List; import java.util.UUID; import lombok.Getter; import lombok.Setter; diff --git a/src/main/java/dev/plex/punishment/PunishmentManager.java b/src/main/java/dev/plex/punishment/PunishmentManager.java index 8714c5c..7439574 100644 --- a/src/main/java/dev/plex/punishment/PunishmentManager.java +++ b/src/main/java/dev/plex/punishment/PunishmentManager.java @@ -110,17 +110,7 @@ public class PunishmentManager extends PlexBase JSONTokener tokener = new JSONTokener(new FileInputStream(file)); JSONObject object = new JSONObject(tokener); object.getJSONObject(punishment.getPunished().toString()).getJSONArray("punishments").put(punishment.toJSON()); - if (plugin.getRedisConnection().isEnabled()) - { - plugin.getRedisConnection().getJedis().set(player.getUuid(), object.toString()); - PlexLog.debug("Added " + player.getUuid() + "'s punishment to the Redis database."); - plugin.getRedisConnection().getJedis().close(); - } - - FileWriter writer = new FileWriter(file); - writer.append(object.toString(8)); - writer.flush(); - writer.close(); + addToRedis(player, file, object); } else { @@ -132,17 +122,7 @@ public class PunishmentManager extends PlexBase punishments.put("punishments", punishmentList); object.put(punishment.getPunished().toString(), punishments); - if (plugin.getRedisConnection().isEnabled()) - { - plugin.getRedisConnection().getJedis().set(player.getUuid(), object.toString()); - PlexLog.debug("Added " + player.getUuid() + "'s punishment to the Redis database."); - plugin.getRedisConnection().getJedis().close(); - } - - FileWriter writer = new FileWriter(file); - writer.append(object.toString(8)); - writer.flush(); - writer.close(); + addToRedis(player, file, object); } } catch (IOException e) @@ -151,6 +131,28 @@ public class PunishmentManager extends PlexBase } } + private void addToRedis(PunishedPlayer player, File file, JSONObject object) + { + try + { + if (plugin.getRedisConnection().isEnabled()) + { + plugin.getRedisConnection().getJedis().set(player.getUuid(), object.toString()); + PlexLog.debug("Added " + player.getUuid() + "'s punishment to the Redis database."); + plugin.getRedisConnection().getJedis().close(); + } + + FileWriter writer = new FileWriter(file); + writer.append(object.toString(8)); + writer.flush(); + writer.close(); + } + catch (IOException ex) + { + ex.printStackTrace(); + } + } + private boolean isNotEmpty(File file) { try @@ -219,18 +221,7 @@ public class PunishmentManager extends PlexBase String jsonPunishmentString = jedis.get(uuid.toString()); JSONObject object = new JSONObject(jsonPunishmentString); - List punishments = object.getJSONObject(uuid.toString()).getJSONArray("punishments").toList().stream().map(obj -> Punishment.fromJson(obj.toString())).collect(Collectors.toList()); - while (punishments.stream().anyMatch(punishment -> punishment.isActive() && punishment.getType() == PunishmentType.BAN)) - { - punishments.stream().filter(Punishment::isActive).filter(punishment -> punishment.getType() == PunishmentType.BAN).findFirst().ifPresent(punishment -> - { - int index = punishments.indexOf(punishment); - punishment.setActive(false); - punishments.set(index, punishment); - }); - } - object.getJSONObject(uuid.toString()).getJSONArray("punishments").clear(); - object.getJSONObject(uuid.toString()).getJSONArray("punishments").putAll(punishments.stream().map(Punishment::toJSON).collect(Collectors.toList())); + setActive(uuid, object, false); jedis.set(uuid.toString(), object.toString()); } @@ -243,18 +234,7 @@ public class PunishmentManager extends PlexBase { JSONTokener tokener = new JSONTokener(fis); JSONObject object = new JSONObject(tokener); - List punishments = object.getJSONObject(uuid.toString()).getJSONArray("punishments").toList().stream().map(obj -> Punishment.fromJson(obj.toString())).collect(Collectors.toList()); - while (punishments.stream().anyMatch(punishment -> punishment.isActive() && punishment.getType() == PunishmentType.BAN)) - { - punishments.stream().filter(Punishment::isActive).filter(punishment -> punishment.getType() == PunishmentType.BAN).findFirst().ifPresent(punishment -> - { - int index = punishments.indexOf(punishment); - punishment.setActive(false); - punishments.set(index, punishment); - }); - } - object.getJSONObject(uuid.toString()).getJSONArray("punishments").clear(); - object.getJSONObject(uuid.toString()).getJSONArray("punishments").putAll(punishments.stream().map(Punishment::toJSON).collect(Collectors.toList())); + setActive(uuid, object, false); FileWriter writer = new FileWriter(file); writer.append(object.toString()); writer.flush(); @@ -267,6 +247,22 @@ public class PunishmentManager extends PlexBase } } + private void setActive(UUID uuid, JSONObject object, boolean active) + { + List punishments = object.getJSONObject(uuid.toString()).getJSONArray("punishments").toList().stream().map(obj -> Punishment.fromJson(obj.toString())).collect(Collectors.toList()); + while (punishments.stream().anyMatch(punishment -> punishment.isActive() && punishment.getType() == PunishmentType.BAN)) + { + punishments.stream().filter(Punishment::isActive).filter(punishment -> punishment.getType() == PunishmentType.BAN).findFirst().ifPresent(punishment -> + { + int index = punishments.indexOf(punishment); + punishment.setActive(active); + punishments.set(index, punishment); + }); + } + object.getJSONObject(uuid.toString()).getJSONArray("punishments").clear(); + object.getJSONObject(uuid.toString()).getJSONArray("punishments").putAll(punishments.stream().map(Punishment::toJSON).collect(Collectors.toList())); + } + private void issuePunishment(PunishedPlayer player, Punishment punishment) { if (punishment.getType() == PunishmentType.FREEZE)