Rework punishment system by deleting the Ban and BanManager and moving everything into Punishment Manager. TODO cache punishments maybe? who knows! add an active field to punishments and fix the ban service to actually unban players

This commit is contained in:
Taah
2022-02-21 16:20:22 -08:00
parent d4578f2255
commit 6f506ac5cb
16 changed files with 295 additions and 381 deletions

View File

@ -23,7 +23,10 @@ public class Punishment
private PunishmentType type;
private String reason;
private boolean customTime;
private boolean active; //Field is only for bans
private LocalDateTime endDate;
public Punishment(UUID punished, UUID punisher)

View File

@ -4,10 +4,17 @@ import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import dev.plex.Plex;
import dev.plex.PlexBase;
import dev.plex.banning.Ban;
import dev.plex.cache.PlayerCache;
import dev.plex.player.PunishedPlayer;
import dev.plex.util.PlexLog;
import dev.plex.util.PlexUtils;
import org.apache.commons.io.FileUtils;
import org.bukkit.Bukkit;
import org.bukkit.scheduler.BukkitRunnable;
import org.json.JSONObject;
import org.json.JSONTokener;
import redis.clients.jedis.Jedis;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileWriter;
@ -18,11 +25,7 @@ import java.time.temporal.ChronoUnit;
import java.util.List;
import java.util.Map;
import java.util.UUID;
import org.apache.commons.io.FileUtils;
import org.bukkit.Bukkit;
import org.bukkit.scheduler.BukkitRunnable;
import org.json.JSONObject;
import org.json.JSONTokener;
import java.util.stream.Collectors;
public class PunishmentManager extends PlexBase
{
@ -48,8 +51,7 @@ public class PunishmentManager extends PlexBase
writer.append(object.toString(8));
writer.flush();
writer.close();
}
else
} else
{
JSONObject object = new JSONObject();
Map<String, List<String>> punishments = Maps.newHashMap();
@ -71,8 +73,7 @@ public class PunishmentManager extends PlexBase
writer.flush();
writer.close();
}
}
catch (IOException e)
} catch (IOException e)
{
e.printStackTrace();
}
@ -83,22 +84,126 @@ public class PunishmentManager extends PlexBase
try
{
return !FileUtils.readFileToString(file, StandardCharsets.UTF_8).trim().isEmpty();
}
catch (IOException e)
} catch (IOException e)
{
e.printStackTrace();
}
return false;
}
public boolean isBanned(UUID uuid)
{
return PlayerCache.getPunishedPlayerMap().containsKey(uuid) && PlayerCache.getPunishedPlayer(uuid).getPunishments().stream().anyMatch(punishment -> punishment.getType() == PunishmentType.BAN && punishment.isActive());
}
public boolean isBanned(PunishedPlayer player)
{
return isBanned(UUID.fromString(player.getUuid()));
}
public List<Punishment> getActiveBans()
{
List<Punishment> punishments = Lists.newArrayList();
Jedis jedis = Plex.get().getRedisConnection().getJedis();
jedis.keys("*").forEach(key ->
{
try
{
UUID uuid = UUID.fromString(key);
String jsonPunishmentString = jedis.get(uuid.toString());
JSONObject object = new JSONObject(jsonPunishmentString);
object.getJSONObject(uuid.toString()).getJSONArray("punishments").forEach(json ->
{
Punishment punishment = Punishment.fromJson(json.toString());
if (punishment.isActive() && punishment.getType() == PunishmentType.BAN)
{
punishments.add(punishment);
}
});
} catch (IllegalArgumentException e)
{
}
});
return punishments;
}
public void unban(Punishment punishment)
{
this.unban(punishment.getPunished());
}
public void unban(UUID uuid)
{
if (Plex.get().getRedisConnection().isEnabled())
{
Jedis jedis = Plex.get().getRedisConnection().getJedis();
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()));
jedis.set(uuid.toString(), object.toString());
}
PunishedPlayer player;
if (PlayerCache.getPunishedPlayerMap().containsKey(uuid))
{
player = PlayerCache.getPunishedPlayer(uuid);
} else
{
player = new PunishedPlayer(uuid);
}
File file = player.getPunishmentsFile();
if (isNotEmpty(file))
{
try (FileInputStream fis = new FileInputStream(file))
{
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()));
FileWriter writer = new FileWriter(file);
writer.append(object.toString());
writer.flush();
writer.close();
} catch (IOException e)
{
e.printStackTrace();
}
}
}
private void issuePunishment(PunishedPlayer player, Punishment punishment)
{
if (punishment.getType() == PunishmentType.BAN)
/*if (punishment.getType() == PunishmentType.BAN)
{
Ban ban = new Ban(punishment.getPunished(), (punishment.getPunisher() == null ? null : punishment.getPunisher()), "", punishment.getReason(), punishment.getEndDate());
Plex.get().getBanManager().executeBan(ban);
// Ban ban = new Ban(punishment.getPunished(), (punishment.getPunisher() == null ? null : punishment.getPunisher()), "", punishment.getReason(), punishment.getEndDate());
// Plex.get().getBanManager().executeBan(ban);
}
else if (punishment.getType() == PunishmentType.FREEZE)
else*/
if (punishment.getType() == PunishmentType.FREEZE)
{
player.setFrozen(true);
LocalDateTime now = LocalDateTime.now();
@ -119,8 +224,7 @@ public class PunishmentManager extends PlexBase
Bukkit.getLogger().info("Unfroze");
}
}.runTaskLater(Plex.get(), 20 * seconds);
}
else if (punishment.getType() == PunishmentType.MUTE)
} else if (punishment.getType() == PunishmentType.MUTE)
{
player.setMuted(true);
LocalDateTime now = LocalDateTime.now();