summaryrefslogtreecommitdiff
path: root/src/main/java/com/keuin/kbackupfabric/util
diff options
context:
space:
mode:
authorKeuin <[email protected]>2020-04-24 17:48:26 +0800
committerkeuin <[email protected]>2020-04-24 17:48:26 +0800
commit474609422202ae33e1c941f67f000f9d88aa297c (patch)
tree02f303e2c94ec4d2ce6c887bd268406482ae09dd /src/main/java/com/keuin/kbackupfabric/util
parentea01faf3947814fe7ecaa3705e5fbf0f171f7e48 (diff)
Version 1.3.1-dev (BugFix)1.3.1-dev
Improve some info output. BugFix: Deleted backup won't immediately disappear in the suggestion list.
Diffstat (limited to 'src/main/java/com/keuin/kbackupfabric/util')
-rw-r--r--src/main/java/com/keuin/kbackupfabric/util/BackupNameSuggestionProvider.java25
-rw-r--r--src/main/java/com/keuin/kbackupfabric/util/PrintUtil.java20
2 files changed, 32 insertions, 13 deletions
diff --git a/src/main/java/com/keuin/kbackupfabric/util/BackupNameSuggestionProvider.java b/src/main/java/com/keuin/kbackupfabric/util/BackupNameSuggestionProvider.java
index 7e87815..ff122b9 100644
--- a/src/main/java/com/keuin/kbackupfabric/util/BackupNameSuggestionProvider.java
+++ b/src/main/java/com/keuin/kbackupfabric/util/BackupNameSuggestionProvider.java
@@ -15,8 +15,8 @@ public class BackupNameSuggestionProvider {
private static final List<String> candidateCacheList = new ArrayList<>();
private static final Object syncSetDirectory = new Object();
- private static final Object syncUpdate = new Object();
- private static final long CACHE_TTL = 10000;
+ private static final Object syncCache = new Object();
+ private static final long CACHE_TTL = 8000;
private static String backupSaveDirectory;
private static long cacheUpdateTime = 0;
@@ -24,12 +24,12 @@ public class BackupNameSuggestionProvider {
synchronized (syncSetDirectory) {
BackupNameSuggestionProvider.backupSaveDirectory = backupSaveDirectory;
}
- // Immediately perform a update
+ // Immediately perform an update
updateCandidateList();
}
public static void updateCandidateList() {
- synchronized (syncUpdate) {
+ synchronized (syncCache) {
try {
File file = new File(backupSaveDirectory);
candidateCacheList.clear();
@@ -57,20 +57,21 @@ public class BackupNameSuggestionProvider {
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
+ }
- 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.
+ 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;
+ return System.currentTimeMillis() - cacheUpdateTime > CACHE_TTL || cacheUpdateTime == 0;
}
}
diff --git a/src/main/java/com/keuin/kbackupfabric/util/PrintUtil.java b/src/main/java/com/keuin/kbackupfabric/util/PrintUtil.java
index 11995e7..8839993 100644
--- a/src/main/java/com/keuin/kbackupfabric/util/PrintUtil.java
+++ b/src/main/java/com/keuin/kbackupfabric/util/PrintUtil.java
@@ -1,6 +1,7 @@
package com.keuin.kbackupfabric.util;
import com.mojang.brigadier.context.CommandContext;
+import net.minecraft.server.PlayerManager;
import net.minecraft.server.command.ServerCommandSource;
import net.minecraft.text.LiteralText;
import net.minecraft.text.Style;
@@ -13,13 +14,30 @@ import org.apache.logging.log4j.Logger;
public final class PrintUtil {
private static final Object syncMessage = new Object();
+ private static final Object syncBroadcast = new Object();
+ private static final Style broadcastStyle = new Style().setColor(Formatting.AQUA);
private static final Style infoStyle = new Style().setColor(Formatting.WHITE);
- private static final Style stressStyle = new Style().setColor(Formatting.AQUA).setBold(true);
+ private static final Style stressStyle = new Style().setColor(Formatting.AQUA);
private static final Style warnStyle = new Style().setColor(Formatting.YELLOW);
private static final Style errorStyle = new Style().setColor(Formatting.DARK_RED);
private static final Logger LOGGER = LogManager.getLogger();
+ private static PlayerManager fuckMojang = null;
+
+ public static void setPlayerManager(PlayerManager playerManager) {
+ if (fuckMojang == null)
+ fuckMojang = playerManager;
+ }
+
+ public static void broadcast(String message) {
+ synchronized (syncBroadcast) {
+ if (fuckMojang != null)
+ fuckMojang.sendToAll(new LiteralText(message).setStyle(broadcastStyle));
+ else
+ PrintUtil.error("Error in PrintUtil.broadcast: PlayerManager is not initialized.");
+ }
+ }
public static CommandContext<ServerCommandSource> msgStress(CommandContext<ServerCommandSource> context, String messageText) {
return msgStress(context, messageText, false);