2021-01-03 07:21:15 +00:00
|
|
|
package dev.plex.punishment;
|
2020-11-06 18:51:47 +00:00
|
|
|
|
|
|
|
import com.google.common.collect.Lists;
|
|
|
|
import com.google.common.collect.Maps;
|
2021-01-03 07:21:15 +00:00
|
|
|
import dev.plex.Plex;
|
2022-02-04 08:18:07 +00:00
|
|
|
import dev.plex.PlexBase;
|
2021-01-03 07:21:15 +00:00
|
|
|
import dev.plex.banning.Ban;
|
|
|
|
import dev.plex.player.PunishedPlayer;
|
2022-02-04 08:18:07 +00:00
|
|
|
import dev.plex.util.PlexLog;
|
2021-01-03 07:21:15 +00:00
|
|
|
import dev.plex.util.PlexUtils;
|
2020-11-06 18:51:47 +00:00
|
|
|
import java.io.File;
|
|
|
|
import java.io.FileInputStream;
|
|
|
|
import java.io.FileWriter;
|
|
|
|
import java.io.IOException;
|
|
|
|
import java.nio.charset.StandardCharsets;
|
2020-11-10 02:47:03 +00:00
|
|
|
import java.util.Date;
|
2020-11-06 18:51:47 +00:00
|
|
|
import java.util.List;
|
|
|
|
import java.util.Map;
|
2020-11-10 02:47:03 +00:00
|
|
|
import java.util.UUID;
|
|
|
|
import java.util.concurrent.TimeUnit;
|
2022-01-27 21:23:01 +00:00
|
|
|
import org.apache.commons.io.FileUtils;
|
|
|
|
import org.bukkit.Bukkit;
|
|
|
|
import org.bukkit.scheduler.BukkitRunnable;
|
|
|
|
import org.json.JSONObject;
|
|
|
|
import org.json.JSONTokener;
|
2020-11-06 18:51:47 +00:00
|
|
|
|
2022-02-04 08:18:07 +00:00
|
|
|
public class PunishmentManager extends PlexBase
|
2020-11-06 18:51:47 +00:00
|
|
|
{
|
|
|
|
public void insertPunishment(PunishedPlayer player, Punishment punishment)
|
|
|
|
{
|
2020-11-10 04:08:37 +00:00
|
|
|
File file = player.getPunishmentsFile();
|
2020-11-06 18:51:47 +00:00
|
|
|
|
2022-01-30 21:03:47 +00:00
|
|
|
try
|
|
|
|
{
|
2020-11-06 18:51:47 +00:00
|
|
|
if (isNotEmpty(file))
|
|
|
|
{
|
|
|
|
JSONTokener tokener = new JSONTokener(new FileInputStream(file));
|
|
|
|
JSONObject object = new JSONObject(tokener);
|
|
|
|
object.getJSONObject(punishment.getPunished().toString()).getJSONArray("punishments").put(punishment.toJSON());
|
2022-02-04 08:18:07 +00:00
|
|
|
if (plugin.getRedisConnection().isEnabled())
|
|
|
|
{
|
|
|
|
plugin.getRedisConnection().getJedis().set(player.getUuid(), object.toString());
|
|
|
|
PlexLog.debug("Added " + player.getUuid() + "'s punishment to the Redis database.");
|
|
|
|
}
|
2020-11-06 18:51:47 +00:00
|
|
|
|
|
|
|
FileWriter writer = new FileWriter(file);
|
|
|
|
writer.append(object.toString(8));
|
|
|
|
writer.flush();
|
|
|
|
writer.close();
|
2022-01-30 21:03:47 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2020-11-06 18:51:47 +00:00
|
|
|
JSONObject object = new JSONObject();
|
|
|
|
Map<String, List<String>> punishments = Maps.newHashMap();
|
|
|
|
|
|
|
|
List<String> punishmentList = Lists.newArrayList();
|
|
|
|
punishmentList.add(punishment.toJSON());
|
|
|
|
|
|
|
|
punishments.put("punishments", punishmentList);
|
|
|
|
object.put(punishment.getPunished().toString(), punishments);
|
2022-02-04 08:18:07 +00:00
|
|
|
if (plugin.getRedisConnection().isEnabled())
|
|
|
|
{
|
|
|
|
plugin.getRedisConnection().getJedis().set(player.getUuid(), object.toString());
|
|
|
|
PlexLog.debug("Added " + player.getUuid() + "'s punishment to the Redis database.");
|
|
|
|
}
|
2020-11-06 18:51:47 +00:00
|
|
|
|
|
|
|
FileWriter writer = new FileWriter(file);
|
|
|
|
writer.append(object.toString(8));
|
|
|
|
writer.flush();
|
|
|
|
writer.close();
|
|
|
|
}
|
2022-01-30 21:03:47 +00:00
|
|
|
}
|
|
|
|
catch (IOException e)
|
|
|
|
{
|
2020-11-06 18:51:47 +00:00
|
|
|
e.printStackTrace();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-30 21:03:47 +00:00
|
|
|
private boolean isNotEmpty(File file)
|
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
2020-11-06 18:51:47 +00:00
|
|
|
return !FileUtils.readFileToString(file, StandardCharsets.UTF_8).trim().isEmpty();
|
2022-01-30 21:03:47 +00:00
|
|
|
}
|
|
|
|
catch (IOException e)
|
|
|
|
{
|
2020-11-06 18:51:47 +00:00
|
|
|
e.printStackTrace();
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2020-11-10 02:47:03 +00:00
|
|
|
private void issuePunishment(PunishedPlayer player, Punishment punishment)
|
|
|
|
{
|
|
|
|
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);
|
2022-01-30 21:03:47 +00:00
|
|
|
}
|
|
|
|
else if (punishment.getType() == PunishmentType.FREEZE)
|
2020-11-10 02:47:03 +00:00
|
|
|
{
|
|
|
|
player.setFrozen(true);
|
|
|
|
Date now = new Date();
|
|
|
|
Date then = punishment.getEndDate();
|
2022-01-30 21:03:47 +00:00
|
|
|
long seconds = TimeUnit.MILLISECONDS.toSeconds(then.getTime() - now.getTime());
|
|
|
|
new BukkitRunnable()
|
|
|
|
{
|
2020-11-10 02:47:03 +00:00
|
|
|
@Override
|
2022-01-30 21:03:47 +00:00
|
|
|
public void run()
|
|
|
|
{
|
2020-11-10 02:47:03 +00:00
|
|
|
if (!player.isFrozen())
|
|
|
|
{
|
|
|
|
this.cancel();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
player.setFrozen(false);
|
|
|
|
Bukkit.broadcastMessage(PlexUtils.tl("unfrozePlayer", "Plex", Bukkit.getOfflinePlayer(UUID.fromString(player.getUuid())).getName()));
|
|
|
|
Bukkit.getLogger().info("Unfroze");
|
|
|
|
}
|
|
|
|
}.runTaskLater(Plex.get(), 20 * seconds);
|
2022-01-30 21:03:47 +00:00
|
|
|
}
|
|
|
|
else if (punishment.getType() == PunishmentType.MUTE)
|
2020-11-10 02:47:03 +00:00
|
|
|
{
|
|
|
|
player.setMuted(true);
|
|
|
|
Date now = new Date();
|
|
|
|
Date then = punishment.getEndDate();
|
2022-01-30 21:03:47 +00:00
|
|
|
long seconds = TimeUnit.MILLISECONDS.toSeconds(then.getTime() - now.getTime());
|
|
|
|
new BukkitRunnable()
|
|
|
|
{
|
2020-11-10 02:47:03 +00:00
|
|
|
@Override
|
2022-01-30 21:03:47 +00:00
|
|
|
public void run()
|
|
|
|
{
|
2020-11-10 02:47:03 +00:00
|
|
|
player.setMuted(false);
|
|
|
|
}
|
|
|
|
}.runTaskLater(Plex.get(), 20 * seconds);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public void doPunishment(PunishedPlayer player, Punishment punishment)
|
|
|
|
{
|
|
|
|
issuePunishment(player, punishment);
|
|
|
|
insertPunishment(player, punishment);
|
|
|
|
}
|
2020-11-06 18:51:47 +00:00
|
|
|
}
|