Plex/src/main/java/me/totalfreedom/plex/rank/RankManager.java
spacerocket62 e716cee5bc so apparently mongo methods needed to be updated, weird amirite
added chat listener
added login msgs in a way ??
added setters to the ranks
changed the default rank obj so it puts prefix, loginmsg, and perms only instead
rankmanager imports any rank changes on startup
plex main class kinda just vibing
2020-10-28 12:07:02 -07:00

95 lines
2.8 KiB
Java

package me.totalfreedom.plex.rank;
import com.google.common.collect.Maps;
import me.totalfreedom.plex.Plex;
import me.totalfreedom.plex.player.PlexPlayer;
import me.totalfreedom.plex.rank.enums.Rank;
import me.totalfreedom.plex.util.PlexLog;
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.Map;
import java.util.stream.Collectors;
public class RankManager
{
private final File defaultRanks;
public RankManager()
{
defaultRanks = new File(new File(Plex.get().getDataFolder() + File.separator + "ranks"), "default-ranks.json");
}
public void generateDefaultRanks()
{
if (defaultRanks.exists())
{
return;
}
try
{
defaultRanks.createNewFile();
Map<String, DefaultRankObj> rankMap = Maps.newHashMap();
for (Rank rank : Rank.values())
{
rankMap.put(rank.name().toUpperCase(), new DefaultRankObj(rank));
}
JSONObject obj = new JSONObject();
if (obj.length() == 0)
{
obj.put("ranks", rankMap);
FileWriter writer = new FileWriter(defaultRanks);
writer.append(obj.toString(4));
writer.flush();
writer.close();
PlexLog.log("Generating default-ranks.json");
}
}
catch (IOException e)
{
e.printStackTrace();
}
}
public void importDefaultRanks()
{
if (!defaultRanks.exists())
{
return;
}
try
{
FileInputStream stream = new FileInputStream(defaultRanks);
JSONTokener tokener = new JSONTokener(stream);
JSONObject object = new JSONObject(tokener);
JSONObject rankObj = object.getJSONObject("ranks");
for (Rank rank : Rank.values())
{
if (rankObj.isNull(rank.name().toUpperCase())) continue;
rank.setLoginMessage(rankObj.getJSONObject(rank.name().toUpperCase()).getString("loginMSG"));
rank.setPrefix(rankObj.getJSONObject(rank.name().toUpperCase()).getString("prefix")); //should i even be doing this
rank.setPermissions(rankObj.getJSONObject(rank.name().toUpperCase()).getJSONArray("permissions").toList().stream().map(Object::toString).collect(Collectors.toList()));
}
}
catch (IOException e)
{
e.printStackTrace();
}
}
public boolean isAdmin(PlexPlayer plexPlayer)
{
return !plexPlayer.getRank().isEmpty() && plexPlayer.getRankFromString().isAtleast(Rank.ADMIN);
}
}