From e65a173890e0b275dd9425c009cb7e9e3a80fa95 Mon Sep 17 00:00:00 2001 From: KnightMiner Date: Mon, 22 Apr 2019 09:57:09 -0500 Subject: [PATCH] Allow isPulseLoaded to check if pulses from other mods are loaded Also adds a static isPulseLoaded method so you can check if a pulse is loaded without registering a pulse manager If you call ModuleFileRepository with no pulse manager, it now uses the global check for pulse loaded --- .../book/repository/ModuleFileRepository.java | 12 ++++-- .../mantle/pulsar/control/PulseManager.java | 38 ++++++++++++++++++- 2 files changed, 45 insertions(+), 5 deletions(-) diff --git a/src/main/java/slimeknights/mantle/client/book/repository/ModuleFileRepository.java b/src/main/java/slimeknights/mantle/client/book/repository/ModuleFileRepository.java index 113734d..dfb32df 100644 --- a/src/main/java/slimeknights/mantle/client/book/repository/ModuleFileRepository.java +++ b/src/main/java/slimeknights/mantle/client/book/repository/ModuleFileRepository.java @@ -2,6 +2,7 @@ package slimeknights.mantle.client.book.repository; import java.util.Arrays; import java.util.List; +import java.util.function.Predicate; import java.util.stream.Collectors; import slimeknights.mantle.client.book.BookLoader; @@ -12,17 +13,22 @@ import slimeknights.mantle.pulsar.control.PulseManager; public class ModuleFileRepository extends FileRepository { - private PulseManager manager; + private Predicate manager; public ModuleFileRepository(PulseManager manager, String location) { super(location); - this.manager = manager; + this.manager = manager::isPulseLoaded; } + public ModuleFileRepository(String location) { + super(location); + this.manager = PulseManager::isPulseLoadedGlobal; + } + @Override public List getSections() { // same as super, except we remove any where the related module is not loaded return Arrays.stream(BookLoader.GSON.fromJson(resourceToString(getResource(getResourceLocation("index.json"))), SectionDataModule[].class)) - .filter((section)->section.module.isEmpty() || manager.isPulseLoaded(section.module)) + .filter((section)->section.module.isEmpty() || manager.test(section.module)) .collect(Collectors.toList()); } } diff --git a/src/main/java/slimeknights/mantle/pulsar/control/PulseManager.java b/src/main/java/slimeknights/mantle/pulsar/control/PulseManager.java index c995eb1..a30f861 100644 --- a/src/main/java/slimeknights/mantle/pulsar/control/PulseManager.java +++ b/src/main/java/slimeknights/mantle/pulsar/control/PulseManager.java @@ -3,6 +3,7 @@ package slimeknights.mantle.pulsar.control; import com.google.common.eventbus.EventBus; import com.google.common.eventbus.Subscribe; +import net.minecraft.util.ResourceLocation; import net.minecraftforge.common.MinecraftForge; import net.minecraftforge.fml.common.FMLCommonHandler; import net.minecraftforge.fml.common.FMLModContainer; @@ -15,6 +16,7 @@ import org.apache.logging.log4j.Logger; import java.lang.reflect.Field; import java.util.Collection; +import java.util.HashMap; import java.util.LinkedHashMap; import java.util.Map; @@ -48,7 +50,8 @@ public class PulseManager { private Logger log; private final boolean useConfig; - private final LinkedHashMap pulses = new LinkedHashMap(); + private static final Map> ALL_PULSES = new HashMap<>(); + private final Map pulses = new LinkedHashMap<>(); // Use the Google @Subscribe to avoid confusion/breaking changes. private final Flightpath flightpath = new Flightpath(new AnnotationLocator(Subscribe.class)); @@ -93,6 +96,8 @@ public class PulseManager { log = LogManager.getLogger("Pulsar-" + modId); flightpath.setExceptionHandler(new BusExceptionHandler(modId)); FMLCommonHandler.instance().registerCrashCallable(new CrashHandler(modId, this)); + // regsister our pulse loader so it can be found by the static method + ALL_PULSES.put(modId, pulses); // Attach us to the mods FML bus attachToContainerEventBus(this); } @@ -233,10 +238,39 @@ public class PulseManager { /** * Check if a given Pulse ID is loaded in this manager. * - * @param pulseId The ID to check. + * @param pulseId The ID to check. If it contains : treats the ID as a resource location and checks the global list * @return Whether the ID was present. */ public boolean isPulseLoaded(String pulseId) { + // if it has a colon, assume its a namespaced ID + if(pulseId.contains(":")) { + return isPulseLoadedGlobal(pulseId); + } + return isPulseLoaded(pulses, pulseId); + } + + /** + * Check if a given Pulse ID is loaded in any manager. + * + * @param pulseId Resource location string to check. Domain is the mod ID, path is the pulse ID + * @return Whether the ID was present. + */ + public static boolean isPulseLoadedGlobal(String pulseId) { + ResourceLocation loc = new ResourceLocation(pulseId); + Map pulses = ALL_PULSES.get(loc.getResourceDomain()); + if(pulses != null) { + return isPulseLoaded(pulses, loc.getResourcePath()); + } + return false; + } + + /** + * Checks whether a pulse is loaded in the given pulse manager + * + * @param pulseId The ID to check. + * @return Whether the ID was present. + */ + private static boolean isPulseLoaded(Map pulses, String pulseId) { for(Map.Entry entry : pulses.entrySet()) { if (entry.getValue().getId().equals(pulseId)) { return true;