Remove some of this duplicate code

This commit is contained in:
Telesphoreo 2022-04-03 16:45:32 -05:00
parent 470f652e0b
commit 894cda2ede
2 changed files with 42 additions and 48 deletions

View File

@ -1,6 +1,5 @@
package dev.plex.punishment; package dev.plex.punishment;
import com.google.common.collect.Lists;
import com.google.gson.GsonBuilder; import com.google.gson.GsonBuilder;
import dev.plex.Plex; import dev.plex.Plex;
import dev.plex.util.MojangUtils; import dev.plex.util.MojangUtils;
@ -9,7 +8,6 @@ import dev.plex.util.adapter.LocalDateTimeDeserializer;
import dev.plex.util.adapter.LocalDateTimeSerializer; import dev.plex.util.adapter.LocalDateTimeSerializer;
import java.time.LocalDateTime; import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter; import java.time.format.DateTimeFormatter;
import java.util.List;
import java.util.UUID; import java.util.UUID;
import lombok.Getter; import lombok.Getter;
import lombok.Setter; import lombok.Setter;

View File

@ -110,17 +110,7 @@ public class PunishmentManager extends PlexBase
JSONTokener tokener = new JSONTokener(new FileInputStream(file)); JSONTokener tokener = new JSONTokener(new FileInputStream(file));
JSONObject object = new JSONObject(tokener); JSONObject object = new JSONObject(tokener);
object.getJSONObject(punishment.getPunished().toString()).getJSONArray("punishments").put(punishment.toJSON()); object.getJSONObject(punishment.getPunished().toString()).getJSONArray("punishments").put(punishment.toJSON());
if (plugin.getRedisConnection().isEnabled()) addToRedis(player, file, object);
{
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();
} }
else else
{ {
@ -132,17 +122,7 @@ public class PunishmentManager extends PlexBase
punishments.put("punishments", punishmentList); punishments.put("punishments", punishmentList);
object.put(punishment.getPunished().toString(), punishments); object.put(punishment.getPunished().toString(), punishments);
if (plugin.getRedisConnection().isEnabled()) addToRedis(player, file, object);
{
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 e) 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) private boolean isNotEmpty(File file)
{ {
try try
@ -219,18 +221,7 @@ public class PunishmentManager extends PlexBase
String jsonPunishmentString = jedis.get(uuid.toString()); String jsonPunishmentString = jedis.get(uuid.toString());
JSONObject object = new JSONObject(jsonPunishmentString); JSONObject object = new JSONObject(jsonPunishmentString);
List<Punishment> punishments = object.getJSONObject(uuid.toString()).getJSONArray("punishments").toList().stream().map(obj -> Punishment.fromJson(obj.toString())).collect(Collectors.toList()); setActive(uuid, object, false);
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()));
jedis.set(uuid.toString(), object.toString()); jedis.set(uuid.toString(), object.toString());
} }
@ -243,18 +234,7 @@ public class PunishmentManager extends PlexBase
{ {
JSONTokener tokener = new JSONTokener(fis); JSONTokener tokener = new JSONTokener(fis);
JSONObject object = new JSONObject(tokener); JSONObject object = new JSONObject(tokener);
List<Punishment> punishments = object.getJSONObject(uuid.toString()).getJSONArray("punishments").toList().stream().map(obj -> Punishment.fromJson(obj.toString())).collect(Collectors.toList()); setActive(uuid, object, false);
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()));
FileWriter writer = new FileWriter(file); FileWriter writer = new FileWriter(file);
writer.append(object.toString()); writer.append(object.toString());
writer.flush(); writer.flush();
@ -267,6 +247,22 @@ public class PunishmentManager extends PlexBase
} }
} }
private void setActive(UUID uuid, JSONObject object, boolean active)
{
List<Punishment> 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) private void issuePunishment(PunishedPlayer player, Punishment punishment)
{ {
if (punishment.getType() == PunishmentType.FREEZE) if (punishment.getType() == PunishmentType.FREEZE)