2021-01-03 07:21:15 +00:00
|
|
|
package dev.plex.rank;
|
2020-10-27 21:56:05 +00:00
|
|
|
|
2022-02-22 04:51:05 +00:00
|
|
|
import com.google.gson.Gson;
|
2021-01-03 07:21:15 +00:00
|
|
|
import dev.plex.Plex;
|
|
|
|
import dev.plex.player.PlexPlayer;
|
2022-01-04 03:04:39 +00:00
|
|
|
import dev.plex.rank.enums.Rank;
|
2022-02-22 04:51:05 +00:00
|
|
|
import dev.plex.rank.enums.Title;
|
|
|
|
import dev.plex.util.PlexUtils;
|
|
|
|
import lombok.SneakyThrows;
|
|
|
|
import org.json.JSONArray;
|
2022-01-27 21:23:01 +00:00
|
|
|
import org.json.JSONObject;
|
|
|
|
import org.json.JSONTokener;
|
2020-10-27 21:56:05 +00:00
|
|
|
|
2022-02-22 04:51:05 +00:00
|
|
|
import java.io.*;
|
|
|
|
import java.util.Arrays;
|
|
|
|
import java.util.List;
|
|
|
|
import java.util.stream.Collectors;
|
|
|
|
|
2020-10-27 21:56:05 +00:00
|
|
|
public class RankManager
|
|
|
|
{
|
2022-02-22 04:51:05 +00:00
|
|
|
private final File options;
|
2020-10-27 21:56:05 +00:00
|
|
|
|
|
|
|
public RankManager()
|
|
|
|
{
|
2020-10-29 02:36:37 +00:00
|
|
|
File ranksFolder = new File(Plex.get().getDataFolder() + File.separator + "ranks");
|
|
|
|
if (!ranksFolder.exists())
|
|
|
|
{
|
|
|
|
ranksFolder.mkdir();
|
|
|
|
}
|
|
|
|
|
2022-02-22 04:51:05 +00:00
|
|
|
options = new File(ranksFolder, "options.json");
|
2020-10-27 21:56:05 +00:00
|
|
|
}
|
|
|
|
|
2022-02-22 04:51:05 +00:00
|
|
|
@SneakyThrows
|
2020-10-27 21:56:05 +00:00
|
|
|
public void generateDefaultRanks()
|
|
|
|
{
|
2022-02-22 04:51:05 +00:00
|
|
|
if (options.exists())
|
2020-10-27 21:56:05 +00:00
|
|
|
{
|
|
|
|
return;
|
2020-10-28 03:49:56 +00:00
|
|
|
}
|
2022-02-22 04:51:05 +00:00
|
|
|
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();
|
2020-10-28 19:07:02 +00:00
|
|
|
}
|
|
|
|
|
2022-01-29 22:35:48 +00:00
|
|
|
public Rank getRankFromString(String rank)
|
|
|
|
{
|
|
|
|
return Rank.valueOf(rank.toUpperCase());
|
|
|
|
}
|
|
|
|
|
2020-10-28 19:07:02 +00:00
|
|
|
public void importDefaultRanks()
|
|
|
|
{
|
2022-02-22 04:51:05 +00:00
|
|
|
if (!options.exists())
|
2020-10-28 19:07:02 +00:00
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-02-22 04:51:05 +00:00
|
|
|
try (FileInputStream fis = new FileInputStream(options))
|
2020-10-28 19:07:02 +00:00
|
|
|
{
|
2022-02-22 04:51:05 +00:00
|
|
|
JSONTokener tokener = new JSONTokener(fis);
|
2020-10-28 19:07:02 +00:00
|
|
|
JSONObject object = new JSONObject(tokener);
|
2022-02-22 04:51:05 +00:00
|
|
|
|
|
|
|
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)
|
2020-10-28 19:07:02 +00:00
|
|
|
{
|
|
|
|
e.printStackTrace();
|
|
|
|
}
|
2022-02-22 04:51:05 +00:00
|
|
|
}
|
2020-10-28 19:07:02 +00:00
|
|
|
|
2022-02-22 04:51:05 +00:00
|
|
|
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 "";
|
2020-10-28 19:07:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public boolean isAdmin(PlexPlayer plexPlayer)
|
|
|
|
{
|
2020-11-02 00:06:08 +00:00
|
|
|
return !plexPlayer.getRank().isEmpty() && plexPlayer.getRankFromString().isAtLeast(Rank.ADMIN);
|
2020-10-27 21:56:05 +00:00
|
|
|
}
|
2022-01-04 03:04:39 +00:00
|
|
|
|
|
|
|
public boolean isSeniorAdmin(PlexPlayer plexPlayer)
|
|
|
|
{
|
|
|
|
return !plexPlayer.getRank().isEmpty() && plexPlayer.getRankFromString().isAtLeast(Rank.SENIOR_ADMIN);
|
|
|
|
}
|
2020-10-27 21:56:05 +00:00
|
|
|
}
|