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

Commit 3e8f584

Browse files
authored
Add more missing function to ModList, FMLModContainer and ModContainer (#87)
FMLModContainer and ModContainer: [API] Add getMod and acceptEvent methods [Internal] setParent and getParent: associated Fabric ModContainer getter and setters ModList: Add most helper functions present in Forge's ModList, these are frequenty used by mods ModLoader(class): A class may used by mods to post event on the FML event bus
1 parent 05ceb6f commit 3e8f584

File tree

5 files changed

+138
-1
lines changed

5 files changed

+138
-1
lines changed

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
import net.minecraftforge.eventbus.api.Event;
3333
import net.minecraftforge.fml.DistExecutor;
3434
import net.minecraftforge.fml.ModContainer;
35+
import net.minecraftforge.fml.ModList;
3536
import net.minecraftforge.fml.ModLoadingContext;
3637
import net.minecraftforge.fml.event.lifecycle.FMLClientSetupEvent;
3738
import net.minecraftforge.fml.event.lifecycle.FMLCommonSetupEvent;
@@ -114,6 +115,7 @@ public void onInitialize() {
114115
throw error;
115116
}
116117

118+
ModList.get().setLoadedMods(mods.values());
117119
// Send initialization events
118120

119121
RegistryEventDispatcher.dispatchRegistryEvents(event -> dispatch(mods, event));

patchwork-fml/src/main/java/net/minecraftforge/fml/ModContainer.java

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,15 @@
2121

2222
import java.util.EnumMap;
2323

24+
import net.minecraftforge.eventbus.api.Event;
2425
import net.minecraftforge.fml.config.ModConfig;
2526

2627
// TODO: Stub
27-
public class ModContainer {
28+
public abstract class ModContainer {
2829
protected final String modId;
2930
protected final String namespace;
3031
protected final EnumMap<ModConfig.Type, ModConfig> configs;
32+
private net.fabricmc.loader.api.ModContainer fabricModContainer;
3133

3234
public ModContainer(String modId) {
3335
this.modId = modId;
@@ -47,4 +49,17 @@ public final String getNamespace() {
4749
public void addConfig(final ModConfig modConfig) {
4850
configs.put(modConfig.getType(), modConfig);
4951
}
52+
53+
public final void setParent(net.fabricmc.loader.api.ModContainer fabricModContainer) {
54+
this.fabricModContainer = fabricModContainer;
55+
}
56+
57+
public final net.fabricmc.loader.api.ModContainer getParent() {
58+
return this.fabricModContainer;
59+
}
60+
61+
public abstract Object getMod();
62+
63+
protected void acceptEvent(Event e) {
64+
}
5065
}

patchwork-fml/src/main/java/net/minecraftforge/fml/ModList.java

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,16 +19,25 @@
1919

2020
package net.minecraftforge.fml;
2121

22+
import java.util.Collection;
2223
import java.util.Collections;
2324
import java.util.HashMap;
2425
import java.util.List;
2526
import java.util.Map;
27+
import java.util.Optional;
28+
import java.util.function.BiConsumer;
29+
import java.util.function.Consumer;
30+
import java.util.function.Function;
2631
import java.util.stream.Collectors;
32+
import java.util.stream.Stream;
2733

34+
import net.minecraftforge.fml.javafmlmod.FMLModContainer;
35+
import net.minecraftforge.fml.loading.moddiscovery.ModFile;
2836
import net.minecraftforge.fml.loading.moddiscovery.ModFileInfo;
2937
import net.minecraftforge.forgespi.language.ModFileScanData;
3038
import org.apache.logging.log4j.LogManager;
3139
import org.apache.logging.log4j.Logger;
40+
import com.google.common.collect.ImmutableList;
3241

3342
import net.fabricmc.loader.api.FabricLoader;
3443
import net.fabricmc.loader.api.ModContainer;
@@ -42,6 +51,7 @@ public class ModList {
4251
private static ModList INSTANCE = new ModList();
4352

4453
private Map<ModContainer, ModFileInfo> modFileInfoMap = new HashMap<>();
54+
private Map<ModContainer, net.minecraftforge.fml.ModContainer> fabricForgeModMap = new HashMap<>();
4555
private List<ModFileScanData> allScanDataCache;
4656

4757
public static ModList get() {
@@ -53,6 +63,10 @@ public boolean isLoaded(String modId) {
5363
return FabricLoader.getInstance().isModLoaded(modId);
5464
}
5565

66+
public List<ModFileInfo> getModFiles() {
67+
return ImmutableList.copyOf(modFileInfoMap.values());
68+
}
69+
5670
public ModFileInfo getModFileById(String modId) {
5771
ModContainer modContainer = FabricLoader.getInstance().getModContainer(modId).orElse(null);
5872

@@ -63,6 +77,39 @@ public ModFileInfo getModFileById(String modId) {
6377
return getModFileByContainer(modContainer);
6478
}
6579

80+
public void setLoadedMods(final Collection<FMLModContainer> collection) {
81+
fabricForgeModMap.clear();
82+
83+
for (net.minecraftforge.fml.ModContainer fmlContainer: collection) {
84+
String modId = fmlContainer.modId;
85+
ModContainer fabricModContainer = FabricLoader.getInstance().getModContainer(modId).orElse(null);
86+
87+
if (fabricModContainer != null) {
88+
fmlContainer.setParent(fabricModContainer);
89+
fabricForgeModMap.put(fabricModContainer, fmlContainer);
90+
} else {
91+
throw new RuntimeException("Cannot find the Fabric ModContainer for Forge mod: " + modId);
92+
}
93+
}
94+
}
95+
96+
public net.minecraftforge.fml.ModContainer getModContainer(ModContainer fabricModContainer) {
97+
return fabricForgeModMap.get(fabricModContainer);
98+
}
99+
100+
@SuppressWarnings("unchecked")
101+
public <T> Optional<T> getModObjectById(String modId) {
102+
return getModContainerById(modId).map(net.minecraftforge.fml.ModContainer::getMod).map(o -> (T) o);
103+
}
104+
105+
public Optional<? extends net.minecraftforge.fml.ModContainer> getModContainerById(String modId) {
106+
return Optional.ofNullable(this.fabricForgeModMap.get(modId));
107+
}
108+
109+
public Optional<? extends net.minecraftforge.fml.ModContainer> getModContainerByObject(Object obj) {
110+
return this.fabricForgeModMap.values().stream().filter(mc -> mc.getMod() == obj).findFirst();
111+
}
112+
66113
private ModFileInfo getModFileByContainer(ModContainer modContainer) {
67114
return modFileInfoMap.computeIfAbsent(modContainer, this::createModFileInfo);
68115
}
@@ -107,6 +154,10 @@ private ModFileInfo createModFileInfo(ModContainer modContainer) {
107154
return new ModFileInfo();
108155
}
109156

157+
public int size() {
158+
return modFileInfoMap.size();
159+
}
160+
110161
public List<ModFileScanData> getAllScanData() {
111162
if (allScanDataCache == null) {
112163
// Even though ModFileScanData lacks an implementation of Object#equals, the default implementation tests
@@ -124,4 +175,20 @@ public List<ModFileScanData> getAllScanData() {
124175

125176
return allScanDataCache;
126177
}
178+
179+
public void forEachModFile(Consumer<ModFile> fileConsumer) {
180+
modFileInfoMap.values().stream().map(ModFileInfo::getFile).forEach(fileConsumer);
181+
}
182+
183+
public <T> Stream<T> applyForEachModFile(Function<ModFile, T> function) {
184+
return modFileInfoMap.values().stream().map(ModFileInfo::getFile).map(function);
185+
}
186+
187+
public void forEachModContainer(BiConsumer<String, net.minecraftforge.fml.ModContainer> modContainerConsumer) {
188+
fabricForgeModMap.forEach((fabric, fml) -> modContainerConsumer.accept(fabric.getMetadata().getId(), fml));
189+
}
190+
191+
public <T> Stream<T> applyForEachModContainer(Function<net.minecraftforge.fml.ModContainer, T> function) {
192+
return fabricForgeModMap.values().stream().map(function);
193+
}
127194
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/*
2+
* Minecraft Forge, Patchwork Project
3+
* Copyright (c) 2016-2020, 2019-2020
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 net.minecraftforge.eventbus.api.Event;
23+
24+
public class ModLoader {
25+
private static ModLoader INSTANCE;
26+
27+
private ModLoader() {
28+
INSTANCE = this;
29+
}
30+
31+
public static ModLoader get() {
32+
return INSTANCE == null ? INSTANCE = new ModLoader() : INSTANCE;
33+
}
34+
35+
public void postEvent(Event e) {
36+
ModList.get().forEachModContainer((id, mc) -> mc.acceptEvent(e));
37+
}
38+
}

patchwork-fml/src/main/java/net/minecraftforge/fml/javafmlmod/FMLModContainer.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727

2828
public class FMLModContainer extends ModContainer {
2929
private final IEventBus eventBus;
30+
private Object instance;
3031

3132
public FMLModContainer(String id) {
3233
super(id);
@@ -47,4 +48,18 @@ public IEventBus getEventBus() {
4748
private void onEventFailed(IEventBus iEventBus, Event event, IEventListener[] listeners, int i, Throwable throwable) {
4849
// TODO
4950
}
51+
52+
@Override
53+
protected void acceptEvent(final Event e) {
54+
this.eventBus.post(e);
55+
}
56+
57+
@Override
58+
public Object getMod() {
59+
return this.instance;
60+
}
61+
62+
public void setMod(Object instance) {
63+
this.instance = instance;
64+
}
5065
}

0 commit comments

Comments
 (0)