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 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<String, IMCMessage> 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<IMCMessage> currentList;
private static final ImmutableList<IMCMessage> emptyIMCList = ImmutableList.<IMCMessage>of();
private static ArrayListMultimap<String, IMCMessage> modMessages = ArrayListMultimap.create();
public ImmutableList<IMCMessage> 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<IMCMessage> 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<IMCMessage> 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<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
* 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
*/
@Deprecated
public Certificate[] getFMLSigningCertificates()
{
CodeSource codeSource = getClass().getClassLoader().getParent().getClass().getProtectionDomain().getCodeSource();