From 2112b0f5e093330b000adff64c3202957dd8a4c0 Mon Sep 17 00:00:00 2001 From: William Date: Wed, 8 Jun 2022 23:20:19 +0100 Subject: [PATCH] Adjust SQLite initialization to use jdbc properties, change runtime library downloading to use libby --- build.gradle | 3 ++ bukkit/build.gradle | 15 +++++---- .../william278/huskhomes/HuskHomesBukkit.java | 32 ++++++++++++++++++- .../huskhomes/messenger/PluginMessenger.java | 2 +- .../huskhomes/player/BukkitPlayer.java | 2 +- common/build.gradle | 18 +++++------ .../net/william278/huskhomes/cache/Cache.java | 1 + .../huskhomes/data/SqLiteDatabase.java | 12 ++++++- .../main/resources/database/sqlite_schema.sql | 6 ---- gradle.properties | 5 ++- 10 files changed, 68 insertions(+), 28 deletions(-) diff --git a/build.gradle b/build.gradle index ea73d728..1b674b9d 100644 --- a/build.gradle +++ b/build.gradle @@ -9,6 +9,8 @@ version "$ext.plugin_version+${versionMetadata()}" ext { set 'version', version.toString() + set 'jedis_version', jedis_version.toString() + set 'sqlite_driver_version', sqlite_driver_version.toString() } import org.apache.tools.ant.filters.ReplaceTokens @@ -27,6 +29,7 @@ allprojects { mavenCentral() maven { url 'https://hub.spigotmc.org/nexus/content/repositories/snapshots/' } maven { url 'https://repo.minebench.de/' } + maven { url 'https://repo.alessiodp.com/releases/' } maven { url 'https://jitpack.io' } } diff --git a/bukkit/build.gradle b/bukkit/build.gradle index 3def02fe..0666757a 100644 --- a/bukkit/build.gradle +++ b/bukkit/build.gradle @@ -1,14 +1,14 @@ dependencies { implementation project(path: ':common') - implementation 'commons-io:commons-io:2.11.0' - implementation 'de.themoep:minedown:1.7.1-SNAPSHOT' - implementation 'dev.dejvokep:boosted-yaml:1.2' implementation 'io.papermc:paperlib:1.0.7' - implementation 'redis.clients:jedis:4.2.3' + implementation 'net.byteflux:libby-bukkit:1.1.5' + compileOnly 'redis.clients:jedis:' + jedis_version + compileOnly 'commons-io:commons-io:2.11.0' + compileOnly 'de.themoep:minedown:1.7.1-SNAPSHOT' + compileOnly 'dev.dejvokep:boosted-yaml:1.2' compileOnly 'org.spigotmc:spigot-api:1.16.5-R0.1-SNAPSHOT' - compileOnly 'org.jetbrains:annotations:23.0.0' } shadowJar { @@ -19,7 +19,8 @@ shadowJar { relocate 'org.intellij', 'net.william278.huskhomes.libraries' relocate 'com.zaxxer', 'net.william278.huskhomes.libraries' relocate 'org.slf4j', 'net.william278.huskhomes.libraries.slf4j' - relocate 'org.xerial', 'net.william278.huskhomes.libraries' + relocate 'com.google', 'net.william278.huskhomes.libraries' + + relocate 'net.byteflux.libby', 'net.william278.huskhomes.libraries.libby' relocate 'io.papermc.lib', 'net.william278.huskhomes.libraries.paperlib' - relocate 'com.google.code', 'net.william278.huskhomes.libraries' } \ No newline at end of file diff --git a/bukkit/src/main/java/net/william278/huskhomes/HuskHomesBukkit.java b/bukkit/src/main/java/net/william278/huskhomes/HuskHomesBukkit.java index 678a93b4..4678ce87 100644 --- a/bukkit/src/main/java/net/william278/huskhomes/HuskHomesBukkit.java +++ b/bukkit/src/main/java/net/william278/huskhomes/HuskHomesBukkit.java @@ -6,6 +6,8 @@ import dev.dejvokep.boostedyaml.settings.dumper.DumperSettings; import dev.dejvokep.boostedyaml.settings.general.GeneralSettings; import dev.dejvokep.boostedyaml.settings.loader.LoaderSettings; import dev.dejvokep.boostedyaml.settings.updater.UpdaterSettings; +import net.byteflux.libby.BukkitLibraryManager; +import net.byteflux.libby.Library; import net.william278.huskhomes.cache.Cache; import net.william278.huskhomes.command.*; import net.william278.huskhomes.config.Locales; @@ -37,6 +39,7 @@ import org.jetbrains.annotations.Nullable; import java.io.File; import java.io.IOException; +import java.util.Arrays; import java.util.List; import java.util.Objects; import java.util.Optional; @@ -89,7 +92,19 @@ public class HuskHomesBukkit extends JavaPlugin implements HuskHomes { @Override public void onLoad() { + // Set the instance instance = this; + + // Load runtime libraries + getLogger().log(Level.INFO, "Loading runtime libraries..."); + final BukkitLibraryManager libraryManager = new BukkitLibraryManager(this); + final Library[] libraries = new Library[]{ + Library.builder().groupId("redis{}clients").artifactId("jedis").version("4.2.3").id("jedis").build(), + Library.builder().groupId("org{}xerial").artifactId("sqlite-jdbc").version("3.36.0.3").id("sqlite-jdbc").build() + }; // todo fetch these versions from gradle properties the proper way + libraryManager.addMavenCentral(); + Arrays.stream(libraries).forEach(libraryManager::loadLibrary); + getLogger().log(Level.INFO, "Successfully loaded runtime libraries."); } @Override @@ -103,6 +118,7 @@ public class HuskHomesBukkit extends JavaPlugin implements HuskHomes { // Initialize the network messenger if (getSettings().getBooleanValue(Settings.ConfigOption.ENABLE_PROXY_MODE)) { + getLogger().log(Level.INFO, "Initializing the network messenger..."); networkMessenger = switch (settings.getStringValue(Settings.ConfigOption.MESSENGER_TYPE).toUpperCase()) { case "PLUGIN_MESSAGE", "PLUGINMESSAGE" -> new PluginMessenger(); case "REDIS", "JEDIS" -> new RedisMessenger(); @@ -114,10 +130,13 @@ public class HuskHomesBukkit extends JavaPlugin implements HuskHomes { return; } networkMessenger.initialize(this); + getLogger().log(Level.INFO, "Successfully initialized the network messenger."); } // Initialize the database - database = switch (settings.getStringValue(Settings.ConfigOption.DATA_STORAGE_TYPE).toUpperCase()) { + getLogger().log(Level.INFO, "Initializing database..."); + final String databaseType = settings.getStringValue(Settings.ConfigOption.DATA_STORAGE_TYPE); + database = switch (databaseType.toUpperCase()) { case "MYSQL" -> new MySqlDatabase(settings, logger, resourceReader); case "SQLITE" -> new SqLiteDatabase(settings, logger, resourceReader); default -> null; @@ -128,9 +147,15 @@ public class HuskHomesBukkit extends JavaPlugin implements HuskHomes { return; } database.initialize().thenRun(() -> { + getLogger().log(Level.INFO, "Successfully initialized the " + databaseType + " database."); + // Initialize the cache + getLogger().log(Level.INFO, "Initializing the system cache..."); + cache = new Cache(); cache.initialize(database); + + getLogger().log(Level.INFO, "Successfully initialized the system cache."); }).thenRun(() -> { // Prepare the teleport manager this.teleportManager = new BukkitTeleportManager(this); @@ -139,15 +164,18 @@ public class HuskHomesBukkit extends JavaPlugin implements HuskHomes { this.savedPositionManager = new SavedPositionManager(database, cache); // Register commands - todo add all here + getLogger().log(Level.INFO, "Registering commands..."); final CommandBase[] commands = new CommandBase[]{ new HomeCommand(this), new SetHomeCommand(this), new HomeListCommand(this), new BackCommand(this)}; for (CommandBase commandBase : commands) { final PluginCommand pluginCommand = getCommand(commandBase.command); if (pluginCommand != null) { + getLogger().log(Level.INFO, "Registered /" + commandBase.command); new BukkitCommand(commandBase, this).register(pluginCommand); } } + getLogger().log(Level.INFO, "Successfully registered " + commands.length + " commands."); // Register listener getServer().getPluginManager().registerEvents(new BukkitEventProcessor(this), this); @@ -224,6 +252,7 @@ public class HuskHomesBukkit extends JavaPlugin implements HuskHomes { // Load from the plugin config public void loadConfigData() { try { + getLogger().log(Level.INFO, "Loading configuration data..."); settings = Settings.load(YamlDocument.create(new File(getDataFolder(), "config.yml"), Objects.requireNonNull(resourceReader.getResource("config.yml")), GeneralSettings.builder().setUseDefaults(false).build(), @@ -234,6 +263,7 @@ public class HuskHomesBukkit extends JavaPlugin implements HuskHomes { "messages-" + settings.getStringValue(Settings.ConfigOption.LANGUAGE) + ".yml"), Objects.requireNonNull(resourceReader.getResource( "languages/" + settings.getStringValue(Settings.ConfigOption.LANGUAGE) + ".yml")))); + getLogger().log(Level.INFO, "Successfully loaded configuration data."); } catch (IOException | NullPointerException e) { getLoggingAdapter().log(Level.SEVERE, "Failed to load data from the config", e); } diff --git a/bukkit/src/main/java/net/william278/huskhomes/messenger/PluginMessenger.java b/bukkit/src/main/java/net/william278/huskhomes/messenger/PluginMessenger.java index aeb5c40e..43b2399e 100644 --- a/bukkit/src/main/java/net/william278/huskhomes/messenger/PluginMessenger.java +++ b/bukkit/src/main/java/net/william278/huskhomes/messenger/PluginMessenger.java @@ -145,7 +145,7 @@ public class PluginMessenger extends NetworkMessenger implements PluginMessageLi @Override @SuppressWarnings("UnstableApiUsage") public void onPluginMessageReceived(@NotNull String channel, @NotNull org.bukkit.entity.Player player, - byte @NotNull [] messageBytes) { + @NotNull byte[] messageBytes) { if (!channel.equals(BUNGEE_PLUGIN_CHANNEL_NAME)) { return; } diff --git a/bukkit/src/main/java/net/william278/huskhomes/player/BukkitPlayer.java b/bukkit/src/main/java/net/william278/huskhomes/player/BukkitPlayer.java index 330b7aa0..e44bfe72 100644 --- a/bukkit/src/main/java/net/william278/huskhomes/player/BukkitPlayer.java +++ b/bukkit/src/main/java/net/william278/huskhomes/player/BukkitPlayer.java @@ -139,7 +139,7 @@ public class BukkitPlayer implements Player { /** * Send a Bukkit plugin message */ - public void sendPluginMessage(@NotNull Plugin source, @NotNull String channel, byte @NotNull [] message) { + public void sendPluginMessage(@NotNull Plugin source, @NotNull String channel, @NotNull byte[] message) { bukkitPlayer.sendPluginMessage(source, channel, message); } } diff --git a/common/build.gradle b/common/build.gradle index 287de1ed..a4bf3473 100644 --- a/common/build.gradle +++ b/common/build.gradle @@ -1,12 +1,13 @@ dependencies { - compileOnly 'commons-io:commons-io:2.11.0' - compileOnly 'dev.dejvokep:boosted-yaml:1.2' - compileOnly 'de.themoep:minedown:1.7.1-SNAPSHOT' - compileOnly 'redis.clients:jedis:4.2.3' - + implementation 'commons-io:commons-io:2.11.0' + implementation 'dev.dejvokep:boosted-yaml:1.2' + implementation 'de.themoep:minedown:1.7.1-SNAPSHOT' implementation 'com.zaxxer:HikariCP:5.0.1' - implementation 'org.xerial:sqlite-jdbc:3.36.0.3' implementation 'com.google.code.gson:gson:2.9.0' + + compileOnly 'redis.clients:jedis:' + jedis_version + compileOnly 'org.xerial:sqlite-jdbc:' + sqlite_driver_version + compileOnly 'org.jetbrains:annotations:23.0.0' } shadowJar { @@ -17,8 +18,5 @@ shadowJar { relocate 'org.intellij', 'net.william278.huskhomes.libraries' relocate 'com.zaxxer', 'net.william278.huskhomes.libraries' relocate 'org.slf4j', 'net.william278.huskhomes.libraries.slf4j' - relocate 'org.xerial', 'net.william278.huskhomes.libraries' - relocate 'io.papermc.lib', 'net.william278.huskhomes.libraries.paperlib' - relocate 'com.google.code', 'net.william278.huskhomes.libraries' - relocate 'redis.clients', 'net.william278.huskhomes.libraries.jedis' + relocate 'com.google', 'net.william278.huskhomes.libraries' } \ No newline at end of file diff --git a/common/src/main/java/net/william278/huskhomes/cache/Cache.java b/common/src/main/java/net/william278/huskhomes/cache/Cache.java index 8f9ef318..d2e3870a 100644 --- a/common/src/main/java/net/william278/huskhomes/cache/Cache.java +++ b/common/src/main/java/net/william278/huskhomes/cache/Cache.java @@ -12,6 +12,7 @@ import java.util.HashMap; import java.util.List; import java.util.UUID; import java.util.concurrent.CompletableFuture; +import java.util.logging.Level; /** * A cache used to hold persistently used data diff --git a/common/src/main/java/net/william278/huskhomes/data/SqLiteDatabase.java b/common/src/main/java/net/william278/huskhomes/data/SqLiteDatabase.java index 8440f713..da904e74 100644 --- a/common/src/main/java/net/william278/huskhomes/data/SqLiteDatabase.java +++ b/common/src/main/java/net/william278/huskhomes/data/SqLiteDatabase.java @@ -10,6 +10,7 @@ import net.william278.huskhomes.util.Logger; import net.william278.huskhomes.util.ResourceReader; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; +import org.sqlite.SQLiteConfig; import java.io.File; import java.io.IOException; @@ -66,11 +67,20 @@ public class SqLiteDatabase extends Database { } // Establish the connection using the JDBC SQLite driver - connection = DriverManager.getConnection("jdbc:sqlite:" + databaseFile.getAbsolutePath()); + Class.forName("org.sqlite.JDBC"); + + SQLiteConfig config = new SQLiteConfig(); + config.enforceForeignKeys(true); + config.setEncoding(SQLiteConfig.Encoding.UTF8); + config.setSynchronous(SQLiteConfig.SynchronousMode.FULL); + + connection = DriverManager.getConnection("jdbc:sqlite:" + databaseFile.getAbsolutePath(), config.toProperties()); } catch (IOException e) { getLogger().log(Level.SEVERE, "An exception occurred creating the database file", e); } catch (SQLException e) { getLogger().log(Level.SEVERE, "An SQL exception occurred initializing the SQLite database", e); + } catch (ClassNotFoundException e) { + getLogger().log(Level.SEVERE, "Failed to load the necessary SQLite driver", e); } } diff --git a/common/src/main/resources/database/sqlite_schema.sql b/common/src/main/resources/database/sqlite_schema.sql index 2ddd49bc..90694376 100644 --- a/common/src/main/resources/database/sqlite_schema.sql +++ b/common/src/main/resources/database/sqlite_schema.sql @@ -1,9 +1,3 @@ -/* Ensure data gets correctly encoded in UTF-8 */ -PRAGMA encoding = 'UTF-8'; - -/* Ensure foreign key constraints are enforced */ -PRAGMA foreign_keys = ON; - /* Create the positions table if it does not exist */ CREATE TABLE IF NOT EXISTS `%positions_table%` ( diff --git a/gradle.properties b/gradle.properties index 7002ea1d..41dc5152 100644 --- a/gradle.properties +++ b/gradle.properties @@ -4,4 +4,7 @@ org.gradle.daemon=true javaVersion=16 plugin_version=3.0 -plugin_archive=huskhomes \ No newline at end of file +plugin_archive=huskhomes + +jedis_version=4.2.3 +sqlite_driver_version=3.36.0.3 \ No newline at end of file