find player by ip + reload system on /plex reload

This commit is contained in:
Taah 2022-03-30 23:50:53 -07:00
parent d350ff4542
commit 6e768c0e0f
4 changed files with 103 additions and 0 deletions

View File

@ -53,6 +53,31 @@ public class DataUtils
} }
} }
/**
* 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);
}
}
/** /**
* Gets a player from cache or from the database * Gets a player from cache or from the database
* *

View File

@ -7,6 +7,8 @@ import dev.morphia.query.experimental.filters.Filters;
import dev.morphia.query.experimental.updates.UpdateOperators; import dev.morphia.query.experimental.updates.UpdateOperators;
import dev.plex.Plex; import dev.plex.Plex;
import dev.plex.player.PlexPlayer; import dev.plex.player.PlexPlayer;
import java.util.Collections;
import java.util.UUID; import java.util.UUID;
/** /**
@ -59,6 +61,25 @@ public class MongoPlayerData
return query2.first(); return query2.first();
} }
/**
* Gets the player from cache or from mongo's database
*
* @param ip The IP address of the player.
* @return a PlexPlayer object
* @see PlexPlayer
*/
public PlexPlayer getByIP(String ip)
{
PlexPlayer player = PlayerCache.getPlexPlayerMap().values().stream().filter(plexPlayer -> plexPlayer.getIps().contains(ip)).findFirst().orElse(null);
if (player != null)
{
return player;
}
Query<PlexPlayer> query2 = datastore.find(PlexPlayer.class).filter(Filters.in("ips", Collections.singleton(ip)));
return query2.first();
}
/** /**
* Updates a player's information in the mongo database * Updates a player's information in the mongo database
* *

View File

@ -10,6 +10,7 @@ import java.sql.ResultSet;
import java.sql.SQLException; import java.sql.SQLException;
import java.util.List; import java.util.List;
import java.util.UUID; import java.util.UUID;
import java.util.concurrent.CompletableFuture;
/** /**
* SQL fetching utilities for players * SQL fetching utilities for players
@ -92,6 +93,61 @@ public class SQLPlayerData
return null; return null;
} }
/**
* Gets the player from cache or from the SQL database
*
* @param ip The IP address of the player.
* @return a PlexPlayer object
* @see PlexPlayer
*/
public PlexPlayer getByIP(String ip)
{
PlexPlayer player = PlayerCache.getPlexPlayerMap().values().stream().filter(plexPlayer -> plexPlayer.getIps().contains(ip)).findFirst().orElse(null);
if (player != null)
{
return player;
}
try (Connection con = Plex.get().getSqlConnection().getCon())
{
PreparedStatement statement = con.prepareStatement("select * from `players` where json_search(ips, ?, ?) IS NOT NULL LIMIT 1");
statement.setString(1, "one");
statement.setString(2, ip);
ResultSet set = statement.executeQuery();
PlexPlayer plexPlayer = null;
while (set.next())
{
String uuid = set.getString("uuid");
String name = set.getString("name");
String loginMSG = set.getString("login_msg");
String prefix = set.getString("prefix");
String rankName = set.getString("rank").toUpperCase();
long coins = set.getLong("coins");
boolean vanished = set.getBoolean("vanished");
boolean commandspy = set.getBoolean("commandspy");
List<String> ips = new Gson().fromJson(set.getString("ips"), new TypeToken<List<String>>()
{
}.getType());
plexPlayer = new PlexPlayer(UUID.fromString(uuid));
plexPlayer.setName(name);
plexPlayer.setLoginMessage(loginMSG);
plexPlayer.setPrefix(prefix);
plexPlayer.setRank(rankName);
plexPlayer.setIps(ips);
plexPlayer.setCoins(coins);
plexPlayer.setVanished(vanished);
plexPlayer.setCommandSpy(commandspy);
}
return plexPlayer;
}
catch (SQLException throwables)
{
throwables.printStackTrace();
}
return null;
}
/** /**
* Updates a player's information in the SQL database * Updates a player's information in the SQL database
* *

View File

@ -50,6 +50,7 @@ public class PlexCMD extends PlexCommand
plugin.getRankManager().importDefaultRanks(); plugin.getRankManager().importDefaultRanks();
send(sender, "Imported ranks"); send(sender, "Imported ranks");
send(sender, "Plex successfully reloaded."); send(sender, "Plex successfully reloaded.");
plugin.setSystem(plugin.config.getString("commands.permissions"));
return null; return null;
} }
else if (args[0].equalsIgnoreCase("redis")) else if (args[0].equalsIgnoreCase("redis"))