mirror of
https://github.com/darksaid98/HuskHomes.git
synced 2026-12-02 14:38:52 +00:00
v4.5: Bulk delete, regex customization, QoL & API work (#486)
* Improve validator error specificity, fix #481 * Add more sanity-checking to cluster IDs, close #482 * Add bulk/player-delete command, close #484, close #257 * Fix checkstyle formatting in tests * Add missing strings to other locale files * Use morepaperlib for runtime command registration on spigot * api: make creation methods return a future, close #473 * Add name/description restriction regex customization * docs: Update config file with new regex options * bump version number to 4.5 * fix typo, correct command confirmation logic * Make confirmation locales more clear * Fix confirmation checking logic
This commit is contained in:
1 parent
e2c9a52911
commit
6bfd69f232
38 files changed
+1161
-692
No files matched your search
@@ -65,6 +65,7 @@ import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
import org.jetbrains.annotations.TestOnly;
|
||||
import space.arim.morepaperlib.MorePaperLib;
|
||||
import space.arim.morepaperlib.commands.CommandRegistration;
|
||||
import space.arim.morepaperlib.scheduling.GracefulScheduling;
|
||||
|
||||
import java.io.File;
|
||||
@@ -177,11 +178,11 @@ public class BukkitHuskHomes extends JavaPlugin implements HuskHomes, BukkitTask
|
||||
}
|
||||
});
|
||||
|
||||
// Register events
|
||||
initialize("events", (plugin) -> this.eventListener = new BukkitEventListener(this));
|
||||
// Register event listener
|
||||
initialize("events", (plugin) -> this.eventListener = registerListener());
|
||||
|
||||
// Register commands
|
||||
initialize("commands", (plugin) -> this.commands = registerCommands());
|
||||
initialize("commands", (plugin) -> this.commands = BukkitCommand.Type.getCommands(this));
|
||||
|
||||
// Initialize the API
|
||||
initialize("API", (plugin) -> HuskHomesAPI.register(this));
|
||||
@@ -191,9 +192,10 @@ public class BukkitHuskHomes extends JavaPlugin implements HuskHomes, BukkitTask
|
||||
this.checkForUpdates();
|
||||
}
|
||||
|
||||
// Register the event listener
|
||||
@NotNull
|
||||
public List<Command> registerCommands() {
|
||||
return BukkitCommand.Type.getCommands(this);
|
||||
protected EventListener registerListener() {
|
||||
return new BukkitEventListener(this);
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -242,15 +244,6 @@ public class BukkitHuskHomes extends JavaPlugin implements HuskHomes, BukkitTask
|
||||
cancelTasks();
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the adventure Bukkit audiences.
|
||||
*
|
||||
* @return The adventure Bukkit audiences
|
||||
*/
|
||||
public BukkitAudiences getAudiences() {
|
||||
return audiences;
|
||||
}
|
||||
|
||||
@Override
|
||||
@NotNull
|
||||
public ConsoleUser getConsole() {
|
||||
@@ -482,11 +475,21 @@ public class BukkitHuskHomes extends JavaPlugin implements HuskHomes, BukkitTask
|
||||
}
|
||||
}
|
||||
|
||||
@NotNull
|
||||
public BukkitAudiences getAudiences() {
|
||||
return audiences;
|
||||
}
|
||||
|
||||
@NotNull
|
||||
public GracefulScheduling getScheduler() {
|
||||
return paperLib.scheduling();
|
||||
}
|
||||
|
||||
@NotNull
|
||||
public CommandRegistration getCommandRegistrar() {
|
||||
return paperLib.commandRegistration();
|
||||
}
|
||||
|
||||
@Override
|
||||
@NotNull
|
||||
public HuskHomes getPlugin() {
|
||||
|
||||
@@ -25,10 +25,7 @@ import net.william278.huskhomes.HuskHomes;
|
||||
import net.william278.huskhomes.teleport.TeleportRequest;
|
||||
import net.william278.huskhomes.user.BukkitUser;
|
||||
import net.william278.huskhomes.user.CommandUser;
|
||||
import org.bukkit.command.CommandExecutor;
|
||||
import org.bukkit.command.CommandSender;
|
||||
import org.bukkit.command.PluginCommand;
|
||||
import org.bukkit.command.TabCompleter;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.bukkit.permissions.Permission;
|
||||
import org.bukkit.permissions.PermissionDefault;
|
||||
@@ -39,27 +36,27 @@ import org.jetbrains.annotations.Nullable;
|
||||
import java.util.*;
|
||||
import java.util.function.Function;
|
||||
|
||||
public class BukkitCommand implements CommandExecutor, TabCompleter {
|
||||
public class BukkitCommand extends org.bukkit.command.Command {
|
||||
|
||||
private final BukkitHuskHomes plugin;
|
||||
private final Command command;
|
||||
|
||||
public BukkitCommand(@NotNull Command command, @NotNull BukkitHuskHomes plugin) {
|
||||
super(command.getName(), command.getDescription(), command.getUsage(), command.getAliases());
|
||||
this.command = command;
|
||||
this.plugin = plugin;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean onCommand(@NotNull CommandSender sender, @NotNull org.bukkit.command.Command command,
|
||||
@NotNull String label, @NotNull String[] args) {
|
||||
public boolean execute(@NotNull CommandSender sender, @NotNull String commandLabel, @NotNull String[] args) {
|
||||
this.command.onExecuted(sender instanceof Player p ? BukkitUser.adapt(p, plugin) : plugin.getConsole(), args);
|
||||
return true;
|
||||
}
|
||||
|
||||
@Nullable
|
||||
@NotNull
|
||||
@Override
|
||||
public List<String> onTabComplete(@NotNull CommandSender sender, @NotNull org.bukkit.command.Command command,
|
||||
@NotNull String alias, @NotNull String[] args) {
|
||||
public List<String> tabComplete(@NotNull CommandSender sender, @NotNull String alias,
|
||||
@NotNull String[] args) throws IllegalArgumentException {
|
||||
if (!(this.command instanceof TabProvider provider)) {
|
||||
return List.of();
|
||||
}
|
||||
@@ -69,37 +66,38 @@ public class BukkitCommand implements CommandExecutor, TabCompleter {
|
||||
|
||||
public void register() {
|
||||
// Register with bukkit
|
||||
final PluginCommand pluginCommand = plugin.getCommand(command.getName());
|
||||
if (pluginCommand == null) {
|
||||
throw new IllegalStateException("Command " + command.getName() + " not found in plugin.yml");
|
||||
}
|
||||
plugin.getCommandRegistrar().getServerCommandMap().register("huskhomes", this);
|
||||
|
||||
// Register permissions
|
||||
addPermission(
|
||||
BukkitCommand.addPermission(
|
||||
plugin,
|
||||
command.getPermission(),
|
||||
command.getUsage(),
|
||||
getPermissionDefault(command.isOperatorCommand())
|
||||
BukkitCommand.getPermissionDefault(command.isOperatorCommand())
|
||||
);
|
||||
final List<Permission> childNodes = command.getAdditionalPermissions()
|
||||
.entrySet().stream()
|
||||
.map((entry) -> addPermission(plugin, entry.getKey(), "", getPermissionDefault(entry.getValue())))
|
||||
.map((entry) -> BukkitCommand.addPermission(
|
||||
plugin,
|
||||
entry.getKey(),
|
||||
"",
|
||||
BukkitCommand.getPermissionDefault(entry.getValue()))
|
||||
)
|
||||
.filter(Objects::nonNull)
|
||||
.toList();
|
||||
if (!childNodes.isEmpty()) {
|
||||
addPermission(plugin, command.getPermission("*"), command.getUsage(), PermissionDefault.FALSE,
|
||||
childNodes.toArray((plugin) -> new Permission[0]));
|
||||
BukkitCommand.addPermission(
|
||||
plugin,
|
||||
command.getPermission("*"),
|
||||
command.getUsage(),
|
||||
PermissionDefault.FALSE,
|
||||
childNodes.toArray(new Permission[0])
|
||||
);
|
||||
}
|
||||
|
||||
// Set command parameters
|
||||
pluginCommand.setExecutor(this);
|
||||
pluginCommand.setTabCompleter(this);
|
||||
pluginCommand.setDescription(command.getDescription());
|
||||
pluginCommand.setPermission(command.getPermission());
|
||||
|
||||
// Register commodore TAB completion
|
||||
if (CommodoreProvider.isSupported() && plugin.getSettings().doBrigadierTabCompletion()) {
|
||||
BrigadierUtil.registerCommodore(plugin, pluginCommand, command);
|
||||
BrigadierUtil.registerCommodore(plugin, this, command);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -178,17 +176,12 @@ public class BukkitCommand implements CommandExecutor, TabCompleter {
|
||||
@NotNull
|
||||
public static List<Command> getCommands(@NotNull BukkitHuskHomes plugin) {
|
||||
return Arrays.stream(values())
|
||||
.map((type) -> {
|
||||
Command command = type.createCommand(plugin);
|
||||
if (plugin.getSettings().isCommandDisabled(command)) {
|
||||
command = new DisabledCommand(command.getName(), plugin);
|
||||
}
|
||||
new BukkitCommand(command, plugin).register();
|
||||
return command;
|
||||
})
|
||||
.filter((command) -> !(command instanceof DisabledCommand))
|
||||
.map((type) -> type.createCommand(plugin))
|
||||
.filter((command) -> !plugin.getSettings().isCommandDisabled(command))
|
||||
.peek((command) -> new BukkitCommand(command, plugin).register())
|
||||
.toList();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
@@ -26,6 +26,7 @@ import net.william278.huskhomes.HuskHomes;
|
||||
import net.william278.huskhomes.position.Position;
|
||||
import net.william278.huskhomes.user.User;
|
||||
import net.william278.huskhomes.util.BukkitAdapter;
|
||||
import net.william278.huskhomes.util.ValidationException;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
import java.util.List;
|
||||
@@ -98,28 +99,29 @@ public class EssentialsXImporter extends Importer {
|
||||
|
||||
@NotNull
|
||||
private String normalizeName(@NotNull String name) {
|
||||
if (plugin.getValidator().isValidName(name)) {
|
||||
try {
|
||||
plugin.getValidator().validateName(name);
|
||||
return name;
|
||||
} catch (ValidationException e) {
|
||||
// Remove spaces
|
||||
name = name.replaceAll(" ", "_");
|
||||
|
||||
// Remove unicode characters
|
||||
if (plugin.getSettings().doRestrictNames()) {
|
||||
name = name.replaceAll("[^A-Za-z0-9_-]", "");
|
||||
}
|
||||
|
||||
// Ensure the name is not blank
|
||||
if (name.isBlank()) {
|
||||
name = "imported-" + UUID.randomUUID().toString().substring(0, 5);
|
||||
}
|
||||
|
||||
// Ensure name is not too long
|
||||
if (name.length() > 16) {
|
||||
name = name.substring(0, 16);
|
||||
}
|
||||
return name;
|
||||
}
|
||||
|
||||
// Remove spaces
|
||||
name = name.replaceAll(" ", "_");
|
||||
|
||||
// Remove unicode characters
|
||||
if (!plugin.getSettings().doAllowUnicodeNames()) {
|
||||
name = name.replaceAll("[^A-Za-z0-9_-]", "");
|
||||
}
|
||||
|
||||
// Ensure the name is not blank
|
||||
if (name.isBlank()) {
|
||||
name = "imported-" + UUID.randomUUID().toString().substring(0, 5);
|
||||
}
|
||||
|
||||
// Ensure name is not too long
|
||||
if (name.length() > 16) {
|
||||
name = name.substring(0, 16);
|
||||
}
|
||||
return name;
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -12,5 +12,20 @@ huskhomes {
|
||||
name brigadier:string single_word;
|
||||
}
|
||||
}
|
||||
delete {
|
||||
player {
|
||||
name brigadier:string single_word;
|
||||
}
|
||||
homes {
|
||||
world brigadier:string single_word {
|
||||
server brigadier:string single_word;
|
||||
}
|
||||
}
|
||||
warps {
|
||||
world brigadier:string single_word {
|
||||
server brigadier:string single_word;
|
||||
}
|
||||
}
|
||||
}
|
||||
update;
|
||||
}
|
||||
@@ -32,6 +32,7 @@ import net.william278.huskhomes.user.OnlineUser;
|
||||
import net.william278.huskhomes.user.User;
|
||||
import net.william278.huskhomes.util.BukkitAdapter;
|
||||
import net.william278.huskhomes.util.TransactionResolver;
|
||||
import net.william278.huskhomes.util.ValidationException;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.junit.jupiter.api.*;
|
||||
import org.junit.jupiter.params.ParameterizedTest;
|
||||
@@ -205,7 +206,7 @@ public class BukkitPluginTests {
|
||||
"ValidName", "Valid_Name", "Valid-Name", "ValidN4me", "ValidName123", "VN-123", "ValidName_123", "V"
|
||||
})
|
||||
public void testValidNameIsValid(@NotNull String name) {
|
||||
Assertions.assertTrue(plugin.getValidator().isValidName(name));
|
||||
Assertions.assertDoesNotThrow(() -> plugin.getValidator().validateName(name));
|
||||
}
|
||||
|
||||
@DisplayName("Test Validator Rejects Invalid Names")
|
||||
@@ -214,7 +215,10 @@ public class BukkitPluginTests {
|
||||
"Invalid Name", "•♣♠", "Invali♣Name", "Invalid.Name", "", " ", "\0", "InvalidName12345678901234567890"
|
||||
})
|
||||
public void testInvalidNameIsInvalid(@NotNull String name) {
|
||||
Assertions.assertFalse(plugin.getValidator().isValidName(name));
|
||||
Assertions.assertThrows(
|
||||
ValidationException.class,
|
||||
() -> plugin.getValidator().validateName(name)
|
||||
);
|
||||
}
|
||||
|
||||
@DisplayName("Test Validator Accepts Valid Descriptions")
|
||||
@@ -227,7 +231,7 @@ public class BukkitPluginTests {
|
||||
+ " description that is 255 characters long and should be accepted by the validator"
|
||||
})
|
||||
public void testValidDescriptionIsValid(@NotNull String description) {
|
||||
Assertions.assertTrue(plugin.getValidator().isValidDescription(description));
|
||||
Assertions.assertDoesNotThrow(() -> plugin.getValidator().validateDescription(description));
|
||||
}
|
||||
|
||||
@DisplayName("Test Validator Rejects Invalid Descriptions")
|
||||
@@ -239,417 +243,425 @@ public class BukkitPluginTests {
|
||||
+ "of 255 characters that are allowed for a description of a home or a warp."
|
||||
})
|
||||
public void testInvalidDescriptionIsInvalid(@NotNull String description) {
|
||||
Assertions.assertFalse(plugin.getValidator().isValidDescription(description));
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@Nested
|
||||
@DisplayName("Cooldown Tests")
|
||||
@TestMethodOrder(MethodOrderer.OrderAnnotation.class)
|
||||
public class CooldownTests {
|
||||
private static User player;
|
||||
private static final TransactionResolver.Action ACTION = TransactionResolver.Action.HOME_TELEPORT;
|
||||
private static final Duration DURATION = Duration.ofSeconds(100);
|
||||
|
||||
@BeforeAll
|
||||
public static void setup() {
|
||||
player = BukkitUser.adapt(server.addPlayer(), plugin);
|
||||
}
|
||||
|
||||
@DisplayName("Test Applying Cooldown")
|
||||
@Order(1)
|
||||
@Test
|
||||
public void testApplyingCooldown() {
|
||||
plugin.getDatabase().ensureUser(player);
|
||||
Assertions.assertTrue(plugin.getDatabase().getUserData(player.getUuid()).isPresent());
|
||||
|
||||
plugin.getDatabase().setCooldown(ACTION, player, Instant.now().plus(DURATION));
|
||||
Assertions.assertTrue(plugin.getDatabase().getCooldown(ACTION, player).isPresent());
|
||||
}
|
||||
|
||||
@DisplayName("Test Removing Cooldown")
|
||||
@Order(2)
|
||||
@Test
|
||||
public void testRemovingCooldown() {
|
||||
Assertions.assertTrue(plugin.getDatabase().getCooldown(ACTION, player).isPresent());
|
||||
plugin.getDatabase().removeCooldown(ACTION, player);
|
||||
Assertions.assertFalse(plugin.getDatabase().getCooldown(ACTION, player).isPresent());
|
||||
}
|
||||
|
||||
@DisplayName("Test Cooldown Expiration")
|
||||
@Order(3)
|
||||
@Test
|
||||
public void testCooldownExpiration() {
|
||||
Assertions.assertTrue(plugin.getDatabase().getCooldown(ACTION, player).isEmpty());
|
||||
plugin.getDatabase().setCooldown(ACTION, player, Instant.now().minus(DURATION));
|
||||
|
||||
final Optional<Instant> cooldown = plugin.getDatabase().getCooldown(ACTION, player);
|
||||
Assertions.assertTrue(cooldown.isPresent());
|
||||
Assertions.assertTrue(cooldown.get().isBefore(Instant.now()));
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
@Nested
|
||||
@DisplayName("Warp Tests")
|
||||
@TestMethodOrder(MethodOrderer.OrderAnnotation.class)
|
||||
public class WarpTests {
|
||||
private static final List<String> WARP_NAMES = List.of(
|
||||
"warple",
|
||||
"example",
|
||||
"exmaination",
|
||||
"123234__44",
|
||||
"a",
|
||||
"1"
|
||||
);
|
||||
|
||||
@DisplayName("Test Warp Creation")
|
||||
@ParameterizedTest(name = "Warp Name: \"{0}\"")
|
||||
@MethodSource("provideWarpData")
|
||||
@Order(1)
|
||||
public void testWarpCreation(@NotNull String name, @NotNull Position position) {
|
||||
plugin.getManager().warps().createWarp(name, position);
|
||||
Assertions.assertTrue(plugin.getDatabase().getWarp(name).isPresent());
|
||||
Assertions.assertTrue(plugin.getManager().warps()
|
||||
.getWarps().stream()
|
||||
.anyMatch(warp -> warp.equals(name)));
|
||||
}
|
||||
|
||||
// test warp renaming
|
||||
@DisplayName("Test Warp Renaming")
|
||||
@ParameterizedTest(name = "Rename: \"{0}\" > \"{0}2\"")
|
||||
@MethodSource("provideWarpData")
|
||||
@Order(2)
|
||||
@SuppressWarnings("unused")
|
||||
public void testWarpRenaming(@NotNull String name, @NotNull Position position) {
|
||||
final String newName = (name + "2");
|
||||
plugin.getManager().warps().setWarpName(name, newName);
|
||||
Assertions.assertTrue(plugin.getDatabase().getWarp(newName).isPresent());
|
||||
Assertions.assertTrue(plugin.getManager().warps()
|
||||
.getWarps().stream()
|
||||
.anyMatch(warp -> warp.equals(newName)));
|
||||
|
||||
// Rename back to original name
|
||||
plugin.getManager().warps().setWarpName(newName, name);
|
||||
Assertions.assertTrue(plugin.getDatabase().getWarp(name).isPresent());
|
||||
Assertions.assertTrue(plugin.getManager().warps()
|
||||
.getWarps().stream()
|
||||
.anyMatch(warp -> warp.equals(name)));
|
||||
}
|
||||
|
||||
@DisplayName("Test Warp Edit Description")
|
||||
@ParameterizedTest(name = "Edit Description: \"{0}\"")
|
||||
@MethodSource("provideWarpData")
|
||||
@Order(3)
|
||||
@SuppressWarnings("unused")
|
||||
public void testWarpChangingDescription(@NotNull String name, @NotNull Position position) {
|
||||
final String description = "This is a test description for " + name + ".";
|
||||
plugin.getManager().warps().setWarpDescription(name, description);
|
||||
final Optional<String> warpDescription = plugin.getDatabase().getWarp(name)
|
||||
.map(Warp::getMeta)
|
||||
.map(PositionMeta::getDescription);
|
||||
Assertions.assertTrue(warpDescription.isPresent());
|
||||
Assertions.assertEquals(description, warpDescription.get());
|
||||
}
|
||||
|
||||
@DisplayName("Test Warp Relocate")
|
||||
@ParameterizedTest(name = "Relocate: \"{0}\"")
|
||||
@MethodSource("provideWarpData")
|
||||
@Order(4)
|
||||
public void testWarpRelocation(@NotNull String name, @NotNull Position position) {
|
||||
final World world = BukkitAdapter.adaptWorld(server.getWorld("world")).orElseThrow();
|
||||
final Position newPosition = Position.at(
|
||||
position.getX() + 10, position.getY() + 10, position.getZ() + 10,
|
||||
0, 0,
|
||||
world, plugin.getServerName()
|
||||
Assertions.assertThrows(
|
||||
ValidationException.class,
|
||||
() -> plugin.getValidator().validateDescription(description)
|
||||
);
|
||||
plugin.getManager().warps().setWarpPosition(name, newPosition);
|
||||
|
||||
final Optional<Position> warpPosition = plugin.getDatabase().getWarp(name).map(warp -> warp);
|
||||
Assertions.assertTrue(warpPosition.isPresent());
|
||||
Assertions.assertEquals(newPosition.getX(), warpPosition.get().getX());
|
||||
Assertions.assertEquals(newPosition.getY(), warpPosition.get().getY());
|
||||
Assertions.assertEquals(newPosition.getZ(), warpPosition.get().getZ());
|
||||
}
|
||||
|
||||
@DisplayName("Test Warp Overwrite")
|
||||
@ParameterizedTest(name = "Overwrite: \"{0}\"")
|
||||
@MethodSource("provideWarpData")
|
||||
@Order(5)
|
||||
public void testWarpOverwrite(@NotNull String name, @NotNull Position position) {
|
||||
final World world = BukkitAdapter.adaptWorld(server.getWorld("world")).orElseThrow();
|
||||
final Position newPosition = Position.at(
|
||||
position.getX() + 10, position.getY() + 10, position.getZ() + 10,
|
||||
0, 0,
|
||||
world, plugin.getServerName()
|
||||
@Nested
|
||||
@DisplayName("Cooldown Tests")
|
||||
@TestMethodOrder(MethodOrderer.OrderAnnotation.class)
|
||||
public class CooldownTests {
|
||||
private static User player;
|
||||
private static final TransactionResolver.Action ACTION = TransactionResolver.Action.HOME_TELEPORT;
|
||||
private static final Duration DURATION = Duration.ofSeconds(100);
|
||||
|
||||
@BeforeAll
|
||||
public static void setup() {
|
||||
player = BukkitUser.adapt(server.addPlayer(), plugin);
|
||||
}
|
||||
|
||||
@DisplayName("Test Applying Cooldown")
|
||||
@Order(1)
|
||||
@Test
|
||||
public void testApplyingCooldown() {
|
||||
plugin.getDatabase().ensureUser(player);
|
||||
Assertions.assertTrue(plugin.getDatabase().getUserData(player.getUuid()).isPresent());
|
||||
|
||||
plugin.getDatabase().setCooldown(ACTION, player, Instant.now().plus(DURATION));
|
||||
Assertions.assertTrue(plugin.getDatabase().getCooldown(ACTION, player).isPresent());
|
||||
}
|
||||
|
||||
@DisplayName("Test Removing Cooldown")
|
||||
@Order(2)
|
||||
@Test
|
||||
public void testRemovingCooldown() {
|
||||
Assertions.assertTrue(plugin.getDatabase().getCooldown(ACTION, player).isPresent());
|
||||
plugin.getDatabase().removeCooldown(ACTION, player);
|
||||
Assertions.assertFalse(plugin.getDatabase().getCooldown(ACTION, player).isPresent());
|
||||
}
|
||||
|
||||
@DisplayName("Test Cooldown Expiration")
|
||||
@Order(3)
|
||||
@Test
|
||||
public void testCooldownExpiration() {
|
||||
Assertions.assertTrue(plugin.getDatabase().getCooldown(ACTION, player).isEmpty());
|
||||
plugin.getDatabase().setCooldown(ACTION, player, Instant.now().minus(DURATION));
|
||||
|
||||
final Optional<Instant> cooldown = plugin.getDatabase().getCooldown(ACTION, player);
|
||||
Assertions.assertTrue(cooldown.isPresent());
|
||||
Assertions.assertTrue(cooldown.get().isBefore(Instant.now()));
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
@Nested
|
||||
@DisplayName("Warp Tests")
|
||||
@TestMethodOrder(MethodOrderer.OrderAnnotation.class)
|
||||
public class WarpTests {
|
||||
private static final List<String> WARP_NAMES = List.of(
|
||||
"warple",
|
||||
"example",
|
||||
"exmaination",
|
||||
"123234__44",
|
||||
"a",
|
||||
"1"
|
||||
);
|
||||
plugin.getManager().warps().createWarp(name, newPosition, true);
|
||||
|
||||
final Optional<Position> warpPosition = plugin.getDatabase().getWarp(name).map(warp -> warp);
|
||||
Assertions.assertTrue(warpPosition.isPresent());
|
||||
Assertions.assertEquals(newPosition.getX(), warpPosition.get().getX());
|
||||
Assertions.assertEquals(newPosition.getY(), warpPosition.get().getY());
|
||||
Assertions.assertEquals(newPosition.getZ(), warpPosition.get().getZ());
|
||||
@DisplayName("Test Warp Creation")
|
||||
@ParameterizedTest(name = "Warp Name: \"{0}\"")
|
||||
@MethodSource("provideWarpData")
|
||||
@Order(1)
|
||||
public void testWarpCreation(@NotNull String name, @NotNull Position position) {
|
||||
plugin.getManager().warps().createWarp(name, position);
|
||||
Assertions.assertTrue(plugin.getDatabase().getWarp(name).isPresent());
|
||||
Assertions.assertTrue(plugin.getManager().warps()
|
||||
.getWarps().stream()
|
||||
.anyMatch(warp -> warp.equals(name)));
|
||||
}
|
||||
|
||||
// test warp renaming
|
||||
@DisplayName("Test Warp Renaming")
|
||||
@ParameterizedTest(name = "Rename: \"{0}\" > \"{0}2\"")
|
||||
@MethodSource("provideWarpData")
|
||||
@Order(2)
|
||||
@SuppressWarnings("unused")
|
||||
public void testWarpRenaming(@NotNull String name, @NotNull Position position) {
|
||||
final String newName = (name + "2");
|
||||
plugin.getManager().warps().setWarpName(name, newName);
|
||||
Assertions.assertTrue(plugin.getDatabase().getWarp(newName).isPresent());
|
||||
Assertions.assertTrue(plugin.getManager().warps()
|
||||
.getWarps().stream()
|
||||
.anyMatch(warp -> warp.equals(newName)));
|
||||
|
||||
// Rename back to original name
|
||||
plugin.getManager().warps().setWarpName(newName, name);
|
||||
Assertions.assertTrue(plugin.getDatabase().getWarp(name).isPresent());
|
||||
Assertions.assertTrue(plugin.getManager().warps()
|
||||
.getWarps().stream()
|
||||
.anyMatch(warp -> warp.equals(name)));
|
||||
}
|
||||
|
||||
@DisplayName("Test Warp Edit Description")
|
||||
@ParameterizedTest(name = "Edit Description: \"{0}\"")
|
||||
@MethodSource("provideWarpData")
|
||||
@Order(3)
|
||||
@SuppressWarnings("unused")
|
||||
public void testWarpChangingDescription(@NotNull String name, @NotNull Position position) {
|
||||
final String description = "This is a test description for " + name + ".";
|
||||
plugin.getManager().warps().setWarpDescription(name, description);
|
||||
final Optional<String> warpDescription = plugin.getDatabase().getWarp(name)
|
||||
.map(Warp::getMeta)
|
||||
.map(PositionMeta::getDescription);
|
||||
Assertions.assertTrue(warpDescription.isPresent());
|
||||
Assertions.assertEquals(description, warpDescription.get());
|
||||
}
|
||||
|
||||
@DisplayName("Test Warp Relocate")
|
||||
@ParameterizedTest(name = "Relocate: \"{0}\"")
|
||||
@MethodSource("provideWarpData")
|
||||
@Order(4)
|
||||
public void testWarpRelocation(@NotNull String name, @NotNull Position position) {
|
||||
final World world = BukkitAdapter.adaptWorld(server.getWorld("world")).orElseThrow();
|
||||
final Position newPosition = Position.at(
|
||||
position.getX() + 10, position.getY() + 10, position.getZ() + 10,
|
||||
0, 0,
|
||||
world, plugin.getServerName()
|
||||
);
|
||||
plugin.getManager().warps().setWarpPosition(name, newPosition);
|
||||
|
||||
final Optional<Position> warpPosition = plugin.getDatabase().getWarp(name).map(warp -> warp);
|
||||
Assertions.assertTrue(warpPosition.isPresent());
|
||||
Assertions.assertEquals(newPosition.getX(), warpPosition.get().getX());
|
||||
Assertions.assertEquals(newPosition.getY(), warpPosition.get().getY());
|
||||
Assertions.assertEquals(newPosition.getZ(), warpPosition.get().getZ());
|
||||
}
|
||||
|
||||
@DisplayName("Test Warp Overwrite")
|
||||
@ParameterizedTest(name = "Overwrite: \"{0}\"")
|
||||
@MethodSource("provideWarpData")
|
||||
@Order(5)
|
||||
public void testWarpOverwrite(@NotNull String name, @NotNull Position position) {
|
||||
final World world = BukkitAdapter.adaptWorld(server.getWorld("world")).orElseThrow();
|
||||
final Position newPosition = Position.at(
|
||||
position.getX() + 10, position.getY() + 10, position.getZ() + 10,
|
||||
0, 0,
|
||||
world, plugin.getServerName()
|
||||
);
|
||||
plugin.getManager().warps().createWarp(name, newPosition, true);
|
||||
|
||||
final Optional<Position> warpPosition = plugin.getDatabase().getWarp(name).map(warp -> warp);
|
||||
Assertions.assertTrue(warpPosition.isPresent());
|
||||
Assertions.assertEquals(newPosition.getX(), warpPosition.get().getX());
|
||||
Assertions.assertEquals(newPosition.getY(), warpPosition.get().getY());
|
||||
Assertions.assertEquals(newPosition.getZ(), warpPosition.get().getZ());
|
||||
}
|
||||
|
||||
@DisplayName("Test Warp Deletion")
|
||||
@ParameterizedTest(name = "Delete Warp: \"{0}\"")
|
||||
@MethodSource("provideWarpData")
|
||||
@Order(6)
|
||||
@SuppressWarnings("unused")
|
||||
public void testWarpDeletion(@NotNull String name, @NotNull Position position) {
|
||||
plugin.getManager().warps().deleteWarp(name);
|
||||
Assertions.assertFalse(plugin.getDatabase().getWarp(name).isPresent());
|
||||
Assertions.assertFalse(plugin.getManager().warps()
|
||||
.getWarps().stream()
|
||||
.anyMatch(warp -> warp.equals(name)));
|
||||
|
||||
plugin.getManager().warps().createWarp(name, position);
|
||||
Assertions.assertTrue(plugin.getDatabase().getWarp(name).isPresent());
|
||||
}
|
||||
|
||||
@DisplayName("Test Querying Warps Case-Insensitively")
|
||||
@ParameterizedTest(name = "Query: \"{0}\"")
|
||||
@MethodSource("provideWarpData")
|
||||
@Order(8)
|
||||
@SuppressWarnings("unused")
|
||||
public void testWarpCaseInsensitiveQuery(@NotNull String name, @NotNull Position position) {
|
||||
final String nameUpper = name.toUpperCase();
|
||||
final Optional<Warp> nameUpperWarp = plugin.getDatabase().getWarp(nameUpper, true);
|
||||
Assertions.assertTrue(nameUpperWarp.isPresent());
|
||||
Assertions.assertEquals(name, nameUpperWarp.get().getName());
|
||||
|
||||
final String nameLower = name.toLowerCase();
|
||||
final Optional<Warp> nameLowerWarp = plugin.getDatabase().getWarp(nameLower, true);
|
||||
Assertions.assertTrue(nameLowerWarp.isPresent());
|
||||
Assertions.assertEquals(name, nameLowerWarp.get().getName());
|
||||
}
|
||||
|
||||
@DisplayName("Test Deleting All Warps")
|
||||
@Order(9)
|
||||
@Test
|
||||
public void testWarpDeleteAll() {
|
||||
final int deleted = plugin.getManager().warps().deleteAllWarps();
|
||||
Assertions.assertTrue(plugin.getManager().warps().getWarps().isEmpty());
|
||||
Assertions.assertTrue(plugin.getDatabase().getWarps().isEmpty());
|
||||
Assertions.assertEquals(WARP_NAMES.size(), deleted);
|
||||
}
|
||||
|
||||
@NotNull
|
||||
public static Stream<Arguments> provideWarpData() {
|
||||
final World world = BukkitAdapter.adaptWorld(server.getWorld("world")).orElseThrow();
|
||||
final Position position = Position.at(0, 0, 0, 0, 0, world, plugin.getServerName());
|
||||
return WARP_NAMES.stream()
|
||||
.flatMap(name -> Stream.of(Arguments.of(name, position)));
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@DisplayName("Test Warp Deletion")
|
||||
@ParameterizedTest(name = "Delete Warp: \"{0}\"")
|
||||
@MethodSource("provideWarpData")
|
||||
@Order(6)
|
||||
@SuppressWarnings("unused")
|
||||
public void testWarpDeletion(@NotNull String name, @NotNull Position position) {
|
||||
plugin.getManager().warps().deleteWarp(name);
|
||||
Assertions.assertFalse(plugin.getDatabase().getWarp(name).isPresent());
|
||||
Assertions.assertFalse(plugin.getManager().warps()
|
||||
.getWarps().stream()
|
||||
.anyMatch(warp -> warp.equals(name)));
|
||||
@Nested
|
||||
@DisplayName("Home Tests")
|
||||
@TestMethodOrder(MethodOrderer.OrderAnnotation.class)
|
||||
public class HomeTests {
|
||||
private static final List<String> HOME_NAMES = List.of(
|
||||
"mr_home",
|
||||
"homble",
|
||||
"seaside",
|
||||
"hovel",
|
||||
"tokyo-3",
|
||||
"testington",
|
||||
"h"
|
||||
);
|
||||
private static BukkitUser homeOwner;
|
||||
|
||||
@DisplayName("Ensure User Data")
|
||||
@BeforeAll
|
||||
public static void createHomeUser() {
|
||||
homeOwner = BukkitUser.adapt(server.addPlayer("TestUser278"), plugin);
|
||||
plugin.getDatabase().ensureUser(homeOwner);
|
||||
Assertions.assertTrue(plugin.getDatabase().getUserData(homeOwner.getUuid()).isPresent());
|
||||
}
|
||||
|
||||
@DisplayName("Test Home Creation")
|
||||
@ParameterizedTest(name = "Home Name: \"{1}\"")
|
||||
@MethodSource("provideHomeData")
|
||||
@Order(1)
|
||||
public void testHomeCreation(@NotNull OnlineUser owner, @NotNull String name, @NotNull Position position) {
|
||||
plugin.getManager().homes().createHome(owner, name, position);
|
||||
Assertions.assertTrue(plugin.getDatabase().getHome(owner, name).isPresent());
|
||||
Assertions.assertTrue(plugin.getManager().homes()
|
||||
.getUserHomes()
|
||||
.get(owner.getUsername()).stream()
|
||||
.anyMatch(home -> home.equals(name)));
|
||||
}
|
||||
|
||||
@DisplayName("Test Home Renaming")
|
||||
@ParameterizedTest(name = "Rename: \"{1}\" > \"{1}2\"")
|
||||
@MethodSource("provideHomeData")
|
||||
@Order(2)
|
||||
@SuppressWarnings("unused")
|
||||
public void testHomeRenaming(@NotNull OnlineUser owner, @NotNull String name, @NotNull Position position) {
|
||||
final String newName = "new_" + name;
|
||||
plugin.getManager().homes().setHomeName(owner, name, newName);
|
||||
Assertions.assertTrue(plugin.getDatabase().getHome(owner, newName).isPresent());
|
||||
Assertions.assertFalse(plugin.getDatabase().getHome(owner, name).isPresent());
|
||||
Assertions.assertTrue(plugin.getManager().homes()
|
||||
.getUserHomes()
|
||||
.get(owner.getUsername()).stream()
|
||||
.anyMatch(home -> home.equals(newName)));
|
||||
Assertions.assertFalse(plugin.getManager().homes()
|
||||
.getUserHomes()
|
||||
.get(owner.getUsername()).stream()
|
||||
.anyMatch(home -> home.equals(name)));
|
||||
|
||||
// Rename back to original name
|
||||
plugin.getManager().homes().setHomeName(owner, newName, name);
|
||||
Assertions.assertTrue(plugin.getDatabase().getHome(owner, name).isPresent());
|
||||
Assertions.assertTrue(plugin.getManager().homes()
|
||||
.getUserHomes()
|
||||
.get(owner.getUsername()).stream()
|
||||
.anyMatch(home -> home.equals(name)));
|
||||
}
|
||||
|
||||
@DisplayName("Test Home Description")
|
||||
@ParameterizedTest(name = "Edit Description: \"{1}\"")
|
||||
@MethodSource("provideHomeData")
|
||||
@Order(3)
|
||||
@SuppressWarnings("unused")
|
||||
public void testHomeDescription(@NotNull OnlineUser owner, @NotNull String name,
|
||||
@NotNull Position position) {
|
||||
final String description = "This is a test description for the home " + name + "!";
|
||||
plugin.getManager().homes().setHomeDescription(owner, name, description);
|
||||
Assertions.assertTrue(plugin.getDatabase().getHome(owner, name).isPresent());
|
||||
|
||||
final Optional<String> homeDescription = plugin.getDatabase().getHome(owner, name)
|
||||
.map(Home::getMeta)
|
||||
.map(PositionMeta::getDescription);
|
||||
Assertions.assertTrue(homeDescription.isPresent());
|
||||
Assertions.assertEquals(description, homeDescription.get());
|
||||
}
|
||||
|
||||
@DisplayName("Test Home Relocation")
|
||||
@ParameterizedTest(name = "Relocate: \"{1}\"")
|
||||
@MethodSource("provideHomeData")
|
||||
@Order(4)
|
||||
public void testHomeRelocation(@NotNull OnlineUser owner, @NotNull String name,
|
||||
@NotNull Position position) {
|
||||
final World world = BukkitAdapter.adaptWorld(server.getWorld("world")).orElseThrow();
|
||||
final Position newPosition = Position.at(
|
||||
position.getX() + 10, position.getY() + 10, position.getZ() + 10,
|
||||
0, 0,
|
||||
world, plugin.getServerName()
|
||||
);
|
||||
plugin.getManager().homes().setHomePosition(owner, name, newPosition);
|
||||
Assertions.assertTrue(plugin.getDatabase().getHome(owner, name).isPresent());
|
||||
|
||||
final Optional<Position> homePosition = plugin.getDatabase().getHome(owner, name).map(home -> home);
|
||||
Assertions.assertTrue(homePosition.isPresent());
|
||||
Assertions.assertEquals(newPosition.getX(), homePosition.get().getX());
|
||||
Assertions.assertEquals(newPosition.getY(), homePosition.get().getY());
|
||||
Assertions.assertEquals(newPosition.getZ(), homePosition.get().getZ());
|
||||
}
|
||||
|
||||
@DisplayName("Test Home Overwrite")
|
||||
@ParameterizedTest(name = "Overwrite: \"{1}\"")
|
||||
@MethodSource("provideHomeData")
|
||||
@Order(5)
|
||||
public void testHomeOverwrite(@NotNull OnlineUser owner, @NotNull String name, @NotNull Position position) {
|
||||
final World world = BukkitAdapter.adaptWorld(server.getWorld("world")).orElseThrow();
|
||||
final Position newPosition = Position.at(
|
||||
position.getX() + 10, position.getY() + 10, position.getZ() + 10,
|
||||
0, 0,
|
||||
world, plugin.getServerName()
|
||||
);
|
||||
plugin.getManager().homes().setHomePosition(owner, name, newPosition);
|
||||
Assertions.assertTrue(plugin.getDatabase().getHome(owner, name).isPresent());
|
||||
|
||||
final Optional<Position> homePosition = plugin.getDatabase().getHome(owner, name).map(home -> home);
|
||||
Assertions.assertTrue(homePosition.isPresent());
|
||||
Assertions.assertEquals(newPosition.getX(), homePosition.get().getX());
|
||||
Assertions.assertEquals(newPosition.getY(), homePosition.get().getY());
|
||||
Assertions.assertEquals(newPosition.getZ(), homePosition.get().getZ());
|
||||
}
|
||||
|
||||
@DisplayName("Test Making Home Public")
|
||||
@ParameterizedTest(name = "Make Public: \"{1}\"")
|
||||
@MethodSource("provideHomeData")
|
||||
@Order(6)
|
||||
@SuppressWarnings("unused")
|
||||
public void testHomeMakePublic(@NotNull OnlineUser owner, @NotNull String name, @NotNull Position p) {
|
||||
plugin.getManager().homes().setHomePrivacy(owner, name, true);
|
||||
Assertions.assertTrue(plugin.getDatabase().getHome(owner, name).isPresent());
|
||||
|
||||
final Optional<Boolean> homePrivacy = plugin.getDatabase().getHome(owner, name).map(Home::isPublic);
|
||||
Assertions.assertTrue(homePrivacy.isPresent());
|
||||
Assertions.assertTrue(homePrivacy.get());
|
||||
Assertions.assertTrue(plugin.getManager().homes().getPublicHomes()
|
||||
.get(owner.getUsername()).contains(name));
|
||||
}
|
||||
|
||||
@DisplayName("Test Making Home Private")
|
||||
@ParameterizedTest(name = "Make Private: \"{1}\"")
|
||||
@MethodSource("provideHomeData")
|
||||
@Order(7)
|
||||
@SuppressWarnings("unused")
|
||||
public void testHomeMakePrivate(@NotNull OnlineUser owner, @NotNull String name, @NotNull Position p) {
|
||||
plugin.getManager().homes().setHomePrivacy(owner, name, false);
|
||||
Assertions.assertTrue(plugin.getDatabase().getHome(owner, name).isPresent());
|
||||
|
||||
final Optional<Boolean> homePrivacy = plugin.getDatabase().getHome(owner, name).map(Home::isPublic);
|
||||
Assertions.assertTrue(homePrivacy.isPresent());
|
||||
Assertions.assertFalse(homePrivacy.get());
|
||||
Assertions.assertFalse(plugin.getManager().homes().getPublicHomes()
|
||||
.getOrDefault(owner.getUsername(), List.of())
|
||||
.contains(name));
|
||||
}
|
||||
|
||||
@DisplayName("Test Querying Homes Case-Insensitively")
|
||||
@ParameterizedTest(name = "Query: \"{1}\"")
|
||||
@MethodSource("provideHomeData")
|
||||
@Order(8)
|
||||
@SuppressWarnings("unused")
|
||||
public void testWarpCaseInsensitiveQuery(@NotNull OnlineUser owner, @NotNull String name,
|
||||
@NotNull Position p) {
|
||||
final String nameUpper = name.toUpperCase();
|
||||
final Optional<Home> nameUpperWarp = plugin.getDatabase().getHome(owner, nameUpper, true);
|
||||
Assertions.assertTrue(nameUpperWarp.isPresent());
|
||||
Assertions.assertEquals(name, nameUpperWarp.get().getName());
|
||||
|
||||
final String nameLower = name.toLowerCase();
|
||||
final Optional<Home> nameLowerWarp = plugin.getDatabase().getHome(owner, nameLower, true);
|
||||
Assertions.assertTrue(nameLowerWarp.isPresent());
|
||||
Assertions.assertEquals(name, nameLowerWarp.get().getName());
|
||||
}
|
||||
|
||||
@DisplayName("Test Home Deletion")
|
||||
@ParameterizedTest(name = "Delete: \"{1}\"")
|
||||
@MethodSource("provideHomeData")
|
||||
@Order(9)
|
||||
public void testHomeDeletion(@NotNull OnlineUser owner, @NotNull String name, @NotNull Position position) {
|
||||
plugin.getManager().homes().deleteHome(owner, name);
|
||||
Assertions.assertFalse(plugin.getDatabase().getHome(owner, name).isPresent());
|
||||
Assertions.assertFalse(plugin.getManager().homes().getUserHomes()
|
||||
.getOrDefault(owner.getUsername(), List.of())
|
||||
.contains(name));
|
||||
|
||||
plugin.getManager().homes().createHome(owner, name, position);
|
||||
}
|
||||
|
||||
@DisplayName("Test Deleting All Homes")
|
||||
@Order(10)
|
||||
@Test
|
||||
public void testDeleteAllHomes() {
|
||||
final int deleted = plugin.getManager().homes().deleteAllHomes(homeOwner);
|
||||
Assertions.assertTrue(plugin.getDatabase().getHomes(homeOwner).isEmpty());
|
||||
Assertions.assertTrue(plugin.getManager().homes().getUserHomes()
|
||||
.get(homeOwner.getUsername()).isEmpty());
|
||||
Assertions.assertEquals(HOME_NAMES.size(), deleted);
|
||||
}
|
||||
|
||||
@NotNull
|
||||
public static Stream<Arguments> provideHomeData() {
|
||||
final World world = BukkitAdapter.adaptWorld(server.getWorld("world")).orElseThrow();
|
||||
final Position position = Position.at(0, 0, 0, 0, 0, world, plugin.getServerName());
|
||||
return HOME_NAMES.stream()
|
||||
.flatMap(name -> Stream.of(Arguments.of(homeOwner, name, position)));
|
||||
}
|
||||
|
||||
plugin.getManager().warps().createWarp(name, position);
|
||||
Assertions.assertTrue(plugin.getDatabase().getWarp(name).isPresent());
|
||||
}
|
||||
|
||||
@DisplayName("Test Querying Warps Case-Insensitively")
|
||||
@ParameterizedTest(name = "Query: \"{0}\"")
|
||||
@MethodSource("provideWarpData")
|
||||
@Order(8)
|
||||
@SuppressWarnings("unused")
|
||||
public void testWarpCaseInsensitiveQuery(@NotNull String name, @NotNull Position position) {
|
||||
final String nameUpper = name.toUpperCase();
|
||||
final Optional<Warp> nameUpperWarp = plugin.getDatabase().getWarp(nameUpper, true);
|
||||
Assertions.assertTrue(nameUpperWarp.isPresent());
|
||||
Assertions.assertEquals(name, nameUpperWarp.get().getName());
|
||||
|
||||
final String nameLower = name.toLowerCase();
|
||||
final Optional<Warp> nameLowerWarp = plugin.getDatabase().getWarp(nameLower, true);
|
||||
Assertions.assertTrue(nameLowerWarp.isPresent());
|
||||
Assertions.assertEquals(name, nameLowerWarp.get().getName());
|
||||
}
|
||||
|
||||
@DisplayName("Test Deleting All Warps")
|
||||
@Order(9)
|
||||
@Test
|
||||
public void testWarpDeleteAll() {
|
||||
final int deleted = plugin.getManager().warps().deleteAllWarps();
|
||||
Assertions.assertTrue(plugin.getManager().warps().getWarps().isEmpty());
|
||||
Assertions.assertTrue(plugin.getDatabase().getWarps().isEmpty());
|
||||
Assertions.assertEquals(WARP_NAMES.size(), deleted);
|
||||
}
|
||||
|
||||
@NotNull
|
||||
public static Stream<Arguments> provideWarpData() {
|
||||
final World world = BukkitAdapter.adaptWorld(server.getWorld("world")).orElseThrow();
|
||||
final Position position = Position.at(0, 0, 0, 0, 0, world, plugin.getServerName());
|
||||
return WARP_NAMES.stream()
|
||||
.flatMap(name -> Stream.of(Arguments.of(name, position)));
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@Nested
|
||||
@DisplayName("Home Tests")
|
||||
@TestMethodOrder(MethodOrderer.OrderAnnotation.class)
|
||||
public class HomeTests {
|
||||
private static final List<String> HOME_NAMES = List.of(
|
||||
"mr_home",
|
||||
"homble",
|
||||
"seaside",
|
||||
"hovel",
|
||||
"tokyo-3",
|
||||
"testington",
|
||||
"h"
|
||||
);
|
||||
private static BukkitUser homeOwner;
|
||||
|
||||
@DisplayName("Ensure User Data")
|
||||
@BeforeAll
|
||||
public static void createHomeUser() {
|
||||
homeOwner = BukkitUser.adapt(server.addPlayer("TestUser278"), plugin);
|
||||
plugin.getDatabase().ensureUser(homeOwner);
|
||||
Assertions.assertTrue(plugin.getDatabase().getUserData(homeOwner.getUuid()).isPresent());
|
||||
}
|
||||
|
||||
@DisplayName("Test Home Creation")
|
||||
@ParameterizedTest(name = "Home Name: \"{1}\"")
|
||||
@MethodSource("provideHomeData")
|
||||
@Order(1)
|
||||
public void testHomeCreation(@NotNull OnlineUser owner, @NotNull String name, @NotNull Position position) {
|
||||
plugin.getManager().homes().createHome(owner, name, position);
|
||||
Assertions.assertTrue(plugin.getDatabase().getHome(owner, name).isPresent());
|
||||
Assertions.assertTrue(plugin.getManager().homes()
|
||||
.getUserHomes()
|
||||
.get(owner.getUsername()).stream()
|
||||
.anyMatch(home -> home.equals(name)));
|
||||
}
|
||||
|
||||
@DisplayName("Test Home Renaming")
|
||||
@ParameterizedTest(name = "Rename: \"{1}\" > \"{1}2\"")
|
||||
@MethodSource("provideHomeData")
|
||||
@Order(2)
|
||||
@SuppressWarnings("unused")
|
||||
public void testHomeRenaming(@NotNull OnlineUser owner, @NotNull String name, @NotNull Position position) {
|
||||
final String newName = "new_" + name;
|
||||
plugin.getManager().homes().setHomeName(owner, name, newName);
|
||||
Assertions.assertTrue(plugin.getDatabase().getHome(owner, newName).isPresent());
|
||||
Assertions.assertFalse(plugin.getDatabase().getHome(owner, name).isPresent());
|
||||
Assertions.assertTrue(plugin.getManager().homes()
|
||||
.getUserHomes()
|
||||
.get(owner.getUsername()).stream()
|
||||
.anyMatch(home -> home.equals(newName)));
|
||||
Assertions.assertFalse(plugin.getManager().homes()
|
||||
.getUserHomes()
|
||||
.get(owner.getUsername()).stream()
|
||||
.anyMatch(home -> home.equals(name)));
|
||||
|
||||
// Rename back to original name
|
||||
plugin.getManager().homes().setHomeName(owner, newName, name);
|
||||
Assertions.assertTrue(plugin.getDatabase().getHome(owner, name).isPresent());
|
||||
Assertions.assertTrue(plugin.getManager().homes()
|
||||
.getUserHomes()
|
||||
.get(owner.getUsername()).stream()
|
||||
.anyMatch(home -> home.equals(name)));
|
||||
}
|
||||
|
||||
@DisplayName("Test Home Description")
|
||||
@ParameterizedTest(name = "Edit Description: \"{1}\"")
|
||||
@MethodSource("provideHomeData")
|
||||
@Order(3)
|
||||
@SuppressWarnings("unused")
|
||||
public void testHomeDescription(@NotNull OnlineUser owner, @NotNull String name, @NotNull Position position) {
|
||||
final String description = "This is a test description for the home " + name + "!";
|
||||
plugin.getManager().homes().setHomeDescription(owner, name, description);
|
||||
Assertions.assertTrue(plugin.getDatabase().getHome(owner, name).isPresent());
|
||||
|
||||
final Optional<String> homeDescription = plugin.getDatabase().getHome(owner, name)
|
||||
.map(Home::getMeta)
|
||||
.map(PositionMeta::getDescription);
|
||||
Assertions.assertTrue(homeDescription.isPresent());
|
||||
Assertions.assertEquals(description, homeDescription.get());
|
||||
}
|
||||
|
||||
@DisplayName("Test Home Relocation")
|
||||
@ParameterizedTest(name = "Relocate: \"{1}\"")
|
||||
@MethodSource("provideHomeData")
|
||||
@Order(4)
|
||||
public void testHomeRelocation(@NotNull OnlineUser owner, @NotNull String name, @NotNull Position position) {
|
||||
final World world = BukkitAdapter.adaptWorld(server.getWorld("world")).orElseThrow();
|
||||
final Position newPosition = Position.at(
|
||||
position.getX() + 10, position.getY() + 10, position.getZ() + 10,
|
||||
0, 0,
|
||||
world, plugin.getServerName()
|
||||
);
|
||||
plugin.getManager().homes().setHomePosition(owner, name, newPosition);
|
||||
Assertions.assertTrue(plugin.getDatabase().getHome(owner, name).isPresent());
|
||||
|
||||
final Optional<Position> homePosition = plugin.getDatabase().getHome(owner, name).map(home -> home);
|
||||
Assertions.assertTrue(homePosition.isPresent());
|
||||
Assertions.assertEquals(newPosition.getX(), homePosition.get().getX());
|
||||
Assertions.assertEquals(newPosition.getY(), homePosition.get().getY());
|
||||
Assertions.assertEquals(newPosition.getZ(), homePosition.get().getZ());
|
||||
}
|
||||
|
||||
@DisplayName("Test Home Overwrite")
|
||||
@ParameterizedTest(name = "Overwrite: \"{1}\"")
|
||||
@MethodSource("provideHomeData")
|
||||
@Order(5)
|
||||
public void testHomeOverwrite(@NotNull OnlineUser owner, @NotNull String name, @NotNull Position position) {
|
||||
final World world = BukkitAdapter.adaptWorld(server.getWorld("world")).orElseThrow();
|
||||
final Position newPosition = Position.at(
|
||||
position.getX() + 10, position.getY() + 10, position.getZ() + 10,
|
||||
0, 0,
|
||||
world, plugin.getServerName()
|
||||
);
|
||||
plugin.getManager().homes().setHomePosition(owner, name, newPosition);
|
||||
Assertions.assertTrue(plugin.getDatabase().getHome(owner, name).isPresent());
|
||||
|
||||
final Optional<Position> homePosition = plugin.getDatabase().getHome(owner, name).map(home -> home);
|
||||
Assertions.assertTrue(homePosition.isPresent());
|
||||
Assertions.assertEquals(newPosition.getX(), homePosition.get().getX());
|
||||
Assertions.assertEquals(newPosition.getY(), homePosition.get().getY());
|
||||
Assertions.assertEquals(newPosition.getZ(), homePosition.get().getZ());
|
||||
}
|
||||
|
||||
@DisplayName("Test Making Home Public")
|
||||
@ParameterizedTest(name = "Make Public: \"{1}\"")
|
||||
@MethodSource("provideHomeData")
|
||||
@Order(6)
|
||||
@SuppressWarnings("unused")
|
||||
public void testHomeMakePublic(@NotNull OnlineUser owner, @NotNull String name, @NotNull Position p) {
|
||||
plugin.getManager().homes().setHomePrivacy(owner, name, true);
|
||||
Assertions.assertTrue(plugin.getDatabase().getHome(owner, name).isPresent());
|
||||
|
||||
final Optional<Boolean> homePrivacy = plugin.getDatabase().getHome(owner, name).map(Home::isPublic);
|
||||
Assertions.assertTrue(homePrivacy.isPresent());
|
||||
Assertions.assertTrue(homePrivacy.get());
|
||||
Assertions.assertTrue(plugin.getManager().homes().getPublicHomes().get(owner.getUsername()).contains(name));
|
||||
}
|
||||
|
||||
@DisplayName("Test Making Home Private")
|
||||
@ParameterizedTest(name = "Make Private: \"{1}\"")
|
||||
@MethodSource("provideHomeData")
|
||||
@Order(7)
|
||||
@SuppressWarnings("unused")
|
||||
public void testHomeMakePrivate(@NotNull OnlineUser owner, @NotNull String name, @NotNull Position p) {
|
||||
plugin.getManager().homes().setHomePrivacy(owner, name, false);
|
||||
Assertions.assertTrue(plugin.getDatabase().getHome(owner, name).isPresent());
|
||||
|
||||
final Optional<Boolean> homePrivacy = plugin.getDatabase().getHome(owner, name).map(Home::isPublic);
|
||||
Assertions.assertTrue(homePrivacy.isPresent());
|
||||
Assertions.assertFalse(homePrivacy.get());
|
||||
Assertions.assertFalse(plugin.getManager().homes().getPublicHomes()
|
||||
.getOrDefault(owner.getUsername(), List.of())
|
||||
.contains(name));
|
||||
}
|
||||
|
||||
@DisplayName("Test Querying Homes Case-Insensitively")
|
||||
@ParameterizedTest(name = "Query: \"{1}\"")
|
||||
@MethodSource("provideHomeData")
|
||||
@Order(8)
|
||||
@SuppressWarnings("unused")
|
||||
public void testWarpCaseInsensitiveQuery(@NotNull OnlineUser owner, @NotNull String name, @NotNull Position p) {
|
||||
final String nameUpper = name.toUpperCase();
|
||||
final Optional<Home> nameUpperWarp = plugin.getDatabase().getHome(owner, nameUpper, true);
|
||||
Assertions.assertTrue(nameUpperWarp.isPresent());
|
||||
Assertions.assertEquals(name, nameUpperWarp.get().getName());
|
||||
|
||||
final String nameLower = name.toLowerCase();
|
||||
final Optional<Home> nameLowerWarp = plugin.getDatabase().getHome(owner, nameLower, true);
|
||||
Assertions.assertTrue(nameLowerWarp.isPresent());
|
||||
Assertions.assertEquals(name, nameLowerWarp.get().getName());
|
||||
}
|
||||
|
||||
@DisplayName("Test Home Deletion")
|
||||
@ParameterizedTest(name = "Delete: \"{1}\"")
|
||||
@MethodSource("provideHomeData")
|
||||
@Order(9)
|
||||
public void testHomeDeletion(@NotNull OnlineUser owner, @NotNull String name, @NotNull Position position) {
|
||||
plugin.getManager().homes().deleteHome(owner, name);
|
||||
Assertions.assertFalse(plugin.getDatabase().getHome(owner, name).isPresent());
|
||||
Assertions.assertFalse(plugin.getManager().homes().getUserHomes()
|
||||
.getOrDefault(owner.getUsername(), List.of())
|
||||
.contains(name));
|
||||
|
||||
plugin.getManager().homes().createHome(owner, name, position);
|
||||
}
|
||||
|
||||
@DisplayName("Test Deleting All Homes")
|
||||
@Order(10)
|
||||
@Test
|
||||
public void testDeleteAllHomes() {
|
||||
final int deleted = plugin.getManager().homes().deleteAllHomes(homeOwner);
|
||||
Assertions.assertTrue(plugin.getDatabase().getHomes(homeOwner).isEmpty());
|
||||
Assertions.assertTrue(plugin.getManager().homes().getUserHomes().get(homeOwner.getUsername()).isEmpty());
|
||||
Assertions.assertEquals(HOME_NAMES.size(), deleted);
|
||||
}
|
||||
|
||||
@NotNull
|
||||
public static Stream<Arguments> provideHomeData() {
|
||||
final World world = BukkitAdapter.adaptWorld(server.getWorld("world")).orElseThrow();
|
||||
final Position position = Position.at(0, 0, 0, 0, 0, world, plugin.getServerName());
|
||||
return HOME_NAMES.stream()
|
||||
.flatMap(name -> Stream.of(Arguments.of(homeOwner, name, position)));
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user