From d604e44195c50eff47c3ff7fb129b7ddf7561272 Mon Sep 17 00:00:00 2001 From: Christian Date: Tue, 18 Dec 2012 13:58:57 -0500 Subject: [PATCH] InterModComms now supports a runtime polling based model for inter-mod comms at runtime. Deprecate method that shouldn't be used. COPY it's content to your mod. Don't CALL it. --- .../fml/common/event/FMLInterModComms.java | 203 +++++++++++++----- .../event/FMLPreInitializationEvent.java | 4 + 2 files changed, 148 insertions(+), 59 deletions(-) diff --git a/common/cpw/mods/fml/common/event/FMLInterModComms.java b/common/cpw/mods/fml/common/event/FMLInterModComms.java index 51e04ea196..56b67537c4 100644 --- a/common/cpw/mods/fml/common/event/FMLInterModComms.java +++ b/common/cpw/mods/fml/common/event/FMLInterModComms.java @@ -2,6 +2,9 @@ package cpw.mods.fml.common.event; import java.util.List; +import net.minecraft.item.ItemStack; +import net.minecraft.nbt.NBTTagCompound; + import com.google.common.base.Function; import com.google.common.base.Functions; import com.google.common.base.Predicate; @@ -13,80 +16,162 @@ import com.google.common.collect.ImmutableListMultimap; import com.google.common.collect.Maps; import com.google.common.collect.Multimaps; +import cpw.mods.fml.common.FMLCommonHandler; import cpw.mods.fml.common.Loader; import cpw.mods.fml.common.LoaderState; import cpw.mods.fml.common.ModContainer; import cpw.mods.fml.common.Mod.Init; import cpw.mods.fml.common.Mod.PostInit; - /** - * Simple intermod communications to receive simple messages directed at you from - * other mods + * Simple intermod communications to receive simple messages directed at you + * from other mods * * @author cpw * */ public class FMLInterModComms { - private static ArrayListMultimap modMessages = ArrayListMultimap.create(); - /** - * Subscribe to this event to receive your messages (they are sent between {@link Init} and {@link PostInit}) - * - * @author cpw - * - */ - public static class IMCEvent extends FMLEvent { - @Override - public void applyModContainer(ModContainer activeContainer) { - currentList = ImmutableList.copyOf(modMessages.get(activeContainer.getModId())); - } - private ImmutableList currentList; + private static final ImmutableList emptyIMCList = ImmutableList.of(); + private static ArrayListMultimap modMessages = ArrayListMultimap.create(); - public ImmutableList getMessages() - { - return currentList; - } - } + /** + * Subscribe to this event to receive your messages (they are sent between + * {@link Init} and {@link PostInit}) + * + * @author cpw + * + */ + public static class IMCEvent extends FMLEvent { + @Override + public void applyModContainer(ModContainer activeContainer) + { + currentList = ImmutableList.copyOf(modMessages.removeAll(activeContainer.getModId())); + } - /** - * You will receive an instance of this for each message sent - * @author cpw - * - */ - public static final class IMCMessage { - /** - * This is the modid of the mod that sent you the message - */ - public final String sender; + private ImmutableList currentList; - /** - * This field, and {@link #value} are both at the mod's discretion - */ - public final String key; - /** - * This field, and {@link #key} are both at the mod's discretion - */ - public final String value; + public ImmutableList getMessages() + { + return currentList; + } + } - private IMCMessage(String sender, String key, String value) - { - this.key = key; - this.value = value; - this.sender = sender; - } - @Override - public String toString() { - return sender; - } - } + /** + * You will receive an instance of this for each message sent + * + * @author cpw + * + */ + public static final class IMCMessage { + /** + * This is the modid of the mod that sent you the message + */ + private String sender; + /** + * This field, and {@link #value} are both at the mod's discretion + */ + public final String key; + /** + * This field, and {@link #key} are both at the mod's discretion + */ + private Object value; - public static boolean sendMessage(String modId, String key, String value) - { - if (Loader.instance().activeModContainer()==null) - { - return false; - } - modMessages.put(modId, new IMCMessage(Loader.instance().activeModContainer().getModId(), key, value)); - return Loader.isModLoaded(modId) && !Loader.instance().hasReachedState(LoaderState.POSTINITIALIZATION); - } + private IMCMessage(String key, Object value) + { + this.key = key; + this.value = value; + } + + @Override + public String toString() + { + return sender; + } + + public String getSender() + { + return this.sender; + } + + void setSender(ModContainer activeModContainer) + { + this.sender = activeModContainer.getModId(); + } + + public String getStringValue() + { + return (String) value; + } + + public NBTTagCompound getNBTValue() + { + return (NBTTagCompound) value; + } + + public ItemStack getItemStackValue() + { + return (ItemStack) value; + } + } + + public static boolean sendMessage(String modId, String key, NBTTagCompound value) + { + return enqueueStartupMessage(modId, new IMCMessage(key, value)); + } + public static boolean sendMessage(String modId, String key, ItemStack value) + { + return enqueueStartupMessage(modId, new IMCMessage(key, value)); + } + public static boolean sendMessage(String modId, String key, String value) + { + return enqueueStartupMessage(modId, new IMCMessage(key, value)); + } + + public static void sendRuntimeMessage(Object sourceMod, String modId, String key, NBTTagCompound value) + { + enqueueMessage(sourceMod, modId, new IMCMessage(key, value)); + } + + public static void sendRuntimeMessage(Object sourceMod, String modId, String key, ItemStack value) + { + enqueueMessage(sourceMod, modId, new IMCMessage(key, value)); + } + + public static void sendRuntimeMessage(Object sourceMod, String modId, String key, String value) + { + enqueueMessage(sourceMod, modId, new IMCMessage(key, value)); + } + + private static boolean enqueueStartupMessage(String modTarget, IMCMessage message) + { + if (Loader.instance().activeModContainer() == null) + { + return false; + } + enqueueMessage(Loader.instance().activeModContainer(), modTarget, message); + return Loader.isModLoaded(modTarget) && !Loader.instance().hasReachedState(LoaderState.POSTINITIALIZATION); + + } + private static void enqueueMessage(Object sourceMod, String modTarget, IMCMessage message) + { + ModContainer mc = FMLCommonHandler.instance().findContainerFor(sourceMod); + if (mc != null && Loader.isModLoaded(modTarget)) + { + message.setSender(mc); + modMessages.put(modTarget, message); + } + } + + public static ImmutableList fetchRuntimeMessages(Object forMod) + { + ModContainer mc = FMLCommonHandler.instance().findContainerFor(forMod); + if (mc != null) + { + return ImmutableList.copyOf(modMessages.removeAll(mc)); + } + else + { + return emptyIMCList; + } + } } diff --git a/common/cpw/mods/fml/common/event/FMLPreInitializationEvent.java b/common/cpw/mods/fml/common/event/FMLPreInitializationEvent.java index ff4404b85a..4bd09b866c 100644 --- a/common/cpw/mods/fml/common/event/FMLPreInitializationEvent.java +++ b/common/cpw/mods/fml/common/event/FMLPreInitializationEvent.java @@ -96,8 +96,12 @@ public class FMLPreInitializationEvent extends FMLStateEvent * Retrieve the FML signing certificates, if any. Validate these against the * published FML certificates in your mod, if you wish. * + * Deprecated because mods should NOT trust this code. Rather + * they should copy this, or something like this, into their own mods. + * * @return Certificates used to sign FML and Forge */ + @Deprecated public Certificate[] getFMLSigningCertificates() { CodeSource codeSource = getClass().getClassLoader().getParent().getClass().getProtectionDomain().getCodeSource();