2021-01-03 07:21:15 +00:00
|
|
|
package dev.plex.rank;
|
2020-10-27 21:56:05 +00:00
|
|
|
|
2020-10-28 19:07:02 +00:00
|
|
|
import com.google.common.collect.Maps;
|
2021-01-03 07:21:15 +00:00
|
|
|
import dev.plex.Plex;
|
|
|
|
import dev.plex.player.PlexPlayer;
|
|
|
|
import dev.plex.util.PlexLog;
|
2020-10-28 19:07:02 +00:00
|
|
|
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;
|
2021-01-03 07:21:15 +00:00
|
|
|
import dev.plex.rank.enums.Rank;
|
2020-11-06 01:29:38 +00:00
|
|
|
import org.json.JSONObject;
|
|
|
|
import org.json.JSONTokener;
|
2020-10-27 21:56:05 +00:00
|
|
|
|
|
|
|
public class RankManager
|
|
|
|
{
|
2020-10-28 03:49:56 +00:00
|
|
|
private final File defaultRanks;
|
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();
|
|
|
|
}
|
|
|
|
|
|
|
|
defaultRanks = new File(ranksFolder, "default-ranks.json");
|
2020-10-27 21:56:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public void generateDefaultRanks()
|
|
|
|
{
|
|
|
|
if (defaultRanks.exists())
|
|
|
|
{
|
|
|
|
return;
|
2020-10-28 03:49:56 +00:00
|
|
|
}
|
2020-10-28 19:07:02 +00:00
|
|
|
try
|
2020-10-28 03:49:56 +00:00
|
|
|
{
|
2020-10-28 19:07:02 +00:00
|
|
|
defaultRanks.createNewFile();
|
|
|
|
|
|
|
|
Map<String, DefaultRankObj> rankMap = Maps.newHashMap();
|
|
|
|
for (Rank rank : Rank.values())
|
2020-10-28 03:49:56 +00:00
|
|
|
{
|
2020-10-28 19:07:02 +00:00
|
|
|
rankMap.put(rank.name().toUpperCase(), new DefaultRankObj(rank));
|
2020-10-28 03:49:56 +00:00
|
|
|
}
|
2020-10-28 19:07:02 +00:00
|
|
|
|
|
|
|
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())
|
2020-10-28 03:49:56 +00:00
|
|
|
{
|
2020-11-06 01:29:38 +00:00
|
|
|
if (rankObj.isNull(rank.name().toUpperCase()))
|
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
2020-10-28 19:07:02 +00:00
|
|
|
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()));
|
|
|
|
|
2020-10-27 21:56:05 +00:00
|
|
|
}
|
|
|
|
}
|
2020-10-28 19:07:02 +00:00
|
|
|
catch (IOException e)
|
|
|
|
{
|
|
|
|
e.printStackTrace();
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
}
|