From 24ebb77860145a6987f2f8e61e1982f94323b059 Mon Sep 17 00:00:00 2001 From: KennyTV Date: Fri, 24 Apr 2020 13:48:22 +0200 Subject: [PATCH] Directly add dependent futures to the list instead of waiting for the extra boolean --- .../api/data/MappingDataLoader.java | 2 -- .../api/protocol/ProtocolRegistry.java | 19 ++++++++----------- 2 files changed, 8 insertions(+), 13 deletions(-) diff --git a/common/src/main/java/us/myles/ViaVersion/api/data/MappingDataLoader.java b/common/src/main/java/us/myles/ViaVersion/api/data/MappingDataLoader.java index 651bcc2d8..e2371aebf 100644 --- a/common/src/main/java/us/myles/ViaVersion/api/data/MappingDataLoader.java +++ b/common/src/main/java/us/myles/ViaVersion/api/data/MappingDataLoader.java @@ -6,7 +6,6 @@ import com.google.gson.JsonIOException; import com.google.gson.JsonObject; import com.google.gson.JsonSyntaxException; import us.myles.ViaVersion.api.Via; -import us.myles.ViaVersion.api.protocol.ProtocolRegistry; import us.myles.ViaVersion.util.GsonUtil; import java.io.File; @@ -34,7 +33,6 @@ public class MappingDataLoader { public static void enableMappingsCache() { cacheJsonMappings = true; - ProtocolRegistry.setKeepExecutorLoaded(true); } /** diff --git a/common/src/main/java/us/myles/ViaVersion/api/protocol/ProtocolRegistry.java b/common/src/main/java/us/myles/ViaVersion/api/protocol/ProtocolRegistry.java index 29111251b..3ae565322 100644 --- a/common/src/main/java/us/myles/ViaVersion/api/protocol/ProtocolRegistry.java +++ b/common/src/main/java/us/myles/ViaVersion/api/protocol/ProtocolRegistry.java @@ -61,7 +61,6 @@ public class ProtocolRegistry { private static Map, CompletableFuture> mappingLoaderFutures = new HashMap<>(); private static ThreadPoolExecutor mappingLoaderExecutor; private static boolean mappingsLoaded; - private static boolean keepExecutorLoaded; static { mappingLoaderExecutor = new ThreadPoolExecutor(5, 16, 45L, TimeUnit.SECONDS, new SynchronousQueue<>()); @@ -329,7 +328,7 @@ public class ProtocolRegistry { */ public static boolean checkForMappingCompletion() { synchronized (MAPPING_LOADER_LOCK) { - if (mappingsLoaded || keepExecutorLoaded) return false; + if (mappingsLoaded) return false; for (CompletableFuture future : mappingLoaderFutures.values()) { // Return if any future hasn't completed yet @@ -361,19 +360,17 @@ public class ProtocolRegistry { } } + public static void addMappingLoaderFuture(Class protocolClass, Class dependsOn, Runnable runnable) { + synchronized (MAPPING_LOADER_LOCK) { + CompletableFuture future = getMappingLoaderFuture(dependsOn).whenCompleteAsync((v, throwable) -> runnable.run(), mappingLoaderExecutor); + mappingLoaderFutures.put(protocolClass, future); + } + } + public static CompletableFuture getMappingLoaderFuture(Class protocolClass) { synchronized (MAPPING_LOADER_LOCK) { if (mappingsLoaded) return null; return mappingLoaderFutures.get(protocolClass); } } - - /** - * If set to true, the executor and mappings will stay loaded, even if all current futures have been completed. - * - * @param keepExecutorLoaded whether to keep the executor and mappings loaded, even if all current futures have been completed - */ - public static void setKeepExecutorLoaded(boolean keepExecutorLoaded) { - ProtocolRegistry.keepExecutorLoaded = keepExecutorLoaded; - } }