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
This commit is contained in:
spacerocket62 2020-10-28 12:07:02 -07:00
parent b5ecd35e80
commit e716cee5bc
7 changed files with 174 additions and 51 deletions

View File

@ -6,6 +6,7 @@ import lombok.Setter;
import me.totalfreedom.plex.cache.MongoPlayerData;
import me.totalfreedom.plex.cache.SQLPlayerData;
import me.totalfreedom.plex.config.MainConfig;
import me.totalfreedom.plex.listeners.ChatListener;
import me.totalfreedom.plex.listeners.PlayerListener;
import me.totalfreedom.plex.rank.RankManager;
import me.totalfreedom.plex.storage.MongoConnection;
@ -78,9 +79,11 @@ public class Plex extends JavaPlugin
}
getServer().getPluginManager().registerEvents(new PlayerListener(), this);
getServer().getPluginManager().registerEvents(new ChatListener(), this);
rankManager = new RankManager();
rankManager.generateDefaultRanks();
rankManager.importDefaultRanks();
}

View File

@ -2,7 +2,9 @@ package me.totalfreedom.plex.cache;
import dev.morphia.Datastore;
import dev.morphia.query.Query;
import dev.morphia.query.UpdateOperations;
import dev.morphia.query.Update;
import dev.morphia.query.experimental.filters.Filters;
import dev.morphia.query.experimental.updates.UpdateOperators;
import me.totalfreedom.plex.Plex;
import me.totalfreedom.plex.player.PlexPlayer;
@ -19,36 +21,38 @@ public class MongoPlayerData
public boolean exists(UUID uuid)
{
Query<PlexPlayer> query = datastore.createQuery(PlexPlayer.class);
Query<PlexPlayer> query = datastore.find(PlexPlayer.class)
.filter(Filters.eq("uuid", uuid.toString()));
return query.field("uuid").exists().field("uuid").equal(uuid.toString()).first() != null;
return query.first() != null;
}
public PlexPlayer getByUUID(UUID uuid)
{
if (PlayerCache.getPlexPlayerMap().containsKey(uuid))
{
return PlayerCache.getPlexPlayerMap().get(uuid);
}
Query<PlexPlayer> query2 = datastore.createQuery(PlexPlayer.class).field("uuid").exists().field("uuid").equal(uuid.toString());
Query<PlexPlayer> query2 = datastore.find(PlexPlayer.class).filter(Filters.eq("uuid", uuid.toString()));
return query2.first();
}
public void update(PlexPlayer player)
{
Query<PlexPlayer> filter = datastore.createQuery(PlexPlayer.class)
.field("uuid").equal(player.getUuid());
Query<PlexPlayer> filter = datastore.find(PlexPlayer.class)
.filter(Filters.eq("uuid", player.getUuid()));
UpdateOperations<PlexPlayer> updateOps = datastore.createUpdateOperations(PlexPlayer.class);
Update<PlexPlayer> updateOps = filter
.update(
UpdateOperators.set("name", player.getName()),
UpdateOperators.set("loginMSG", player.getLoginMSG()),
UpdateOperators.set("prefix", player.getPrefix()),
UpdateOperators.set("rank", player.getRank().toLowerCase()),
UpdateOperators.set("ips", player.getIps()),
UpdateOperators.set("coins", player.getCoins()));
updateOps.set("name", player.getName());
updateOps.set("loginMSG", player.getLoginMSG());
updateOps.set("prefix", player.getPrefix());
updateOps.set("rank", player.getRank().toLowerCase());
updateOps.set("ips", player.getIps());
updateOps.set("coins", player.getCoins());
datastore.update(filter, updateOps);
updateOps.execute();
}
public void save(PlexPlayer plexPlayer)

View File

@ -0,0 +1,28 @@
package me.totalfreedom.plex.listeners;
import me.totalfreedom.plex.Plex;
import me.totalfreedom.plex.cache.PlayerCache;
import me.totalfreedom.plex.player.PlexPlayer;
import me.totalfreedom.plex.rank.RankManager;
import net.md_5.bungee.api.ChatColor;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.player.AsyncPlayerChatEvent;
public class ChatListener implements Listener
{
@EventHandler
public void onChat(AsyncPlayerChatEvent event)
{
PlexPlayer plexPlayer = PlayerCache.getPlexPlayerMap().get(event.getPlayer().getUniqueId());
if (!plexPlayer.getPrefix().isEmpty())
{
event.setFormat(String.format("%s %s §7» %s", plexPlayer.getPrefix(), ChatColor.RESET + plexPlayer.getName(), event.getMessage()));
} else if (Plex.get().getRankManager().isAdmin(plexPlayer))
{
event.setFormat(String.format("%s %s §7» %s", plexPlayer.getRankFromString().getPrefix(), ChatColor.RESET + plexPlayer.getName(), event.getMessage()));
}
}
}

View File

