|
| 1 | +/* |
| 2 | + * Minecraft Forge, Patchwork Project |
| 3 | + * Copyright (c) 2016-2019, 2019 |
| 4 | + * |
| 5 | + * This library is free software; you can redistribute it and/or |
| 6 | + * modify it under the terms of the GNU Lesser General Public |
| 7 | + * License as published by the Free Software Foundation version 2.1 |
| 8 | + * of the License. |
| 9 | + * |
| 10 | + * This library is distributed in the hope that it will be useful, |
| 11 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 13 | + * Lesser General Public License for more details. |
| 14 | + * |
| 15 | + * You should have received a copy of the GNU Lesser General Public |
| 16 | + * License along with this library; if not, write to the Free Software |
| 17 | + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
| 18 | + */ |
| 19 | + |
| 20 | +package net.minecraftforge.fml; |
| 21 | + |
| 22 | +import java.util.Iterator; |
| 23 | +import java.util.Spliterator; |
| 24 | +import java.util.concurrent.ConcurrentHashMap; |
| 25 | +import java.util.concurrent.ConcurrentLinkedQueue; |
| 26 | +import java.util.concurrent.ConcurrentMap; |
| 27 | +import java.util.function.Consumer; |
| 28 | +import java.util.function.Predicate; |
| 29 | +import java.util.function.Supplier; |
| 30 | +import java.util.stream.Stream; |
| 31 | +import java.util.stream.StreamSupport; |
| 32 | + |
| 33 | +public class InterModComms { |
| 34 | + public static final class IMCMessage { |
| 35 | + private final String modId; |
| 36 | + private final String method; |
| 37 | + private final String senderModId; |
| 38 | + private final Supplier<?> thing; |
| 39 | + |
| 40 | + IMCMessage(String senderModId, String modId, String method, Supplier<?> thing) { |
| 41 | + this.senderModId = senderModId; |
| 42 | + this.modId = modId; |
| 43 | + this.method = method; |
| 44 | + this.thing = thing; |
| 45 | + } |
| 46 | + |
| 47 | + /** |
| 48 | + * @return The modid of the sender. This is supplied by the caller, or by the active mod container context. |
| 49 | + * Consider it unreliable. |
| 50 | + */ |
| 51 | + public final String getSenderModId() { |
| 52 | + return this.senderModId; |
| 53 | + } |
| 54 | + |
| 55 | + /** |
| 56 | + * @return The modid being sent to. |
| 57 | + */ |
| 58 | + public final String getModId() { |
| 59 | + return this.modId; |
| 60 | + } |
| 61 | + |
| 62 | + /** |
| 63 | + * @return The method being sent to. |
| 64 | + */ |
| 65 | + public final String getMethod() { |
| 66 | + return this.method; |
| 67 | + } |
| 68 | + |
| 69 | + /** |
| 70 | + * @param <T> The type of the message. |
| 71 | + * @return A {@link Supplier} of the message. |
| 72 | + */ |
| 73 | + @SuppressWarnings("unchecked") |
| 74 | + public final <T> Supplier<T> getMessageSupplier() { |
| 75 | + return (Supplier<T>) this.thing; |
| 76 | + } |
| 77 | + } |
| 78 | + |
| 79 | + private static ConcurrentMap<String, ConcurrentLinkedQueue<IMCMessage>> containerQueues = new ConcurrentHashMap<>(); |
| 80 | + |
| 81 | + /** |
| 82 | + * Send IMC to remote. Sender will default to the active modcontainer, or minecraft if not. |
| 83 | + * |
| 84 | + * @param modId the mod id to send to |
| 85 | + * @param method the method name to send |
| 86 | + * @param thing the thing associated with the method name |
| 87 | + * @return true if the message was enqueued for sending (the target modid is loaded) |
| 88 | + */ |
| 89 | + public static boolean sendTo(final String modId, final String method, final Supplier<?> thing) { |
| 90 | + if (!ModList.get().isLoaded(modId)) { |
| 91 | + return false; |
| 92 | + } |
| 93 | + containerQueues.computeIfAbsent(modId, k -> new ConcurrentLinkedQueue<>()).add(new IMCMessage(ModLoadingContext.get().getActiveContainer().getModId(), modId, method, thing)); |
| 94 | + return true; |
| 95 | + } |
| 96 | + |
| 97 | + /** |
| 98 | + * Send IMC to remote. |
| 99 | + * |
| 100 | + * @param senderModId the mod id you are sending from |
| 101 | + * @param modId the mod id to send to |
| 102 | + * @param method the method name to send |
| 103 | + * @param thing the thing associated with the method name |
| 104 | + * @return true if the message was enqueued for sending (the target modid is loaded) |
| 105 | + */ |
| 106 | + public static boolean sendTo(final String senderModId, final String modId, final String method, final Supplier<?> thing) { |
| 107 | + if (!ModList.get().isLoaded(modId)) { |
| 108 | + return false; |
| 109 | + } |
| 110 | + containerQueues.computeIfAbsent(modId, k -> new ConcurrentLinkedQueue<>()).add(new IMCMessage(senderModId, modId, method, thing)); |
| 111 | + return true; |
| 112 | + } |
| 113 | + |
| 114 | + /** |
| 115 | + * Retrieve pending messages for your modid. Use the predicate to filter the method name. |
| 116 | + * |
| 117 | + * @param modId the modid you are querying for |
| 118 | + * @param methodMatcher a predicate for the method you are interested in |
| 119 | + * @return All messages passing the supplied method predicate |
| 120 | + */ |
| 121 | + public static Stream<IMCMessage> getMessages(final String modId, final Predicate<String> methodMatcher) { |
| 122 | + ConcurrentLinkedQueue<IMCMessage> queue = containerQueues.get(modId); |
| 123 | + if (queue == null) { |
| 124 | + return Stream.empty(); |
| 125 | + } |
| 126 | + return StreamSupport.stream(new QueueFilteringSpliterator(queue, methodMatcher), false); |
| 127 | + } |
| 128 | + |
| 129 | + /** |
| 130 | + * Retrieve all message for your modid. |
| 131 | + * |
| 132 | + * @param modId the modid you are querying for |
| 133 | + * @return All messages |
| 134 | + */ |
| 135 | + public static Stream<IMCMessage> getMessages(final String modId) { |
| 136 | + return getMessages(modId, s -> Boolean.TRUE); |
| 137 | + } |
| 138 | + |
| 139 | + private static class QueueFilteringSpliterator implements Spliterator<IMCMessage> { |
| 140 | + private final ConcurrentLinkedQueue<IMCMessage> queue; |
| 141 | + private final Predicate<String> methodFilter; |
| 142 | + private final Iterator<IMCMessage> iterator; |
| 143 | + |
| 144 | + public QueueFilteringSpliterator(final ConcurrentLinkedQueue<IMCMessage> queue, final Predicate<String> methodFilter) { |
| 145 | + this.queue = queue; |
| 146 | + this.iterator = queue.iterator(); |
| 147 | + this.methodFilter = methodFilter; |
| 148 | + } |
| 149 | + |
| 150 | + @Override |
| 151 | + public int characteristics() { |
| 152 | + return Spliterator.CONCURRENT | Spliterator.NONNULL | Spliterator.ORDERED; |
| 153 | + } |
| 154 | + |
| 155 | + @Override |
| 156 | + public long estimateSize() { |
| 157 | + return queue.size(); |
| 158 | + } |
| 159 | + |
| 160 | + @Override |
| 161 | + public boolean tryAdvance(final Consumer<? super IMCMessage> action) { |
| 162 | + IMCMessage next; |
| 163 | + do { |
| 164 | + if (!iterator.hasNext()) { |
| 165 | + return false; |
| 166 | + } |
| 167 | + next = this.iterator.next(); |
| 168 | + } |
| 169 | + while (!methodFilter.test(next.method)); |
| 170 | + action.accept(next); |
| 171 | + this.iterator.remove(); |
| 172 | + return true; |
| 173 | + } |
| 174 | + |
| 175 | + @Override |
| 176 | + public Spliterator<IMCMessage> trySplit() { |
| 177 | + return null; |
| 178 | + } |
| 179 | + |
| 180 | + } |
| 181 | +} |
0 commit comments