diff options
author | Keuin <[email protected]> | 2021-01-27 10:16:04 +0800 |
---|---|---|
committer | keuin <[email protected]> | 2021-01-27 10:16:04 +0800 |
commit | 096aedf577847f06c46584071d51a8c4acbd6ab4 (patch) | |
tree | 009ed1afc2c6324dab84d77ff12f8ec42d640b4e /src/main/java | |
parent | d3ea25a70a93d71b6f00941466faf3e9c691185f (diff) |
Use mojang implementation for command suggesting
Diffstat (limited to 'src/main/java')
-rw-r--r-- | src/main/java/com/keuin/kbackupfabric/backup/suggestion/BackupNameSuggestionProvider.java | 74 |
1 files changed, 39 insertions, 35 deletions
diff --git a/src/main/java/com/keuin/kbackupfabric/backup/suggestion/BackupNameSuggestionProvider.java b/src/main/java/com/keuin/kbackupfabric/backup/suggestion/BackupNameSuggestionProvider.java index 9eb6981..72b5c73 100644 --- a/src/main/java/com/keuin/kbackupfabric/backup/suggestion/BackupNameSuggestionProvider.java +++ b/src/main/java/com/keuin/kbackupfabric/backup/suggestion/BackupNameSuggestionProvider.java @@ -1,15 +1,12 @@ package com.keuin.kbackupfabric.backup.suggestion; import com.mojang.brigadier.suggestion.SuggestionProvider; -import com.mojang.brigadier.suggestion.Suggestions; -import com.mojang.brigadier.suggestion.SuggestionsBuilder; +import net.minecraft.server.command.CommandSource; 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 { @@ -18,28 +15,30 @@ public class BackupNameSuggestionProvider { private static final Object syncCache = new Object(); private static final long CACHE_TTL = 8000; private static String backupSaveDirectory; - private static long cacheUpdateTime = 0; + private static int cacheUpdateTime = 0; public static void setBackupSaveDirectory(String backupSaveDirectory) { synchronized (syncSetDirectory) { BackupNameSuggestionProvider.backupSaveDirectory = backupSaveDirectory; } - // Immediately perform an update + // update immediately 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(f.getName()); - cacheUpdateTime = System.currentTimeMillis(); - } catch (NullPointerException ignored) { + synchronized (syncSetDirectory) { + try { + File file = new File(backupSaveDirectory); + candidateCacheList.clear(); + File[] files = file.listFiles(); + if (files == null) + return; + for (File f : files) + candidateCacheList.add(f.getName()); + cacheUpdateTime = (int) System.currentTimeMillis(); + } catch (NullPointerException ignored) { + } } } } @@ -50,28 +49,33 @@ public class BackupNameSuggestionProvider { // } public static SuggestionProvider<ServerCommandSource> getProvider() { - return (context, builder) -> getCompletableFuture(builder); +// return (context, builder) -> getCompletableFuture(builder); + return (context, builder) -> { + if (isCacheExpired()) + updateCandidateList(); + return CommandSource.suggestMatching(candidateCacheList, 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 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; + return ((int) System.currentTimeMillis()) - cacheUpdateTime > CACHE_TTL || cacheUpdateTime == 0; } } |