diff --git a/build.gradle b/build.gradle index 1b674b9d..1e3c753e 100644 --- a/build.gradle +++ b/build.gradle @@ -22,17 +22,27 @@ allprojects { compileJava.options.encoding = 'UTF-8' javadoc.options.encoding = 'UTF-8' - compileJava.options.release.set 16 + compileJava.options.release.set 17 repositories { mavenLocal() mavenCentral() maven { url 'https://hub.spigotmc.org/nexus/content/repositories/snapshots/' } + maven { url 'https://repo.papermc.io/repository/maven-public/' } maven { url 'https://repo.minebench.de/' } maven { url 'https://repo.alessiodp.com/releases/' } maven { url 'https://jitpack.io' } } + dependencies { + testImplementation 'org.junit.jupiter:junit-jupiter-api:5.9.0' + testImplementation 'org.junit.jupiter:junit-jupiter-engine:5.9.0' + } + + test { + useJUnitPlatform() + } + processResources { filter ReplaceTokens as Class, beginToken: '${', endToken: '}', tokens: rootProject.ext.properties diff --git a/bukkit/build.gradle b/bukkit/build.gradle index 86df3b00..17602cfd 100644 --- a/bukkit/build.gradle +++ b/bukkit/build.gradle @@ -3,14 +3,20 @@ dependencies { implementation 'org.bstats:bstats-bukkit:3.0.0' implementation 'io.papermc:paperlib:1.0.7' - compileOnly 'org.spigotmc:spigot-api:1.16.5-R0.1-SNAPSHOT' + compileOnly 'org.spigotmc:spigot-api:1.16.5-R0.1-SNAPSHOT' compileOnly 'redis.clients:jedis:' + jedis_version compileOnly 'commons-io:commons-io:2.11.0' compileOnly 'com.google.guava:guava:31.1-jre' compileOnly 'de.themoep:minedown:1.7.1-SNAPSHOT' compileOnly 'org.jetbrains:annotations:23.0.0' compileOnly 'net.william278:Annotaml:1.0.1' + + testImplementation 'com.github.seeseemelk:MockBukkit-v1.19:2.117.0' + testImplementation 'redis.clients:jedis:' + jedis_version + testImplementation 'org.xerial:sqlite-jdbc:' + sqlite_driver_version + testImplementation 'mysql:mysql-connector-java:8.0.30' + testImplementation 'org.apache.commons:commons-text:1.9' } shadowJar { diff --git a/bukkit/src/main/java/net/william278/huskhomes/BukkitHuskHomes.java b/bukkit/src/main/java/net/william278/huskhomes/BukkitHuskHomes.java index 216db72c..95d1420a 100644 --- a/bukkit/src/main/java/net/william278/huskhomes/BukkitHuskHomes.java +++ b/bukkit/src/main/java/net/william278/huskhomes/BukkitHuskHomes.java @@ -29,7 +29,9 @@ import org.bukkit.Location; import org.bukkit.World; import org.bukkit.command.PluginCommand; import org.bukkit.permissions.PermissionDefault; +import org.bukkit.plugin.PluginDescriptionFile; import org.bukkit.plugin.java.JavaPlugin; +import org.bukkit.plugin.java.JavaPluginLoader; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; @@ -182,7 +184,7 @@ public class BukkitHuskHomes extends JavaPlugin implements HuskHomes { try { new Metrics(this, METRICS_ID); } catch (final Exception e) { - getLoggingAdapter().log(Level.WARNING, "Skipped bStats metrics initialization due to an exception."); + getLoggingAdapter().log(Level.WARNING, "Skipped bStats metrics initialization."); } // Check for updates @@ -320,6 +322,19 @@ public class BukkitHuskHomes extends JavaPlugin implements HuskHomes { }); } + // Default constructor + public BukkitHuskHomes() + { + super(); + } + + // Super constructor for unit testing + protected BukkitHuskHomes(@NotNull JavaPluginLoader loader, @NotNull PluginDescriptionFile description, + @NotNull File dataFolder, @NotNull File file) + { + super(loader, description, dataFolder, file); + } + public static final class BukkitAdapter { public static Optional adaptLocation(net.william278.huskhomes.position.Location location) { diff --git a/bukkit/src/main/java/net/william278/huskhomes/command/BukkitCommand.java b/bukkit/src/main/java/net/william278/huskhomes/command/BukkitCommand.java index ffbf23cf..05413d85 100644 --- a/bukkit/src/main/java/net/william278/huskhomes/command/BukkitCommand.java +++ b/bukkit/src/main/java/net/william278/huskhomes/command/BukkitCommand.java @@ -9,7 +9,6 @@ import org.jetbrains.annotations.Nullable; import java.util.Collections; import java.util.List; -import java.util.logging.Level; /** * Bukkit executor that implements and executes {@link CommandBase}s @@ -38,7 +37,6 @@ public class BukkitCommand implements CommandExecutor, TabExecutor { */ public void register(@NotNull PluginCommand pluginCommand) { try { - plugin.getLoggingAdapter().log(Level.INFO, "Registering executor for command " + pluginCommand.getName() + "..."); pluginCommand.setExecutor(this); pluginCommand.setTabCompleter(this); pluginCommand.setPermission(command.permission); 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 98f818f2..22cd42c9 100644 --- a/bukkit/src/main/java/net/william278/huskhomes/player/BukkitPlayer.java +++ b/bukkit/src/main/java/net/william278/huskhomes/player/BukkitPlayer.java @@ -7,7 +7,7 @@ import net.william278.huskhomes.BukkitHuskHomes; import net.william278.huskhomes.position.Location; import net.william278.huskhomes.position.Position; import net.william278.huskhomes.teleport.TeleportResult; -import net.william278.huskhomes.util.EconomyUnsupportedException; +import net.william278.huskhomes.EconomyUnsupportedException; import org.bukkit.Bukkit; import org.bukkit.entity.Player; import org.bukkit.event.player.PlayerTeleportEvent; diff --git a/bukkit/src/main/resources/plugin.yml b/bukkit/src/main/resources/plugin.yml index 1200a165..3303fc32 100644 --- a/bukkit/src/main/resources/plugin.yml +++ b/bukkit/src/main/resources/plugin.yml @@ -6,8 +6,8 @@ version: '${version}' api-version: 1.16 libraries: - 'redis.clients:jedis:${jedis_version}' - - 'mysql:mysql-connector-java:8.0.29' - - 'org.xerial:sqlite-jdbc:3.36.0.3' + - 'mysql:mysql-connector-java:8.0.30' + - 'org.xerial:sqlite-jdbc:${sqlite_driver_version}' - 'org.apache.commons:commons-text:1.9' commands: diff --git a/bukkit/src/test/java/net/william278/huskhomes/BukkitPluginTests.java b/bukkit/src/test/java/net/william278/huskhomes/BukkitPluginTests.java new file mode 100644 index 00000000..a2327e15 --- /dev/null +++ b/bukkit/src/test/java/net/william278/huskhomes/BukkitPluginTests.java @@ -0,0 +1,54 @@ +package net.william278.huskhomes; + +import be.seeseemelk.mockbukkit.MockBukkit; +import be.seeseemelk.mockbukkit.ServerMock; +import be.seeseemelk.mockbukkit.entity.PlayerMock; +import net.william278.huskhomes.command.BukkitCommandType; +import net.william278.huskhomes.player.BukkitPlayer; +import net.william278.huskhomes.position.Home; +import net.william278.huskhomes.position.PositionMeta; +import org.junit.jupiter.api.AfterAll; +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.Test; + +/** + * Uses MockBukkit to test the plugin on a mock Paper server implementing the Bukkit 1.19 API. + */ +public class BukkitPluginTests { + + public static ServerMock server; + public static BukkitHuskHomes plugin; + + @BeforeAll + public static void setUp() { + server = MockBukkit.mock(); + plugin = MockBukkit.load(BukkitHuskHomes.class); + } + + @AfterAll + public static void tearDown() { + MockBukkit.unmock(); + } + + @Test + public void testPluginEnables() { + Assertions.assertTrue(plugin.isEnabled()); + } + + @Test + public void testCommandRegistration() { + // Assert that all commands in BukkitCommandType are registered by the plugin + for (BukkitCommandType command : BukkitCommandType.values()) { + Assertions.assertNotNull(plugin.getCommand(command.commandBase.command)); + } + } + + @Test + public void testPlayerAdaption() { + // Assert that the player adapter is working + PlayerMock player = server.addPlayer(); + Assertions.assertNotNull(BukkitPlayer.adapt(player)); + } + +} diff --git a/common/src/main/java/net/william278/huskhomes/EconomyUnsupportedException.java b/common/src/main/java/net/william278/huskhomes/EconomyUnsupportedException.java new file mode 100644 index 00000000..b2e6faa3 --- /dev/null +++ b/common/src/main/java/net/william278/huskhomes/EconomyUnsupportedException.java @@ -0,0 +1,12 @@ +package net.william278.huskhomes; + +/** + * Indicates that economy features of the plugin have tried to be accessed when they are not enabled. + */ +public class EconomyUnsupportedException extends HuskHomesException { + + public EconomyUnsupportedException(String message) { + super(message); + } + +} diff --git a/common/src/main/java/net/william278/huskhomes/HuskHomesException.java b/common/src/main/java/net/william278/huskhomes/HuskHomesException.java new file mode 100644 index 00000000..99daf129 --- /dev/null +++ b/common/src/main/java/net/william278/huskhomes/HuskHomesException.java @@ -0,0 +1,18 @@ +package net.william278.huskhomes; + +import org.jetbrains.annotations.NotNull; + +/** + * Indicates an exception caused by HuskHomes' plugin logic occurred + */ +public class HuskHomesException extends RuntimeException { + + public HuskHomesException(@NotNull String message) { + super(message); + } + + public HuskHomesException(@NotNull String message, @NotNull Throwable cause) { + super(message, cause); + } + +} diff --git a/common/src/main/java/net/william278/huskhomes/HuskHomesInitializationException.java b/common/src/main/java/net/william278/huskhomes/HuskHomesInitializationException.java index e4746932..1d819346 100644 --- a/common/src/main/java/net/william278/huskhomes/HuskHomesInitializationException.java +++ b/common/src/main/java/net/william278/huskhomes/HuskHomesInitializationException.java @@ -5,7 +5,7 @@ import org.jetbrains.annotations.NotNull; /** * Indicates an exception occurred while initialising the HuskHomes plugin */ -public class HuskHomesInitializationException extends RuntimeException { +public class HuskHomesInitializationException extends HuskHomesException { public HuskHomesInitializationException(@NotNull String message) { super(message); } diff --git a/common/src/main/java/net/william278/huskhomes/database/SqLiteDatabase.java b/common/src/main/java/net/william278/huskhomes/database/SqLiteDatabase.java index 4d019dab..449633ac 100644 --- a/common/src/main/java/net/william278/huskhomes/database/SqLiteDatabase.java +++ b/common/src/main/java/net/william278/huskhomes/database/SqLiteDatabase.java @@ -127,7 +127,13 @@ public class SqLiteDatabase extends Database { statement.setString(8, position.server.name); statement.executeUpdate(); - return statement.getGeneratedKeys().getInt(1); + // Return the ID of the newly inserted row + try (ResultSet resultSet = statement.getGeneratedKeys()) { + if (resultSet.next()) { + return resultSet.getInt(1); + } + } + throw new SQLException("No generated key found"); } } @@ -170,7 +176,13 @@ public class SqLiteDatabase extends Database { statement.setTimestamp(4, new Timestamp(Instant.ofEpochSecond(position.meta.timestamp).toEpochMilli())); statement.executeUpdate(); - return statement.getGeneratedKeys().getInt(1); + // Return the ID of the newly inserted row + try (ResultSet resultSet = statement.getGeneratedKeys()) { + if (resultSet.next()) { + return resultSet.getInt(1); + } + } + throw new SQLException("No generated key found"); } } @@ -884,6 +896,7 @@ public class SqLiteDatabase extends Database { statement.executeUpdate(); } } catch (SQLException e) { + e.printStackTrace(); getLogger().log(Level.SEVERE, "Failed to set a home to the database for " + home.owner.username, e); } diff --git a/common/src/main/java/net/william278/huskhomes/player/OnlineUser.java b/common/src/main/java/net/william278/huskhomes/player/OnlineUser.java index 2e723b0e..039f2779 100644 --- a/common/src/main/java/net/william278/huskhomes/player/OnlineUser.java +++ b/common/src/main/java/net/william278/huskhomes/player/OnlineUser.java @@ -4,7 +4,7 @@ import de.themoep.minedown.MineDown; import net.william278.huskhomes.position.Location; import net.william278.huskhomes.position.Position; import net.william278.huskhomes.teleport.TeleportResult; -import net.william278.huskhomes.util.EconomyUnsupportedException; +import net.william278.huskhomes.EconomyUnsupportedException; import org.jetbrains.annotations.NotNull; import java.util.UUID; diff --git a/common/src/main/java/net/william278/huskhomes/position/SavedPositionManager.java b/common/src/main/java/net/william278/huskhomes/position/SavedPositionManager.java index e3fdd5fa..4b472b70 100644 --- a/common/src/main/java/net/william278/huskhomes/position/SavedPositionManager.java +++ b/common/src/main/java/net/william278/huskhomes/position/SavedPositionManager.java @@ -50,7 +50,7 @@ public class SavedPositionManager { cache.homes.putIfAbsent(home.owner.uuid, new ArrayList<>()); cache.homes.get(home.owner.uuid).add(home.meta.name); cache.positionLists.clear(); - return database.setHome(home).thenApply(ignored -> + return database.setHome(home).thenApply(value -> new SetResult(SetResult.ResultType.SUCCESS, home)).join(); } return new SetResult(SetResult.ResultType.FAILED_DUPLICATE, null); @@ -188,7 +188,12 @@ public class SavedPositionManager { /** * The position was not set; the description uses illegal characters */ - FAILED_DESCRIPTION_CHARACTERS(false); + FAILED_DESCRIPTION_CHARACTERS(false), + + /** + * The position was not set; the position is not in a valid world + */ + FAILED_ERROR(false); public final boolean successful; diff --git a/common/src/main/java/net/william278/huskhomes/util/EconomyUnsupportedException.java b/common/src/main/java/net/william278/huskhomes/util/EconomyUnsupportedException.java deleted file mode 100644 index b6a4ebd8..00000000 --- a/common/src/main/java/net/william278/huskhomes/util/EconomyUnsupportedException.java +++ /dev/null @@ -1,12 +0,0 @@ -package net.william278.huskhomes.util; - -/** - * Signals that economy features of the plugin have tried to be accessed when they are not enabled. - */ -public class EconomyUnsupportedException extends IllegalStateException { - - public EconomyUnsupportedException(String message) { - super(message); - } - -} diff --git a/gradle.properties b/gradle.properties index 41dc5152..1b1df77c 100644 --- a/gradle.properties +++ b/gradle.properties @@ -1,10 +1,10 @@ org.gradle.jvmargs='-Dfile.encoding=UTF-8' org.gradle.daemon=true -javaVersion=16 +javaVersion=17 plugin_version=3.0 plugin_archive=huskhomes jedis_version=4.2.3 -sqlite_driver_version=3.36.0.3 \ No newline at end of file +sqlite_driver_version=3.39.2.0 \ No newline at end of file