Bump modlauncher to 3.2.x. Add a sender modid and document IMC. Closes #5746

Signed-off-by: cpw <cpw+github@weeksfamily.ca>
This commit is contained in:
cpw committed 2019-08-04 22:19:02 -04:00
1 parent 5048739b7f
commit 0fd8fa2211
2 files changed
+60 -3

No files matched your search

@@ -36,42 +36,99 @@ public class InterModComms
public static final class IMCMessage {
private final String modId;
private final String method;
private final String senderModId;
private final Supplier<?> thing;
IMCMessage(String modId, String method, Supplier<?> thing)
IMCMessage(String senderModId, String modId, String method, Supplier<?> thing)
{
this.senderModId = senderModId;
this.modId = modId;
this.method = method;
this.thing = thing;
}
/**
* @return The modid of the sender. This is supplied by the caller, or by the active mod container context.
* Consider it unreliable.
*/
public final String getSenderModId() {
return this.senderModId;
}
/**
* @return The modid being sent to.
*/
public final String getModId() {
return this.modId;
}
/**
* @return The method being sent to.
*/
public final String getMethod() {
return this.method;
}
/**
* @param <T> The type of the message.
* @return A {@link Supplier} of the message.
*/
@SuppressWarnings("unchecked")
public final <T> Supplier<T> getMessageSupplier() {
return (Supplier<T>)this.thing;
}
}
private static ConcurrentMap<String, ConcurrentLinkedQueue<IMCMessage>> containerQueues = new ConcurrentHashMap<>();
/**
* Send IMC to remote. Sender will default to the active modcontainer, or minecraft if not.
*
* @param modId the mod id to send to
* @param method the method name to send
* @param thing the thing associated with the method name
* @return true if the message was enqueued for sending (the target modid is loaded)
*/
public static boolean sendTo(final String modId, final String method, final Supplier<?> thing) {
if (!ModList.get().isLoaded(modId)) return false;
containerQueues.computeIfAbsent(modId, k->new ConcurrentLinkedQueue<>()).add(new IMCMessage(modId, method, thing));
containerQueues.computeIfAbsent(modId, k->new ConcurrentLinkedQueue<>()).add(new IMCMessage(ModLoadingContext.get().getActiveContainer().getModId(), modId, method, thing));
return true;
}
/**
* Send IMC to remote.
*
* @param senderModId the mod id you are sending from
* @param modId the mod id to send to
* @param method the method name to send
* @param thing the thing associated with the method name
* @return true if the message was enqueued for sending (the target modid is loaded)
*/
public static boolean sendTo(final String senderModId, final String modId, final String method, final Supplier<?> thing) {
if (!ModList.get().isLoaded(modId)) return false;
containerQueues.computeIfAbsent(modId, k->new ConcurrentLinkedQueue<>()).add(new IMCMessage(senderModId, modId, method, thing));
return true;
}
/**
* Retrieve pending messages for your modid. Use the predicate to filter the method name.
*
* @param modId the modid you are querying for
* @param methodMatcher a predicate for the method you are interested in
* @return All messages passing the supplied method predicate
*/
public static Stream<IMCMessage> getMessages(final String modId, final Predicate<String> methodMatcher) {
ConcurrentLinkedQueue<IMCMessage> queue = containerQueues.get(modId);
if (queue == null) return Stream.empty();
return StreamSupport.stream(new QueueFilteringSpliterator(queue, methodMatcher), false);
}
/**
* Retrieve all message for your modid.
*
* @param modId the modid you are querying for
* @return All messages
*/
public static Stream<IMCMessage> getMessages(final String modId) {
return getMessages(modId, s->Boolean.TRUE);
}