A bit more work on the API

This commit is contained in:
Focusvity 2022-05-18 20:31:15 +10:00
parent 7fe28ae8cb
commit 7efa151157
No known key found for this signature in database
GPG Key ID: 85AD157561ABE94B
19 changed files with 89 additions and 47 deletions

View File

@ -0,0 +1,11 @@
package dev.plex.api;
import java.util.Map;
import java.util.UUID;
public interface IPlayerCache<T>
{
Map<UUID, T> getPlexPlayerMap();
T getPlexPlayer(UUID uuid);
}

View File

@ -0,0 +1,6 @@
package dev.plex.api;
public interface PlexApi
{
IPlayerCache<?> getPlayerCache();
}

View File

@ -0,0 +1,6 @@
package dev.plex.api;
public interface PlexApiProvider
{
PlexApi getApi();
}

View File

@ -4,7 +4,7 @@ import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.format.NamedTextColor;
import org.json.JSONObject;
public interface IRank
public interface IRank<T>
{
int getLevel();
@ -20,7 +20,7 @@ public interface IRank
NamedTextColor getColor();
boolean isAtLeast(IRank rank);
boolean isAtLeast(T rank);
JSONObject toJSON();
}

View File

@ -2,7 +2,8 @@ package dev.plex;
import dev.plex.admin.Admin;
import dev.plex.admin.AdminList;
import dev.plex.api.permission.IPermissionHandler;
import dev.plex.api.PlexApi;
import dev.plex.api.PlexApiProvider;
import dev.plex.api.plugin.PlexPlugin;
import dev.plex.cache.DataUtils;
import dev.plex.cache.PlayerCache;
@ -36,15 +37,12 @@ import dev.plex.world.CustomWorld;
import java.io.File;
import lombok.Getter;
import lombok.Setter;
import net.milkbowl.vault.chat.Chat;
import net.milkbowl.vault.permission.Permission;
import org.bstats.bukkit.Metrics;
import org.bukkit.Bukkit;
import org.bukkit.plugin.RegisteredServiceProvider;
@Getter
@Setter
public class Plex extends PlexPlugin
public class Plex extends PlexPlugin implements PlexApiProvider
{
private static Plex plugin;
@ -53,6 +51,8 @@ public class Plex extends PlexPlugin
public Config indefBans;
public Config commands;
private PlexProvider provider;
public File modulesFolder;
private StorageType storageType = StorageType.SQLITE;
@ -62,6 +62,8 @@ public class Plex extends PlexPlugin
private MongoConnection mongoConnection;
private RedisConnection redisConnection;
private PlayerCache playerCache;
private MongoPlayerData mongoPlayerData;
private SQLPlayerData sqlPlayerData;
@ -114,6 +116,7 @@ public class Plex extends PlexPlugin
{
config.load();
messages.load();
// Don't add default entries to indefinite ban file
indefBans.load(false);
commands.load(false);
@ -212,6 +215,8 @@ public class Plex extends PlexPlugin
}
this.getServer().getMessenger().registerOutgoingPluginChannel(this, "BungeeCord");
provider = new PlexProvider();
moduleManager.enableModules();
}
@ -220,7 +225,7 @@ public class Plex extends PlexPlugin
{
Bukkit.getOnlinePlayers().forEach(player ->
{
PlexPlayer plexPlayer = PlayerCache.getPlexPlayerMap().get(player.getUniqueId()); //get the player because it's literally impossible for them to not have an object
PlexPlayer plexPlayer = playerCache.getPlexPlayerMap().get(player.getUniqueId()); //get the player because it's literally impossible for them to not have an object
if (plugin.getRankManager().isAdmin(plexPlayer))
{
@ -262,7 +267,7 @@ public class Plex extends PlexPlugin
Bukkit.getOnlinePlayers().forEach(player ->
{
PlexPlayer plexPlayer = DataUtils.getPlayer(player.getUniqueId());
PlayerCache.getPlexPlayerMap().put(player.getUniqueId(), plexPlayer); //put them into the cache
playerCache.getPlexPlayerMap().put(player.getUniqueId(), plexPlayer); //put them into the cache
if (plugin.getRankManager().isAdmin(plexPlayer))
{
Admin admin = new Admin(plexPlayer.getUuid());
@ -272,4 +277,10 @@ public class Plex extends PlexPlugin
}
});
}
@Override
public PlexApi getApi()
{
return provider;
}
}

View File

@ -0,0 +1,14 @@
package dev.plex;
import dev.plex.api.IPlayerCache;
import dev.plex.api.PlexApi;
import dev.plex.player.PlexPlayer;
public class PlexProvider implements PlexApi
{
@Override
public IPlayerCache<PlexPlayer> getPlayerCache()
{
return Plex.get().getPlayerCache();
}
}

View File

@ -55,9 +55,9 @@ public class DataUtils
public static PlexPlayer getPlayer(UUID uuid, boolean loadExtraData)
{
if (PlayerCache.getPlexPlayerMap().containsKey(uuid))
if (Plex.get().getPlayerCache().getPlexPlayerMap().containsKey(uuid))
{
return PlayerCache.getPlexPlayerMap().get(uuid);
return Plex.get().getPlayerCache().getPlexPlayerMap().get(uuid);
}
if (Plex.get().getStorageType() == StorageType.MONGODB)
@ -77,7 +77,7 @@ public class DataUtils
public static PlexPlayer getPlayer(String username, boolean loadExtraData)
{
Optional<PlexPlayer> plexPlayer = PlayerCache.getPlexPlayerMap().values().stream().filter(player -> player.getName().equalsIgnoreCase(username)).findFirst();
Optional<PlexPlayer> plexPlayer = Plex.get().getPlayerCache().getPlexPlayerMap().values().stream().filter(player -> player.getName().equalsIgnoreCase(username)).findFirst();
if (plexPlayer.isPresent())
{
return plexPlayer.get();
@ -102,7 +102,7 @@ public class DataUtils
*/
public static PlexPlayer getPlayerByIP(String ip)
{
PlexPlayer player = PlayerCache.getPlexPlayerMap().values().stream().filter(plexPlayer -> plexPlayer.getIps().contains(ip)).findFirst().orElse(null);
PlexPlayer player = Plex.get().getPlayerCache().getPlexPlayerMap().values().stream().filter(plexPlayer -> plexPlayer.getIps().contains(ip)).findFirst().orElse(null);
if (player != null)
{
return player;

View File

@ -1,6 +1,7 @@
package dev.plex.cache;
import com.google.common.collect.Maps;
import dev.plex.api.IPlayerCache;
import dev.plex.player.PlexPlayer;
import java.util.Map;
import java.util.UUID;
@ -9,7 +10,7 @@ import java.util.UUID;
* Cache storage
*/
public class PlayerCache
public class PlayerCache implements IPlayerCache<PlexPlayer>
{
/**
* A key/value pair where the key is the unique ID of the Plex Player
@ -25,7 +26,7 @@ public class PlayerCache
// {
// return punishedPlayerMap;
// }
public static Map<UUID, PlexPlayer> getPlexPlayerMap()
public Map<UUID, PlexPlayer> getPlexPlayerMap()
{
return plexPlayerMap;
}
@ -39,7 +40,7 @@ public class PlayerCache
return getPunishedPlayerMap().get(uuid);
}
*/
public static PlexPlayer getPlexPlayer(UUID uuid)
public PlexPlayer getPlexPlayer(UUID uuid)
{
return getPlexPlayerMap().get(uuid);
}

View File

@ -131,7 +131,7 @@ public abstract class PlexCommand extends Command implements PluginIdentifiableC
if (sender instanceof Player player)
{
PlexPlayer plexPlayer = PlayerCache.getPlexPlayerMap().get(player.getUniqueId());
PlexPlayer plexPlayer = plugin.getPlayerCache().getPlexPlayerMap().get(player.getUniqueId());
if (plugin.getSystem().equalsIgnoreCase("ranks"))
{
@ -587,7 +587,7 @@ public abstract class PlexCommand extends Command implements PluginIdentifiableC
protected PlexPlayer getOnlinePlexPlayer(String name)
{
Player player = getNonNullPlayer(name);
PlexPlayer plexPlayer = PlayerCache.getPlexPlayer(player.getUniqueId());
PlexPlayer plexPlayer = plugin.getPlayerCache().getPlexPlayer(player.getUniqueId());
if (plexPlayer == null)
{
throw new PlayerNotFoundException();

View File

@ -38,7 +38,7 @@ public class AdminChatCMD extends PlexCommand
{
if (plugin.getSystem().equalsIgnoreCase("ranks"))
{
PlexPlayer plexPlayer = PlayerCache.getPlexPlayerMap().get(player.getUniqueId());
PlexPlayer plexPlayer = plugin.getPlayerCache().getPlexPlayerMap().get(player.getUniqueId());
if (plexPlayer.getRankFromString().isAtLeast(Rank.ADMIN) && plexPlayer.isAdminActive())
{
player.sendMessage(PlexUtils.messageComponent("adminChatFormat", sender.getName(), message));

View File

@ -42,7 +42,7 @@ public class ChatListener extends PlexListener
@Override
public void doChat(AsyncChatEvent event)
{
PlexPlayer plexPlayer = PlayerCache.getPlexPlayerMap().get(event.getPlayer().getUniqueId());
PlexPlayer plexPlayer = plugin.getPlayerCache().getPlexPlayerMap().get(event.getPlayer().getUniqueId());
Component prefix = plugin.getRankManager().getPrefix(plexPlayer);
if (prefix != null)

View File

@ -25,7 +25,7 @@ public class CommandListener extends PlexListener
@EventHandler(priority = EventPriority.HIGHEST)
public void onPlayerCommandPreprocess(PlayerCommandPreprocessEvent event)
{
Bukkit.getOnlinePlayers().stream().filter(pl -> PlayerCache.getPlexPlayer(pl.getUniqueId()).isCommandSpy() && PlayerCache.getPlexPlayer(pl.getUniqueId()).isAdminActive()).forEach(pl ->
Bukkit.getOnlinePlayers().stream().filter(pl -> plugin.getPlayerCache().getPlexPlayer(pl.getUniqueId()).isCommandSpy() && plugin.getPlayerCache().getPlexPlayer(pl.getUniqueId()).isAdminActive()).forEach(pl ->
{
Player player = event.getPlayer();
String command = event.getMessage();
@ -103,13 +103,11 @@ public class CommandListener extends PlexListener
BlockedCommand cmd = cmdRef.get();
switch (cmd.getRequiredLevel().toLowerCase(Locale.ROOT))
{
case "e" ->
{
case "e" -> {
event.setCancelled(true);
event.getPlayer().sendMessage(cmd.getMessage());
}
case "a" ->
{
case "a" -> {
if (plexPlayer.isAdminActive() && plexPlayer.getRankFromString().isAtLeast(Rank.ADMIN))
{
return;
@ -117,8 +115,7 @@ public class CommandListener extends PlexListener
event.setCancelled(true);
event.getPlayer().sendMessage(cmd.getMessage());
}
case "s" ->
{
case "s" -> {
if (plexPlayer.isAdminActive() && plexPlayer.getRankFromString().isAtLeast(Rank.SENIOR_ADMIN))
{
return;

View File

@ -69,7 +69,7 @@ public class PlayerListener<T> extends PlexListener
DataUtils.update(plexPlayer);
}
}
PlayerCache.getPlexPlayerMap().put(player.getUniqueId(), plexPlayer);
plugin.getPlayerCache().getPlexPlayerMap().put(player.getUniqueId(), plexPlayer);
if (plexPlayer.isLockedUp())
{
player.openInventory(player.getInventory());
@ -108,7 +108,7 @@ public class PlayerListener<T> extends PlexListener
@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
PlexPlayer plexPlayer = plugin.getPlayerCache().getPlexPlayerMap().get(event.getPlayer().getUniqueId()); //get the player because it's literally impossible for them to not have an object
if (plugin.getRankManager().isAdmin(plexPlayer))
{
@ -116,7 +116,7 @@ public class PlayerListener<T> extends PlexListener
}
DataUtils.update(plexPlayer);
PlayerCache.getPlexPlayerMap().remove(event.getPlayer().getUniqueId()); //remove them from cache
plugin.getPlayerCache().getPlexPlayerMap().remove(event.getPlayer().getUniqueId()); //remove them from cache
}
@EventHandler(priority = EventPriority.HIGHEST)

View File

@ -155,7 +155,7 @@ public class WorldListener extends PlexListener
private boolean checkPermission(Player player, boolean showMessage)
{
PlexPlayer plexPlayer = PlayerCache.getPlexPlayerMap().get(player.getUniqueId());
PlexPlayer plexPlayer = plugin.getPlayerCache().getPlexPlayerMap().get(player.getUniqueId());
World world = player.getWorld();
if (plugin.getSystem().equalsIgnoreCase("permissions"))
{

View File

@ -1,6 +1,7 @@
package dev.plex.menu;
import com.google.common.collect.Lists;
import dev.plex.Plex;
import dev.plex.cache.DataUtils;
import dev.plex.cache.PlayerCache;
import dev.plex.player.PlexPlayer;
@ -164,7 +165,7 @@ public class PunishedPlayerMenu extends AbstractMenu
SkullMeta meta = (SkullMeta)item.getItemMeta();
OfflinePlayer player = meta.getOwningPlayer();
assert player != null;
PlexPlayer punishedPlayer = DataUtils.getPlayer(player.getUniqueId()) == null ? null : PlayerCache.getPlexPlayer(player.getUniqueId());
PlexPlayer punishedPlayer = DataUtils.getPlayer(player.getUniqueId()) == null ? null : Plex.get().getPlayerCache().getPlexPlayer(player.getUniqueId());
if (punishedPlayer == null)
{
event.getWhoClicked().sendMessage(ChatColor.RED + "This player does not exist. Try doing /punishments <player> instead.");

View File

@ -9,7 +9,7 @@ import net.kyori.adventure.text.format.NamedTextColor;
import org.json.JSONObject;
@Getter
public enum Rank implements IRank
public enum Rank implements IRank<Rank>
{
IMPOSTOR(-1, "<aqua>an <yellow>Impostor<reset>", "Impostor", "<dark_gray>[<yellow>Imp<dark_gray>]", NamedTextColor.YELLOW),
NONOP(0, "a <white>Non-Op<reset>", "Non-Op", "", NamedTextColor.WHITE),
@ -41,11 +41,6 @@ public enum Rank implements IRank
this.color = color;
}
public boolean isAtLeast(IRank rank)
{
return this.level >= rank.getLevel();
}
public boolean isAtLeast(Rank rank)
{
return this.level >= rank.getLevel();

View File

@ -61,9 +61,9 @@ public class MongoPlayerData
*/
public PlexPlayer getByUUID(UUID uuid)
{
if (PlayerCache.getPlexPlayerMap().containsKey(uuid))
if (Plex.get().getPlayerCache().getPlexPlayerMap().containsKey(uuid))
{
return PlayerCache.getPlexPlayerMap().get(uuid);
return Plex.get().getPlayerCache().getPlexPlayerMap().get(uuid);
}
Query<PlexPlayer> query2 = datastore.find(PlexPlayer.class).filter(Filters.eq("uuid", uuid));
@ -72,7 +72,7 @@ public class MongoPlayerData
public PlexPlayer getByName(String username)
{
PlexPlayer player = PlayerCache.getPlexPlayerMap().values().stream().filter(plexPlayer -> plexPlayer.getName().equalsIgnoreCase(username)).findFirst().orElse(null);
PlexPlayer player = Plex.get().getPlayerCache().getPlexPlayerMap().values().stream().filter(plexPlayer -> plexPlayer.getName().equalsIgnoreCase(username)).findFirst().orElse(null);
if (player != null)
{
return player;
@ -91,7 +91,7 @@ public class MongoPlayerData
*/
public PlexPlayer getByIP(String ip)
{
PlexPlayer player = PlayerCache.getPlexPlayerMap().values().stream().filter(plexPlayer -> plexPlayer.getIps().contains(ip)).findFirst().orElse(null);
PlexPlayer player = Plex.get().getPlayerCache().getPlexPlayerMap().values().stream().filter(plexPlayer -> plexPlayer.getIps().contains(ip)).findFirst().orElse(null);
if (player != null)
{
return player;

View File

@ -70,9 +70,9 @@ public class SQLPlayerData
*/
public PlexPlayer getByUUID(UUID uuid, boolean loadExtraData)
{
if (PlayerCache.getPlexPlayerMap().containsKey(uuid))
if (Plex.get().getPlayerCache().getPlexPlayerMap().containsKey(uuid))
{
return PlayerCache.getPlexPlayerMap().get(uuid);
return Plex.get().getPlayerCache().getPlexPlayerMap().get(uuid);
}
try (Connection con = Plex.get().getSqlConnection().getCon())
@ -120,7 +120,7 @@ public class SQLPlayerData
public PlexPlayer getByName(String username, boolean loadExtraData)
{
PlexPlayer player = PlayerCache.getPlexPlayerMap().values().stream().filter(plexPlayer -> plexPlayer.getName().equalsIgnoreCase(username)).findFirst().orElse(null);
PlexPlayer player = Plex.get().getPlayerCache().getPlexPlayerMap().values().stream().filter(plexPlayer -> plexPlayer.getName().equalsIgnoreCase(username)).findFirst().orElse(null);
if (player != null)
{
return player;
@ -177,7 +177,7 @@ public class SQLPlayerData
*/
public PlexPlayer getByIP(String ip)
{
PlexPlayer player = PlayerCache.getPlexPlayerMap().values().stream().filter(plexPlayer -> plexPlayer.getIps().contains(ip)).findFirst().orElse(null);
PlexPlayer player = Plex.get().getPlayerCache().getPlexPlayerMap().values().stream().filter(plexPlayer -> plexPlayer.getIps().contains(ip)).findFirst().orElse(null);
if (player != null)
{
return player;

View File

@ -220,7 +220,7 @@ public class PlexUtils implements PlexBase
public static void broadcastToAdmins(Component component)
{
Bukkit.getOnlinePlayers().stream().filter(pl -> PlayerCache.getPlexPlayer(pl.getUniqueId()).isAdminActive()).forEach(pl ->
Bukkit.getOnlinePlayers().stream().filter(pl -> plugin.getPlayerCache().getPlexPlayer(pl.getUniqueId()).isAdminActive()).forEach(pl ->
{
pl.sendMessage(component);
});