This commit is contained in:
2021-01-03 01:21:15 -06:00
parent 8ecc1d2fce
commit 5bafa1122c
91 changed files with 452 additions and 460 deletions

View File

@ -0,0 +1,52 @@
package dev.plex.punishment;
import com.google.common.collect.Lists;
import com.google.gson.Gson;
import lombok.Getter;
import lombok.Setter;
import java.util.Date;
import java.util.List;
import java.util.UUID;
@Getter
@Setter
public class Punishment
{
private final UUID punished;
private final UUID punisher;
private final List<String> IPS;
private String punishedUsername;
private PunishmentType type;
private String reason;
private boolean customTime;
private Date endDate;
public Punishment(UUID punished, UUID punisher)
{
this.punished = punished;
this.punisher = punisher;
this.IPS = Lists.newArrayList();
this.punishedUsername = "";
this.type = null;
this.reason = "";
this.customTime = false;
this.endDate = null;
}
public String toJSON()
{
return new Gson().toJson(this);
}
public static Punishment fromJson(String json)
{
return new Gson().fromJson(json, Punishment.class);
}
}

View File

@ -0,0 +1,123 @@
package dev.plex.punishment;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import dev.plex.Plex;
import dev.plex.banning.Ban;
import dev.plex.player.PunishedPlayer;
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 java.io.File;
import java.io.FileInputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.UUID;
import java.util.concurrent.TimeUnit;
public class PunishmentManager
{
public void insertPunishment(PunishedPlayer player, Punishment punishment)
{
File file = player.getPunishmentsFile();
try {
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());
FileWriter writer = new FileWriter(file);
writer.append(object.toString(8));
writer.flush();
writer.close();
} else {
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);
FileWriter writer = new FileWriter(file);
writer.append(object.toString(8));
writer.flush();
writer.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
private boolean isNotEmpty(File file) {
try {
return !FileUtils.readFileToString(file, StandardCharsets.UTF_8).trim().isEmpty();
} catch (IOException e) {
e.printStackTrace();
}
return false;
}
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);
} else if (punishment.getType() == PunishmentType.FREEZE)
{
player.setFrozen(true);
Date now = new Date();
Date then = punishment.getEndDate();
long seconds = TimeUnit.MILLISECONDS.toSeconds(then.getTime() - now.getTime());
new BukkitRunnable() {
@Override
public void run() {
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);
} else if (punishment.getType() == PunishmentType.MUTE)
{
player.setMuted(true);
Date now = new Date();
Date then = punishment.getEndDate();
long seconds = TimeUnit.MILLISECONDS.toSeconds(then.getTime() - now.getTime());
new BukkitRunnable() {
@Override
public void run() {
player.setMuted(false);
}
}.runTaskLater(Plex.get(), 20 * seconds);
}
}
public void doPunishment(PunishedPlayer player, Punishment punishment)
{
issuePunishment(player, punishment);
insertPunishment(player, punishment);
}
}

View File

@ -0,0 +1,7 @@
package dev.plex.punishment;
public enum PunishmentType
{
MUTE, FREEZE, BAN;
}