mirror of
https://github.com/darksaid98/HuskHomes.git
synced 2026-12-02 06:28:49 +00:00
Add EssentialsX importer, update docs (#390)
This commit is contained in:
1 parent
dba87cf428
commit
58f1dc6111
25 files changed
+581
-105
No files matched your search
@@ -33,6 +33,7 @@ import net.william278.huskhomes.config.Spawn;
|
||||
import net.william278.huskhomes.database.Database;
|
||||
import net.william278.huskhomes.event.EventDispatcher;
|
||||
import net.william278.huskhomes.hook.*;
|
||||
import net.william278.huskhomes.importer.Importer;
|
||||
import net.william278.huskhomes.manager.Manager;
|
||||
import net.william278.huskhomes.network.Broker;
|
||||
import net.william278.huskhomes.position.Location;
|
||||
@@ -55,6 +56,7 @@ import java.lang.reflect.InvocationTargetException;
|
||||
import java.util.*;
|
||||
import java.util.function.Consumer;
|
||||
import java.util.logging.Level;
|
||||
import java.util.stream.Collectors;
|
||||
import java.util.stream.Stream;
|
||||
|
||||
/**
|
||||
@@ -260,6 +262,14 @@ public interface HuskHomes extends TaskRunner, EventDispatcher, SafetyResolver {
|
||||
return getHook(MapHook.class);
|
||||
}
|
||||
|
||||
@NotNull
|
||||
default List<Importer> getImporters() {
|
||||
return getHooks().stream()
|
||||
.filter(hook -> Importer.class.isAssignableFrom(hook.getClass()))
|
||||
.map(Importer.class::cast)
|
||||
.collect(Collectors.toList());
|
||||
}
|
||||
|
||||
/**
|
||||
* Perform an economy check on the {@link OnlineUser}; returning {@code true} if it passes the check
|
||||
*
|
||||
|
||||
@@ -26,13 +26,17 @@ import net.william278.desertwell.about.AboutMenu;
|
||||
import net.william278.desertwell.util.UpdateChecker;
|
||||
import net.william278.huskhomes.HuskHomes;
|
||||
import net.william278.huskhomes.config.Locales;
|
||||
import net.william278.huskhomes.importer.Importer;
|
||||
import net.william278.huskhomes.user.CommandUser;
|
||||
import net.william278.paginedown.PaginatedList;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Optional;
|
||||
import java.util.stream.Collectors;
|
||||
import java.util.stream.IntStream;
|
||||
|
||||
public class HuskHomesCommand extends Command implements TabProvider {
|
||||
|
||||
@@ -40,6 +44,7 @@ public class HuskHomesCommand extends Command implements TabProvider {
|
||||
"about", false,
|
||||
"help", false,
|
||||
"reload", true,
|
||||
"import", true,
|
||||
"update", true
|
||||
);
|
||||
|
||||
@@ -108,11 +113,44 @@ public class HuskHomesCommand extends Command implements TabProvider {
|
||||
plugin.getLocales().getLocale("update_available", checked.getLatestVersion().toString(),
|
||||
plugin.getVersion().toString()).ifPresent(executor::sendMessage);
|
||||
});
|
||||
case "import" -> {
|
||||
if (plugin.getImporters().isEmpty()) {
|
||||
plugin.getLocales().getLocale("error_no_importers_available")
|
||||
.ifPresent(executor::sendMessage);
|
||||
return;
|
||||
}
|
||||
this.importData(executor, removeFirstArg(args));
|
||||
}
|
||||
default -> plugin.getLocales().getLocale("error_invalid_syntax", getUsage())
|
||||
.ifPresent(executor::sendMessage);
|
||||
}
|
||||
}
|
||||
|
||||
private void importData(@NotNull CommandUser executor, String[] args) {
|
||||
switch (parseStringArg(args, 0).orElse("list")) {
|
||||
case "start" -> parseStringArg(args, 1).ifPresentOrElse(
|
||||
name -> {
|
||||
final Optional<Importer> importer = plugin.getImporters().stream()
|
||||
.filter(available -> available.getImporterName().equalsIgnoreCase(name)).findFirst();
|
||||
if (importer.isEmpty()) {
|
||||
plugin.getLocales().getLocale("error_invalid_importer")
|
||||
.ifPresent(executor::sendMessage);
|
||||
return;
|
||||
}
|
||||
importer.get().start(executor);
|
||||
},
|
||||
() -> plugin.getLocales().getLocale("error_invalid_syntax",
|
||||
"/" + getName() + " import start <importer>")
|
||||
.ifPresent(executor::sendMessage)
|
||||
);
|
||||
case "list" -> executor.sendMessage(getImporterList()
|
||||
.getNearestValidPage(parseIntArg(args, 1).orElse(1)));
|
||||
default -> plugin.getLocales().getLocale("error_invalid_syntax",
|
||||
"/" + getName() + " import <start|list>")
|
||||
.ifPresent(executor::sendMessage);
|
||||
}
|
||||
}
|
||||
|
||||
@NotNull
|
||||
private PaginatedList getCommandList(@NotNull CommandUser user) {
|
||||
return PaginatedList.of(plugin.getCommands().stream()
|
||||
@@ -131,10 +169,41 @@ public class HuskHomesCommand extends Command implements TabProvider {
|
||||
.build());
|
||||
}
|
||||
|
||||
@Override
|
||||
@NotNull
|
||||
private PaginatedList getImporterList() {
|
||||
return PaginatedList.of(plugin.getImporters().stream()
|
||||
.map(importer -> plugin.getLocales().getRawLocale("importer_list_item",
|
||||
Locales.escapeText(importer.getImporterName()),
|
||||
Locales.escapeText(importer.getSupportedImportData().stream()
|
||||
.map(Importer.ImportData::getName)
|
||||
.collect(Collectors.joining(", "))))
|
||||
.orElse(importer.getName()))
|
||||
.collect(Collectors.toList()),
|
||||
plugin.getLocales().getBaseList(Math.min(plugin.getSettings().getListItemsPerPage(), 6))
|
||||
.setHeaderFormat(plugin.getLocales().getRawLocale("importer_list_title").orElse(""))
|
||||
.setItemSeparator("\n").setCommand("/huskhomes:huskhomes import list")
|
||||
.build());
|
||||
}
|
||||
|
||||
@Override
|
||||
@Nullable
|
||||
public List<String> suggest(@NotNull CommandUser user, @NotNull String[] args) {
|
||||
return args.length < 2 ? SUB_COMMANDS.keySet().stream().sorted().toList() : List.of();
|
||||
return switch (args.length) {
|
||||
case 0, 1 -> SUB_COMMANDS.keySet().stream().sorted().toList();
|
||||
case 2 -> switch (args[0].toLowerCase()) {
|
||||
case "help" -> IntStream.rangeClosed(1, getCommandList(user).getTotalPages())
|
||||
.mapToObj(Integer::toString).toList();
|
||||
case "import" -> List.of("start", "list");
|
||||
default -> null;
|
||||
};
|
||||
case 3 -> {
|
||||
if (!args[0].equalsIgnoreCase("import") && !args[1].equalsIgnoreCase("start")) {
|
||||
yield null;
|
||||
}
|
||||
yield plugin.getImporters().stream().map(Importer::getImporterName).toList();
|
||||
}
|
||||
default -> null;
|
||||
};
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,96 @@
|
||||
package net.william278.huskhomes.importer;
|
||||
|
||||
import net.kyori.adventure.text.Component;
|
||||
import net.kyori.adventure.text.format.TextColor;
|
||||
import net.william278.huskhomes.HuskHomes;
|
||||
import net.william278.huskhomes.hook.Hook;
|
||||
import net.william278.huskhomes.user.CommandUser;
|
||||
import net.william278.huskhomes.user.OnlineUser;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
import java.time.LocalDateTime;
|
||||
import java.time.temporal.ChronoUnit;
|
||||
import java.util.List;
|
||||
import java.util.logging.Level;
|
||||
|
||||
public abstract class Importer extends Hook {
|
||||
|
||||
protected final HuskHomes plugin;
|
||||
private final String name;
|
||||
private final List<ImportData> supportedImportData;
|
||||
|
||||
protected Importer(@NotNull String name, @NotNull List<ImportData> supportedData, @NotNull HuskHomes plugin) {
|
||||
super(plugin, name + " Importer");
|
||||
this.name = name;
|
||||
this.supportedImportData = supportedData;
|
||||
this.plugin = plugin;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void initialize() {
|
||||
// No initialization required
|
||||
}
|
||||
|
||||
public final void start(@NotNull CommandUser user) {
|
||||
final LocalDateTime startTime = LocalDateTime.now();
|
||||
log(user, Level.INFO, "⌚ Starting " + name + " data import...");
|
||||
|
||||
for (ImportData data : supportedImportData) {
|
||||
try {
|
||||
log(user, Level.INFO, "│ ⌚ Importing " + data.getName() + "...");
|
||||
final int entries = importData(data);
|
||||
log(user, Level.INFO, "│ └ ✔ Imported " + data.getName() + " (" + entries + " entries)");
|
||||
} catch (Throwable e) {
|
||||
log(user, Level.WARNING, "│ └ ❌ Failed to import " + data.getName() + ": " + e.getMessage(), e);
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
final long timeTaken = startTime.until(LocalDateTime.now(), ChronoUnit.SECONDS);
|
||||
log(user, Level.INFO, "✔ Completed import from " + name + " (took " + timeTaken + "s)");
|
||||
}
|
||||
|
||||
protected abstract int importData(@NotNull ImportData importData) throws Throwable;
|
||||
|
||||
protected final void log(@NotNull CommandUser user, @NotNull Level level, @NotNull String message, @NotNull Throwable... e) {
|
||||
message = "[Importer] " + message;
|
||||
if (user instanceof OnlineUser online) {
|
||||
final TextColor color = level == Level.SEVERE || level == Level.WARNING
|
||||
? TextColor.color(0xff3300)
|
||||
: TextColor.color(0xfff021);
|
||||
online.sendMessage(Component.text(message, color));
|
||||
}
|
||||
plugin.log(level, message, e);
|
||||
}
|
||||
|
||||
@NotNull
|
||||
public String getImporterName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
@NotNull
|
||||
public List<ImportData> getSupportedImportData() {
|
||||
return supportedImportData;
|
||||
}
|
||||
|
||||
/**
|
||||
* Represents types of data that can be imported
|
||||
*/
|
||||
public enum ImportData {
|
||||
USERS("User Data"),
|
||||
HOMES("Homes"),
|
||||
WARPS("Warps");
|
||||
|
||||
private final String name;
|
||||
|
||||
ImportData(@NotNull String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
@NotNull
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
Reference in new issue
Block a user