Last active
October 14, 2024 15:53
-
-
Save No-Eul/209ee9ecf88b243ec0eb9a038e174877 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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