diff --git a/Plan/src/main/java/com/djrapitops/plan/Plan.java b/Plan/src/main/java/com/djrapitops/plan/Plan.java index 33f92c816..972393101 100644 --- a/Plan/src/main/java/com/djrapitops/plan/Plan.java +++ b/Plan/src/main/java/com/djrapitops/plan/Plan.java @@ -44,8 +44,6 @@ import main.java.com.djrapitops.plan.settings.Settings; import main.java.com.djrapitops.plan.settings.locale.Locale; import main.java.com.djrapitops.plan.settings.locale.Msg; import main.java.com.djrapitops.plan.settings.theme.Theme; -import main.java.com.djrapitops.plan.systems.DatabaseSystem; -import main.java.com.djrapitops.plan.systems.FileSystem; import main.java.com.djrapitops.plan.systems.Systems; import main.java.com.djrapitops.plan.systems.cache.DataCache; import main.java.com.djrapitops.plan.systems.cache.GeolocationCache; @@ -57,10 +55,12 @@ import main.java.com.djrapitops.plan.systems.listeners.*; import main.java.com.djrapitops.plan.systems.processing.Processor; import main.java.com.djrapitops.plan.systems.processing.importing.importers.OfflinePlayerImporter; import main.java.com.djrapitops.plan.systems.queue.ProcessingQueue; +import main.java.com.djrapitops.plan.systems.store.FileSystem; +import main.java.com.djrapitops.plan.systems.store.config.ConfigSystem; +import main.java.com.djrapitops.plan.systems.store.database.DBSystem; import main.java.com.djrapitops.plan.systems.tasks.TPSCountTimer; import main.java.com.djrapitops.plan.systems.webserver.PageCache; import main.java.com.djrapitops.plan.systems.webserver.WebServer; -import main.java.com.djrapitops.plan.utilities.file.FileUtil; import main.java.com.djrapitops.plan.utilities.file.export.HtmlExport; import main.java.com.djrapitops.plan.utilities.metrics.BStats; import org.bukkit.ChatColor; @@ -84,7 +84,6 @@ public class Plan extends BukkitPlugin implements IPlan { private API api; - private Config config; private Theme theme; private Systems systems; @@ -145,10 +144,7 @@ public class Plan extends BukkitPlugin implements IPlan { try { systems = new Systems(this); FileSystem.getInstance().init(); - - config = new Config(FileSystem.getConfigFile()); - config.copyDefaults(FileUtil.lines(this, "config.yml")); - config.save(); + ConfigSystem.getInstance().init(); Log.setDebugMode(Settings.DEBUG.toString()); @@ -186,7 +182,7 @@ public class Plan extends BukkitPlugin implements IPlan { serverVariableHolder = new ServerVariableHolder(getServer()); Benchmark.stop("Enable", "Reading server variables"); - DatabaseSystem.getInstance().init(); + DBSystem.getInstance().init(); Benchmark.start("WebServer Initialization"); webServer = new WebServer(this); @@ -359,11 +355,7 @@ public class Plan extends BukkitPlugin implements IPlan { @Override public void onReload() { - try { - config.read(); - } catch (IOException e) { - Log.toLog(this.getClass().getName(), e); - } + ConfigSystem.reload(); } private void registerListeners() { @@ -393,7 +385,7 @@ public class Plan extends BukkitPlugin implements IPlan { */ @Deprecated public Database getDB() { - return DatabaseSystem.getInstance().getActiveDatabase(); + return DBSystem.getInstance().getActiveDatabase(); } /** @@ -473,7 +465,7 @@ public class Plan extends BukkitPlugin implements IPlan { @Override public Config getMainConfig() { - return config; + return ConfigSystem.getInstance().getConfig(); } public InformationManager getInfoManager() { diff --git a/Plan/src/main/java/com/djrapitops/plan/PlanBungee.java b/Plan/src/main/java/com/djrapitops/plan/PlanBungee.java index 48be33677..2149b8c63 100644 --- a/Plan/src/main/java/com/djrapitops/plan/PlanBungee.java +++ b/Plan/src/main/java/com/djrapitops/plan/PlanBungee.java @@ -23,8 +23,6 @@ import main.java.com.djrapitops.plan.settings.Settings; import main.java.com.djrapitops.plan.settings.locale.Locale; import main.java.com.djrapitops.plan.settings.locale.Msg; import main.java.com.djrapitops.plan.settings.theme.Theme; -import main.java.com.djrapitops.plan.systems.DatabaseSystem; -import main.java.com.djrapitops.plan.systems.FileSystem; import main.java.com.djrapitops.plan.systems.Systems; import main.java.com.djrapitops.plan.systems.info.BungeeInformationManager; import main.java.com.djrapitops.plan.systems.info.InformationManager; @@ -32,9 +30,11 @@ import main.java.com.djrapitops.plan.systems.info.server.BungeeServerInfoManager import main.java.com.djrapitops.plan.systems.listeners.BungeePlayerListener; import main.java.com.djrapitops.plan.systems.processing.Processor; import main.java.com.djrapitops.plan.systems.queue.ProcessingQueue; +import main.java.com.djrapitops.plan.systems.store.FileSystem; +import main.java.com.djrapitops.plan.systems.store.config.ConfigSystem; +import main.java.com.djrapitops.plan.systems.store.database.DBSystem; import main.java.com.djrapitops.plan.systems.tasks.TPSCountTimer; import main.java.com.djrapitops.plan.systems.webserver.WebServer; -import main.java.com.djrapitops.plan.utilities.file.FileUtil; import main.java.com.djrapitops.plan.utilities.file.export.HtmlExport; import net.md_5.bungee.api.ChatColor; @@ -49,7 +49,6 @@ import java.util.UUID; */ public class PlanBungee extends BungeePlugin implements IPlan { - private Config config; private Theme theme; private Systems systems; @@ -67,10 +66,7 @@ public class PlanBungee extends BungeePlugin implements IPlan { try { systems = new Systems(this); FileSystem.getInstance().init(); - - config = new Config(FileSystem.getConfigFile()); - config.copyDefaults(FileUtil.lines(this, "bungeeconfig.yml")); - config.save(); + ConfigSystem.getInstance().init(); Log.setDebugMode(Settings.DEBUG.toString()); @@ -95,7 +91,7 @@ public class PlanBungee extends BungeePlugin implements IPlan { theme = new Theme(); - DatabaseSystem.getInstance().init(); + DBSystem.getInstance().init(); registerCommand("planbungee", new PlanBungeeCommand(this)); @@ -181,17 +177,13 @@ public class PlanBungee extends BungeePlugin implements IPlan { @Override public void onReload() { - try { - config.read(); - } catch (IOException e) { - Log.toLog(this.getClass().getName(), e); - } + ConfigSystem.reload(); } @Override @Deprecated public Database getDB() { - return DatabaseSystem.getInstance().getActiveDatabase(); + return DBSystem.getInstance().getActiveDatabase(); } public BungeeServerInfoManager getServerInfoManager() { @@ -228,7 +220,7 @@ public class PlanBungee extends BungeePlugin implements IPlan { @Override public Config getMainConfig() { - return config; + return ConfigSystem.getInstance().getConfig(); } @Override diff --git a/Plan/src/main/java/com/djrapitops/plan/settings/Settings.java b/Plan/src/main/java/com/djrapitops/plan/settings/Settings.java index c9bd7ca0b..3ac86f5f5 100644 --- a/Plan/src/main/java/com/djrapitops/plan/settings/Settings.java +++ b/Plan/src/main/java/com/djrapitops/plan/settings/Settings.java @@ -2,7 +2,7 @@ package main.java.com.djrapitops.plan.settings; import com.djrapitops.plugin.api.Check; import com.djrapitops.plugin.api.config.Config; -import main.java.com.djrapitops.plan.utilities.MiscUtils; +import main.java.com.djrapitops.plan.systems.store.config.ConfigSystem; import java.util.List; @@ -154,7 +154,7 @@ public enum Settings { } private Config getConfig() { - return MiscUtils.getIPlan().getMainConfig(); + return ConfigSystem.getInstance().getConfig(); } public static ServerSpecificSettings serverSpecific() { diff --git a/Plan/src/main/java/com/djrapitops/plan/systems/Systems.java b/Plan/src/main/java/com/djrapitops/plan/systems/Systems.java index 51077f7a9..b68d4a076 100644 --- a/Plan/src/main/java/com/djrapitops/plan/systems/Systems.java +++ b/Plan/src/main/java/com/djrapitops/plan/systems/Systems.java @@ -4,7 +4,15 @@ */ package main.java.com.djrapitops.plan.systems; -import main.java.com.djrapitops.plan.api.IPlan; +import main.java.com.djrapitops.plan.Plan; +import main.java.com.djrapitops.plan.PlanBungee; +import main.java.com.djrapitops.plan.systems.store.FileSystem; +import main.java.com.djrapitops.plan.systems.store.config.ConfigSystem; +import main.java.com.djrapitops.plan.systems.store.config.PlanBungeeConfigSystem; +import main.java.com.djrapitops.plan.systems.store.config.PlanConfigSystem; +import main.java.com.djrapitops.plan.systems.store.database.DBSystem; +import main.java.com.djrapitops.plan.systems.store.database.PlanBungeeDBSystem; +import main.java.com.djrapitops.plan.systems.store.database.PlanDBSystem; import main.java.com.djrapitops.plan.utilities.MiscUtils; import org.apache.commons.lang3.ArrayUtils; @@ -15,18 +23,38 @@ import org.apache.commons.lang3.ArrayUtils; */ public class Systems { - FileSystem fileSystem; - DatabaseSystem databaseSystem; + private FileSystem fileSystem; + private DBSystem databaseSystem; + private ConfigSystem configSystem; - public Systems(IPlan plugin) { - databaseSystem = new DatabaseSystem(); + /** + * Constructor for Bukkit version. + * + * @param plugin Plan instance + */ + public Systems(Plan plugin) { fileSystem = new FileSystem(plugin); + configSystem = new PlanConfigSystem(); + databaseSystem = new PlanDBSystem(); + + } + + /** + * Constructor for Bungee version. + * + * @param plugin PlanBungee instance + */ + public Systems(PlanBungee plugin) { + fileSystem = new FileSystem(plugin); + configSystem = new PlanBungeeConfigSystem(); + databaseSystem = new PlanBungeeDBSystem(); } private SubSystem[] getSubSystems() { return new SubSystem[]{ - databaseSystem, - fileSystem + fileSystem, + configSystem, + databaseSystem }; } @@ -41,4 +69,16 @@ public class Systems { public static Systems getInstance() { return MiscUtils.getIPlan().getSystems(); } + + public FileSystem getFileSystem() { + return fileSystem; + } + + public DBSystem getDatabaseSystem() { + return databaseSystem; + } + + public ConfigSystem getConfigSystem() { + return configSystem; + } } \ No newline at end of file diff --git a/Plan/src/main/java/com/djrapitops/plan/systems/FileSystem.java b/Plan/src/main/java/com/djrapitops/plan/systems/store/FileSystem.java similarity index 65% rename from Plan/src/main/java/com/djrapitops/plan/systems/FileSystem.java rename to Plan/src/main/java/com/djrapitops/plan/systems/store/FileSystem.java index 3b0efb849..fced95bf3 100644 --- a/Plan/src/main/java/com/djrapitops/plan/systems/FileSystem.java +++ b/Plan/src/main/java/com/djrapitops/plan/systems/store/FileSystem.java @@ -2,12 +2,18 @@ * Licence is provided in the jar as license.yml also here: * https://github.com/Rsl1122/Plan-PlayerAnalytics/blob/master/Plan/src/main/resources/license.yml */ -package main.java.com.djrapitops.plan.systems; +package main.java.com.djrapitops.plan.systems.store; import main.java.com.djrapitops.plan.api.IPlan; import main.java.com.djrapitops.plan.api.exceptions.PlanEnableException; +import main.java.com.djrapitops.plan.systems.SubSystem; +import main.java.com.djrapitops.plan.systems.Systems; +import main.java.com.djrapitops.plan.utilities.MiscUtils; +import main.java.com.djrapitops.plan.utilities.file.FileUtil; import java.io.File; +import java.io.IOException; +import java.util.List; /** * //TODO Class Javadoc Comment @@ -24,7 +30,7 @@ public class FileSystem implements SubSystem { } public static FileSystem getInstance() { - return Systems.getInstance().fileSystem; + return Systems.getInstance().getFileSystem(); } public static File getDataFolder() { @@ -35,6 +41,10 @@ public class FileSystem implements SubSystem { return getInstance().configFile; } + public static List readFromResource(String fileName) throws IOException { + return FileUtil.lines(MiscUtils.getIPlan(), fileName); + } + @Override public void init() throws PlanEnableException { dataFolder.mkdirs(); diff --git a/Plan/src/main/java/com/djrapitops/plan/systems/store/config/ConfigSystem.java b/Plan/src/main/java/com/djrapitops/plan/systems/store/config/ConfigSystem.java new file mode 100644 index 000000000..d78cbe7e3 --- /dev/null +++ b/Plan/src/main/java/com/djrapitops/plan/systems/store/config/ConfigSystem.java @@ -0,0 +1,58 @@ +/* + * Licence is provided in the jar as license.yml also here: + * https://github.com/Rsl1122/Plan-PlayerAnalytics/blob/master/Plan/src/main/resources/license.yml + */ +package main.java.com.djrapitops.plan.systems.store.config; + +import com.djrapitops.plugin.api.config.Config; +import com.djrapitops.plugin.api.utility.log.Log; +import main.java.com.djrapitops.plan.api.exceptions.PlanEnableException; +import main.java.com.djrapitops.plan.systems.SubSystem; +import main.java.com.djrapitops.plan.systems.Systems; +import main.java.com.djrapitops.plan.systems.store.FileSystem; + +import java.io.IOException; + +/** + * //TODO Class Javadoc Comment + * + * @author Rsl1122 + */ +public abstract class ConfigSystem implements SubSystem { + + protected Config config; + + public static ConfigSystem getInstance() { + return Systems.getInstance().getConfigSystem(); + } + + public Config getConfig() { + return config; + } + + @Override + public void init() throws PlanEnableException { + try { + config = new Config(FileSystem.getConfigFile()); + copyDefaults(); + config.save(); + } catch (IOException e) { + throw new PlanEnableException("Config Subsystem failed to initialize", e); + } + } + + protected abstract void copyDefaults() throws IOException; + + @Override + public void close() { + + } + + public static void reload() { + try { + getInstance().config.read(); + } catch (IOException e) { + Log.toLog(ConfigSystem.class, e); + } + } +} \ No newline at end of file diff --git a/Plan/src/main/java/com/djrapitops/plan/systems/store/config/PlanBungeeConfigSystem.java b/Plan/src/main/java/com/djrapitops/plan/systems/store/config/PlanBungeeConfigSystem.java new file mode 100644 index 000000000..f9496777c --- /dev/null +++ b/Plan/src/main/java/com/djrapitops/plan/systems/store/config/PlanBungeeConfigSystem.java @@ -0,0 +1,22 @@ +/* + * Licence is provided in the jar as license.yml also here: + * https://github.com/Rsl1122/Plan-PlayerAnalytics/blob/master/Plan/src/main/resources/license.yml + */ +package main.java.com.djrapitops.plan.systems.store.config; + +import main.java.com.djrapitops.plan.systems.store.FileSystem; + +import java.io.IOException; + +/** + * //TODO Class Javadoc Comment + * + * @author Rsl1122 + */ +public class PlanBungeeConfigSystem extends ConfigSystem { + + @Override + protected void copyDefaults() throws IOException { + config.copyDefaults(FileSystem.readFromResource("bungeeconfig.yml")); + } +} \ No newline at end of file diff --git a/Plan/src/main/java/com/djrapitops/plan/systems/store/config/PlanConfigSystem.java b/Plan/src/main/java/com/djrapitops/plan/systems/store/config/PlanConfigSystem.java new file mode 100644 index 000000000..b8f637b02 --- /dev/null +++ b/Plan/src/main/java/com/djrapitops/plan/systems/store/config/PlanConfigSystem.java @@ -0,0 +1,22 @@ +/* + * Licence is provided in the jar as license.yml also here: + * https://github.com/Rsl1122/Plan-PlayerAnalytics/blob/master/Plan/src/main/resources/license.yml + */ +package main.java.com.djrapitops.plan.systems.store.config; + +import main.java.com.djrapitops.plan.systems.store.FileSystem; + +import java.io.IOException; + +/** + * //TODO Class Javadoc Comment + * + * @author Rsl1122 + */ +public class PlanConfigSystem extends ConfigSystem { + + @Override + protected void copyDefaults() throws IOException { + config.copyDefaults(FileSystem.readFromResource("config.yml")); + } +} \ No newline at end of file diff --git a/Plan/src/main/java/com/djrapitops/plan/systems/store/database/DBSystem.java b/Plan/src/main/java/com/djrapitops/plan/systems/store/database/DBSystem.java new file mode 100644 index 000000000..3bec338fe --- /dev/null +++ b/Plan/src/main/java/com/djrapitops/plan/systems/store/database/DBSystem.java @@ -0,0 +1,83 @@ +/* + * Licence is provided in the jar as license.yml also here: + * https://github.com/Rsl1122/Plan-PlayerAnalytics/blob/master/Plan/src/main/resources/license.yml + */ +package main.java.com.djrapitops.plan.systems.store.database; + +import com.djrapitops.plugin.api.Benchmark; +import com.djrapitops.plugin.api.utility.log.Log; +import com.djrapitops.plugin.utilities.Verify; +import main.java.com.djrapitops.plan.api.exceptions.DatabaseInitException; +import main.java.com.djrapitops.plan.api.exceptions.PlanEnableException; +import main.java.com.djrapitops.plan.database.Database; +import main.java.com.djrapitops.plan.settings.locale.Locale; +import main.java.com.djrapitops.plan.settings.locale.Msg; +import main.java.com.djrapitops.plan.systems.SubSystem; +import main.java.com.djrapitops.plan.systems.Systems; + +import java.sql.SQLException; +import java.util.Set; + +/** + * //TODO Class Javadoc Comment + * + * @author Rsl1122 + */ +public abstract class DBSystem implements SubSystem { + + protected Database db; + protected Set databases; + + public static DBSystem getInstance() { + return Systems.getInstance().getDatabaseSystem(); + } + + @Override + public void init() throws PlanEnableException { + try { + Benchmark.start("Init Database"); + Log.info(Locale.get(Msg.ENABLE_DB_INIT).toString()); + initDatabase(); + Log.info(Locale.get(Msg.ENABLE_DB_INFO).parse(db.getConfigName())); + Benchmark.stop("Systems", "Init Database"); + } catch (DatabaseInitException e) { + throw new PlanEnableException(db.getName() + "-Database failed to initialize", e); + } + } + + protected abstract void initDatabase() throws DatabaseInitException; + + public Set getDatabases() { + return databases; + } + + public void setDatabases(Set databases) { + this.databases = databases; + } + + @Override + public void close() { + try { + if (db != null) { + db.close(); + } + } catch (SQLException e) { + Log.toLog(this.getClass().getName(), e); + } + } + + public Database getActiveDatabase() { + return db; + } + + public static Database getActiveDatabase(String dbName) throws DatabaseInitException { + for (Database database : DBSystem.getInstance().getDatabases()) { + String dbConfigName = database.getConfigName(); + if (Verify.equalsIgnoreCase(dbName, dbConfigName)) { + database.init(); + return database; + } + } + throw new DatabaseInitException(Locale.get(Msg.ENABLE_FAIL_WRONG_DB) + " " + dbName); + } +} \ No newline at end of file diff --git a/Plan/src/main/java/com/djrapitops/plan/systems/store/database/PlanBungeeDBSystem.java b/Plan/src/main/java/com/djrapitops/plan/systems/store/database/PlanBungeeDBSystem.java new file mode 100644 index 000000000..189a8c6f2 --- /dev/null +++ b/Plan/src/main/java/com/djrapitops/plan/systems/store/database/PlanBungeeDBSystem.java @@ -0,0 +1,24 @@ +/* + * Licence is provided in the jar as license.yml also here: + * https://github.com/Rsl1122/Plan-PlayerAnalytics/blob/master/Plan/src/main/resources/license.yml + */ +package main.java.com.djrapitops.plan.systems.store.database; + +import main.java.com.djrapitops.plan.api.exceptions.DatabaseInitException; +import main.java.com.djrapitops.plan.database.databases.MySQLDB; + +/** + * //TODO Class Javadoc Comment + * + * @author Rsl1122 + */ +public class PlanBungeeDBSystem extends DBSystem { + + + @Override + protected void initDatabase() throws DatabaseInitException { + db = new MySQLDB(); + databases.add(db); + db.init(); + } +} \ No newline at end of file diff --git a/Plan/src/main/java/com/djrapitops/plan/systems/store/database/PlanDBSystem.java b/Plan/src/main/java/com/djrapitops/plan/systems/store/database/PlanDBSystem.java new file mode 100644 index 000000000..cc9ea0d00 --- /dev/null +++ b/Plan/src/main/java/com/djrapitops/plan/systems/store/database/PlanDBSystem.java @@ -0,0 +1,31 @@ +/* + * Licence is provided in the jar as license.yml also here: + * https://github.com/Rsl1122/Plan-PlayerAnalytics/blob/master/Plan/src/main/resources/license.yml + */ +package main.java.com.djrapitops.plan.systems.store.database; + +import main.java.com.djrapitops.plan.api.exceptions.DatabaseInitException; +import main.java.com.djrapitops.plan.database.databases.MySQLDB; +import main.java.com.djrapitops.plan.database.databases.SQLiteDB; +import main.java.com.djrapitops.plan.settings.Settings; + +import java.util.HashSet; + +/** + * //TODO Class Javadoc Comment + * + * @author Rsl1122 + */ +public class PlanDBSystem extends DBSystem { + + @Override + protected void initDatabase() throws DatabaseInitException { + databases = new HashSet<>(); + databases.add(new MySQLDB()); + databases.add(new SQLiteDB()); + + String dbType = Settings.DB_TYPE.toString().toLowerCase().trim(); + db = getActiveDatabase(dbType); + db.init(); + } +} \ No newline at end of file diff --git a/Plan/src/main/java/com/djrapitops/plan/utilities/ManageUtils.java b/Plan/src/main/java/com/djrapitops/plan/utilities/ManageUtils.java index e45c2c36c..155007365 100644 --- a/Plan/src/main/java/com/djrapitops/plan/utilities/ManageUtils.java +++ b/Plan/src/main/java/com/djrapitops/plan/utilities/ManageUtils.java @@ -6,7 +6,7 @@ import main.java.com.djrapitops.plan.api.exceptions.DatabaseInitException; import main.java.com.djrapitops.plan.database.Database; import main.java.com.djrapitops.plan.database.databases.SQLiteDB; import main.java.com.djrapitops.plan.database.tables.move.BatchOperationTable; -import main.java.com.djrapitops.plan.systems.DatabaseSystem; +import main.java.com.djrapitops.plan.systems.store.database.DBSystem; import java.sql.SQLException; import java.util.*; @@ -76,6 +76,6 @@ public class ManageUtils { @Deprecated public static Database getDB(String dbName) throws DatabaseInitException { - return DatabaseSystem.getActiveDatabase(dbName); + return DBSystem.getActiveDatabase(dbName); } }