start punishment system

This commit is contained in:
spacerocket62 2020-11-06 10:51:47 -08:00
parent 87abb0a964
commit 2d18c16358
7 changed files with 168 additions and 1 deletions

View File

@ -44,6 +44,13 @@
<version>20200518</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.8.0</version>
<scope>compile</scope>
</dependency>
<!-- DATABASING -->
<dependency>

View File

@ -8,6 +8,7 @@ import me.totalfreedom.plex.cache.SQLPlayerData;
import me.totalfreedom.plex.config.Config;
import me.totalfreedom.plex.handlers.CommandHandler;
import me.totalfreedom.plex.handlers.ListenerHandler;
import me.totalfreedom.plex.punishment.PunishmentManager;
import me.totalfreedom.plex.rank.RankManager;
import me.totalfreedom.plex.storage.MongoConnection;
import me.totalfreedom.plex.storage.RedisConnection;
@ -36,6 +37,8 @@ public class Plex extends JavaPlugin
private RankManager rankManager;
private PunishmentManager punishmentManager;
private AdminList adminList;
public static Plex get()
@ -97,6 +100,9 @@ public class Plex extends JavaPlugin
rankManager.importDefaultRanks();
PlexLog.log("Rank Manager initialized");
punishmentManager = new PunishmentManager();
PlexLog.log("Punishment Manager initialized");
adminList = new AdminList();
generateWorlds();

View File

@ -8,6 +8,8 @@ import org.bukkit.Bukkit;
public class DataUtils
{
/* PLEX PLAYER METHODS */
public static boolean hasPlayedBefore(UUID uuid)
{
if (Plex.get().getStorageType() == StorageType.MONGO)
@ -64,4 +66,6 @@ public class DataUtils
}
}
/* REDIS METHODS AT ONE POINT FOR BANS, AND JSON METHODS FOR PUNISHMENTS */
}

View File

@ -11,6 +11,8 @@ import me.totalfreedom.plex.command.impl.FionnCMD;
import me.totalfreedom.plex.listener.PlexListener;
import me.totalfreedom.plex.player.PlexPlayer;
import me.totalfreedom.plex.player.PunishedPlayer;
import me.totalfreedom.plex.punishment.Punishment;
import me.totalfreedom.plex.punishment.PunishmentType;
import me.totalfreedom.plex.util.PlexLog;
import org.bukkit.ChatColor;
import org.bukkit.Location;
@ -99,8 +101,14 @@ public class PlayerListener extends PlexListener
{
event.setJoinMessage(ChatColor.AQUA + player.getName() + " is " + plexPlayer.getRankFromString().getLoginMSG());
}
}
/*Punishment test = new Punishment(player.getUniqueId(), player.getUniqueId());
test.setPunishedUsername(player.getName());
test.setReason("hii");
test.setType(PunishmentType.FREEZE);
plugin.getPunishmentManager().insertPunishment(PlayerCache.getPunishedPlayer(player.getUniqueId()), test);*/
}
// saving the player's data

View File

@ -0,0 +1,51 @@
package me.totalfreedom.plex.punishment;
import com.google.common.collect.Lists;
import com.google.gson.Gson;
import lombok.Getter;
import lombok.Setter;
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 long 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 = 0;
}
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,84 @@
package me.totalfreedom.plex.punishment;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import me.totalfreedom.plex.Plex;
import me.totalfreedom.plex.cache.DataUtils;
import me.totalfreedom.plex.player.PunishedPlayer;
import me.totalfreedom.plex.util.PlexLog;
import org.apache.commons.io.FileUtils;
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.List;
import java.util.Map;
public class PunishmentManager
{
public void insertPunishment(PunishedPlayer player, Punishment punishment)
{
File folder = new File(Plex.get().getDataFolder() + File.separator + "punishments");
if (!folder.exists())
{
folder.mkdir();
}
File file = new File(folder, player.getUuid() + ".json");
if (!file.exists())
{
try {
file.createNewFile();
PlexLog.log("Created new punishment file for " + player.getUuid() + " (" + DataUtils.getPlayer(punishment.getPunished()).getName() + ")");
} catch (IOException e) {
e.printStackTrace();
}
}
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;
}
}

View File

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