mirror of
https://github.com/plexusorg/Plex.git
synced 2024-12-22 09:07:37 +00:00
yes
you can call me a god mongodb storing is done
This commit is contained in:
parent
5162f3fcfb
commit
d4cf8b7414
12
pom.xml
12
pom.xml
@ -30,18 +30,30 @@
|
||||
<version>1.16.3-R0.1-SNAPSHOT</version>
|
||||
<scope>provided</scope>
|
||||
</dependency>
|
||||
|
||||
<!-- UTILITIES -->
|
||||
|
||||
<dependency>
|
||||
<groupId>org.projectlombok</groupId>
|
||||
<artifactId>lombok</artifactId>
|
||||
<version>1.18.12</version>
|
||||
<scope>compile</scope>
|
||||
</dependency>
|
||||
|
||||
<!-- DATABASING -->
|
||||
|
||||
<dependency>
|
||||
<groupId>dev.morphia.morphia</groupId>
|
||||
<artifactId>core</artifactId>
|
||||
<version>1.5.2</version>
|
||||
<scope>compile</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>redis.clients</groupId>
|
||||
<artifactId>jedis</artifactId>
|
||||
<version>3.3.0</version>
|
||||
<scope>compile</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
|
@ -3,8 +3,10 @@ package me.totalfreedom.plex;
|
||||
import lombok.AccessLevel;
|
||||
import lombok.Getter;
|
||||
import lombok.Setter;
|
||||
import me.totalfreedom.plex.cache.MongoPlayerData;
|
||||
import me.totalfreedom.plex.config.Config;
|
||||
import me.totalfreedom.plex.config.YamlConfig;
|
||||
import me.totalfreedom.plex.listeners.PlayerListener;
|
||||
import me.totalfreedom.plex.storage.MongoConnection;
|
||||
import me.totalfreedom.plex.storage.SQLConnection;
|
||||
import me.totalfreedom.plex.storage.StorageType;
|
||||
@ -19,11 +21,13 @@ public class Plex extends JavaPlugin
|
||||
@Setter(AccessLevel.NONE)
|
||||
private static Plex plugin;
|
||||
|
||||
private StorageType storageType;
|
||||
private StorageType storageType = StorageType.SQLITE;
|
||||
|
||||
private SQLConnection sqlConnection;
|
||||
private MongoConnection mongoConnection;
|
||||
|
||||
private MongoPlayerData mongoPlayerData;
|
||||
|
||||
@Override
|
||||
public void onLoad()
|
||||
{
|
||||
@ -42,6 +46,14 @@ public class Plex extends JavaPlugin
|
||||
public void onEnable()
|
||||
{
|
||||
PlexUtils.testConnections();
|
||||
|
||||
if (storageType == StorageType.MONGO)
|
||||
{
|
||||
mongoPlayerData = new MongoPlayerData();
|
||||
}
|
||||
|
||||
getServer().getPluginManager().registerEvents(new PlayerListener(), this);
|
||||
PlexLog.log(storageType.name());
|
||||
}
|
||||
|
||||
@Override
|
||||
|
74
src/main/java/me/totalfreedom/plex/cache/MongoPlayerData.java
vendored
Normal file
74
src/main/java/me/totalfreedom/plex/cache/MongoPlayerData.java
vendored
Normal file
@ -0,0 +1,74 @@
|
||||
package me.totalfreedom.plex.cache;
|
||||
|
||||
import com.google.common.collect.Maps;
|
||||
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 java.util.Map;
|
||||
import java.util.UUID;
|
||||
|
||||
public class MongoPlayerData
|
||||
{
|
||||
|
||||
private Map<UUID, PunishedPlayer> punishedPlayerMap = Maps.newHashMap();
|
||||
private Map<UUID, PlexPlayer> plexPlayerMap = Maps.newHashMap();
|
||||
|
||||
private PlexPlayerDAO plexPlayerDAO;
|
||||
|
||||
public MongoPlayerData()
|
||||
{
|
||||
this.plexPlayerDAO = new PlexPlayerDAO(PlexPlayer.class, Plex.get().getMongoConnection().getDatastore());
|
||||
}
|
||||
|
||||
public boolean exists(UUID uuid) {
|
||||
|
||||
Query<PlexPlayer> query = plexPlayerDAO.createQuery();
|
||||
|
||||
if (query.field("uuid").exists().field("uuid").equal(uuid.toString()).find().tryNext() != null)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public PlexPlayer getByUUID(UUID uuid) {
|
||||
|
||||
if (plexPlayerMap.containsKey(uuid))
|
||||
{
|
||||
return plexPlayerMap.get(uuid);
|
||||
}
|
||||
Query<PlexPlayer> query2 = plexPlayerDAO.createQuery().field("uuid").exists().field("uuid").equal(uuid.toString());
|
||||
return query2.first();
|
||||
}
|
||||
|
||||
public void update(PlexPlayer player)
|
||||
{
|
||||
Query<PlexPlayer> filter = plexPlayerDAO.createQuery()
|
||||
.field("uuid").equal(player.getUuid());
|
||||
|
||||
UpdateOperations<PlexPlayer> updateOps = plexPlayerDAO.createUpdateOperations();
|
||||
|
||||
updateOps.set("name", player.getName());
|
||||
updateOps.set("loginMSG", player.getLoginMSG());
|
||||
updateOps.set("prefix", player.getPrefix());
|
||||
updateOps.set("rank", player.getRank() == null ? "" : player.getRank().name().toLowerCase());
|
||||
updateOps.set("ips", player.getIps());
|
||||
plexPlayerDAO.update(filter, updateOps);
|
||||
}
|
||||
|
||||
|
||||
public Map<UUID, PlexPlayer> getPlexPlayerMap() {
|
||||
return plexPlayerMap;
|
||||
}
|
||||
|
||||
public Map<UUID, PunishedPlayer> getPunishedPlayerMap() {
|
||||
return punishedPlayerMap;
|
||||
}
|
||||
|
||||
public PlexPlayerDAO getPlexPlayerDAO() {
|
||||
return plexPlayerDAO;
|
||||
}
|
||||
}
|
49
src/main/java/me/totalfreedom/plex/cache/PlexPlayerDAO.java
vendored
Normal file
49
src/main/java/me/totalfreedom/plex/cache/PlexPlayerDAO.java
vendored
Normal file
@ -0,0 +1,49 @@
|
||||
package me.totalfreedom.plex.cache;
|
||||
|
||||
import dev.morphia.DAO;
|
||||
import dev.morphia.Datastore;
|
||||
import dev.morphia.Key;
|
||||
import dev.morphia.dao.BasicDAO;
|
||||
import dev.morphia.query.Query;
|
||||
import dev.morphia.query.UpdateOperations;
|
||||
import dev.morphia.query.UpdateResults;
|
||||
import me.totalfreedom.plex.player.PlexPlayer;
|
||||
|
||||
public class PlexPlayerDAO extends BasicDAO<PlexPlayer, Object> {
|
||||
|
||||
|
||||
public PlexPlayerDAO(Class<PlexPlayer> entityclass, Datastore ds)
|
||||
{
|
||||
super(entityclass, ds);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean exists(Query<PlexPlayer> query) {
|
||||
return super.exists(query);
|
||||
}
|
||||
|
||||
@Override
|
||||
public PlexPlayer findOne(String key, Object value) {
|
||||
return super.findOne(key, value);
|
||||
}
|
||||
|
||||
@Override
|
||||
public PlexPlayer get(Object id) {
|
||||
return super.get(id);
|
||||
}
|
||||
|
||||
@Override
|
||||
public UpdateResults update(Query<PlexPlayer> query, UpdateOperations<PlexPlayer> ops) {
|
||||
return super.update(query, ops);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object obj) {
|
||||
return super.equals(obj);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Key<PlexPlayer> save(PlexPlayer entity) {
|
||||
return super.save(entity);
|
||||
}
|
||||
}
|
@ -0,0 +1,62 @@
|
||||
package me.totalfreedom.plex.listeners;
|
||||
|
||||
import me.totalfreedom.plex.Plex;
|
||||
import me.totalfreedom.plex.cache.MongoPlayerData;
|
||||
import me.totalfreedom.plex.player.PlexPlayer;
|
||||
import me.totalfreedom.plex.player.PunishedPlayer;
|
||||
import me.totalfreedom.plex.util.PlexLog;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.bukkit.event.EventHandler;
|
||||
import org.bukkit.event.Listener;
|
||||
import org.bukkit.event.player.PlayerJoinEvent;
|
||||
import org.bukkit.event.player.PlayerQuitEvent;
|
||||
|
||||
import java.util.Arrays;
|
||||
|
||||
public class PlayerListener implements Listener
|
||||
{
|
||||
|
||||
private MongoPlayerData mongoPlayerData = Plex.get().getMongoPlayerData() != null ? Plex.get().getMongoPlayerData() : null;
|
||||
|
||||
@EventHandler
|
||||
public void onJoin(PlayerJoinEvent event)
|
||||
{
|
||||
Player player = event.getPlayer();
|
||||
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
|
||||
{
|
||||
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.setName(player.getName()); //set the name of the player
|
||||
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
|
||||
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 {
|
||||
PlexPlayer plexPlayer = Plex.get().getMongoPlayerData().getByUUID(player.getUniqueId()); //oh they do exist!
|
||||
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()));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@EventHandler
|
||||
public void onQuit(PlayerQuitEvent event)
|
||||
{
|
||||
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
|
||||
Plex.get().getMongoPlayerData().update(plexPlayer); //update the player's document
|
||||
|
||||
Plex.get().getMongoPlayerData().getPlexPlayerMap().remove(event.getPlayer().getUniqueId()); //remove them from cache
|
||||
Plex.get().getMongoPlayerData().getPunishedPlayerMap().remove(event.getPlayer().getUniqueId());
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
@ -4,8 +4,10 @@ import dev.morphia.annotations.Entity;
|
||||
import dev.morphia.annotations.Id;
|
||||
import dev.morphia.annotations.IndexOptions;
|
||||
import dev.morphia.annotations.Indexed;
|
||||
import lombok.AccessLevel;
|
||||
import lombok.Getter;
|
||||
import lombok.Setter;
|
||||
import me.totalfreedom.plex.rank.Rank;
|
||||
import me.totalfreedom.plex.storage.MongoConnection;
|
||||
|
||||
import java.util.ArrayList;
|
||||
@ -18,22 +20,24 @@ import java.util.UUID;
|
||||
@Entity(value = "players", noClassnameStored = true)
|
||||
public class PlexPlayer
|
||||
{
|
||||
@Setter(AccessLevel.NONE)
|
||||
@Id
|
||||
private String id;
|
||||
|
||||
@Setter(AccessLevel.NONE)
|
||||
@Indexed(options = @IndexOptions(unique = true))
|
||||
private String uuid;
|
||||
|
||||
@Indexed
|
||||
private String name;
|
||||
|
||||
private String loginMSG;
|
||||
private String prefix;
|
||||
|
||||
private Rank rank;
|
||||
|
||||
private List<String> ips;
|
||||
|
||||
private boolean muted;
|
||||
private boolean frozen;
|
||||
|
||||
//insert Rank check
|
||||
|
||||
public PlexPlayer(){}
|
||||
|
||||
public PlexPlayer(UUID playerUUID)
|
||||
@ -44,10 +48,12 @@ public class PlexPlayer
|
||||
|
||||
this.name = "";
|
||||
|
||||
this.loginMSG = "";
|
||||
this.prefix = "";
|
||||
|
||||
this.ips = new ArrayList<>();
|
||||
|
||||
this.muted = false;
|
||||
this.frozen = false;
|
||||
this.rank = null;
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -0,0 +1,29 @@
|
||||
package me.totalfreedom.plex.player;
|
||||
|
||||
import lombok.AccessLevel;
|
||||
import lombok.Getter;
|
||||
import lombok.Setter;
|
||||
|
||||
import java.util.UUID;
|
||||
|
||||
@Getter
|
||||
@Setter
|
||||
public class PunishedPlayer
|
||||
{
|
||||
//everything in here will be stored in redis
|
||||
|
||||
@Setter(AccessLevel.NONE)
|
||||
private String uuid;
|
||||
|
||||
|
||||
private boolean muted;
|
||||
private boolean frozen;
|
||||
|
||||
public PunishedPlayer(UUID playerUUID)
|
||||
{
|
||||
this.uuid = playerUUID.toString();
|
||||
this.muted = false;
|
||||
this.frozen = false;
|
||||
}
|
||||
|
||||
}
|
24
src/main/java/me/totalfreedom/plex/rank/Rank.java
Normal file
24
src/main/java/me/totalfreedom/plex/rank/Rank.java
Normal file
@ -0,0 +1,24 @@
|
||||
package me.totalfreedom.plex.rank;
|
||||
|
||||
import org.bukkit.ChatColor;
|
||||
|
||||
public enum Rank
|
||||
{
|
||||
|
||||
IMPOSTER(0, ChatColor.AQUA + "an " + ChatColor.YELLOW + "Imposter", ChatColor.YELLOW + "[IMP]"),
|
||||
ADMIN(1, ChatColor.AQUA + "an " + ChatColor.AQUA + "Admin", ChatColor.AQUA + "[ADMIN]"),
|
||||
SENIOR_ADMIN(2, ChatColor.AQUA + "a " + ChatColor.LIGHT_PURPLE + "Senior Admin", ChatColor.LIGHT_PURPLE + "[SrA]"),
|
||||
EXECUTIVE(3, ChatColor.AQUA + "an " + ChatColor.RED + "Executive", ChatColor.RED + "[EXEC]");
|
||||
|
||||
private int level;
|
||||
private String loginMSG;
|
||||
private String prefix;
|
||||
|
||||
Rank(int level, String loginMSG, String prefix)
|
||||
{
|
||||
this.level = level;
|
||||
this.loginMSG = loginMSG;
|
||||
this.prefix = prefix;
|
||||
}
|
||||
|
||||
}
|
23
src/main/java/me/totalfreedom/plex/rank/Title.java
Normal file
23
src/main/java/me/totalfreedom/plex/rank/Title.java
Normal file
@ -0,0 +1,23 @@
|
||||
package me.totalfreedom.plex.rank;
|
||||
|
||||
import org.bukkit.ChatColor;
|
||||
|
||||
public enum Title
|
||||
{
|
||||
|
||||
MASTER_BUILDER(0,ChatColor.AQUA + "a " + ChatColor.DARK_AQUA + "Master Builder", ChatColor.DARK_AQUA + "[MB]"),
|
||||
DEV(1, ChatColor.AQUA + "a " + ChatColor.DARK_PURPLE + "Developer", ChatColor.DARK_PURPLE + "[DEV]"),
|
||||
OWNER(2, ChatColor.AQUA + "an " + ChatColor.BLUE + "Owner", ChatColor.BLUE + "[Owner]");
|
||||
|
||||
private int level;
|
||||
private String loginMSG;
|
||||
private String prefix;
|
||||
|
||||
Title(int level, String loginMSG, String prefix)
|
||||
{
|
||||
this.level = level;
|
||||
this.loginMSG = loginMSG;
|
||||
this.prefix = prefix;
|
||||
}
|
||||
|
||||
}
|
@ -24,7 +24,7 @@ public class MongoConnection
|
||||
int port = plugin.getConfig().getInt("data.central.port");
|
||||
String username = plugin.getConfig().getString("data.central.user");
|
||||
String password = plugin.getConfig().getString("data.central.password");
|
||||
String database = plugin.getConfig().getString("data.central.database");
|
||||
String database = plugin.getConfig().getString("data.central.db");
|
||||
|
||||
String connectionString = "mongodb://" + username + ":" + password + "@" + host + ":" + port + "/?authSource=" + database;
|
||||
|
||||
|
@ -0,0 +1,33 @@
|
||||
package me.totalfreedom.plex.storage;
|
||||
|
||||
import me.totalfreedom.plex.Plex;
|
||||
import redis.clients.jedis.Jedis;
|
||||
import redis.clients.jedis.JedisPool;
|
||||
import redis.clients.jedis.JedisPoolConfig;
|
||||
|
||||
public class RedisConnection
|
||||
{
|
||||
|
||||
private JedisPool pool;
|
||||
private Jedis jedis;
|
||||
|
||||
public JedisPool openPool()
|
||||
{
|
||||
ClassLoader previous = Thread.currentThread().getContextClassLoader();
|
||||
Thread.currentThread().setContextClassLoader(RedisConnection.class.getClassLoader());
|
||||
this.pool = new JedisPool(new JedisPoolConfig(), Plex.get().getConfig().getString("data.side.hostname"), Plex.get().getConfig().getInt("data.side.port"));
|
||||
Thread.currentThread().setContextClassLoader(previous);
|
||||
return pool;
|
||||
}
|
||||
|
||||
public Jedis getJedis()
|
||||
{
|
||||
this.jedis = pool.getResource();
|
||||
if (Plex.get().getConfig().getBoolean("data.side.auth"))
|
||||
{
|
||||
jedis.auth(Plex.get().getConfig().getString("data.side.password"));
|
||||
}
|
||||
return jedis;
|
||||
}
|
||||
|
||||
}
|
@ -1,6 +1,7 @@
|
||||
package me.totalfreedom.plex.storage;
|
||||
|
||||
import me.totalfreedom.plex.Plex;
|
||||
import me.totalfreedom.plex.util.PlexLog;
|
||||
|
||||
import java.io.File;
|
||||
import java.sql.Connection;
|
||||
@ -19,7 +20,7 @@ public class SQLConnection
|
||||
int port = plugin.getConfig().getInt("data.central.port");
|
||||
String username = plugin.getConfig().getString("data.central.user");
|
||||
String password = plugin.getConfig().getString("data.central.password");
|
||||
String database = plugin.getConfig().getString("data.central.database");
|
||||
String database = plugin.getConfig().getString("data.central.db");
|
||||
|
||||
try {
|
||||
if (plugin.getConfig().getString("data.central.storage").equalsIgnoreCase("sqlite"))
|
||||
@ -30,11 +31,27 @@ public class SQLConnection
|
||||
else if (plugin.getConfig().getString("data.central.storage").equalsIgnoreCase("mysql"))
|
||||
{
|
||||
connection = DriverManager.getConnection("jdbc:mysql://" + host + ":" + port + "/" + database, username, password);
|
||||
Plex.get().setStorageType(StorageType.MONGO);
|
||||
Plex.get().setStorageType(StorageType.SQL);
|
||||
}
|
||||
} catch (SQLException throwables) {
|
||||
throwables.printStackTrace();
|
||||
}
|
||||
|
||||
try {
|
||||
connection.prepareStatement("CREATE TABLE IF NOT EXISTS `players` (\n" +
|
||||
"\t`uuid` VARCHAR(46),\n" +
|
||||
"\t`name` VARCHAR(18),\n" +
|
||||
"\t`login_msg` VARCHAR(256),\n" +
|
||||
"\t`prefix` VARCHAR(30),\n" +
|
||||
"\t`rank` VARCHAR(256),\n" +
|
||||
"\t`ips` VARCHAR(65535),\n" +
|
||||
"\tPRIMARY KEY (`uuid`)\n" +
|
||||
");").execute();
|
||||
PlexLog.log("Successfully created table `players`!");
|
||||
} catch (SQLException throwables) {
|
||||
throwables.printStackTrace();
|
||||
}
|
||||
|
||||
return connection;
|
||||
}
|
||||
|
||||
|
@ -23,7 +23,7 @@ public class PlexUtils
|
||||
Plex.get().getSqlConnection().getCon().close();
|
||||
} catch (SQLException throwables) {
|
||||
}
|
||||
} else if (Plex.get().getMongoConnection() != null)
|
||||
} else if (Plex.get().getMongoConnection().getDatastore() != null)
|
||||
{
|
||||
PlexLog.log("Successfully enabled MongoDB!");
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user