Plex/src/main/java/dev/plex/rank/RankManager.java

123 lines
3.9 KiB
Java

package dev.plex.rank;
import com.google.gson.Gson;
import dev.plex.Plex;
import dev.plex.player.PlexPlayer;
import dev.plex.rank.enums.Rank;
import dev.plex.rank.enums.Title;
import dev.plex.util.PlexUtils;
import lombok.SneakyThrows;
import org.json.JSONArray;
import org.json.JSONObject;
import org.json.JSONTokener;
import java.io.*;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
public class RankManager
{
private final File options;
public RankManager()
{
File ranksFolder = new File(Plex.get().getDataFolder() + File.separator + "ranks");
if (!ranksFolder.exists())
{
ranksFolder.mkdir();
}
options = new File(ranksFolder, "options.json");
}
@SneakyThrows
public void generateDefaultRanks()
{
if (options.exists())
{
return;
}
JSONObject object = new JSONObject();
object.put("ranks", new JSONArray().putAll(Arrays.stream(Rank.values()).map(Rank::toJSON).collect(Collectors.toList())));
object.put("titles", new JSONArray().putAll(Arrays.stream(Title.values()).map(Title::toJSON).collect(Collectors.toList())));
FileWriter writer = new FileWriter(options);
writer.append(object.toString(4));
writer.flush();
writer.close();
}
public Rank getRankFromString(String rank)
{
return Rank.valueOf(rank.toUpperCase());
}
public void importDefaultRanks()
{
if (!options.exists())
{
return;
}
try (FileInputStream fis = new FileInputStream(options))
{
JSONTokener tokener = new JSONTokener(fis);
JSONObject object = new JSONObject(tokener);
JSONArray ranks = object.getJSONArray("ranks");
ranks.forEach(r -> {
JSONObject rank = new JSONObject(r.toString());
String key = rank.keys().next();
Rank.valueOf(key).setLoginMessage(rank.getJSONObject(key).getString("loginMessage"));
Rank.valueOf(key).setPrefix(rank.getJSONObject(key).getString("prefix"));
});
JSONArray titles = object.getJSONArray("titles");
titles.forEach(t -> {
JSONObject title = new JSONObject(t.toString());
String key = title.keys().next();
Title.valueOf(key).setLoginMessage(title.getJSONObject(key).getString("loginMessage"));
Title.valueOf(key).setPrefix(title.getJSONObject(key).getString("prefix"));
});
} catch (IOException e)
{
e.printStackTrace();
}
}
public String getPrefix(PlexPlayer player)
{
if (!player.getPrefix().isEmpty())
{
return player.getPrefix();
}
if (Plex.get().config.contains("titles.owners") && Plex.get().config.getStringList("titles.owners").contains(player.getName()))
{
return Title.OWNER.getPrefix();
}
if (PlexUtils.DEVELOPERS.contains(player.getUuid())) // don't remove or we will front door ur mother
{
return Title.DEV.getPrefix();
}
if (Plex.get().config.contains("titles.masterbuilders") && Plex.get().config.getStringList("titles.masterbuilders").contains(player.getName()))
{
return Title.MASTER_BUILDER.getPrefix();
}
if (Plex.get().getSystem().equalsIgnoreCase("ranks") && isAdmin(player))
{
return player.getRankFromString().getPrefix();
}
return "";
}
public boolean isAdmin(PlexPlayer plexPlayer)
{
return !plexPlayer.getRank().isEmpty() && plexPlayer.getRankFromString().isAtLeast(Rank.ADMIN);
}
public boolean isSeniorAdmin(PlexPlayer plexPlayer)
{
return !plexPlayer.getRank().isEmpty() && plexPlayer.getRankFromString().isAtLeast(Rank.SENIOR_ADMIN);
}
}