diff options
author | Keuin <[email protected]> | 2020-08-02 18:31:50 +0800 |
---|---|---|
committer | keuin <[email protected]> | 2020-08-02 18:31:50 +0800 |
commit | d9c2af035ac2edb68bc8b6a1f94b7c772c3509c8 (patch) | |
tree | 4a27dd9a313090ae75d35e4d27d5bdd40b1eb218 /src/main/java/com/keuin/kbackupfabric/util/BackupNameSuggestionProvider.java | |
parent | f09d420df609194a17e9d9ec63d710df8135aa8e (diff) |
Refactor & object tree impl. stage 1
Diffstat (limited to 'src/main/java/com/keuin/kbackupfabric/util/BackupNameSuggestionProvider.java')
-rw-r--r-- | src/main/java/com/keuin/kbackupfabric/util/BackupNameSuggestionProvider.java | 77 |
1 files changed, 0 insertions, 77 deletions
diff --git a/src/main/java/com/keuin/kbackupfabric/util/BackupNameSuggestionProvider.java b/src/main/java/com/keuin/kbackupfabric/util/BackupNameSuggestionProvider.java deleted file mode 100644 index ff122b9..0000000 --- a/src/main/java/com/keuin/kbackupfabric/util/BackupNameSuggestionProvider.java +++ /dev/null @@ -1,77 +0,0 @@ -package com.keuin.kbackupfabric.util; - -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; - } -} |