From 5162f3fcfb49ef4ca839b7fed31d618f5a845b48 Mon Sep 17 00:00:00 2001 From: spacerocket62 Date: Tue, 27 Oct 2020 11:14:34 -0700 Subject: [PATCH] packs is going to yell at me Added custom Config classes Added Mongo / SQLite / SQL switches Setup main config.yml --- pom.xml | 38 +++++++++++++ src/main/java/me/totalfreedom/plex/Plex.java | 34 ++++++++++++ .../me/totalfreedom/plex/config/Config.java | 40 ++++++++++++++ .../totalfreedom/plex/config/YamlConfig.java | 44 +++++++++++++++ .../totalfreedom/plex/player/PlexPlayer.java | 53 +++++++++++++++++++ .../plex/storage/MongoConnection.java | 44 +++++++++++++++ .../plex/storage/SQLConnection.java | 42 +++++++++++++++ .../plex/storage/StorageType.java | 7 +++ .../me/totalfreedom/plex/util/PlexLog.java | 18 +++++++ .../me/totalfreedom/plex/util/PlexUtils.java | 32 +++++++++++ src/main/resources/config.yml | 26 +++++++++ src/main/resources/database.db | 0 src/main/resources/plugin.yml | 1 + 13 files changed, 379 insertions(+) create mode 100644 src/main/java/me/totalfreedom/plex/config/Config.java create mode 100644 src/main/java/me/totalfreedom/plex/config/YamlConfig.java create mode 100644 src/main/java/me/totalfreedom/plex/player/PlexPlayer.java create mode 100644 src/main/java/me/totalfreedom/plex/storage/MongoConnection.java create mode 100644 src/main/java/me/totalfreedom/plex/storage/SQLConnection.java create mode 100644 src/main/java/me/totalfreedom/plex/storage/StorageType.java create mode 100644 src/main/java/me/totalfreedom/plex/util/PlexLog.java create mode 100644 src/main/java/me/totalfreedom/plex/util/PlexUtils.java create mode 100644 src/main/resources/config.yml create mode 100644 src/main/resources/database.db diff --git a/pom.xml b/pom.xml index 2486446..be0f66d 100644 --- a/pom.xml +++ b/pom.xml @@ -30,6 +30,18 @@ 1.16.3-R0.1-SNAPSHOT provided + + org.projectlombok + lombok + 1.18.12 + compile + + + dev.morphia.morphia + core + 1.5.2 + compile + @@ -39,5 +51,31 @@ true + + + org.apache.maven.plugins + maven-compiler-plugin + + 7 + 7 + + + + org.apache.maven.plugins + maven-shade-plugin + 3.2.4 + + + package + + shade + + + false + + + + + \ No newline at end of file diff --git a/src/main/java/me/totalfreedom/plex/Plex.java b/src/main/java/me/totalfreedom/plex/Plex.java index c29d305..d3d71d1 100644 --- a/src/main/java/me/totalfreedom/plex/Plex.java +++ b/src/main/java/me/totalfreedom/plex/Plex.java @@ -1,21 +1,55 @@ package me.totalfreedom.plex; +import lombok.AccessLevel; +import lombok.Getter; +import lombok.Setter; +import me.totalfreedom.plex.config.Config; +import me.totalfreedom.plex.config.YamlConfig; +import me.totalfreedom.plex.storage.MongoConnection; +import me.totalfreedom.plex.storage.SQLConnection; +import me.totalfreedom.plex.storage.StorageType; +import me.totalfreedom.plex.util.PlexLog; +import me.totalfreedom.plex.util.PlexUtils; import org.bukkit.plugin.java.JavaPlugin; +@Getter +@Setter public class Plex extends JavaPlugin { + @Setter(AccessLevel.NONE) + private static Plex plugin; + + private StorageType storageType; + + private SQLConnection sqlConnection; + private MongoConnection mongoConnection; + @Override public void onLoad() { + plugin = this; + + getConfig().options().copyDefaults(true); + saveConfig(); + + saveResource("database.db", false); + + sqlConnection = new SQLConnection(); + mongoConnection = new MongoConnection(); } @Override public void onEnable() { + PlexUtils.testConnections(); } @Override public void onDisable() { } + + public static Plex get() { + return plugin; + } } \ No newline at end of file diff --git a/src/main/java/me/totalfreedom/plex/config/Config.java b/src/main/java/me/totalfreedom/plex/config/Config.java new file mode 100644 index 0000000..7d24386 --- /dev/null +++ b/src/main/java/me/totalfreedom/plex/config/Config.java @@ -0,0 +1,40 @@ +package me.totalfreedom.plex.config; + +import me.totalfreedom.plex.Plex; +import me.totalfreedom.plex.util.PlexLog; + +import java.io.File; +import java.io.IOException; + +public class Config +{ + + private File file; + + + public Config(String name, boolean copy) + { + if (copy) + { + Plex.get().saveResource(name, false); + } else { + file = new File(Plex.get().getDataFolder(), name); + if (!file.exists()) + { + try { + file.createNewFile(); + PlexLog.log("Generating " + name + " configuration file!"); + } catch (IOException e) { + PlexLog.error(String.format("An error occured trying to create the following file: %s", name)); + e.printStackTrace(); + } + } else { + PlexLog.log(name + " configuration file was loaded."); + } + } + } + + public File getFile() { + return file; + } +} diff --git a/src/main/java/me/totalfreedom/plex/config/YamlConfig.java b/src/main/java/me/totalfreedom/plex/config/YamlConfig.java new file mode 100644 index 0000000..e2c3fac --- /dev/null +++ b/src/main/java/me/totalfreedom/plex/config/YamlConfig.java @@ -0,0 +1,44 @@ +package me.totalfreedom.plex.config; + +import org.bukkit.configuration.InvalidConfigurationException; +import org.bukkit.configuration.file.YamlConfiguration; + +import java.io.File; +import java.io.IOException; + +public class YamlConfig +{ + + private YamlConfiguration config; + private File file; + + public YamlConfig(File file) + { + this.file = file; + this.config = YamlConfiguration.loadConfiguration(file); + } + + public YamlConfiguration get() { + return config; + } + + public void save() + { + try { + this.config.save(file); + } catch (IOException e) { + e.printStackTrace(); + } + } + + public void reload() + { + try { + this.config.load(file); + } catch (IOException e) { + e.printStackTrace(); + } catch (InvalidConfigurationException e) { + e.printStackTrace(); + } + } +} diff --git a/src/main/java/me/totalfreedom/plex/player/PlexPlayer.java b/src/main/java/me/totalfreedom/plex/player/PlexPlayer.java new file mode 100644 index 0000000..d397503 --- /dev/null +++ b/src/main/java/me/totalfreedom/plex/player/PlexPlayer.java @@ -0,0 +1,53 @@ +package me.totalfreedom.plex.player; + +import dev.morphia.annotations.Entity; +import dev.morphia.annotations.Id; +import dev.morphia.annotations.IndexOptions; +import dev.morphia.annotations.Indexed; +import lombok.Getter; +import lombok.Setter; +import me.totalfreedom.plex.storage.MongoConnection; + +import java.util.ArrayList; +import java.util.List; +import java.util.UUID; + +@Getter +@Setter + +@Entity(value = "players", noClassnameStored = true) +public class PlexPlayer +{ + @Id + private String id; + + @Indexed(options = @IndexOptions(unique = true)) + private String uuid; + + @Indexed + private String name; + + private List ips; + + private boolean muted; + private boolean frozen; + + //insert Rank check + + public PlexPlayer(){} + + public PlexPlayer(UUID playerUUID) + { + this.uuid = playerUUID.toString(); + + this.id = uuid.substring(0, 8); + + this.name = ""; + + this.ips = new ArrayList<>(); + + this.muted = false; + this.frozen = false; + } + +} diff --git a/src/main/java/me/totalfreedom/plex/storage/MongoConnection.java b/src/main/java/me/totalfreedom/plex/storage/MongoConnection.java new file mode 100644 index 0000000..eefc517 --- /dev/null +++ b/src/main/java/me/totalfreedom/plex/storage/MongoConnection.java @@ -0,0 +1,44 @@ +package me.totalfreedom.plex.storage; + +import com.mongodb.MongoClient; +import com.mongodb.MongoClientURI; +import dev.morphia.Datastore; +import dev.morphia.Morphia; +import me.totalfreedom.plex.Plex; +import me.totalfreedom.plex.player.PlexPlayer; + +public class MongoConnection +{ + + // USE MORPHIA API FOR MONGO <3 + + private Plex plugin = Plex.get(); + + public Datastore getDatastore() + { + if (!plugin.getConfig().getString("data.central.storage").equalsIgnoreCase("mongodb")) + { + return null; + } + String host = plugin.getConfig().getString("data.central.hostname"); + 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 connectionString = "mongodb://" + username + ":" + password + "@" + host + ":" + port + "/?authSource=" + database; + + MongoClient client = new MongoClient(new MongoClientURI(connectionString)); + + Morphia morphia = new Morphia(); + Datastore datastore = morphia.createDatastore(client, database); + + datastore.getMapper().addMappedClass(PlexPlayer.class); + datastore.ensureIndexes(); + + plugin.setStorageType(StorageType.MONGO); + + return datastore; + } + +} diff --git a/src/main/java/me/totalfreedom/plex/storage/SQLConnection.java b/src/main/java/me/totalfreedom/plex/storage/SQLConnection.java new file mode 100644 index 0000000..0b2aada --- /dev/null +++ b/src/main/java/me/totalfreedom/plex/storage/SQLConnection.java @@ -0,0 +1,42 @@ +package me.totalfreedom.plex.storage; + +import me.totalfreedom.plex.Plex; + +import java.io.File; +import java.sql.Connection; +import java.sql.DriverManager; +import java.sql.SQLException; + +public class SQLConnection +{ + private Plex plugin = Plex.get(); + + private Connection connection; + + public Connection getCon() + { + String host = plugin.getConfig().getString("data.central.hostname"); + 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"); + + try { + if (plugin.getConfig().getString("data.central.storage").equalsIgnoreCase("sqlite")) + { + connection = DriverManager.getConnection("jdbc:sqlite:" + new File(plugin.getDataFolder(), "database.db").getAbsolutePath()); + Plex.get().setStorageType(StorageType.SQLITE); + } + 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); + } + } catch (SQLException throwables) { + throwables.printStackTrace(); + } + return connection; + } + + +} diff --git a/src/main/java/me/totalfreedom/plex/storage/StorageType.java b/src/main/java/me/totalfreedom/plex/storage/StorageType.java new file mode 100644 index 0000000..2072670 --- /dev/null +++ b/src/main/java/me/totalfreedom/plex/storage/StorageType.java @@ -0,0 +1,7 @@ +package me.totalfreedom.plex.storage; + +public enum StorageType +{ + MONGO, SQL, SQLITE; + +} diff --git a/src/main/java/me/totalfreedom/plex/util/PlexLog.java b/src/main/java/me/totalfreedom/plex/util/PlexLog.java new file mode 100644 index 0000000..74fdcad --- /dev/null +++ b/src/main/java/me/totalfreedom/plex/util/PlexLog.java @@ -0,0 +1,18 @@ +package me.totalfreedom.plex.util; + +import me.totalfreedom.plex.Plex; + +public class PlexLog +{ + + public static void log(String message) + { + Plex.get().getServer().getConsoleSender().sendMessage(String.format("§e[Plex] §7%s", message)); + } + + public static void error(String message) + { + Plex.get().getServer().getConsoleSender().sendMessage(String.format("§c[Plex Error] §6%s", message)); + } + +} diff --git a/src/main/java/me/totalfreedom/plex/util/PlexUtils.java b/src/main/java/me/totalfreedom/plex/util/PlexUtils.java new file mode 100644 index 0000000..b2df986 --- /dev/null +++ b/src/main/java/me/totalfreedom/plex/util/PlexUtils.java @@ -0,0 +1,32 @@ +package me.totalfreedom.plex.util; + +import me.totalfreedom.plex.Plex; +import me.totalfreedom.plex.storage.StorageType; + +import java.sql.SQLException; + +public class PlexUtils +{ + + public static void testConnections() + { + if (Plex.get().getSqlConnection().getCon() != null) + { + if (Plex.get().getStorageType() == StorageType.SQL) + { + PlexLog.log("Successfully enabled MySQL!"); + } else if (Plex.get().getStorageType() == StorageType.SQLITE) + { + PlexLog.log("Successfully enabled SQLite!"); + } + try { + Plex.get().getSqlConnection().getCon().close(); + } catch (SQLException throwables) { + } + } else if (Plex.get().getMongoConnection() != null) + { + PlexLog.log("Successfully enabled MongoDB!"); + } + } + +} diff --git a/src/main/resources/config.yml b/src/main/resources/config.yml new file mode 100644 index 0000000..9ccedcb --- /dev/null +++ b/src/main/resources/config.yml @@ -0,0 +1,26 @@ +# -------------------------------# +# # +# P L E X # +# # +# -------------------------------# + +# ------------------------------ # +# # +# You can use MySQL, MongoDB # +# or SQLite for the data # +# type # +# -------------------------------# + +data: + central: + storage: sqlite + user: "" + password: "" + hostname: 127.0.0.1 + port: 27017 + db: "plex" + side: # This is redis, leave password blank if auth is false + auth: true + hostname: 127.0.0.1 + port: 6379 + password: "" \ No newline at end of file diff --git a/src/main/resources/database.db b/src/main/resources/database.db new file mode 100644 index 0000000..e69de29 diff --git a/src/main/resources/plugin.yml b/src/main/resources/plugin.yml index 01db9cd..a2309d7 100644 --- a/src/main/resources/plugin.yml +++ b/src/main/resources/plugin.yml @@ -2,3 +2,4 @@ name: ${project.name} version: ${project.version} author: Telesphoreo main: me.totalfreedom.plex.Plex +api-version: 1.16