mirror of
https://github.com/plexusorg/Plex.git
synced 2024-12-23 01:27:37 +00:00
Remove some of this duplicate code
This commit is contained in:
parent
470f652e0b
commit
894cda2ede
@ -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;
|
||||
|
@ -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<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(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<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(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<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)
|
||||
{
|
||||
if (punishment.getType() == PunishmentType.FREEZE)
|
||||
|
Loading…
Reference in New Issue
Block a user