2021-01-03 07:21:15 +00:00
|
|
|
package dev.plex.player;
|
2020-10-27 20:12:38 +00:00
|
|
|
|
2022-01-04 03:04:39 +00:00
|
|
|
import com.google.common.collect.Lists;
|
2022-02-22 00:20:22 +00:00
|
|
|
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;
|
2022-01-04 03:04:39 +00:00
|
|
|
import dev.plex.event.PunishedPlayerFreezeEvent;
|
|
|
|
import dev.plex.event.PunishedPlayerMuteEvent;
|
2021-01-03 07:21:15 +00:00
|
|
|
import dev.plex.punishment.Punishment;
|
2022-02-04 08:18:07 +00:00
|
|
|
import dev.plex.util.PlexLog;
|
2022-01-27 21:23:01 +00:00
|
|
|
import lombok.AccessLevel;
|
|
|
|
import lombok.Getter;
|
|
|
|
import lombok.Setter;
|
2022-02-22 00:20:22 +00:00
|
|
|
import lombok.SneakyThrows;
|
2022-01-27 21:23:01 +00:00
|
|
|
import org.apache.commons.io.FileUtils;
|
|
|
|
import org.bukkit.Bukkit;
|
2022-02-22 00:20:22 +00:00
|
|
|
import org.json.JSONArray;
|
2022-01-27 21:23:01 +00:00
|
|
|
import org.json.JSONObject;
|
|
|
|
import org.json.JSONTokener;
|
2022-01-04 03:04:39 +00:00
|
|
|
|
2022-02-22 00:20:22 +00:00
|
|
|
import java.io.File;
|
|
|
|
import java.io.FileInputStream;
|
|
|
|
import java.io.FileNotFoundException;
|
|
|
|
import java.io.IOException;
|
|
|
|
import java.nio.charset.StandardCharsets;
|
|
|
|
import java.util.List;
|
|
|
|
import java.util.Map;
|
|
|
|
import java.util.UUID;
|
|
|
|
import java.util.stream.Collectors;
|
|
|
|
|
2020-10-27 20:12:38 +00:00
|
|
|
@Getter
|
2022-02-04 08:18:07 +00:00
|
|
|
public class PunishedPlayer extends PlexBase
|
2020-10-27 20:12:38 +00:00
|
|
|
{
|
|
|
|
//everything in here will be stored in redis
|
|
|
|
@Setter(AccessLevel.NONE)
|
|
|
|
private String uuid;
|
|
|
|
|
|
|
|
private boolean muted;
|
|
|
|
private boolean frozen;
|
|
|
|
|
|
|
|
public PunishedPlayer(UUID playerUUID)
|
|
|
|
{
|
|
|
|
this.uuid = playerUUID.toString();
|
|
|
|
this.muted = false;
|
|
|
|
this.frozen = false;
|
|
|
|
}
|
|
|
|
|
2020-11-03 02:58:56 +00:00
|
|
|
public void setFrozen(boolean frozen)
|
|
|
|
{
|
|
|
|
PunishedPlayerFreezeEvent e = new PunishedPlayerFreezeEvent(this, this.frozen);
|
|
|
|
Bukkit.getServer().getPluginManager().callEvent(e);
|
|
|
|
if (!e.isCancelled())
|
2020-11-06 01:29:38 +00:00
|
|
|
{
|
2020-11-03 02:58:56 +00:00
|
|
|
this.frozen = frozen;
|
2020-11-06 01:29:38 +00:00
|
|
|
}
|
2020-11-03 02:58:56 +00:00
|
|
|
}
|
2020-11-10 02:47:03 +00:00
|
|
|
|
|
|
|
public void setMuted(boolean muted)
|
|
|
|
{
|
|
|
|
PunishedPlayerMuteEvent e = new PunishedPlayerMuteEvent(this, this.muted);
|
|
|
|
Bukkit.getServer().getPluginManager().callEvent(e);
|
|
|
|
if (!e.isCancelled())
|
|
|
|
{
|
|
|
|
this.muted = muted;
|
|
|
|
}
|
|
|
|
}
|
2020-11-10 04:08:37 +00:00
|
|
|
|
|
|
|
public File getPunishmentsFile()
|
|
|
|
{
|
|
|
|
File folder = new File(Plex.get().getDataFolder() + File.separator + "punishments");
|
|
|
|
if (!folder.exists())
|
|
|
|
{
|
|
|
|
folder.mkdir();
|
|
|
|
}
|
|
|
|
|
|
|
|
File file = new File(folder, getUuid() + ".json");
|
|
|
|
if (!file.exists())
|
|
|
|
{
|
2022-01-30 21:03:47 +00:00
|
|
|
try
|
|
|
|
{
|
2020-11-10 04:08:37 +00:00
|
|
|
file.createNewFile();
|
2022-02-22 00:20:22 +00:00
|
|
|
} catch (IOException e)
|
2022-01-30 21:03:47 +00:00
|
|
|
{
|
2020-11-10 04:08:37 +00:00
|
|
|
e.printStackTrace();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return file;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2022-02-22 00:20:22 +00:00
|
|
|
@SneakyThrows
|
|
|
|
public void convertPunishments()
|
|
|
|
{
|
|
|
|
if (!plugin.getRedisConnection().isEnabled()) return;
|
|
|
|
List<Punishment> punishments = Lists.newArrayList();
|
|
|
|
|
|
|
|
File file = getPunishmentsFile();
|
|
|
|
//Converting from File to Redis
|
|
|
|
if (isNotEmpty(file))
|
|
|
|
{
|
2022-02-22 07:11:37 +00:00
|
|
|
PlexLog.debug("Starting converting punishments from file to Redis for " + uuid + "...");
|
2022-02-22 00:20:22 +00:00
|
|
|
JSONTokener tokener = new JSONTokener(new FileInputStream(file));
|
|
|
|
JSONObject object = new JSONObject(tokener);
|
|
|
|
JSONArray array = object.getJSONObject(getUuid()).getJSONArray("punishments");
|
|
|
|
for (int i = 0; i < array.toList().size(); i++)
|
|
|
|
{
|
|
|
|
Punishment punishment = Punishment.fromJson(array.get(i).toString());
|
|
|
|
punishments.add(punishment);
|
|
|
|
}
|
|
|
|
PlexLog.debug("Successfully converted all file punishments into array (" + punishments.size() + ")");
|
|
|
|
if (!punishments.isEmpty())
|
|
|
|
{
|
|
|
|
Map<String, List<String>> filesList = Maps.newHashMap();
|
|
|
|
filesList.put("punishments", punishments.stream().map(Punishment::toJSON).collect(Collectors.toList()));
|
|
|
|
JSONObject obj = new JSONObject().put(uuid, filesList);
|
|
|
|
if (plugin.getRedisConnection().getJedis().exists(uuid))
|
|
|
|
{
|
|
|
|
PlexLog.debug("File and Redis Matches? " + plugin.getRedisConnection().getJedis().get(uuid).equalsIgnoreCase(obj.toString()));
|
|
|
|
if (!plugin.getRedisConnection().getJedis().get(uuid).equalsIgnoreCase(obj.toString()))
|
|
|
|
{
|
|
|
|
plugin.getRedisConnection().getJedis().set(uuid, obj.toString());
|
|
|
|
PlexLog.debug("Updated Redis Punishments to match with file");
|
|
|
|
}
|
|
|
|
} else
|
|
|
|
{
|
|
|
|
plugin.getRedisConnection().getJedis().set(uuid, obj.toString());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@SneakyThrows
|
2020-11-10 04:08:37 +00:00
|
|
|
public List<Punishment> getPunishments()
|
|
|
|
{
|
|
|
|
List<Punishment> punishments = Lists.newArrayList();
|
|
|
|
|
2022-02-04 08:18:07 +00:00
|
|
|
if (plugin.getRedisConnection().isEnabled())
|
|
|
|
{
|
|
|
|
PlexLog.debug("Getting punishments from Redis...");
|
2022-02-22 00:20:22 +00:00
|
|
|
if (!plugin.getRedisConnection().getJedis().exists(uuid)) return punishments;
|
2022-02-04 08:18:07 +00:00
|
|
|
String strObj = plugin.getRedisConnection().getJedis().get(uuid);
|
2022-02-22 00:20:22 +00:00
|
|
|
if (strObj.isEmpty() || !strObj.startsWith("{")) return punishments;
|
|
|
|
JSONObject object = new JSONObject(strObj);
|
|
|
|
object.getJSONObject(uuid).getJSONArray("punishments").forEach(obj ->
|
2022-02-04 08:18:07 +00:00
|
|
|
{
|
2022-02-22 00:20:22 +00:00
|
|
|
JSONObject converted = new JSONObject(obj.toString());
|
|
|
|
if (converted.isNull("active"))
|
|
|
|
{
|
|
|
|
converted.put("active", false);
|
|
|
|
}
|
|
|
|
Punishment punishment = Punishment.fromJson(converted.toString());
|
2022-02-04 08:18:07 +00:00
|
|
|
punishments.add(punishment);
|
|
|
|
});
|
2022-02-04 19:30:05 +00:00
|
|
|
plugin.getRedisConnection().getJedis().close();
|
2022-02-04 08:18:07 +00:00
|
|
|
return punishments;
|
|
|
|
}
|
|
|
|
|
2020-11-10 04:08:37 +00:00
|
|
|
File file = getPunishmentsFile();
|
|
|
|
|
|
|
|
if (isNotEmpty(file))
|
|
|
|
{
|
2022-01-30 21:03:47 +00:00
|
|
|
try
|
|
|
|
{
|
2022-02-04 08:18:07 +00:00
|
|
|
PlexLog.debug("Getting punishments from locally stored JSON files...");
|
2020-11-10 04:08:37 +00:00
|
|
|
JSONTokener tokener = new JSONTokener(new FileInputStream(file));
|
|
|
|
JSONObject object = new JSONObject(tokener);
|
2022-01-30 21:03:47 +00:00
|
|
|
object.getJSONObject(getUuid()).getJSONArray("punishments").forEach(obj ->
|
|
|
|
{
|
2020-11-10 04:08:37 +00:00
|
|
|
Punishment punishment = Punishment.fromJson(obj.toString());
|
|
|
|
punishments.add(punishment);
|
|
|
|
});
|
2022-02-22 00:20:22 +00:00
|
|
|
} catch (FileNotFoundException e)
|
2022-01-30 21:03:47 +00:00
|
|
|
{
|
2020-11-10 04:08:37 +00:00
|
|
|
e.printStackTrace();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return punishments;
|
|
|
|
}
|
|
|
|
|
2022-01-30 21:03:47 +00:00
|
|
|
private boolean isNotEmpty(File file)
|
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
2020-11-10 04:08:37 +00:00
|
|
|
return !FileUtils.readFileToString(file, StandardCharsets.UTF_8).trim().isEmpty();
|
2022-02-22 00:20:22 +00:00
|
|
|
} catch (IOException e)
|
2022-01-30 21:03:47 +00:00
|
|
|
{
|
2020-11-10 04:08:37 +00:00
|
|
|
e.printStackTrace();
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
2020-10-27 20:12:38 +00:00
|
|
|
}
|