Move Plex to API-driven plugin and fix NoClassDefFoundError on startup

This commit is contained in:
Focusvity
2022-04-24 14:16:14 +10:00
parent edeecb51f4
commit f9a577035b
346 changed files with 736 additions and 118 deletions

View File

@ -0,0 +1,142 @@
package dev.plex.cache;
import dev.plex.Plex;
import dev.plex.player.PlexPlayer;
import dev.plex.storage.StorageType;
import java.util.UUID;
/**
* Parent cache class
*/
public class DataUtils
{
/**
* Checks if the player has been on the server before
*
* @param uuid The unique ID of the player
* @return true if the player is registered in the database
*/
public static boolean hasPlayedBefore(UUID uuid)
{
if (Plex.get().getStorageType() == StorageType.MONGODB)
{
return Plex.get().getMongoPlayerData().exists(uuid);
}
else
{
return Plex.get().getSqlPlayerData().exists(uuid);
}
}
public static boolean hasPlayedBefore(String username)
{
if (Plex.get().getStorageType() == StorageType.MONGODB)
{
return Plex.get().getMongoPlayerData().exists(username);
}
else
{
return Plex.get().getSqlPlayerData().exists(username);
}
}
/**
* Gets a player from cache or from the database
*
* @param uuid The unique ID of the player
* @return a PlexPlayer object
* @see PlexPlayer
*/
public static PlexPlayer getPlayer(UUID uuid)
{
if (PlayerCache.getPlexPlayerMap().containsKey(uuid))
{
return PlayerCache.getPlexPlayerMap().get(uuid);
}
if (Plex.get().getStorageType() == StorageType.MONGODB)
{
return Plex.get().getMongoPlayerData().getByUUID(uuid);
}
else
{
return Plex.get().getSqlPlayerData().getByUUID(uuid);
}
}
public static PlexPlayer getPlayer(String username)
{
if (Plex.get().getStorageType() == StorageType.MONGODB)
{
return Plex.get().getMongoPlayerData().getByName(username);
}
else
{
return Plex.get().getSqlPlayerData().getByName(username);
}
}
/**
* Gets a player from cache or from the database
*
* @param ip The IP address of the player.
* @return a PlexPlayer object
* @see PlexPlayer
*/
public static PlexPlayer getPlayerByIP(String ip)
{
PlexPlayer player = PlayerCache.getPlexPlayerMap().values().stream().filter(plexPlayer -> plexPlayer.getIps().contains(ip)).findFirst().orElse(null);
if (player != null)
{
return player;
}
if (Plex.get().getStorageType() == StorageType.MONGODB)
{
return Plex.get().getMongoPlayerData().getByIP(ip);
}
else
{
return Plex.get().getSqlPlayerData().getByIP(ip);
}
}
/**
* Updates a player's information in the database
*
* @param plexPlayer The PlexPlayer to update
* @see PlexPlayer
*/
public static void update(PlexPlayer plexPlayer)
{
if (Plex.get().getStorageType() == StorageType.MONGODB)
{
Plex.get().getMongoPlayerData().update(plexPlayer);
}
else
{
Plex.get().getSqlPlayerData().update(plexPlayer);
}
}
/**
* Inserts a player's information in the database
*
* @param plexPlayer The PlexPlayer to insert
* @see PlexPlayer
*/
public static void insert(PlexPlayer plexPlayer)
{
if (Plex.get().getStorageType() == StorageType.MONGODB)
{
Plex.get().getMongoPlayerData().save(plexPlayer);
}
else
{
Plex.get().getSqlPlayerData().insert(plexPlayer);
}
}
/* REDIS METHODS AT ONE POINT FOR BANS, AND JSON METHODS FOR PUNISHMENTS */
}

View File

@ -0,0 +1,46 @@
package dev.plex.cache;
import com.google.common.collect.Maps;
import dev.plex.player.PlexPlayer;
import java.util.Map;
import java.util.UUID;
/**
* Cache storage
*/
public class PlayerCache
{
/**
* A key/value pair where the key is the unique ID of the Plex Player
*/
private static final Map<UUID, PlexPlayer> plexPlayerMap = Maps.newHashMap();
/**
* A key/value pair where the key is the unique ID of the Punished Player
*/
// private static final Map<UUID, PunishedPlayer> punishedPlayerMap = Maps.newHashMap();
// public static Map<UUID, PunishedPlayer> getPunishedPlayerMap()
// {
// return punishedPlayerMap;
// }
public static Map<UUID, PlexPlayer> getPlexPlayerMap()
{
return plexPlayerMap;
}
/*public static PunishedPlayer getPunishedPlayer(UUID uuid)
{
if (!getPunishedPlayerMap().containsKey(uuid))
{
getPunishedPlayerMap().put(uuid, new PunishedPlayer(uuid));
}
return getPunishedPlayerMap().get(uuid);
}
*/
public static PlexPlayer getPlexPlayer(UUID uuid)
{
return getPlexPlayerMap().get(uuid);
}
}