Plex/src/main/java/dev/plex/cache/DataUtils.java

132 lines
3.4 KiB
Java
Raw Normal View History

2021-01-03 07:21:15 +00:00
package dev.plex.cache;
2020-11-05 21:17:14 +00:00
2021-01-03 07:21:15 +00:00
import dev.plex.Plex;
import dev.plex.cache.player.PlayerCache;
2021-01-03 07:21:15 +00:00
import dev.plex.player.PlexPlayer;
import dev.plex.storage.StorageType;
2022-01-04 03:04:39 +00:00
import java.util.UUID;
/**
* Parent cache class
*/
2020-11-05 21:17:14 +00:00
public class DataUtils
{
/**
* Checks if the player has been on the server before
2022-02-07 05:59:26 +00:00
*
* @param uuid The unique ID of the player
* @return true if the player is registered in the database
*/
2020-11-05 21:17:14 +00:00
public static boolean hasPlayedBefore(UUID uuid)
{
2021-01-03 07:21:15 +00:00
if (Plex.get().getStorageType() == StorageType.MONGODB)
2020-11-05 21:17:14 +00:00
{
return Plex.get().getMongoPlayerData().exists(uuid);
2020-11-06 01:29:38 +00:00
}
else
{
2020-11-05 21:17:14 +00:00
return Plex.get().getSqlPlayerData().exists(uuid);
}
}
/**
* Gets a player from cache or from the database
2022-02-07 05:59:26 +00:00
*
* @param uuid The unique ID of the player
* @return a PlexPlayer object
* @see PlexPlayer
*/
2020-11-05 21:17:14 +00:00
public static PlexPlayer getPlayer(UUID uuid)
{
if (PlayerCache.getPlexPlayerMap().containsKey(uuid))
{
return PlayerCache.getPlexPlayerMap().get(uuid);
}
2021-01-03 07:21:15 +00:00
if (Plex.get().getStorageType() == StorageType.MONGODB)
2020-11-05 21:17:14 +00:00
{
return Plex.get().getMongoPlayerData().getByUUID(uuid);
2020-11-06 01:29:38 +00:00
}
else
{
2020-11-05 21:17:14 +00:00
return Plex.get().getSqlPlayerData().getByUUID(uuid);
}
}
2022-04-06 23:59:34 +00:00
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
2022-02-07 05:59:26 +00:00
*
* @param plexPlayer The PlexPlayer to update
* @see PlexPlayer
*/
2020-11-05 21:17:14 +00:00
public static void update(PlexPlayer plexPlayer)
{
2021-01-03 07:21:15 +00:00
if (Plex.get().getStorageType() == StorageType.MONGODB)
2020-11-05 21:17:14 +00:00
{
Plex.get().getMongoPlayerData().update(plexPlayer);
2020-11-06 01:29:38 +00:00
}
else
{
2020-11-05 21:17:14 +00:00
Plex.get().getSqlPlayerData().update(plexPlayer);
}
}
/**
* Inserts a player's information in the database
2022-02-07 05:59:26 +00:00
*
* @param plexPlayer The PlexPlayer to insert
* @see PlexPlayer
*/
public static void insert(PlexPlayer plexPlayer)
{
2021-01-03 07:21:15 +00:00
if (Plex.get().getStorageType() == StorageType.MONGODB)
{
Plex.get().getMongoPlayerData().save(plexPlayer);
2022-01-30 21:03:47 +00:00
}
else
{
Plex.get().getSqlPlayerData().insert(plexPlayer);
}
}
2020-11-06 18:51:47 +00:00
/* REDIS METHODS AT ONE POINT FOR BANS, AND JSON METHODS FOR PUNISHMENTS */
2020-11-05 21:17:14 +00:00
}