diff --git a/src/main/java/slimeknights/mantle/pulsar/config/IConfiguration.java b/src/main/java/slimeknights/mantle/pulsar/config/IConfiguration.java index 62ab13b..4350ab4 100644 --- a/src/main/java/slimeknights/mantle/pulsar/config/IConfiguration.java +++ b/src/main/java/slimeknights/mantle/pulsar/config/IConfiguration.java @@ -2,7 +2,6 @@ package slimeknights.mantle.pulsar.config; import javax.annotation.ParametersAreNonnullByDefault; -import net.minecraftforge.common.ForgeConfigSpec.BooleanValue; import slimeknights.mantle.pulsar.pulse.PulseMeta; /** @@ -13,62 +12,34 @@ import slimeknights.mantle.pulsar.pulse.PulseMeta; @ParametersAreNonnullByDefault public interface IConfiguration { - /** - * Used via @PulseManager to determine the type of config file used. - * - * @return Whether the config file is a TOML file or not. - */ - boolean isUsingTomlConfig(); - /** * Perform any configuration loading required. */ void load(); + /** + * Perform anything after all pulses have been registered. + */ + void postLoad(); + /** * Gets whether the given module is enabled in the config. * * @param meta The pulse metadata. * @return Whether the module is enabled. */ - default boolean isModuleEnabled(PulseMeta meta) { - return false; - } + boolean isModuleEnabled(PulseMeta meta); /** - * Gets whether the given module is enabled in the config. - * Use this if you are using TOML files (PulsarConfig). - * - * @param booleanValue The boolean value returned by getConfigEntry - * @return Whether the module is enabled. - */ - default boolean isModuleEnabled(BooleanValue booleanValue) { - return false; - } - - /** - * Gets the specific boolean value for the pulsar module. - * Use this if you are using TOML files (PulsarConfig). + * Perform any action whenever a new pulse get's registered. * * @param meta The pulse metadata. - * @return The BooleanValue issued by the TOML config + * @return Whether the module is enabled. */ - default BooleanValue getConfigEntry(PulseMeta meta) { - return null; - } + void addPulse(PulseMeta meta); /** * Flush configuration to disk/database/whatever. */ void flush(); - - /** - * Used only by TOML configs to set the description to use for the configs. - */ - default void popBuilder() {} - - /** - * Used only by TOML configs to clear the description used by the config settings. - */ - default void pushBuilder() {} } diff --git a/src/main/java/slimeknights/mantle/pulsar/config/PulsarConfig.java b/src/main/java/slimeknights/mantle/pulsar/config/PulsarConfig.java index b696c31..0f8f739 100644 --- a/src/main/java/slimeknights/mantle/pulsar/config/PulsarConfig.java +++ b/src/main/java/slimeknights/mantle/pulsar/config/PulsarConfig.java @@ -7,10 +7,14 @@ import net.minecraftforge.fml.loading.FMLPaths; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; import slimeknights.mantle.pulsar.control.PulseManager; +import slimeknights.mantle.pulsar.internal.Configuration; import slimeknights.mantle.pulsar.pulse.PulseMeta; +import javax.annotation.Nonnull; import java.nio.file.Path; +import java.util.LinkedHashMap; import java.util.Locale; +import java.util.Map; public class PulsarConfig implements IConfiguration { @@ -25,6 +29,7 @@ public class PulsarConfig implements IConfiguration private boolean builderPushed; private boolean configBuilt; + private final Map modules = new LinkedHashMap<>(); /** * Creates the config to be used later. @@ -39,16 +44,16 @@ public class PulsarConfig implements IConfiguration this.description = description.toLowerCase(Locale.US); this.builderPushed = false; this.configBuilt = false; + BUILDER.push(this.description); } @Override - public boolean isUsingTomlConfig() { - return true; - } + public void load() {} @Override - public void load() { + public void postLoad() { if(!this.configBuilt) { + BUILDER.pop(); SPEC = BUILDER.build(); SPEC.setConfig(config); } @@ -57,18 +62,26 @@ public class PulsarConfig implements IConfiguration } @Override - public void pushBuilder() { - if(!builderPushed) BUILDER.push(this.description); + public boolean isModuleEnabled(@Nonnull PulseMeta meta) { + ForgeConfigSpec.BooleanValue entry = modules.get(meta.getId()); - builderPushed = true; + if (entry == null) { + modules.put(meta.getId(), BUILDER.comment(meta.getDescription()).worldRestart().define(meta.getId(), meta.isDefaultEnabled())); + return meta.isEnabled(); + } else { + return entry.get(); + } } @Override - public void popBuilder() { - BUILDER.pop(); + public void addPulse(@Nonnull PulseMeta meta) { + ForgeConfigSpec.BooleanValue entry = modules.get(meta.getId()); + if (entry == null) { + modules.put(meta.getId(), BUILDER.comment(meta.getDescription()).worldRestart().define(meta.getId(), meta.isDefaultEnabled())); + } } - @Override + /*@Override public boolean isModuleEnabled(ForgeConfigSpec.BooleanValue configValue) { return configValue.get(); } @@ -78,7 +91,7 @@ public class PulsarConfig implements IConfiguration ForgeConfigSpec.BooleanValue value = BUILDER.comment(meta.getDescription()).worldRestart().define(meta.getId(), meta.isDefaultEnabled()); return value; - } + }*/ @Override public void flush() { diff --git a/src/main/java/slimeknights/mantle/pulsar/control/PulseManager.java b/src/main/java/slimeknights/mantle/pulsar/control/PulseManager.java index e510d8f..3af1a34 100644 --- a/src/main/java/slimeknights/mantle/pulsar/control/PulseManager.java +++ b/src/main/java/slimeknights/mantle/pulsar/control/PulseManager.java @@ -52,7 +52,6 @@ public class PulseManager { private Logger log; private final boolean useConfig; - private final boolean usingATomlConfig; private static final Map> ALL_PULSES = new HashMap<>(); private final Map pulses = new LinkedHashMap<>(); @@ -77,7 +76,6 @@ public class PulseManager { public PulseManager(String configName) { init(); useConfig = true; - usingATomlConfig = false; conf = new Configuration(configName, log); } @@ -92,7 +90,6 @@ public class PulseManager { public PulseManager(IConfiguration config) { init(); useConfig = true; - usingATomlConfig = config.isUsingTomlConfig(); conf = config; } @@ -131,7 +128,7 @@ public class PulseManager { if (blockNewRegistrations) throw new RuntimeException("A mod tried to register a plugin after preinit! Pulse: " + pulse); if (!configLoaded) { - if(!usingATomlConfig) conf.load(); + conf.load(); configLoaded = true; } @@ -170,18 +167,8 @@ public class PulseManager { PulseMeta meta = new PulseMeta(id, description, forced, enabled, defaultEnabled); meta.setMissingDeps(missingDeps || !hasRequiredPulses(meta, pulseDeps)); - if(usingATomlConfig) { - conf.pushBuilder(); - meta.setConfigEntry(getConfigEntryForConfig(meta)); - allPulses.put(pulse, meta); - } else { - meta.setEnabled(getEnabledFromConfig(meta)); - - if (meta.isEnabled()) { - pulses.put(pulse, meta); - flightpath.register(pulse); - } - } + conf.addPulse(meta); + allPulses.put(pulse, meta); } /** @@ -302,57 +289,26 @@ public class PulseManager { public String toString() { return "PulseManager[" + id + "]"; } - /** - * If you go and use PulsarConfig, you will need to call this method after you register all your pulses. - * - * This is due to how TOML configs work; If you try to get the value too early; it throws NullPointerException + * Enable or Disable all Pulses registered + * + * This MUST be called after you register all your pulses using registerPulse and must be called BEFORE preinit */ public void enablePulses() { - if(usingATomlConfig) { - conf.popBuilder(); - conf.load(); + if(blockNewRegistrations) throw new RuntimeException("A mod tried to enable their plugins after preinit!"); - for (Map.Entry entry : allPulses.entrySet()) { - PulseMeta meta = entry.getValue(); - Object pulse = entry.getKey(); + if(configLoaded) conf.postLoad(); - meta.setEnabled(getEnabledFromConfig(meta.getConfigEntry(), meta)); + for (Map.Entry entry : allPulses.entrySet()) { + PulseMeta meta = entry.getValue(); + Object pulse = entry.getKey(); - if (meta.isEnabled()) { - pulses.put(pulse, meta); - flightpath.register(pulse); - } + meta.setEnabled(getEnabledFromConfig(meta)); + + if (meta.isEnabled()) { + pulses.put(pulse, meta); + flightpath.register(pulse); } - } else { - log.info("Calling enablePulses isn't needed unless you are using a TOML file."); } } - - /** - * Used for getting the config entry from the pulse meta. - * - * @param meta the pulse meta - * - * @return the config entry - */ - private BooleanValue getConfigEntryForConfig(PulseMeta meta) { - if (meta.isForced() || !useConfig) return null; - - return conf.getConfigEntry(meta); - } - - /** - * Used to check if the pulse should be enabled or not. - * - * @param boolValue the config entry - * @param meta the pulse meta - * - * @return Whether the pulse should be enabled. - */ - private boolean getEnabledFromConfig(BooleanValue boolValue, PulseMeta meta) { - if (meta.isForced() || !useConfig) return true; // Forced or no config set. - - return conf.isModuleEnabled(boolValue); - } } diff --git a/src/main/java/slimeknights/mantle/pulsar/flightpath/Flightpath.java b/src/main/java/slimeknights/mantle/pulsar/flightpath/Flightpath.java index 2238fed..95830d5 100755 --- a/src/main/java/slimeknights/mantle/pulsar/flightpath/Flightpath.java +++ b/src/main/java/slimeknights/mantle/pulsar/flightpath/Flightpath.java @@ -2,12 +2,15 @@ package slimeknights.mantle.pulsar.flightpath; import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; +import java.lang.reflect.Type; import java.util.LinkedHashMap; import java.util.Map; import java.util.Set; import javax.annotation.ParametersAreNonnullByDefault; +import net.minecraftforge.eventbus.api.Event; +import net.minecraftforge.eventbus.api.IGenericEvent; import slimeknights.mantle.pulsar.flightpath.lib.AnnotationLocator; import slimeknights.mantle.pulsar.flightpath.lib.BlackholeExceptionHandler; @@ -22,8 +25,7 @@ public class Flightpath { private final ISubscriberLocator locator; // Note we *MUST* use a linked map, otherwise order is lost. - private final LinkedHashMap>> subscribers = new LinkedHashMap>>(); + private final LinkedHashMap, Map>> subscribers = new LinkedHashMap, Map>>(); // Anything that manipulates state *MUST* acquire this lock. It prevents awkward issues when iterating. private final Object lock = new Object(); @@ -77,18 +79,22 @@ public class Flightpath { * * @param evt The event to post. */ - public void post(Object evt) { + public void post(Event evt) { synchronized (lock) { - for (Map.Entry>> ent : subscribers.entrySet()) { - for (Map.Entry> objEnt: ent.getValue().entrySet()) { - if (!objEnt.getKey().isAssignableFrom(evt.getClass())) continue; - Set ms = objEnt.getValue(); - for (Method m : ms) { + for (Map.Entry, Map>> ent : subscribers.entrySet()) { + for (Map.Entry, Map> obj: ent.getValue().entrySet()) { + if (!obj.getKey().isAssignableFrom(evt.getClass())) continue; + for(Map.Entry objEnt : obj.getValue().entrySet()){ + Method m = objEnt.getKey(); + Type filter = objEnt.getValue(); + try { - boolean access = m.isAccessible(); - m.setAccessible(true); - m.invoke(ent.getKey(), evt); - m.setAccessible(access); + if (filter == null || filter == ((IGenericEvent)evt).getGenericType()) { + boolean access = m.isAccessible(); + m.setAccessible(true); + m.invoke(ent.getKey(), evt); + m.setAccessible(access); + } } catch(InvocationTargetException ex) { // thrown when a method throws an exception, so use that exception instead of the wrapper for a better stacktrace if(ex.getTargetException() instanceof Exception) { diff --git a/src/main/java/slimeknights/mantle/pulsar/flightpath/ISubscriberLocator.java b/src/main/java/slimeknights/mantle/pulsar/flightpath/ISubscriberLocator.java index 39f4a21..64d6f03 100755 --- a/src/main/java/slimeknights/mantle/pulsar/flightpath/ISubscriberLocator.java +++ b/src/main/java/slimeknights/mantle/pulsar/flightpath/ISubscriberLocator.java @@ -1,6 +1,7 @@ package slimeknights.mantle.pulsar.flightpath; import java.lang.reflect.Method; +import java.lang.reflect.Type; import java.util.Map; import java.util.Set; @@ -14,6 +15,6 @@ import javax.annotation.ParametersAreNonnullByDefault; public interface ISubscriberLocator { @Nonnull - Map> findSubscribers(Object obj); + Map, Map> findSubscribers(Object obj); } diff --git a/src/main/java/slimeknights/mantle/pulsar/flightpath/lib/AnnotationLocator.java b/src/main/java/slimeknights/mantle/pulsar/flightpath/lib/AnnotationLocator.java index 9efc33e..6abfb29 100755 --- a/src/main/java/slimeknights/mantle/pulsar/flightpath/lib/AnnotationLocator.java +++ b/src/main/java/slimeknights/mantle/pulsar/flightpath/lib/AnnotationLocator.java @@ -2,6 +2,8 @@ package slimeknights.mantle.pulsar.flightpath.lib; import java.lang.annotation.Annotation; import java.lang.reflect.Method; +import java.lang.reflect.ParameterizedType; +import java.lang.reflect.Type; import java.util.HashMap; import java.util.HashSet; import java.util.Map; @@ -10,6 +12,7 @@ import java.util.Set; import javax.annotation.Nonnull; import javax.annotation.ParametersAreNonnullByDefault; +import net.minecraftforge.eventbus.api.IGenericEvent; import slimeknights.mantle.pulsar.flightpath.ISubscriberLocator; /** @@ -27,16 +30,32 @@ public class AnnotationLocator implements ISubscriberLocator { } @Override - public @Nonnull Map> findSubscribers(Object obj) { - Map> methods = new HashMap>(); + public @Nonnull Map, Map> findSubscribers(Object obj) { + Map, Map> methods = new HashMap, Map>(); + for (Method m : obj.getClass().getMethods()) { if (m.isAnnotationPresent(annotation) && m.getParameterTypes().length == 1) { - Class param = m.getParameterTypes()[0]; - if (!methods.containsKey(param)) methods.put(param, new HashSet()); - methods.get(param).add(m); + Class param = m.getParameterTypes()[0]; + + Type filter = null; + + if(IGenericEvent.class.isAssignableFrom(param)) { + Type type = m.getGenericParameterTypes()[0]; + + if (type instanceof ParameterizedType) { + filter = ((ParameterizedType) type).getActualTypeArguments()[0]; + + if (filter instanceof ParameterizedType) { + filter = ((ParameterizedType) filter).getRawType(); + } + } + } + + if (!methods.containsKey(param)) methods.put(param, new HashMap()); + methods.get(param).put(m, filter); } } + return methods; } - } diff --git a/src/main/java/slimeknights/mantle/pulsar/internal/Configuration.java b/src/main/java/slimeknights/mantle/pulsar/internal/Configuration.java index 803ce81..71684bf 100644 --- a/src/main/java/slimeknights/mantle/pulsar/internal/Configuration.java +++ b/src/main/java/slimeknights/mantle/pulsar/internal/Configuration.java @@ -53,27 +53,33 @@ public class Configuration implements IConfiguration { this.logger = logger; } - @Override - public boolean isUsingTomlConfig() { - return false; - } - @Override public void load() { getModulesFromJson(); } + @Override + public void postLoad() {} + @Override public boolean isModuleEnabled(@Nonnull PulseMeta meta) { ConfigEntry entry = modules.get(meta.getId()); if (entry == null) { - modules.put(meta.getId(), new ConfigEntry(meta.isDefaultEnabled(), meta.getDescription())); return meta.isEnabled(); } else { return entry.getEnabled(); } } + @Override + public void addPulse(@Nonnull PulseMeta meta) { + ConfigEntry entry = modules.get(meta.getId()); + + if (entry == null) { + modules.put(meta.getId(), new ConfigEntry(meta.isDefaultEnabled(), meta.getDescription())); + } + } + @Override public void flush() { writeModulesToJson(); diff --git a/src/main/java/slimeknights/mantle/pulsar/pulse/PulseMeta.java b/src/main/java/slimeknights/mantle/pulsar/pulse/PulseMeta.java index d7c01ed..81fc979 100644 --- a/src/main/java/slimeknights/mantle/pulsar/pulse/PulseMeta.java +++ b/src/main/java/slimeknights/mantle/pulsar/pulse/PulseMeta.java @@ -1,7 +1,5 @@ package slimeknights.mantle.pulsar.pulse; -import net.minecraftforge.common.ForgeConfigSpec.BooleanValue; - import javax.annotation.Nullable; import javax.annotation.ParametersAreNonnullByDefault; @@ -16,7 +14,6 @@ public class PulseMeta { private String id, description; private boolean forced, enabled, defaultEnabled; private boolean missingDeps = false; - private BooleanValue configEntry; public PulseMeta(String id, @Nullable String description, boolean forced, boolean enabled, boolean defaultEnabled) { this.id = id; @@ -53,12 +50,4 @@ public class PulseMeta { public boolean isDefaultEnabled() { return defaultEnabled; } - - public void setConfigEntry(BooleanValue entry) { - this.configEntry = entry; - } - - public BooleanValue getConfigEntry() { - return this.configEntry; - } }