|
1 | 1 | package se.icus.mag.modmenusettings;
|
2 | 2 |
|
3 |
| -import com.google.common.collect.ImmutableMap; |
4 |
| -import com.terraformersmc.modmenu.api.ConfigScreenFactory; |
5 | 3 | import com.terraformersmc.modmenu.api.ModMenuApi;
|
| 4 | +import com.terraformersmc.modmenu.util.ModMenuApiMarker; |
| 5 | +import io.github.prospector.modmenu.api.ConfigScreenFactory; |
6 | 6 | import net.fabricmc.loader.api.EntrypointException;
|
7 | 7 | import net.fabricmc.loader.api.FabricLoader;
|
8 | 8 | import net.fabricmc.loader.api.entrypoint.EntrypointContainer;
|
9 | 9 | import net.fabricmc.loader.api.metadata.ModMetadata;
|
10 | 10 | import net.minecraft.client.gui.screen.Screen;
|
11 | 11 | import org.apache.logging.log4j.Level;
|
12 |
| -import org.apache.logging.log4j.LogManager; |
13 |
| -import org.apache.logging.log4j.Logger; |
14 | 12 |
|
15 | 13 | import java.util.*;
|
16 |
| -import java.util.function.Supplier; |
| 14 | +import java.util.stream.Collectors; |
17 | 15 |
|
18 | 16 | public class ModRegistry {
|
19 |
| - private static List<Supplier<Map<String, ConfigScreenFactory<?>>>> dynamicScreenFactories = new ArrayList<>(); |
20 |
| - private static ImmutableMap<String, ConfigScreenFactory<?>> configScreenFactories = ImmutableMap.of(); |
21 |
| - public static final Logger LOGGER = LogManager.getLogger("Mod Menu"); |
22 |
| - public static final Map<String, String> CONFIGABLE_MODS = new HashMap<>(); |
| 17 | + private static final Map<String, String> CONFIGABLE_MODS_NAMES = new HashMap<>(); |
| 18 | + private static final Map<String, ConfigScreenFactory<?>> factories = new HashMap<>(); |
| 19 | + private static final Map<String, ConfigScreenFactory<?>> overridingFactories = new HashMap<>(); |
23 | 20 |
|
24 | 21 | public static void registerMods() {
|
25 |
| - Map<String, ConfigScreenFactory<?>> factories = new HashMap<>(); |
| 22 | + List<EntrypointContainer<ModMenuApiMarker>> modList = FabricLoader.getInstance().getEntrypointContainers("modmenu", ModMenuApiMarker.class); |
26 | 23 |
|
27 |
| - /* Current API */ |
28 |
| - List<EntrypointContainer<ModMenuApi>> modList = FabricLoader.getInstance().getEntrypointContainers("modmenu", ModMenuApi.class); |
29 |
| - for (EntrypointContainer<ModMenuApi> entryPoint : modList) { |
| 24 | + for (EntrypointContainer<ModMenuApiMarker> entryPoint : modList) { |
30 | 25 | ModMetadata metadata = entryPoint.getProvider().getMetadata();
|
31 | 26 | String modId = metadata.getId();
|
| 27 | + ModMenuSettings.LOGGER.log(Level.INFO,"found mod1: " + modId); |
| 28 | + |
32 | 29 | try {
|
33 |
| - String modName = metadata.getName(); |
34 |
| - LOGGER.log(Level.INFO,"found mod1: " + modId); |
35 |
| - ModMenuApi marker = entryPoint.getEntrypoint(); |
| 30 | + ModMenuApiMarker marker = entryPoint.getEntrypoint(); |
| 31 | + if (marker instanceof ModMenuApi) { |
| 32 | + ModMenuApi marker2 = (ModMenuApi) marker; |
36 | 33 |
|
37 |
| - LOGGER.log(Level.INFO, "is new mod1: " + modId); |
38 |
| - CONFIGABLE_MODS.put(modId, modName); |
| 34 | + factories.put(modId, marker2.getModConfigScreenFactory()); |
| 35 | + overridingFactories.putAll(marker2.getProvidedConfigScreenFactories()); |
| 36 | + } else if (marker instanceof io.github.prospector.modmenu.api.ModMenuApi) { |
| 37 | + io.github.prospector.modmenu.api.ModMenuApi marker2 = (io.github.prospector.modmenu.api.ModMenuApi) marker; |
39 | 38 |
|
40 |
| - factories.put(modId, marker.getModConfigScreenFactory()); |
41 |
| - dynamicScreenFactories.add(marker::getProvidedConfigScreenFactories); |
| 39 | + factories.put(modId, marker2.getModConfigScreenFactory()); |
| 40 | + overridingFactories.putAll(marker2.getProvidedConfigScreenFactories()); |
| 41 | + } else { |
| 42 | + ModMenuSettings.LOGGER.warn("class problem with " + modId); |
| 43 | + continue; |
| 44 | + } |
| 45 | + CONFIGABLE_MODS_NAMES.put(modId, metadata.getName()); |
42 | 46 | } catch (EntrypointException e) {
|
43 | 47 | // Ignore incompatible mods, they are either broken or implement the old API
|
44 |
| - LOGGER.warn("problem with " + modId + e); |
| 48 | + ModMenuSettings.LOGGER.warn("problem with " + modId + e); |
45 | 49 | }
|
46 | 50 | }
|
| 51 | + } |
47 | 52 |
|
48 |
| - /* Legacy API */ |
49 |
| - List<EntrypointContainer<io.github.prospector.modmenu.api.ModMenuApi>> mod2List = FabricLoader.getInstance().getEntrypointContainers("modmenu", io.github.prospector.modmenu.api.ModMenuApi.class); |
| 53 | + public static List<String> getAllModIds() { |
| 54 | + Comparator<String> sorter = Comparator.comparing(modId -> modId.toLowerCase(Locale.ROOT)); |
50 | 55 |
|
51 |
| - for (EntrypointContainer<io.github.prospector.modmenu.api.ModMenuApi> entryPoint : mod2List) { |
52 |
| - ModMetadata metadata = entryPoint.getProvider().getMetadata(); |
53 |
| - String modId = metadata.getId(); |
54 |
| - try { |
55 |
| - String modName = metadata.getName(); |
56 |
| - LOGGER.log(Level.INFO,"found mod2: " + modId); |
57 |
| - io.github.prospector.modmenu.api.ModMenuApi marker = entryPoint.getEntrypoint(); |
58 |
| - LOGGER.log(Level.INFO,"is old mod2: " + modId); |
59 |
| - CONFIGABLE_MODS.put(modId, modName); |
60 |
| - |
61 |
| - factories.put(modId, screen -> marker.getModConfigScreenFactory().create(screen)); |
62 |
| - marker.getProvidedConfigScreenFactories().forEach((id, legacyFactory) -> factories.put(id, legacyFactory::create)); |
63 |
| - } catch (EntrypointException e) { |
64 |
| - // Ignore incompatible mods, they are either broken or implement the new API |
65 |
| - LOGGER.warn("problem with " + modId + e); |
66 |
| - } |
67 |
| - } |
| 56 | + return CONFIGABLE_MODS_NAMES.keySet().stream().sorted(sorter) |
| 57 | + .filter(modId -> !modId.equals("minecraft")).collect(Collectors.toList()); |
| 58 | + } |
68 | 59 |
|
69 |
| - configScreenFactories = ImmutableMap.copyOf(factories); |
| 60 | + public static String getModName(String modId) { |
| 61 | + return CONFIGABLE_MODS_NAMES.get(modId); |
70 | 62 | }
|
71 | 63 |
|
72 | 64 | public static Screen getConfigScreen(String modid, Screen menuScreen) {
|
73 |
| - ConfigScreenFactory<?> factory = configScreenFactories.get(modid); |
| 65 | + ConfigScreenFactory<?> factory = factories.getOrDefault(modid, overridingFactories.get(modid)); |
74 | 66 | if (factory != null) {
|
75 | 67 | return factory.create(menuScreen);
|
76 | 68 | }
|
77 |
| - for (Supplier<Map<String, ConfigScreenFactory<?>>> dynamicFactoriesSupplier : dynamicScreenFactories) { |
78 |
| - factory = dynamicFactoriesSupplier.get().get(modid); |
79 |
| - if (factory != null) { |
80 |
| - return factory.create(menuScreen); |
81 |
| - } |
82 |
| - } |
| 69 | + |
83 | 70 | return null;
|
84 | 71 | }
|
85 | 72 | }
|
0 commit comments