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.
This commit is contained in:
Christian 2012-12-18 13:58:57 -05:00
parent 926766b487
commit 2fc0ba693c
2 changed files with 148 additions and 59 deletions

View file

@ -2,6 +2,9 @@ package cpw.mods.fml.common.event;
import java.util.List; 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.Function;
import com.google.common.base.Functions; import com.google.common.base.Functions;
import com.google.common.base.Predicate; import com.google.common.base.Predicate;
@ -13,33 +16,38 @@ import com.google.common.collect.ImmutableListMultimap;
import com.google.common.collect.Maps; import com.google.common.collect.Maps;
import com.google.common.collect.Multimaps; import com.google.common.collect.Multimaps;
import cpw.mods.fml.common.FMLCommonHandler;
import cpw.mods.fml.common.Loader; import cpw.mods.fml.common.Loader;
import cpw.mods.fml.common.LoaderState; import cpw.mods.fml.common.LoaderState;
import cpw.mods.fml.common.ModContainer; import cpw.mods.fml.common.ModContainer;
import cpw.mods.fml.common.Mod.Init; import cpw.mods.fml.common.Mod.Init;
import cpw.mods.fml.common.Mod.PostInit; import cpw.mods.fml.common.Mod.PostInit;
/** /**
* Simple intermod communications to receive simple messages directed at you from * Simple intermod communications to receive simple messages directed at you
* other mods * from other mods
* *
* @author cpw * @author cpw
* *
*/ */
public class FMLInterModComms { public class FMLInterModComms {
private static final ImmutableList<IMCMessage> emptyIMCList = ImmutableList.<IMCMessage>of();
private static ArrayListMultimap<String, IMCMessage> modMessages = ArrayListMultimap.create(); private static ArrayListMultimap<String, IMCMessage> modMessages = ArrayListMultimap.create();
/** /**
* Subscribe to this event to receive your messages (they are sent between {@link Init} and {@link PostInit}) * Subscribe to this event to receive your messages (they are sent between
* {@link Init} and {@link PostInit})
* *
* @author cpw * @author cpw
* *
*/ */
public static class IMCEvent extends FMLEvent { public static class IMCEvent extends FMLEvent {
@Override @Override
public void applyModContainer(ModContainer activeContainer) { public void applyModContainer(ModContainer activeContainer)
currentList = ImmutableList.copyOf(modMessages.get(activeContainer.getModId())); {
currentList = ImmutableList.copyOf(modMessages.removeAll(activeContainer.getModId()));
} }
private ImmutableList<IMCMessage> currentList; private ImmutableList<IMCMessage> currentList;
public ImmutableList<IMCMessage> getMessages() public ImmutableList<IMCMessage> getMessages()
@ -50,6 +58,7 @@ public class FMLInterModComms {
/** /**
* You will receive an instance of this for each message sent * You will receive an instance of this for each message sent
*
* @author cpw * @author cpw
* *
*/ */
@ -57,8 +66,7 @@ public class FMLInterModComms {
/** /**
* This is the modid of the mod that sent you the message * This is the modid of the mod that sent you the message
*/ */
public final String sender; private String sender;
/** /**
* This field, and {@link #value} are both at the mod's discretion * This field, and {@link #value} are both at the mod's discretion
*/ */
@ -66,27 +74,104 @@ public class FMLInterModComms {
/** /**
* This field, and {@link #key} are both at the mod's discretion * This field, and {@link #key} are both at the mod's discretion
*/ */
public final String value; private Object value;
private IMCMessage(String sender, String key, String value) private IMCMessage(String key, Object value)
{ {
this.key = key; this.key = key;
this.value = value; this.value = value;
this.sender = sender;
} }
@Override @Override
public String toString() { public String toString()
{
return sender; 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) public static boolean sendMessage(String modId, String key, String value)
{ {
if (Loader.instance().activeModContainer()==null) 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; return false;
} }
modMessages.put(modId, new IMCMessage(Loader.instance().activeModContainer().getModId(), key, value)); enqueueMessage(Loader.instance().activeModContainer(), modTarget, message);
return Loader.isModLoaded(modId) && !Loader.instance().hasReachedState(LoaderState.POSTINITIALIZATION); 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<IMCMessage> fetchRuntimeMessages(Object forMod)
{
ModContainer mc = FMLCommonHandler.instance().findContainerFor(forMod);
if (mc != null)
{
return ImmutableList.copyOf(modMessages.removeAll(mc));
}
else
{
return emptyIMCList;
}
} }
} }

View file

@ -96,8 +96,12 @@ public class FMLPreInitializationEvent extends FMLStateEvent
* Retrieve the FML signing certificates, if any. Validate these against the * Retrieve the FML signing certificates, if any. Validate these against the
* published FML certificates in your mod, if you wish. * published FML certificates in your mod, if you wish.
* *
* Deprecated because mods should <b>NOT</b> trust this code. Rather
* they should copy this, or something like this, into their own mods.
*
* @return Certificates used to sign FML and Forge * @return Certificates used to sign FML and Forge
*/ */
@Deprecated
public Certificate[] getFMLSigningCertificates() public Certificate[] getFMLSigningCertificates()
{ {
CodeSource codeSource = getClass().getClassLoader().getParent().getClass().getProtectionDomain().getCodeSource(); CodeSource codeSource = getClass().getClassLoader().getParent().getClass().getProtectionDomain().getCodeSource();