Skip to content

Instantly share code, notes, and snippets.

@No-Eul
Last active October 14, 2024 15:53
Show Gist options
  • Save No-Eul/209ee9ecf88b243ec0eb9a038e174877 to your computer and use it in GitHub Desktop.
Save No-Eul/209ee9ecf88b243ec0eb9a038e174877 to your computer and use it in GitHub Desktop.
import net.fabricmc.loader.api.FabricLoader;
import net.fabricmc.loader.api.ModContainer;
import java.net.URISyntaxException;
import java.nio.file.Path;
import java.util.HashMap;
import java.util.Map;
import java.util.Optional;
public class ModFinder {
private final Map<Path, ModContainer> modsByJarPath = new HashMap<>();
public Optional<ModContainer> find(Class<?> clazz) {
try {
Path path = Path.of(clazz.getProtectionDomain().getCodeSource().getLocation().toURI());
if (path.getFileSystem().getPathMatcher("glob:**/build/classes/*/main").matches(path))
path = path.resolveSibling("../../resources/main").normalize();
return Optional.ofNullable(modsByJarPath.computeIfAbsent(path, key -> {
for (ModContainer mod : FabricLoader.getInstance().getAllMods()) {
if (mod.getOrigin().getPaths().contains(key))
return mod;
}
return null;
}));
} catch (URISyntaxException e) {
return Optional.empty();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment