Skip to content

Commit 87fb999

Browse files
committed
Add default global dependency override for cloth-config2
This is a common aliased mod dependency that needs an override to work with its forge version, which uses a different modid. To provide more comfort to our users, we add a default override for this dependency.
1 parent af6c8b5 commit 87fb999

File tree

2 files changed

+32
-3
lines changed

2 files changed

+32
-3
lines changed

src/main/java/dev/su5ed/sinytra/connector/ConnectorUtil.java

+27-2
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,22 @@
55
import com.google.gson.GsonBuilder;
66
import cpw.mods.modlauncher.api.ServiceRunner;
77
import dev.su5ed.sinytra.connector.locator.EmbeddedDependencies;
8+
import net.fabricmc.loader.api.metadata.ModDependency;
9+
import net.fabricmc.loader.impl.metadata.ModDependencyImpl;
810
import net.minecraftforge.fml.loading.FMLPaths;
911

1012
import java.io.IOException;
1113
import java.io.UncheckedIOException;
1214
import java.nio.file.Files;
1315
import java.nio.file.Path;
1416
import java.util.Collection;
17+
import java.util.List;
18+
import java.util.Map;
1519
import java.util.Set;
1620
import java.util.regex.Pattern;
1721

22+
import static cpw.mods.modlauncher.api.LamdbaExceptionUtils.uncheck;
23+
1824
public final class ConnectorUtil {
1925
public static final String MIXIN_CONFIGS_ATTRIBUTE = "MixinConfigs";
2026
public static final String FABRIC_MOD_JSON = "fabric.mod.json";
@@ -99,6 +105,11 @@ public final class ConnectorUtil {
99105
"null",
100106
"_"
101107
);
108+
// Common aliased mod dependencies that don't work with forge ports, which use a different modid.
109+
// They're too annoying to override individually in each mod, so we provide this small QoL feature for the user's comfort
110+
private static final Map<String, String> GLOBAL_DEPENDENCY_OVERRIDES = Map.of(
111+
"cloth-config2", "cloth_config"
112+
);
102113

103114
private static final boolean CACHE_ENABLED;
104115

@@ -162,8 +173,22 @@ public static <V> V uncheckThrowable(UncheckedSupplier<V> supplier) {
162173
}
163174
}
164175

165-
public static String stripColor(String p_14407_) {
166-
return STRIP_COLOR_PATTERN.matcher(p_14407_).replaceAll("");
176+
public static String stripColor(String str) {
177+
return str != null ? STRIP_COLOR_PATTERN.matcher(str).replaceAll("") : null;
178+
}
179+
180+
public static List<ModDependency> applyGlobalDependencyOverrides(Collection<ModDependency> dependencies) {
181+
return dependencies.stream()
182+
.map(dependency -> {
183+
for (Map.Entry<String, String> entry : GLOBAL_DEPENDENCY_OVERRIDES.entrySet()) {
184+
String key = entry.getKey();
185+
if (dependency.getKind() == ModDependency.Kind.DEPENDS && dependency.getModId().equals(key)) {
186+
return uncheck(() -> new ModDependencyImpl(ModDependency.Kind.DEPENDS, entry.getValue(), List.of("*")));
187+
}
188+
}
189+
return dependency;
190+
})
191+
.toList();
167192
}
168193

169194
@FunctionalInterface

src/main/java/dev/su5ed/sinytra/connector/loader/ConnectorLoaderModMetadata.java

+5-1
Original file line numberDiff line numberDiff line change
@@ -28,13 +28,16 @@ public class ConnectorLoaderModMetadata implements LoaderModMetadata {
2828

2929
private final LoaderModMetadata wrapped;
3030
private final String normalModid;
31+
private List<ModDependency> dependencies;
3132

3233
public ConnectorLoaderModMetadata(LoaderModMetadata wrapped) {
3334
this.wrapped = wrapped;
3435
// Adjust modid to accomodate Java Module System requirements
3536
String replaced = this.wrapped.getId().replace('-', '_');
3637
// If modid is amongst reserved keywords, add a suffix
3738
this.normalModid = ConnectorUtil.isJavaReservedKeyword(replaced) ? replaced + NORMALIZER_SUFFIX : replaced;
39+
// Apply global dependency overrides to mods
40+
this.dependencies = ConnectorUtil.applyGlobalDependencyOverrides(this.wrapped.getDependencies());
3841
}
3942

4043
/**
@@ -113,6 +116,7 @@ public void setVersion(Version version) {
113116
@Override
114117
public void setDependencies(Collection<ModDependency> dependencies) {
115118
this.wrapped.setDependencies(dependencies);
119+
this.dependencies = ConnectorUtil.applyGlobalDependencyOverrides(this.wrapped.getDependencies());
116120
}
117121

118122
@Override
@@ -147,7 +151,7 @@ public ModEnvironment getEnvironment() {
147151

148152
@Override
149153
public Collection<ModDependency> getDependencies() {
150-
return this.wrapped.getDependencies();
154+
return this.dependencies;
151155
}
152156

153157
@Override

0 commit comments

Comments
 (0)