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.player.PlexPlayer;
|
|
|
|
import dev.plex.storage.StorageType;
|
2022-01-04 03:04:39 +00:00
|
|
|
import java.util.UUID;
|
|
|
|
|
2022-02-05 23:14:23 +00:00
|
|
|
/**
|
|
|
|
* Parent cache class
|
|
|
|
*/
|
2020-11-05 21:17:14 +00:00
|
|
|
public class DataUtils
|
|
|
|
{
|
2022-02-05 23:14:23 +00:00
|
|
|
/**
|
|
|
|
* Checks if the player has been on the server before
|
2022-02-07 05:59:26 +00:00
|
|
|
*
|
2022-02-05 23:14:23 +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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-10 22:02:50 +00:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-02-05 23:14:23 +00:00
|
|
|
/**
|
|
|
|
* Gets a player from cache or from the database
|
2022-02-07 05:59:26 +00:00
|
|
|
*
|
2022-02-05 23:14:23 +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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-03-31 06:50:53 +00:00
|
|
|
/**
|
|
|
|
* 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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-02-05 23:14:23 +00:00
|
|
|
/**
|
|
|
|
* Updates a player's information in the database
|
2022-02-07 05:59:26 +00:00
|
|
|
*
|
2022-02-05 23:14:23 +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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-02-05 23:14:23 +00:00
|
|
|
/**
|
|
|
|
* Inserts a player's information in the database
|
2022-02-07 05:59:26 +00:00
|
|
|
*
|
2022-02-05 23:14:23 +00:00
|
|
|
* @param plexPlayer The PlexPlayer to insert
|
|
|
|
* @see PlexPlayer
|
|
|
|
*/
|
2020-11-06 18:19:38 +00:00
|
|
|
public static void insert(PlexPlayer plexPlayer)
|
|
|
|
{
|
2021-01-03 07:21:15 +00:00
|
|
|
if (Plex.get().getStorageType() == StorageType.MONGODB)
|
2020-11-06 18:19:38 +00:00
|
|
|
{
|
|
|
|
Plex.get().getMongoPlayerData().save(plexPlayer);
|
2022-01-30 21:03:47 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2020-11-06 18:19:38 +00:00
|
|
|
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
|
|
|
}
|