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

151 lines
4.9 KiB
Java
Raw Normal View History

2021-01-03 07:21:15 +00:00
package dev.plex.rank;
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;
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;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Arrays;
import java.util.stream.Collectors;
public class RankManager
{
private final File options;
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();
}
options = new File(ranksFolder, "options.json");
}
@SneakyThrows
public void generateDefaultRanks()
{
if (options.exists())
{
return;
2020-10-28 03:49:56 +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();
}
2022-01-29 22:35:48 +00:00
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 String getLoginMessage(PlexPlayer player)
{
if (!player.getLoginMessage().isEmpty())
{
return player.getLoginMessage();
}
if (Plex.get().config.contains("titles.owners") && Plex.get().config.getStringList("titles.owners").contains(player.getName()))
{
return Title.OWNER.getLoginMessage();
}
if (PlexUtils.DEVELOPERS.contains(player.getUuid())) // don't remove or we will front door ur mother
{
return Title.DEV.getLoginMessage();
}
if (Plex.get().config.contains("titles.masterbuilders") && Plex.get().config.getStringList("titles.masterbuilders").contains(player.getName()))
{
return Title.MASTER_BUILDER.getLoginMessage();
}
if (Plex.get().getSystem().equalsIgnoreCase("ranks") && isAdmin(player))
{
return player.getRankFromString().getLoginMessage();
}
return "";
}
public boolean isAdmin(PlexPlayer plexPlayer)
{
return !plexPlayer.getRank().isEmpty() && plexPlayer.getRankFromString().isAtLeast(Rank.ADMIN);
}
2022-01-04 03:04:39 +00:00
public boolean isSeniorAdmin(PlexPlayer plexPlayer)
{
return !plexPlayer.getRank().isEmpty() && plexPlayer.getRankFromString().isAtLeast(Rank.SENIOR_ADMIN);
}
}