Skip to content
This repository was archived by the owner on Jun 3, 2024. It is now read-only.

Commit c00b7c6

Browse files
committed
Remove module + impl everything
Also includes a fix for FMLFingerprintViolationEvent
1 parent 5d385b0 commit c00b7c6

9 files changed

Lines changed: 317 additions & 47 deletions

File tree

patchwork-dispatcher/src/main/java/com/patchworkmc/impl/Patchwork.java

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,12 @@
2828
import net.minecraftforge.event.RegistryEvent;
2929
import net.minecraftforge.eventbus.api.Event;
3030
import net.minecraftforge.fml.ModContainer;
31+
import net.minecraftforge.fml.ModList;
3132
import net.minecraftforge.fml.ModLoadingContext;
3233
import net.minecraftforge.fml.event.lifecycle.FMLCommonSetupEvent;
3334
import net.minecraftforge.fml.event.lifecycle.FMLLoadCompleteEvent;
35+
import net.minecraftforge.fml.event.lifecycle.InterModEnqueueEvent;
36+
import net.minecraftforge.fml.event.lifecycle.InterModProcessEvent;
3437
import net.minecraftforge.fml.javafmlmod.FMLJavaModLoadingContext;
3538
import net.minecraftforge.fml.javafmlmod.FMLModContainer;
3639
import net.minecraftforge.registries.ForgeRegistry;
@@ -98,7 +101,7 @@ private static void dispatch(Map<ForgeInitializer, FMLModContainer> mods, Event
98101
@Override
99102
public void onInitialize() {
100103
Map<ForgeInitializer, FMLModContainer> mods = new HashMap<>();
101-
104+
List<String> modIds = new ArrayList<>();
102105
// Construct forge mods
103106

104107
for (ForgeInitializer initializer : FabricLoader.getInstance().getEntrypoints("patchwork", ForgeInitializer.class)) {
@@ -112,13 +115,19 @@ public void onInitialize() {
112115
ModLoadingContext.get().setActiveContainer(null, "minecraft");
113116

114117
mods.put(initializer, container);
118+
modIds.add(initializer.getModId());
115119
}
116120

121+
// Init ModList
122+
ModList.create(modIds);
117123
// Send initialization events
118124

119125
dispatchRegistryEvents(mods);
120-
dispatch(mods, new FMLCommonSetupEvent(new ModContainer("minecraft"))); // TODO: One per modcontainer
121-
dispatch(mods, new FMLLoadCompleteEvent(new ModContainer("minecraft"))); // TODO: Ditto
126+
// TODO: One per modcontainer
127+
dispatch(mods, new FMLCommonSetupEvent(new ModContainer("minecraft")));
128+
dispatch(mods, new InterModEnqueueEvent(new ModContainer("minecraft")));
129+
dispatch(mods, new InterModProcessEvent(new ModContainer("minecraft")));
130+
dispatch(mods, new FMLLoadCompleteEvent(new ModContainer("minecraft")));
122131

123132
MinecraftForge.EVENT_BUS.start();
124133
}

patchwork-events-imc/build.gradle

Lines changed: 0 additions & 6 deletions
This file was deleted.

patchwork-events-imc/src/main/resources/fabric.mod.json

Lines changed: 0 additions & 32 deletions
This file was deleted.

patchwork-events-lifecycle/src/main/java/net/minecraftforge/fml/event/lifecycle/FMLFingerprintViolationEvent.java

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,15 @@
3131
* <p>TODO: Patchwork will never fire this event. All jars that pass through patchwork are modified sweepingly.</p>
3232
*/
3333
public class FMLFingerprintViolationEvent extends ModLifecycleEvent {
34-
private final boolean isDirectory;
35-
private final Set<String> fingerprints;
36-
private final File source;
37-
private final String expectedFingerprint;
34+
private boolean isDirectory;
35+
private Set<String> fingerprints;
36+
private File source;
37+
private String expectedFingerprint;
38+
39+
// For EventBus
40+
public FMLFingerprintViolationEvent() {
41+
super();
42+
}
3843

3944
public FMLFingerprintViolationEvent(boolean isDirectory, File source, ImmutableSet<String> fingerprints, String expectedFingerprint) {
4045
super(null);
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
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.event.lifecycle;
21+
22+
import net.minecraftforge.fml.ModContainer;
23+
24+
/**
25+
* This is the third of four commonly called events during mod lifecycle startup.
26+
*
27+
* Called before {@link InterModProcessEvent}
28+
* Called after {@link FMLClientSetupEvent} or {@link FMLDedicatedServerSetupEvent}
29+
*
30+
*
31+
* Enqueue {@link net.minecraftforge.fml.InterModComms} messages to other mods with this event.
32+
*
33+
* This is a parallel dispatch event.
34+
*/
35+
public class InterModEnqueueEvent extends ModLifecycleEvent {
36+
// For EventBus
37+
public InterModEnqueueEvent() {
38+
super();
39+
}
40+
41+
public InterModEnqueueEvent(final ModContainer container) {
42+
super(container);
43+
}
44+
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
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.event.lifecycle;
21+
22+
import java.util.function.Predicate;
23+
24+
import net.minecraftforge.fml.ModContainer;
25+
26+
/**
27+
* This is the fourth of four commonly called events during mod lifecycle startup.
28+
*
29+
* Called after {@link InterModEnqueueEvent}
30+
*
31+
* Retrieve {@link net.minecraftforge.fml.InterModComms} {@link net.minecraftforge.fml.InterModComms.IMCMessage} suppliers
32+
* and process them as you wish with this event.
33+
*
34+
* This is a parallel dispatch event.
35+
*
36+
* @see #getIMCStream()
37+
* @see #getIMCStream(Predicate)
38+
*/
39+
public class InterModProcessEvent extends ModLifecycleEvent {
40+
// For EventBus
41+
public InterModProcessEvent() {
42+
super();
43+
}
44+
45+
public InterModProcessEvent(final ModContainer container) {
46+
super(container);
47+
}
48+
}
Lines changed: 181 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,181 @@
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

Comments
 (0)