Start work on Migrators

This commit is contained in:
William committed 2022-08-27 00:46:24 +01:00
1 parent ca9e60394d
commit bee1c811b2
9 files changed
+416 -7

No files matched your search

@@ -12,6 +12,7 @@ import net.william278.huskhomes.hook.EconomyHook;
import net.william278.huskhomes.hook.MapHook;
import net.william278.huskhomes.hook.PluginHook;
import net.william278.huskhomes.messenger.NetworkMessenger;
import net.william278.huskhomes.migrator.Migrator;
import net.william278.huskhomes.player.OnlineUser;
import net.william278.huskhomes.position.Location;
import net.william278.huskhomes.position.SavedPositionManager;
@@ -155,6 +156,13 @@ public interface HuskHomes {
@NotNull
EventDispatcher getEventDispatcher();
/**
* The list of available {@link Migrator}s
*
* @return the list of available {@link Migrator}
*/
List<Migrator> getMigrators();
/**
* The {@link Spawn} location of this server
*
@@ -3,6 +3,7 @@ package net.william278.huskhomes.command;
import de.themoep.minedown.MineDown;
import net.william278.desertwell.AboutMenu;
import net.william278.huskhomes.HuskHomes;
import net.william278.huskhomes.migrator.Migrator;
import net.william278.huskhomes.player.OnlineUser;
import net.william278.huskhomes.util.Permission;
import org.jetbrains.annotations.NotNull;
@@ -11,6 +12,7 @@ import org.jetbrains.annotations.Nullable;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Optional;
import java.util.logging.Level;
import java.util.stream.Collectors;
@@ -104,6 +106,8 @@ public class HuskHomesCommand extends CommandBase implements ConsoleExecutable,
new MineDown("[HuskHomes](#00fb9a bold) [| HuskHomes is up-to-date."
+ " (Currently running: v" + plugin.getPluginVersion() + "](#00fb9a)"))));
}
case "migrate" -> plugin.getLocales().getLocale("error_console_command_only")
.ifPresent(onlineUser::sendMessage);
}
}
@@ -140,9 +144,49 @@ public class HuskHomesCommand extends CommandBase implements ConsoleExecutable,
() -> plugin.getLoggingAdapter().log(Level.INFO,
"HuskHomes is up to date" +
" (Currently running v" + plugin.getPluginVersion() + ")")));
case "migrate" -> {
if (args.length < 2) {
plugin.getLoggingAdapter().log(Level.INFO,
"Please choose a migrator, then run \"huskhomes migrate <migrator>\"");
logMigratorsList();
return;
}
final Optional<Migrator> selectedMigrator = plugin.getMigrators().stream().filter(availableMigrator ->
availableMigrator.getIdentifier().equalsIgnoreCase(args[1])).findFirst();
selectedMigrator.ifPresentOrElse(migrator -> {
if (args.length < 3) {
plugin.getLoggingAdapter().log(Level.INFO, migrator.getHelpMenu());
return;
}
switch (args[2]) {
case "start" -> migrator.start().thenAccept(succeeded -> {
if (succeeded) {
plugin.getLoggingAdapter().log(Level.INFO, "Migration completed successfully!");
} else {
plugin.getLoggingAdapter().log(Level.WARNING, "Migration failed!");
}
});
case "set" -> migrator.handleConfigurationCommand(Arrays.copyOfRange(args, 3, args.length));
default -> plugin.getLoggingAdapter().log(Level.INFO,
"Invalid syntax. Console usage: \"huskhomes migrate " + args[1] + " <start/set>");
}
}, () -> {
plugin.getLoggingAdapter().log(Level.INFO,
"Please specify a valid migrator.\n" +
"If a migrator is not available, please verify that you meet the prerequisites to use it.");
logMigratorsList();
});
}
}
}
private void logMigratorsList() {
plugin.getLoggingAdapter().log(Level.INFO,
"List of available migrators:\nMigrator ID / Migrator Name:\n" +
plugin.getMigrators().stream()
.map(migrator -> migrator.getIdentifier() + " - " + migrator.getName())
.collect(Collectors.joining("\n")));
}
private void sendAboutMenu(@NotNull OnlineUser onlineUser) {
if (!onlineUser.hasPermission(Permission.COMMAND_HUSKHOMES_ABOUT.node)) {
@@ -261,12 +261,12 @@ public class Settings {
* Represents the names of tables in the database
*/
public enum TableName {
PLAYER_DATA("huskhomes_player_data"),
PLAYER_DATA("huskhomes_users"),
POSITION_DATA("huskhomes_position_data"),
SAVED_POSITION_DATA("huskhomes_saved_position_data"),
HOME_DATA("huskhomes_home_data"),
WARP_DATA("huskhomes_warp_data"),
TELEPORT_DATA("huskhomes_teleport_data");
SAVED_POSITION_DATA("huskhomes_saved_positions"),
HOME_DATA("huskhomes_homes"),
WARP_DATA("huskhomes_warps"),
TELEPORT_DATA("huskhomes_teleports");
private final String defaultName;
@@ -0,0 +1,68 @@
package net.william278.huskhomes.migrator;
import net.william278.huskhomes.HuskHomes;
import org.jetbrains.annotations.NotNull;
import java.util.concurrent.CompletableFuture;
/**
* A migrator that imports legacy and other plugin's data into HuskHomes' format.
*/
public abstract class Migrator {
protected final HuskHomes plugin;
protected Migrator(@NotNull HuskHomes implementor) {
this.plugin = implementor;
}
/**
* Start the migrator
*
* @return A future that will be completed when the migrator is done
*/
public abstract CompletableFuture<Boolean> start();
/**
* Handle a command that sets migrator configuration parameters
*
* @param args The command arguments
*/
public abstract void handleConfigurationCommand(@NotNull String[] args);
/**
* Obfuscates a data string to prevent important data from being logged to console
*
* @param dataString The data string to obfuscate
* @return The data string obfuscated with stars (*)
*/
@NotNull
protected final String obfuscateDataString(@NotNull String dataString) {
return (dataString.length() > 1 ? dataString.charAt(0) + "*".repeat(dataString.length() - 1) : "");
}
/**
* Get the unique identifier of the migrator
*
* @return The unique identifier of the migrator
*/
@NotNull
public abstract String getIdentifier();
/**
* Get the display name of the migrator
*
* @return The name of the migrator
*/
@NotNull
public abstract String getName();
/**
* Get the help splash menu of the migrator
*
* @return The help splash menu of the migrator
*/
@NotNull
public abstract String getHelpMenu();
}