diff --git a/README.md b/README.md index f1ac0de1..4a8fd744 100644 --- a/README.md +++ b/README.md @@ -10,7 +10,7 @@ The best RandomTP mod ever! [![Release](https://img.shields.io/github/v/release/Picono435/PicoJobs?style=for-the-badge&include_prereleases&sort=semver)][releases] -[![Available For](https://img.shields.io/static/v1?label=Available%20For&style=for-the-badge&color=34aa2f&message=1.17.x-1.18.x)][bukkitdev] +[![Available For](https://img.shields.io/static/v1?label=Available%20For&style=for-the-badge&color=34aa2f&message=1.18.x-1.8.x)][bukkitdev] [![BukkitDev Downloads](https://img.shields.io/badge/dynamic/json?color=f16436&style=for-the-badge&label=BukkitDev&query=downloadCount&url=https://addons-ecs.forgesvc.net/api/v2/addon/385252&logo=CurseForge)][bukkitdev] [![SpigotMC Downloads](https://img.shields.io/badge/dynamic/json?color=f16436&style=for-the-badge&label=SpigotMC&query=stats.downloads&url=https%3A%2F%2Fapi.spigotmc.org%2Fsimple%2F0.2%2Findex.php%3Faction%3DgetResource%26id%3D82784&logo=SpigotMC)][spigotmc] diff --git a/build.gradle b/build.gradle index 0f930e0b..bab2d6d2 100644 --- a/build.gradle +++ b/build.gradle @@ -22,7 +22,7 @@ targetCompatibility = '1.8' versioning { releaseMode = { nextTag, lastTag, currentTag, extension -> - "1.0-beta-4" + "1.0-beta-5" } as Object } @@ -82,14 +82,14 @@ dependencies { // Slimjar implementation slimjar("1.2.6") // Spigot - compileOnly 'org.spigotmc:spigot-api:1.17.1-R0.1-SNAPSHOT' + compileOnly 'org.spigotmc:spigot-api:1.18-R0.1-SNAPSHOT' // Storage Methods - slim 'org.mongodb:mongodb-driver-sync:4.3.3' + slim 'org.mongodb:mongodb-driver-sync:4.4.0' slim 'com.zaxxer:HikariCP:4.0.3' slim 'com.h2database:h2:1.4.200' slim 'org.xerial:sqlite-jdbc:3.36.0.3' slim 'org.mariadb.jdbc:mariadb-java-client:2.7.4' - slim 'mysql:mysql-connector-java:8.0.26' + slim 'mysql:mysql-connector-java:8.0.27' implementation 'org.spongepowered:configurate-core:4.1.2' implementation 'org.spongepowered:configurate-gson:4.1.2' implementation 'org.spongepowered:configurate-yaml:4.1.2' @@ -103,13 +103,13 @@ dependencies { implementation 'com.github.mkremins:fanciful:18dc039f5b' slim 'com.fasterxml.jackson.core:jackson-databind:2.13.0' slim 'com.fasterxml.jackson.dataformat:jackson-dataformat-yaml:2.13.0' - implementation 'org.apache.maven:maven-artifact:3.8.3' + implementation 'org.apache.maven:maven-artifact:3.8.4' implementation 'org.bstats:bstats-bukkit:2.2.1' slim 'org.slf4j:slf4j-simple:1.7.32' slim 'org.slf4j:slf4j-api:1.7.32' testImplementation 'com.github.seeseemelk:MockBukkit-v1.16:1.0.0' - testImplementation 'org.junit.jupiter:junit-jupiter-engine:5.8.1' - testCompileOnly 'org.spigotmc:spigot-api:1.17.1-R0.1-SNAPSHOT' + testImplementation 'org.junit.jupiter:junit-jupiter-engine:5.8.2' + testCompileOnly 'org.spigotmc:spigot-api:1.18-R0.1-SNAPSHOT' testCompileOnly('me.clip:placeholderapi:2.10.10') { transitive = false } testCompileOnly('com.github.MilkBowl:VaultAPI:1.7') { transitive = false } testCompileOnly('org.black_ixx:playerpoints:3.0.3') { transitive = false } diff --git a/src/main/java/com/gmail/picono435/picojobs/PicoJobsPlugin.java b/src/main/java/com/gmail/picono435/picojobs/PicoJobsPlugin.java index 0f839fb1..417d4e7f 100644 --- a/src/main/java/com/gmail/picono435/picojobs/PicoJobsPlugin.java +++ b/src/main/java/com/gmail/picono435/picojobs/PicoJobsPlugin.java @@ -1,28 +1,23 @@ package com.gmail.picono435.picojobs; -import java.io.BufferedOutputStream; -import java.io.BufferedReader; -import java.io.File; -import java.io.FileOutputStream; -import java.io.InputStream; -import java.io.InputStreamReader; -import java.io.OutputStream; +import java.io.*; import java.lang.reflect.Method; import java.net.HttpURLConnection; import java.net.MalformedURLException; import java.net.URL; import java.net.URLConnection; import java.nio.charset.StandardCharsets; +import java.nio.file.Path; +import java.sql.PreparedStatement; import java.util.*; import java.util.concurrent.Callable; import java.util.logging.*; +import com.gmail.picono435.picojobs.storage.sql.file.H2Storage; import com.gmail.picono435.picojobs.utils.GitHubAPI; -import io.github.slimjar.logging.ProcessLogger; import io.github.slimjar.resolver.data.Mirror; import io.github.slimjar.resolver.data.Repository; import io.github.slimjar.resolver.mirrors.MirrorSelector; -import io.github.slimjar.resolver.mirrors.SimpleMirrorSelector; import org.apache.maven.artifact.versioning.DefaultArtifactVersion; import org.bstats.bukkit.Metrics; import org.bstats.charts.DrilldownPie; @@ -72,6 +67,7 @@ import com.google.gson.JsonParser; import io.github.slimjar.app.builder.ApplicationBuilder; +import org.codehaus.plexus.util.FileUtils; public class PicoJobsPlugin extends JavaPlugin { @@ -86,6 +82,7 @@ protected PicoJobsPlugin(JavaPluginLoader loader, PluginDescriptionFile descript //PLUGIN private static PicoJobsPlugin instance; public static boolean isTestEnvironment = false; + private static boolean wasUpdated; private String serverVersion; private boolean oldVersion; private String lastestPluginVersion; @@ -181,11 +178,11 @@ public void run() { PicoJobsAPI.getStorageManager().initializeStorageFactory(); if(!isTestEnvironment) { metrics.addCustomChart(new SingleLineChart("created_jobs", new Callable() { - @Override - public Integer call() throws Exception { - return jobs.size(); - } - })); + @Override + public Integer call() throws Exception { + return jobs.size(); + } + })); } // REGISTERING COMMANDS @@ -219,6 +216,25 @@ public Integer call() throws Exception { public void onDisable() { sendConsoleMessage(Level.INFO, "Disconnecting connection to storage..."); jobs.clear(); + + if(wasUpdated && PicoJobsAPI.getStorageManager().getStorageFactory() instanceof H2Storage) { + try { + // Copy the current database to a -old file + FileUtils.copyFile(PicoJobsPlugin.getInstance().getDataFolder().toPath().resolve("storage").resolve("picojobs-h2.mv.db").toFile(), PicoJobsPlugin.getInstance().getDataFolder().toPath().resolve("storage").resolve("picojobs-h2-old.mv.db").toFile()); + // Set the path where the script file will be located + Path scriptFile = PicoJobsPlugin.getInstance().getDataFolder().toPath().resolve("storage").resolve("script").toAbsolutePath(); + // Backup database to script + ((H2Storage) PicoJobsAPI.getStorageManager().getStorageFactory()).backupDataTo(scriptFile.toFile()); + PicoJobsAPI.getStorageManager().destroyStorageFactory(); + // Delete the current database + PicoJobsPlugin.getInstance().getDataFolder().toPath().toAbsolutePath().resolve("storage").resolve("picojobs-h2.mv.db").toFile().delete(); + sendConsoleMessage(Level.INFO, "The plugin was succefully disabled."); + return; + } catch (Exception e) { + e.printStackTrace(); + } + } + PicoJobsAPI.getStorageManager().destroyStorageFactory(); sendConsoleMessage(Level.INFO, "The plugin was succefully disabled."); @@ -440,6 +456,7 @@ public void run() { public boolean updatePlugin(CommandSender p, String message) { try { + wasUpdated = true; URL url = new URL(PicoJobsPlugin.getInstance().getLastestDownloadUrl()); Method getFileMethod = JavaPlugin.class.getDeclaredMethod("getFile"); diff --git a/src/main/java/com/gmail/picono435/picojobs/storage/sql/file/FlatfileStorageFactory.java b/src/main/java/com/gmail/picono435/picojobs/storage/sql/file/FlatfileStorageFactory.java index 3c85b856..10bd02fc 100644 --- a/src/main/java/com/gmail/picono435/picojobs/storage/sql/file/FlatfileStorageFactory.java +++ b/src/main/java/com/gmail/picono435/picojobs/storage/sql/file/FlatfileStorageFactory.java @@ -1,5 +1,6 @@ package com.gmail.picono435.picojobs.storage.sql.file; +import java.io.File; import java.lang.reflect.Constructor; import java.sql.Connection; import java.sql.PreparedStatement; @@ -26,6 +27,24 @@ protected void destroyStorage() { return; } } + + public void backupDataTo(File file) throws Exception { + try(Connection conn = getConnection()) { + PreparedStatement stm = conn.prepareStatement("SCRIPT TO ?"); + stm.setString(1, file.getAbsolutePath()); + stm.execute(); + stm.close(); + } + } + + public void retrieveDataFrom(File file) throws Exception { + try(Connection conn = getConnection()) { + PreparedStatement stm = conn.prepareStatement("RUNSCRIPT FROM ?"); + stm.setString(1, file.getAbsolutePath()); + stm.execute(); + stm.close(); + } + } @Override public boolean createPlayer(UUID uuid) throws Exception { diff --git a/src/main/java/com/gmail/picono435/picojobs/storage/sql/file/H2Storage.java b/src/main/java/com/gmail/picono435/picojobs/storage/sql/file/H2Storage.java index b1ce47e7..2258a155 100644 --- a/src/main/java/com/gmail/picono435/picojobs/storage/sql/file/H2Storage.java +++ b/src/main/java/com/gmail/picono435/picojobs/storage/sql/file/H2Storage.java @@ -1,10 +1,14 @@ package com.gmail.picono435.picojobs.storage.sql.file; +import java.io.File; +import java.nio.file.Path; import java.sql.Connection; import java.sql.PreparedStatement; import java.util.Properties; import com.gmail.picono435.picojobs.PicoJobsPlugin; +import com.gmail.picono435.picojobs.api.PicoJobsAPI; +import org.codehaus.plexus.util.FileUtils; public class H2Storage extends FlatfileStorageFactory { @@ -20,8 +24,18 @@ protected boolean initializeStorage() throws Exception { @Override protected Connection getConnection() throws Exception { - if(this.conn == null || this.conn.isClosed()) { + if(this.conn == null || this.conn.isClosed() || !this.conn.isValid(15)) { this.conn = (Connection) this.connectionConstructor.newInstance("jdbc:h2:" + PicoJobsPlugin.getInstance().getDataFolder().toPath().toAbsolutePath().resolve("storage").resolve("picojobs-h2"), new Properties()); + Path scriptFile = PicoJobsPlugin.getInstance().getDataFolder().toPath().resolve("storage").resolve("script").toAbsolutePath(); + if(scriptFile.toFile().exists()) { + // Import data to the database + PreparedStatement stm = conn.prepareStatement("RUNSCRIPT FROM ?"); + stm.setString(1, scriptFile.toString()); + stm.execute(); + stm.close(); + scriptFile.toFile().delete(); + this.conn = (Connection) this.connectionConstructor.newInstance("jdbc:h2:" + PicoJobsPlugin.getInstance().getDataFolder().toPath().toAbsolutePath().resolve("storage").resolve("picojobs-h2"), new Properties()); + } } return this.conn; }