From 096aedf577847f06c46584071d51a8c4acbd6ab4 Mon Sep 17 00:00:00 2001 From: Keuin Date: Wed, 27 Jan 2021 10:16:04 +0800 Subject: Use mojang implementation for command suggesting --- .../suggestion/BackupNameSuggestionProvider.java | 74 ++++++++++++---------- 1 file changed, 39 insertions(+), 35 deletions(-) (limited to 'src/main/java/com/keuin/kbackupfabric') 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 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 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 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; } } -- cgit v1.2.3