@ -8,8 +8,10 @@ import me.totalfreedom.plex.cache.SQLPlayerData;
import me.totalfreedom.plex.player.PlexPlayer;
import me.totalfreedom.plex.player.PunishedPlayer;
import me.totalfreedom.plex.util.PlexLog;
import org.bukkit.ChatColor;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority;
import org.bukkit.event.Listener;
import org.bukkit.event.player.PlayerJoinEvent;
import org.bukkit.event.player.PlayerQuitEvent;
@ -19,8 +21,8 @@ public class PlayerListener implements Listener
private final MongoPlayerData mongoPlayerData = Plex.get().getMongoPlayerData() != null ? Plex.get().getMongoPlayerData() : null;
private final SQLPlayerData sqlPlayerData = Plex.get().getSqlPlayerData() != null ? Plex.get().getSqlPlayerData() : null;
@EventHandler
public void onJoin(PlayerJoinEvent event)
@EventHandler(priority = EventPriority.HIGHEST)
public void onPlayerSetup(PlayerJoinEvent event)
{
Player player = event.getPlayer();
@ -62,10 +64,23 @@ public class PlayerListener implements Listener
PlayerCache.getPlexPlayerMap().put(player.getUniqueId(), plexPlayer); //put them into the cache
PlayerCache.getPunishedPlayerMap().put(player.getUniqueId(), new PunishedPlayer(player.getUniqueId()));
assert plexPlayer != null;
if (Plex.get().getRankManager().isAdmin(plexPlayer))
{
if (!plexPlayer.getLoginMSG().isEmpty())
{
event.setJoinMessage(ChatColor.AQUA + player.getName() + " is " + plexPlayer.getLoginMSG());
} else {
event.setJoinMessage(ChatColor.AQUA + player.getName() + " is " + plexPlayer.getRankFromString().getLoginMSG());
}
@EventHandler
public void onQuit(PlayerQuitEvent event)
}
}
@EventHandler(priority = EventPriority.HIGHEST)
public void onPlayerSave(PlayerQuitEvent event)
{
PlexPlayer plexPlayer = PlayerCache.getPlexPlayerMap().get(event.getPlayer().getUniqueId()); //get the player because it's literally impossible for them to not have an object
@ -81,4 +96,5 @@ public class PlayerListener implements Listener
PlayerCache.getPlexPlayerMap().remove(event.getPlayer().getUniqueId()); //remove them from cache
PlayerCache.getPunishedPlayerMap().remove(event.getPlayer().getUniqueId());
}
}

View File

@ -8,12 +8,15 @@ import me.totalfreedom.plex.rank.enums.Rank;
@Getter
public class DefaultRankObj
{
private final String name;
private final String prefix;
private final String loginMSG;
private final List<String> permissions;
public DefaultRankObj(Rank rank)
{
this.name = rank.name().toUpperCase();
this.prefix = rank.getPrefix();
this.loginMSG = rank.getLoginMSG();
this.permissions = Lists.newArrayList();
permissions.add("example.permission");
}
}

View File

@ -1,14 +1,19 @@
package me.totalfreedom.plex.rank;
import com.google.common.collect.Lists;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.List;
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
{
@ -25,22 +30,20 @@ public class RankManager
{
return;
}
else
{
try
{
defaultRanks.createNewFile();
List<DefaultRankObj> ranks = Lists.newArrayList();
Map<String, DefaultRankObj> rankMap = Maps.newHashMap();
for (Rank rank : Rank.values())
{
ranks.add(new DefaultRankObj(rank));
rankMap.put(rank.name().toUpperCase(), new DefaultRankObj(rank));
}
JSONObject obj = new JSONObject();
if (obj.length() == 0)
{
obj.put("ranks", ranks);
obj.put("ranks", rankMap);
FileWriter writer = new FileWriter(defaultRanks);
writer.append(obj.toString(4));
@ -54,5 +57,38 @@ public class RankManager
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);
}
}

View File

@ -1,7 +1,10 @@
package me.totalfreedom.plex.rank.enums;
import com.google.common.collect.Lists;
import org.bukkit.ChatColor;
import java.util.List;
public enum Rank
{
IMPOSTER(-1, ChatColor.AQUA + "an " + ChatColor.YELLOW + "Imposter", ChatColor.YELLOW + "[Imp]"),
@ -11,13 +14,17 @@ public enum Rank
SENIOR_ADMIN(3, ChatColor.GOLD + "a " + ChatColor.GOLD + "Senior Admin", ChatColor.GOLD + "[SrA]"),
EXECUTIVE(4, ChatColor.RED + "an " + ChatColor.RED + "Executive", ChatColor.RED + "[Exec]");
private final String loginMessage;
private final String prefix;
private String loginMessage;
private String prefix;
private int level;
private List<String> permissions;
Rank(int level, String loginMessage, String prefix)
{
this.level = level;
this.loginMessage = loginMessage;
this.prefix = prefix;
this.permissions = Lists.newArrayList();
}
public String getPrefix()
@ -29,4 +36,30 @@ public enum Rank
{
return loginMessage;
}
public int getLevel() {
return level;
}
public void setLoginMessage(String msg)
{
this.loginMessage = msg;
}
public void setPrefix(String prefix) {
this.prefix = prefix;
}
public boolean isAtleast(Rank rank)
{
return getLevel() >= rank.getLevel();
}
public List<String> getPermissions() {
return permissions;
}
public void setPermissions(List<String> permissions) {
this.permissions = permissions;
}
}