Skip to content

Commit

Permalink
Fix modmenu bridge loading client classes on server
Browse files Browse the repository at this point in the history
Fixes #6
  • Loading branch information
Su5eD committed Oct 14, 2023
1 parent 4ed307a commit e631fe9
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 60 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ displayTest = 'IGNORE_ALL_VERSION'
side="BOTH"
[[dependencies.connectorextras_geckolib_fabric_compat]]
modId="geckolib"
mandatory=true
mandatory=false
versionRange="[4.2,)"
ordering="NONE"
side="BOTH"
Original file line number Diff line number Diff line change
@@ -1,68 +1,12 @@
package dev.su5ed.sinytra.connectorextras.modmenu;

import com.mojang.logging.LogUtils;
import com.terraformersmc.modmenu.api.ConfigScreenFactory;
import com.terraformersmc.modmenu.api.ModMenuApi;
import net.fabricmc.loader.api.FabricLoader;
import net.minecraft.client.gui.screens.Screen;
import net.minecraftforge.client.ConfigScreenHandler;
import net.minecraftforge.client.gui.ModListScreen;
import net.minecraftforge.fml.ModList;
import net.minecraftforge.fml.common.Mod;
import org.slf4j.Logger;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Mod(ModMenuCompat.MODID)
public class ModMenuCompat {
public static final String MODID = "connectorextras_modmenu_bridge";
private static final Logger LOGGER = LogUtils.getLogger();
private static final String MODMENU_MODID = "modmenu";

private static boolean initialized = false;

public static void init() {
if (initialized) {
return;
}
initialized = true;

Map<String, ConfigScreenFactory<?>> modFactories = new HashMap<>();
List<Map<String, ConfigScreenFactory<?>>> providedFactories = new ArrayList<>();
FabricLoader.getInstance().getEntrypointContainers(MODMENU_MODID, ModMenuApi.class).forEach(container -> {
String modId = container.getProvider().getMetadata().getId();
try {
ModMenuApi entry = container.getEntrypoint();
modFactories.put(modId, entry.getModConfigScreenFactory());
providedFactories.add(entry.getProvidedConfigScreenFactories());
} catch (Throwable t) {
LOGGER.error("Failed to load ModMenuApi entrypoint for {}", modId, t);
}
});

providedFactories.forEach(map -> map.forEach(modFactories::putIfAbsent));
providedFactories.clear();

Screen dummyParent = new ModListScreen(null);
modFactories.forEach((modId, factory) -> {
// Ensure factory is active. This is required to avoid cases where the integration conditionally
// disables itself (e.g. when cloth config is absent) and returns a dummy factory.
try {
if (factory.create(dummyParent) == null) {
return;
}
} catch (Throwable t) {
// If an error occurs, it might be due to us creating the factory so early.
// Since we can't be sure about the factory's status, continue ahead
LOGGER.warn("Error testing config screen factory status for mod {}", modId, t);
}
public ModMenuCompat() {

ModList.get().getModContainerById(modId).ifPresent(fmlContainer ->
fmlContainer.registerExtensionPoint(ConfigScreenHandler.ConfigScreenFactory.class,
() -> new ConfigScreenHandler.ConfigScreenFactory((mc, screen) -> factory.create(screen))));
});
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
package dev.su5ed.sinytra.connectorextras.modmenu;

import com.mojang.logging.LogUtils;
import com.terraformersmc.modmenu.api.ConfigScreenFactory;
import com.terraformersmc.modmenu.api.ModMenuApi;
import net.fabricmc.loader.api.FabricLoader;
import net.minecraft.client.gui.screens.Screen;
import net.minecraftforge.client.ConfigScreenHandler;
import net.minecraftforge.client.gui.ModListScreen;
import net.minecraftforge.fml.ModList;
import org.slf4j.Logger;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class ModMenuCompatSetup {
private static final Logger LOGGER = LogUtils.getLogger();
private static final String MODMENU_MODID = "modmenu";

private static boolean initialized = false;

public static void init() {
if (initialized) {
return;
}
initialized = true;

Map<String, ConfigScreenFactory<?>> modFactories = new HashMap<>();
List<Map<String, ConfigScreenFactory<?>>> providedFactories = new ArrayList<>();
FabricLoader.getInstance().getEntrypointContainers(MODMENU_MODID, ModMenuApi.class).forEach(container -> {
String modId = container.getProvider().getMetadata().getId();
try {
ModMenuApi entry = container.getEntrypoint();
modFactories.put(modId, entry.getModConfigScreenFactory());
providedFactories.add(entry.getProvidedConfigScreenFactories());
} catch (Throwable t) {
LOGGER.error("Failed to load ModMenuApi entrypoint for {}", modId, t);
}
});

providedFactories.forEach(map -> map.forEach(modFactories::putIfAbsent));
providedFactories.clear();

Screen dummyParent = new ModListScreen(null);
modFactories.forEach((modId, factory) -> {
// Ensure factory is active. This is required to avoid cases where the integration conditionally
// disables itself (e.g. when cloth config is absent) and returns a dummy factory.
try {
if (factory.create(dummyParent) == null) {
return;
}
} catch (Throwable t) {
// If an error occurs, it might be due to us creating the factory so early.
// Since we can't be sure about the factory's status, continue ahead
LOGGER.warn("Error testing config screen factory status for mod {}", modId, t);
}

ModList.get().getModContainerById(modId).ifPresent(fmlContainer ->
fmlContainer.registerExtensionPoint(ConfigScreenHandler.ConfigScreenFactory.class,
() -> new ConfigScreenHandler.ConfigScreenFactory((mc, screen) -> factory.create(screen))));
});
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package dev.su5ed.sinytra.connectorextras.modmenu.mixin;

import dev.su5ed.sinytra.connectorextras.modmenu.ModMenuCompat;
import dev.su5ed.sinytra.connectorextras.modmenu.ModMenuCompatSetup;
import net.minecraftforge.client.gui.ModListScreen;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.injection.At;
Expand All @@ -12,6 +12,6 @@ public class ModListScreenMixin {

@Inject(method = "init", at = @At("HEAD"))
private void initModMenuCompat(CallbackInfo ci) {
ModMenuCompat.init();
ModMenuCompatSetup.init();
}
}

0 comments on commit e631fe9

Please sign in to comment.