|
18 | 18 | import net.minecraft.client.gui.Screen; |
19 | 19 | import net.minecraft.client.gui.options.ScreenOptions; |
20 | 20 | import net.minecraft.client.gui.options.data.OptionsPages; |
21 | | -import org.jspecify.annotations.NonNull; |
22 | 21 | import org.slf4j.Logger; |
23 | 22 | import org.slf4j.LoggerFactory; |
24 | 23 |
|
25 | 24 | import java.text.NumberFormat; |
26 | 25 | import java.util.*; |
27 | 26 | import java.util.function.Function; |
28 | 27 |
|
29 | | -@SuppressWarnings("java:S2386") |
30 | 28 | public class ModMenu implements ModInitializer { |
31 | | - public static final String MOD_ID = "modmenu"; |
32 | | - public static final Logger LOGGER = LoggerFactory.getLogger(MOD_ID); |
33 | | - public static final Gson GSON = new GsonBuilder().setFieldNamingPolicy(FieldNamingPolicy.LOWER_CASE_WITH_UNDERSCORES).setPrettyPrinting().create(); |
| 29 | + public static final String MOD_ID = "modmenu"; |
| 30 | + public static final Logger LOGGER = LoggerFactory.getLogger(MOD_ID); |
| 31 | + public static final Gson GSON = new GsonBuilder().setFieldNamingPolicy(FieldNamingPolicy.LOWER_CASE_WITH_UNDERSCORES).setPrettyPrinting().create(); |
34 | 32 |
|
35 | | - private static final Map<String, Runnable> LEGACY_CONFIG_SCREEN_TASKS = new HashMap<>(); |
36 | | - public static final List<String> LIBRARY_MODS = new ArrayList<>(); |
37 | | - public static final Set<String> CLIENTSIDE_MODS = new HashSet<>(); |
| 33 | + private static final Map<String, Runnable> LEGACY_CONFIG_SCREEN_TASKS = new HashMap<>(); |
| 34 | + public static final List<String> LIBRARY_MODS = new ArrayList<>(); |
| 35 | + public static final Set<String> CLIENTSIDE_MODS = new HashSet<>(); |
38 | 36 | public static final Set<String> DEPRECATED_MODS = new HashSet<>(); |
39 | | - public static final Set<String> PATCHWORK_FORGE_MODS = new HashSet<>(); |
| 37 | + public static final Set<String> PATCHWORK_FORGE_MODS = new HashSet<>(); |
40 | 38 | public static final Map<String, Map<String, Map.Entry<Integer, Integer>>> CUSTOM_BADGE_MODS = new HashMap<>(); |
41 | | - public static final LinkedListMultimap<ModContainer, ModContainer> PARENT_MAP = LinkedListMultimap.create(); |
42 | | - private static ImmutableMap<@NonNull String, @NonNull Function<Screen, ? extends Screen>> configScreenFactories = ImmutableMap.of(); |
43 | | - |
44 | | - public static boolean hasConfigScreenFactory(String modid) { |
45 | | - return configScreenFactories.containsKey(modid); |
46 | | - } |
47 | | - |
48 | | - public static Screen getConfigScreen(String modid, Screen menuScreen) { |
49 | | - Function<Screen, ? extends Screen> factory = configScreenFactories.get(modid); |
50 | | - return factory != null ? factory.apply(menuScreen) : null; |
51 | | - } |
52 | | - |
53 | | - public static void openConfigScreen(String modid) { |
54 | | - Runnable opener = LEGACY_CONFIG_SCREEN_TASKS.get(modid); |
55 | | - if (opener != null) opener.run(); |
56 | | - } |
57 | | - |
58 | | - @SuppressWarnings("unused") |
59 | | - public static void addLegacyConfigScreenTask(String modid, Runnable task) { |
60 | | - LEGACY_CONFIG_SCREEN_TASKS.putIfAbsent(modid, task); |
61 | | - } |
62 | | - |
63 | | - public static boolean hasLegacyConfigScreenTask(String modid) { |
64 | | - return LEGACY_CONFIG_SCREEN_TASKS.containsKey(modid); |
65 | | - } |
66 | | - |
67 | | - public static void addLibraryMod(String modid) { |
68 | | - if (LIBRARY_MODS.contains(modid)) return; |
69 | | - |
70 | | - LIBRARY_MODS.add(modid); |
71 | | - } |
72 | | - |
73 | | - @SuppressWarnings("RedundantCollectionOperation") |
74 | | - @Override |
75 | | - public void onInitialize() { |
76 | | - ModMenuConfigManager.initializeConfig(); |
77 | | - ImmutableMap.Builder<@NonNull String, @NonNull Function<Screen, ? extends Screen>> factories = ImmutableMap.builder(); |
78 | | - FabricLoader.getInstance().getEntrypointContainers(MOD_ID, ModMenuApi.class).forEach(entrypoint -> { |
79 | | - ModMenuApi api = entrypoint.getEntrypoint(); |
80 | | - ModContainer mod = entrypoint.getProvider(); |
| 39 | + public static final LinkedListMultimap<ModContainer, ModContainer> PARENT_MAP = LinkedListMultimap.create(); |
| 40 | + private static ImmutableMap<String, Function<Screen, ? extends Screen>> configScreenFactories = ImmutableMap.of(); |
| 41 | + |
| 42 | + public static boolean hasConfigScreenFactory(String modid) { |
| 43 | + return configScreenFactories.containsKey(modid); |
| 44 | + } |
| 45 | + |
| 46 | + public static Screen getConfigScreen(String modid, Screen menuScreen) { |
| 47 | + Function<Screen, ? extends Screen> factory = configScreenFactories.get(modid); |
| 48 | + return factory != null ? factory.apply(menuScreen) : null; |
| 49 | + } |
| 50 | + |
| 51 | + public static void openConfigScreen(String modid) { |
| 52 | + Runnable opener = LEGACY_CONFIG_SCREEN_TASKS.get(modid); |
| 53 | + if (opener != null) opener.run(); |
| 54 | + } |
| 55 | + |
| 56 | + public static void addLegacyConfigScreenTask(String modid, Runnable task) { |
| 57 | + LEGACY_CONFIG_SCREEN_TASKS.putIfAbsent(modid, task); |
| 58 | + } |
| 59 | + |
| 60 | + public static boolean hasLegacyConfigScreenTask(String modid) { |
| 61 | + return LEGACY_CONFIG_SCREEN_TASKS.containsKey(modid); |
| 62 | + } |
| 63 | + |
| 64 | + public static void addLibraryMod(String modid) { |
| 65 | + if (LIBRARY_MODS.contains(modid)) return; |
| 66 | + |
| 67 | + LIBRARY_MODS.add(modid); |
| 68 | + } |
| 69 | + |
| 70 | + @SuppressWarnings("RedundantCollectionOperation") |
| 71 | + @Override |
| 72 | + public void onInitialize() { |
| 73 | + ModMenuConfigManager.initializeConfig(); |
| 74 | + ImmutableMap.Builder<String, Function<Screen, ? extends Screen>> factories = ImmutableMap.builder(); |
| 75 | + FabricLoader.getInstance().getEntrypointContainers("modmenu", ModMenuApi.class).forEach(entrypoint -> { |
| 76 | + ModMenuApi api = entrypoint.getEntrypoint(); |
| 77 | + ModContainer mod = entrypoint.getProvider(); |
81 | 78 | try { |
82 | 79 | api.getClass().getDeclaredMethod("getConfigScreenFactory"); // Make sure the method is implemented |
83 | 80 | factories.put(mod.getMetadata().getId(), api.getConfigScreenFactory()); |
84 | | - } catch (NoSuchMethodException ignored) { /* noop */} |
| 81 | + } catch (NoSuchMethodException ignored) {} |
85 | 82 | api.attachCustomBadges((name, outlineColor, fillColor) -> { |
86 | 83 | Map<String, Map.Entry<Integer, Integer>> map = new HashMap<>(); |
87 | 84 | map.put(name, new AbstractMap.SimpleEntry<>(outlineColor, fillColor)); |
88 | 85 | CUSTOM_BADGE_MODS.put(mod.getMetadata().getId(), map); |
89 | 86 | }); |
90 | 87 | }); |
91 | | - factories.put("minecraft", (screenBase -> new ScreenOptions(screenBase, OptionsPages.GENERAL))); |
92 | | - configScreenFactories = factories.build(); |
93 | | - Collection<ModContainer> mods = FabricLoader.getInstance().getAllMods(); |
94 | | - HardcodedUtil.initializeHardcodings(); |
95 | | - for (ModContainer mod : mods) { |
96 | | - ModMetadata metadata = mod.getMetadata(); |
97 | | - String id = metadata.getId(); |
98 | | - // API badges |
99 | | - if (metadata.containsCustomValue("modmenu:api") && metadata.getCustomValue("modmenu:api").getAsBoolean()) { |
100 | | - addLibraryMod(id); |
101 | | - } |
102 | | - |
103 | | - // Client side badges |
104 | | - if (metadata.getEnvironment().equals(ModEnvironment.CLIENT)) { |
105 | | - CLIENTSIDE_MODS.add(id); |
106 | | - } |
107 | | - if (metadata.containsCustomValue("modmenu:clientsideOnly") && metadata.getCustomValue("modmenu:clientsideOnly").getAsBoolean()) { |
108 | | - LOGGER.warn("Found mod with id \"{}\" using deprecated value \"modmenu:clientsideOnly\"!", metadata.getId()); |
109 | | - if (!(CLIENTSIDE_MODS.contains(id))) CLIENTSIDE_MODS.add(id); |
110 | | - } |
111 | | - |
112 | | - // Deprecated badges |
| 88 | + factories.put("minecraft", (screenBase -> new ScreenOptions(screenBase, OptionsPages.GENERAL))); |
| 89 | + configScreenFactories = factories.build(); |
| 90 | + Collection<ModContainer> mods = FabricLoader.getInstance().getAllMods(); |
| 91 | + HardcodedUtil.initializeHardcodings(); |
| 92 | + for (ModContainer mod : mods) { |
| 93 | + ModMetadata metadata = mod.getMetadata(); |
| 94 | + String id = metadata.getId(); |
| 95 | + // API badges |
| 96 | + if (metadata.containsCustomValue("modmenu:api") && metadata.getCustomValue("modmenu:api").getAsBoolean()) { |
| 97 | + addLibraryMod(id); |
| 98 | + } |
| 99 | + |
| 100 | + // Client side badges |
| 101 | + if (metadata.getEnvironment().equals(ModEnvironment.CLIENT)) { |
| 102 | + CLIENTSIDE_MODS.add(id); |
| 103 | + } |
| 104 | + if (metadata.containsCustomValue("modmenu:clientsideOnly") && metadata.getCustomValue("modmenu:clientsideOnly").getAsBoolean()) { |
| 105 | + LOGGER.warn("Found mod with id \"{}\" using deprecated value \"modmenu:clientsideOnly\"!", metadata.getId()); |
| 106 | + if (!(CLIENTSIDE_MODS.contains(id))) CLIENTSIDE_MODS.add(id); |
| 107 | + } |
| 108 | + |
| 109 | + // Deprecated badges |
113 | 110 | if (metadata.containsCustomValue("modmenu:deprecated") && metadata.getCustomValue("modmenu:deprecated").getAsBoolean()) { |
114 | 111 | DEPRECATED_MODS.add(id); |
115 | 112 | } |
116 | 113 |
|
117 | | - // Patchwork (unused) |
118 | | - if (metadata.containsCustomValue("patchwork:source") && metadata.getCustomValue("patchwork:source").getAsObject() != null) { |
119 | | - CustomValue.CvObject object = metadata.getCustomValue("patchwork:source").getAsObject(); |
120 | | - if ("forge".equals(object.get("loader").getAsString())) { |
121 | | - PATCHWORK_FORGE_MODS.add(id); |
122 | | - } |
123 | | - } |
124 | | - |
125 | | - // Parent mods |
126 | | - if (metadata.containsCustomValue("modmenu:parent")) { |
127 | | - String parentId = metadata.getCustomValue("modmenu:parent").getAsString(); |
128 | | - if (parentId != null) { |
129 | | - Optional<ModContainer> parent = FabricLoader.getInstance().getModContainer(parentId); |
130 | | - parent.ifPresent(modContainer -> PARENT_MAP.put(modContainer, mod)); |
131 | | - } |
132 | | - } else { |
133 | | - HardcodedUtil.hardcodeModuleMetadata(mod, metadata, id); |
134 | | - } |
135 | | - } |
136 | | - } |
137 | | - |
138 | | - public static String getFormattedModCount() { |
139 | | - return NumberFormat.getInstance().format(FabricLoader.getInstance().getAllMods().size()); |
140 | | - } |
| 114 | + // Patchwork (unused) |
| 115 | + if (metadata.containsCustomValue("patchwork:source") && metadata.getCustomValue("patchwork:source").getAsObject() != null) { |
| 116 | + CustomValue.CvObject object = metadata.getCustomValue("patchwork:source").getAsObject(); |
| 117 | + if ("forge".equals(object.get("loader").getAsString())) { |
| 118 | + PATCHWORK_FORGE_MODS.add(id); |
| 119 | + } |
| 120 | + } |
| 121 | + |
| 122 | + // Parent mods |
| 123 | + if (metadata.containsCustomValue("modmenu:parent")) { |
| 124 | + String parentId = metadata.getCustomValue("modmenu:parent").getAsString(); |
| 125 | + if (parentId != null) { |
| 126 | + Optional<ModContainer> parent = FabricLoader.getInstance().getModContainer(parentId); |
| 127 | + parent.ifPresent(modContainer -> PARENT_MAP.put(modContainer, mod)); |
| 128 | + } |
| 129 | + } else { |
| 130 | + HardcodedUtil.hardcodeModuleMetadata(mod, metadata, id); |
| 131 | + } |
| 132 | + } |
| 133 | + } |
| 134 | + |
| 135 | + public static String getFormattedModCount() { |
| 136 | + return NumberFormat.getInstance().format(FabricLoader.getInstance().getAllMods().size()); |
| 137 | + } |
141 | 138 | } |
0 commit comments