summaryrefslogtreecommitdiff
path: root/src/main/java/com/keuin/kbackupfabric/util/backup/suggestion
diff options
context:
space:
mode:
Diffstat (limited to 'src/main/java/com/keuin/kbackupfabric/util/backup/suggestion')
-rw-r--r--src/main/java/com/keuin/kbackupfabric/util/backup/suggestion/BackupMethodSuggestionProvider.java36
-rw-r--r--src/main/java/com/keuin/kbackupfabric/util/backup/suggestion/BackupNameSuggestionProvider.java78
2 files changed, 114 insertions, 0 deletions
diff --git a/src/main/java/com/keuin/kbackupfabric/util/backup/suggestion/BackupMethodSuggestionProvider.java b/src/main/java/com/keuin/kbackupfabric/util/backup/suggestion/BackupMethodSuggestionProvider.java
new file mode 100644
index 0000000..320d9bf
--- /dev/null
+++ b/src/main/java/com/keuin/kbackupfabric/util/backup/suggestion/BackupMethodSuggestionProvider.java
@@ -0,0 +1,36 @@
+package com.keuin.kbackupfabric.util.backup.suggestion;
+
+import com.keuin.kbackupfabric.util.backup.BackupType;
+import com.mojang.brigadier.suggestion.SuggestionProvider;
+import com.mojang.brigadier.suggestion.Suggestions;
+import com.mojang.brigadier.suggestion.SuggestionsBuilder;
+import net.minecraft.server.command.ServerCommandSource;
+
+import java.util.Arrays;
+import java.util.LinkedList;
+import java.util.List;
+import java.util.Locale;
+import java.util.concurrent.CompletableFuture;
+
+public class BackupMethodSuggestionProvider {
+
+ private static final List<String> suggestions = Arrays.asList(
+ BackupType.OBJECT_TREE_BACKUP.getName(),
+ BackupType.PRIMITIVE_ZIP_BACKUP.getName()
+ ); // All backup methods
+
+ public static SuggestionProvider<ServerCommandSource> getProvider() {
+ return (context, builder) -> getCompletableFuture(builder);
+ }
+
+ private static CompletableFuture<Suggestions> getCompletableFuture(SuggestionsBuilder builder) {
+ String remaining = builder.getRemaining().toLowerCase(Locale.ROOT);
+ for (String string : suggestions) { // Iterate through the supplied list
+ if (string.toLowerCase(Locale.ROOT).startsWith(remaining)) {
+ builder.suggest(string); // Add every single entry to suggestions list.
+ }
+ }
+ return builder.buildFuture(); // Create the CompletableFuture containing all the suggestions
+ }
+
+}
diff --git a/src/main/java/com/keuin/kbackupfabric/util/backup/suggestion/BackupNameSuggestionProvider.java b/src/main/java/com/keuin/kbackupfabric/util/backup/suggestion/BackupNameSuggestionProvider.java
new file mode 100644
index 0000000..f6f4056
--- /dev/null
+++ b/src/main/java/com/keuin/kbackupfabric/util/backup/suggestion/BackupNameSuggestionProvider.java
@@ -0,0 +1,78 @@
+package com.keuin.kbackupfabric.util.backup.suggestion;
+
+import com.keuin.kbackupfabric.util.backup.BackupFilesystemUtil;
+import com.mojang.brigadier.suggestion.SuggestionProvider;
+import com.mojang.brigadier.suggestion.Suggestions;
+import com.mojang.brigadier.suggestion.SuggestionsBuilder;
+import net.minecraft.server.command.ServerCommandSource;
+
+import java.io.File;
+import java.util.ArrayList;
+import java.util.List;
+import java.util.Locale;
+import java.util.concurrent.CompletableFuture;
+
+public class BackupNameSuggestionProvider {
+
+ private static final List<String> candidateCacheList = new ArrayList<>();
+ private static final Object syncSetDirectory = new Object();
+ private static final Object syncCache = new Object();
+ private static final long CACHE_TTL = 8000;
+ private static String backupSaveDirectory;
+ private static long cacheUpdateTime = 0;
+
+ public static void setBackupSaveDirectory(String backupSaveDirectory) {
+ synchronized (syncSetDirectory) {
+ BackupNameSuggestionProvider.backupSaveDirectory = backupSaveDirectory;
+ }
+ // Immediately perform an update
+ updateCandidateList();
+ }
+
+ public static void updateCandidateList() {
+ synchronized (syncCache) {
+ try {
+ File file = new File(backupSaveDirectory);
+ candidateCacheList.clear();
+ File[] files = file.listFiles();
+ if (files == null)
+ return;
+ for (File f : files)
+ candidateCacheList.add(BackupFilesystemUtil.getBackupName(f.getName()));
+ cacheUpdateTime = System.currentTimeMillis();
+ } catch (NullPointerException ignored) {
+ }
+ }
+ }
+
+// private static void updateCandidateList(Collection<String> stringCollection) {
+// candidateList.clear();
+// candidateList.addAll(stringCollection);
+// }
+
+ public static SuggestionProvider<ServerCommandSource> getProvider() {
+ return (context, builder) -> getCompletableFuture(builder);
+ }
+
+ private static CompletableFuture<Suggestions> getCompletableFuture(SuggestionsBuilder builder) {
+ if (isCacheExpired())
+ updateCandidateList();
+ String remaining = builder.getRemaining().toLowerCase(Locale.ROOT);
+ synchronized (syncCache) {
+ if (candidateCacheList.isEmpty()) { // If the list is empty then return no suggestions
+ return Suggestions.empty(); // No suggestions
+ }
+
+ for (String string : candidateCacheList) { // Iterate through the supplied list
+ if (string.toLowerCase(Locale.ROOT).startsWith(remaining)) {
+ builder.suggest(string); // Add every single entry to suggestions list.
+ }
+ }
+ }
+ return builder.buildFuture(); // Create the CompletableFuture containing all the suggestions
+ }
+
+ private static boolean isCacheExpired() {
+ return System.currentTimeMillis() - cacheUpdateTime > CACHE_TTL || cacheUpdateTime == 0;
+ }
+}