001    package cpw.mods.fml.common.event;
002    
003    import java.util.List;
004    
005    import com.google.common.base.Function;
006    import com.google.common.base.Functions;
007    import com.google.common.base.Predicate;
008    import com.google.common.base.Predicates;
009    import com.google.common.collect.ArrayListMultimap;
010    import com.google.common.collect.FluentIterable;
011    import com.google.common.collect.ImmutableList;
012    import com.google.common.collect.ImmutableListMultimap;
013    import com.google.common.collect.Maps;
014    import com.google.common.collect.Multimaps;
015    
016    import cpw.mods.fml.common.Loader;
017    import cpw.mods.fml.common.LoaderState;
018    import cpw.mods.fml.common.ModContainer;
019    import cpw.mods.fml.common.Mod.Init;
020    import cpw.mods.fml.common.Mod.PostInit;
021    
022    
023    /**
024     * Simple intermod communications to receive simple messages directed at you from
025     * other mods
026     *
027     * @author cpw
028     *
029     */
030    public class FMLInterModComms {
031        private static ArrayListMultimap<String, IMCMessage> modMessages = ArrayListMultimap.create();
032        /**
033         * Subscribe to this event to receive your messages (they are sent between {@link Init} and {@link PostInit})
034         *
035         * @author cpw
036         *
037         */
038        public static class IMCEvent extends FMLEvent {
039            @Override
040            public void applyModContainer(ModContainer activeContainer) {
041                currentList = ImmutableList.copyOf(modMessages.get(activeContainer.getModId()));
042            }
043            private ImmutableList<IMCMessage> currentList;
044    
045            public ImmutableList<IMCMessage> getMessages()
046            {
047                return currentList;
048            }
049        }
050    
051        /**
052         * You will receive an instance of this for each message sent
053         * @author cpw
054         *
055         */
056        public static final class IMCMessage {
057            /**
058             * This is the modid of the mod that sent you the message
059             */
060            public final String sender;
061    
062            /**
063             * This field, and {@link #value} are both at the mod's discretion
064             */
065            public final String key;
066            /**
067             * This field, and {@link #key} are both at the mod's discretion
068             */
069            public final String value;
070    
071            private IMCMessage(String sender, String key, String value)
072            {
073                this.key = key;
074                this.value = value;
075                this.sender = sender;
076            }
077            @Override
078            public String toString() {
079                return sender;
080            }
081        }
082    
083        public static boolean sendMessage(String modId, String key, String value)
084        {
085            if (Loader.instance().activeModContainer()==null)
086            {
087                return false;
088            }
089            modMessages.put(modId, new IMCMessage(Loader.instance().activeModContainer().getModId(), key, value));
090            return Loader.isModLoaded(modId) && !Loader.instance().hasReachedState(LoaderState.POSTINITIALIZATION);
091        }
092    }