mirror of
https://github.com/plexusorg/Plex.git
synced 2024-12-22 17:17:37 +00:00
added sql support so now people can use mongo or sql
This commit is contained in:
parent
d4cf8b7414
commit
b8357b6d1d
@ -4,6 +4,7 @@ import lombok.AccessLevel;
|
|||||||
import lombok.Getter;
|
import lombok.Getter;
|
||||||
import lombok.Setter;
|
import lombok.Setter;
|
||||||
import me.totalfreedom.plex.cache.MongoPlayerData;
|
import me.totalfreedom.plex.cache.MongoPlayerData;
|
||||||
|
import me.totalfreedom.plex.cache.SQLPlayerData;
|
||||||
import me.totalfreedom.plex.config.Config;
|
import me.totalfreedom.plex.config.Config;
|
||||||
import me.totalfreedom.plex.config.YamlConfig;
|
import me.totalfreedom.plex.config.YamlConfig;
|
||||||
import me.totalfreedom.plex.listeners.PlayerListener;
|
import me.totalfreedom.plex.listeners.PlayerListener;
|
||||||
@ -27,6 +28,7 @@ public class Plex extends JavaPlugin
|
|||||||
private MongoConnection mongoConnection;
|
private MongoConnection mongoConnection;
|
||||||
|
|
||||||
private MongoPlayerData mongoPlayerData;
|
private MongoPlayerData mongoPlayerData;
|
||||||
|
private SQLPlayerData sqlPlayerData;
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onLoad()
|
public void onLoad()
|
||||||
@ -50,6 +52,8 @@ public class Plex extends JavaPlugin
|
|||||||
if (storageType == StorageType.MONGO)
|
if (storageType == StorageType.MONGO)
|
||||||
{
|
{
|
||||||
mongoPlayerData = new MongoPlayerData();
|
mongoPlayerData = new MongoPlayerData();
|
||||||
|
} else {
|
||||||
|
sqlPlayerData = new SQLPlayerData();
|
||||||
}
|
}
|
||||||
|
|
||||||
getServer().getPluginManager().registerEvents(new PlayerListener(), this);
|
getServer().getPluginManager().registerEvents(new PlayerListener(), this);
|
||||||
|
@ -13,9 +13,6 @@ import java.util.UUID;
|
|||||||
public class MongoPlayerData
|
public class MongoPlayerData
|
||||||
{
|
{
|
||||||
|
|
||||||
private Map<UUID, PunishedPlayer> punishedPlayerMap = Maps.newHashMap();
|
|
||||||
private Map<UUID, PlexPlayer> plexPlayerMap = Maps.newHashMap();
|
|
||||||
|
|
||||||
private PlexPlayerDAO plexPlayerDAO;
|
private PlexPlayerDAO plexPlayerDAO;
|
||||||
|
|
||||||
public MongoPlayerData()
|
public MongoPlayerData()
|
||||||
@ -36,9 +33,9 @@ public class MongoPlayerData
|
|||||||
|
|
||||||
public PlexPlayer getByUUID(UUID uuid) {
|
public PlexPlayer getByUUID(UUID uuid) {
|
||||||
|
|
||||||
if (plexPlayerMap.containsKey(uuid))
|
if (PlayerCache.getPlexPlayerMap().containsKey(uuid))
|
||||||
{
|
{
|
||||||
return plexPlayerMap.get(uuid);
|
return PlayerCache.getPlexPlayerMap().get(uuid);
|
||||||
}
|
}
|
||||||
Query<PlexPlayer> query2 = plexPlayerDAO.createQuery().field("uuid").exists().field("uuid").equal(uuid.toString());
|
Query<PlexPlayer> query2 = plexPlayerDAO.createQuery().field("uuid").exists().field("uuid").equal(uuid.toString());
|
||||||
return query2.first();
|
return query2.first();
|
||||||
@ -59,15 +56,6 @@ public class MongoPlayerData
|
|||||||
plexPlayerDAO.update(filter, updateOps);
|
plexPlayerDAO.update(filter, updateOps);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
public Map<UUID, PlexPlayer> getPlexPlayerMap() {
|
|
||||||
return plexPlayerMap;
|
|
||||||
}
|
|
||||||
|
|
||||||
public Map<UUID, PunishedPlayer> getPunishedPlayerMap() {
|
|
||||||
return punishedPlayerMap;
|
|
||||||
}
|
|
||||||
|
|
||||||
public PlexPlayerDAO getPlexPlayerDAO() {
|
public PlexPlayerDAO getPlexPlayerDAO() {
|
||||||
return plexPlayerDAO;
|
return plexPlayerDAO;
|
||||||
}
|
}
|
||||||
|
23
src/main/java/me/totalfreedom/plex/cache/PlayerCache.java
vendored
Normal file
23
src/main/java/me/totalfreedom/plex/cache/PlayerCache.java
vendored
Normal file
@ -0,0 +1,23 @@
|
|||||||
|
package me.totalfreedom.plex.cache;
|
||||||
|
|
||||||
|
import com.google.common.collect.Maps;
|
||||||
|
import me.totalfreedom.plex.player.PlexPlayer;
|
||||||
|
import me.totalfreedom.plex.player.PunishedPlayer;
|
||||||
|
|
||||||
|
import java.util.Map;
|
||||||
|
import java.util.UUID;
|
||||||
|
|
||||||
|
public class PlayerCache
|
||||||
|
{
|
||||||
|
|
||||||
|
private static Map<UUID, PlexPlayer> plexPlayerMap = Maps.newHashMap();
|
||||||
|
private static Map<UUID, PunishedPlayer> punishedPlayerMap = Maps.newHashMap();
|
||||||
|
|
||||||
|
public static Map<UUID, PunishedPlayer> getPunishedPlayerMap() {
|
||||||
|
return punishedPlayerMap;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static Map<UUID, PlexPlayer> getPlexPlayerMap() {
|
||||||
|
return plexPlayerMap;
|
||||||
|
}
|
||||||
|
}
|
115
src/main/java/me/totalfreedom/plex/cache/SQLPlayerData.java
vendored
Normal file
115
src/main/java/me/totalfreedom/plex/cache/SQLPlayerData.java
vendored
Normal file
@ -0,0 +1,115 @@
|
|||||||
|
package me.totalfreedom.plex.cache;
|
||||||
|
|
||||||
|
import com.google.common.collect.Maps;
|
||||||
|
import com.google.common.reflect.TypeToken;
|
||||||
|
import com.google.gson.Gson;
|
||||||
|
import dev.morphia.query.Query;
|
||||||
|
import dev.morphia.query.UpdateOperations;
|
||||||
|
import me.totalfreedom.plex.Plex;
|
||||||
|
import me.totalfreedom.plex.player.PlexPlayer;
|
||||||
|
import me.totalfreedom.plex.player.PunishedPlayer;
|
||||||
|
import me.totalfreedom.plex.rank.Rank;
|
||||||
|
|
||||||
|
import java.sql.Connection;
|
||||||
|
import java.sql.PreparedStatement;
|
||||||
|
import java.sql.ResultSet;
|
||||||
|
import java.sql.SQLException;
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Map;
|
||||||
|
import java.util.UUID;
|
||||||
|
|
||||||
|
public class SQLPlayerData
|
||||||
|
{
|
||||||
|
|
||||||
|
private final String SELECT = "SELECT * FROM `players` WHERE uuid=?";
|
||||||
|
private final String UPDATE = "UPDATE `players` SET name=?, login_msg=?, prefix=?, rank=?, ips=? WHERE uuid=?";
|
||||||
|
private final String INSERT = "INSERT INTO `players` (`uuid`, `name`, `login_msg`, `prefix`, `rank`, `ips`) VALUES (?, ?, ?, ?, ?, ?);";
|
||||||
|
|
||||||
|
public boolean exists(UUID uuid)
|
||||||
|
{
|
||||||
|
try (Connection con = Plex.get().getSqlConnection().getCon())
|
||||||
|
{
|
||||||
|
PreparedStatement statement = con.prepareStatement(SELECT);
|
||||||
|
statement.setString(1, uuid.toString());
|
||||||
|
ResultSet set = statement.executeQuery();
|
||||||
|
return set.next();
|
||||||
|
}
|
||||||
|
catch (SQLException throwables) {
|
||||||
|
throwables.printStackTrace();
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
public PlexPlayer getByUUID(UUID uuid)
|
||||||
|
{
|
||||||
|
if (PlayerCache.getPlexPlayerMap().containsKey(uuid))
|
||||||
|
{
|
||||||
|
return PlayerCache.getPlexPlayerMap().get(uuid);
|
||||||
|
}
|
||||||
|
|
||||||
|
try (Connection con = Plex.get().getSqlConnection().getCon())
|
||||||
|
{
|
||||||
|
PreparedStatement statement = con.prepareStatement(SELECT);
|
||||||
|
statement.setString(1, uuid.toString());
|
||||||
|
ResultSet set = statement.executeQuery();
|
||||||
|
PlexPlayer plexPlayer = new PlexPlayer(uuid);
|
||||||
|
while (set.next())
|
||||||
|
{
|
||||||
|
String name = set.getString("name");
|
||||||
|
String loginMSG = set.getString("login_msg");
|
||||||
|
String prefix = set.getString("prefix");
|
||||||
|
String rankName = set.getString("rank").toUpperCase();
|
||||||
|
Rank rank = Rank.valueOf(rankName);
|
||||||
|
List<String> ips = new Gson().fromJson(set.getString("ips"), new TypeToken<List<String>>(){}.getType());
|
||||||
|
plexPlayer.setName(name);
|
||||||
|
plexPlayer.setLoginMSG(loginMSG);
|
||||||
|
plexPlayer.setPrefix(prefix);
|
||||||
|
plexPlayer.setRank(rankName.isEmpty() ? null : rank);
|
||||||
|
plexPlayer.setIps(ips);
|
||||||
|
}
|
||||||
|
return plexPlayer;
|
||||||
|
}
|
||||||
|
catch (SQLException throwables) {
|
||||||
|
throwables.printStackTrace();
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void update(PlexPlayer player)
|
||||||
|
{
|
||||||
|
try (Connection con = Plex.get().getSqlConnection().getCon())
|
||||||
|
{
|
||||||
|
PreparedStatement statement = con.prepareStatement(UPDATE);
|
||||||
|
statement.setString(1, player.getName());
|
||||||
|
statement.setString(2, player.getLoginMSG());
|
||||||
|
statement.setString(3, player.getPrefix());
|
||||||
|
statement.setString(4, player.getRank() == null ? "" : player.getRank().name().toLowerCase());
|
||||||
|
statement.setString(5, new Gson().toJson(player.getIps()));
|
||||||
|
statement.setString(6, player.getUuid());
|
||||||
|
statement.executeUpdate();
|
||||||
|
}
|
||||||
|
catch (SQLException throwables) {
|
||||||
|
throwables.printStackTrace();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public void insert(PlexPlayer player)
|
||||||
|
{
|
||||||
|
try (Connection con = Plex.get().getSqlConnection().getCon())
|
||||||
|
{
|
||||||
|
PreparedStatement statement = con.prepareStatement(INSERT);
|
||||||
|
statement.setString(1, player.getUuid());
|
||||||
|
statement.setString(2, player.getName());
|
||||||
|
statement.setString(3, player.getLoginMSG());
|
||||||
|
statement.setString(4, player.getPrefix());
|
||||||
|
statement.setString(5, player.getRank() == null ? "" : player.getRank().name().toLowerCase());
|
||||||
|
statement.setString(6, new Gson().toJson(player.getIps()));
|
||||||
|
statement.execute();
|
||||||
|
}
|
||||||
|
catch (SQLException throwables) {
|
||||||
|
throwables.printStackTrace();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -2,6 +2,8 @@ package me.totalfreedom.plex.listeners;
|
|||||||
|
|
||||||
import me.totalfreedom.plex.Plex;
|
import me.totalfreedom.plex.Plex;
|
||||||
import me.totalfreedom.plex.cache.MongoPlayerData;
|
import me.totalfreedom.plex.cache.MongoPlayerData;
|
||||||
|
import me.totalfreedom.plex.cache.PlayerCache;
|
||||||
|
import me.totalfreedom.plex.cache.SQLPlayerData;
|
||||||
import me.totalfreedom.plex.player.PlexPlayer;
|
import me.totalfreedom.plex.player.PlexPlayer;
|
||||||
import me.totalfreedom.plex.player.PunishedPlayer;
|
import me.totalfreedom.plex.player.PunishedPlayer;
|
||||||
import me.totalfreedom.plex.util.PlexLog;
|
import me.totalfreedom.plex.util.PlexLog;
|
||||||
@ -17,46 +19,67 @@ public class PlayerListener implements Listener
|
|||||||
{
|
{
|
||||||
|
|
||||||
private MongoPlayerData mongoPlayerData = Plex.get().getMongoPlayerData() != null ? Plex.get().getMongoPlayerData() : null;
|
private MongoPlayerData mongoPlayerData = Plex.get().getMongoPlayerData() != null ? Plex.get().getMongoPlayerData() : null;
|
||||||
|
private SQLPlayerData sqlPlayerData = Plex.get().getSqlPlayerData() != null ? Plex.get().getSqlPlayerData() : null;
|
||||||
|
|
||||||
@EventHandler
|
@EventHandler
|
||||||
public void onJoin(PlayerJoinEvent event)
|
public void onJoin(PlayerJoinEvent event)
|
||||||
{
|
{
|
||||||
Player player = event.getPlayer();
|
Player player = event.getPlayer();
|
||||||
|
|
||||||
|
PlexPlayer plexPlayer = null;
|
||||||
|
|
||||||
if (mongoPlayerData != null) // Alright, check if we're saving with Mongo first
|
if (mongoPlayerData != null) // Alright, check if we're saving with Mongo first
|
||||||
{
|
{
|
||||||
if (!mongoPlayerData.exists(player.getUniqueId())) //okay, we're saving with mongo! now check if the player's document exists
|
if (!mongoPlayerData.exists(player.getUniqueId())) //okay, we're saving with mongo! now check if the player's document exists
|
||||||
{
|
{
|
||||||
PlexLog.log("AYO THIS MAN DONT EXIST"); // funi msg
|
PlexLog.log("AYO THIS MAN DONT EXIST"); // funi msg
|
||||||
PlexPlayer plexPlayer = new PlexPlayer(player.getUniqueId()); //it doesn't! okay so now create the object
|
plexPlayer = new PlexPlayer(player.getUniqueId()); //it doesn't! okay so now create the object
|
||||||
plexPlayer.setName(player.getName()); //set the name of the player
|
plexPlayer.setName(player.getName()); //set the name of the player
|
||||||
plexPlayer.setIps(Arrays.asList(player.getAddress().getAddress().getHostAddress().trim())); //set the arraylist of ips
|
plexPlayer.setIps(Arrays.asList(player.getAddress().getAddress().getHostAddress().trim())); //set the arraylist of ips
|
||||||
|
|
||||||
Plex.get().getMongoPlayerData().getPlexPlayerMap().put(player.getUniqueId(), plexPlayer); //put them into the cache
|
mongoPlayerData.getPlexPlayerDAO().save(plexPlayer); //and put their document in mongo collection
|
||||||
Plex.get().getMongoPlayerData().getPunishedPlayerMap().put(player.getUniqueId(), new PunishedPlayer(player.getUniqueId()));
|
|
||||||
|
|
||||||
Plex.get().getMongoPlayerData().getPlexPlayerDAO().save(plexPlayer); //and put their document in mongo collection
|
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
PlexPlayer plexPlayer = Plex.get().getMongoPlayerData().getByUUID(player.getUniqueId()); //oh they do exist!
|
plexPlayer = mongoPlayerData.getByUUID(player.getUniqueId()); //oh they do exist!
|
||||||
|
plexPlayer.setName(plexPlayer.getName()); //set the name!
|
||||||
|
}
|
||||||
|
} else if (sqlPlayerData != null)
|
||||||
|
{
|
||||||
|
if (!sqlPlayerData.exists(player.getUniqueId())) //okay, we're saving with mongo! now check if the player's document exists
|
||||||
|
{
|
||||||
|
PlexLog.log("AYO THIS MAN DONT EXIST"); // funi msg
|
||||||
|
plexPlayer = new PlexPlayer(player.getUniqueId()); //it doesn't! okay so now create the object
|
||||||
|
plexPlayer.setName(player.getName()); //set the name of the player
|
||||||
|
plexPlayer.setIps(Arrays.asList(player.getAddress().getAddress().getHostAddress().trim())); //set the arraylist of ips
|
||||||
|
|
||||||
|
sqlPlayerData.insert(plexPlayer); //and put their document in mongo collection
|
||||||
|
|
||||||
|
} else {
|
||||||
|
plexPlayer = sqlPlayerData.getByUUID(player.getUniqueId()); //oh they do exist!
|
||||||
plexPlayer.setName(plexPlayer.getName()); //set the name!
|
plexPlayer.setName(plexPlayer.getName()); //set the name!
|
||||||
Plex.get().getMongoPlayerData().getPlexPlayerMap().put(player.getUniqueId(), plexPlayer); //cache them!
|
|
||||||
Plex.get().getMongoPlayerData().getPunishedPlayerMap().put(player.getUniqueId(), new PunishedPlayer(player.getUniqueId()));
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
PlayerCache.getPlexPlayerMap().put(player.getUniqueId(), plexPlayer); //put them into the cache
|
||||||
|
PlayerCache.getPunishedPlayerMap().put(player.getUniqueId(), new PunishedPlayer(player.getUniqueId()));
|
||||||
}
|
}
|
||||||
|
|
||||||
@EventHandler
|
@EventHandler
|
||||||
public void onQuit(PlayerQuitEvent event)
|
public void onQuit(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
|
||||||
|
|
||||||
if (mongoPlayerData != null) //back to mongo checking
|
if (mongoPlayerData != null) //back to mongo checking
|
||||||
{
|
{
|
||||||
PlexPlayer plexPlayer = Plex.get().getMongoPlayerData().getPlexPlayerMap().get(event.getPlayer().getUniqueId()); //get the player because it's literally impossible for them to not have an object
|
mongoPlayerData.update(plexPlayer); //update the player's document
|
||||||
Plex.get().getMongoPlayerData().update(plexPlayer); //update the player's document
|
} else if (sqlPlayerData != null)
|
||||||
|
{
|
||||||
Plex.get().getMongoPlayerData().getPlexPlayerMap().remove(event.getPlayer().getUniqueId()); //remove them from cache
|
sqlPlayerData.update(plexPlayer);
|
||||||
Plex.get().getMongoPlayerData().getPunishedPlayerMap().remove(event.getPlayer().getUniqueId());
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
PlayerCache.getPlexPlayerMap().remove(event.getPlayer().getUniqueId()); //remove them from cache
|
||||||
|
PlayerCache.getPunishedPlayerMap().remove(event.getPlayer().getUniqueId());
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -47,7 +47,6 @@ public class SQLConnection
|
|||||||
"\t`ips` VARCHAR(65535),\n" +
|
"\t`ips` VARCHAR(65535),\n" +
|
||||||
"\tPRIMARY KEY (`uuid`)\n" +
|
"\tPRIMARY KEY (`uuid`)\n" +
|
||||||
");").execute();
|
");").execute();
|
||||||
PlexLog.log("Successfully created table `players`!");
|
|
||||||
} catch (SQLException throwables) {
|
} catch (SQLException throwables) {
|
||||||
throwables.printStackTrace();
|
throwables.printStackTrace();
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user