简化 DefaultLauncher#getEnvVars 与 McbbsModpackExportTask#execute#5271
简化 DefaultLauncher#getEnvVars 与 McbbsModpackExportTask#execute#5271CiiLu wants to merge 4 commits intoHMCL-dev:mainfrom
DefaultLauncher#getEnvVars 与 McbbsModpackExportTask#execute#5271Conversation
There was a problem hiding this comment.
Pull request overview
This PR attempts to simplify two methods by replacing explicit conditional checks with loop-based iteration. It adds a new getLibraries() helper method to LibraryAnalyzer that returns all detected library types.
Changes:
- Added
getLibraries()method toLibraryAnalyzerthat returns all library types present in the analyzed version - Refactored
McbbsModpackExportTask#executeto use a loop overgetLibraries()instead of explicitifPresent()calls for each library type - Refactored
DefaultLauncher#getEnvVarsto use a loop overgetModLoaders()instead of explicit checks for each mod loader type
Reviewed changes
Copilot reviewed 3 out of 3 changed files in this pull request and generated 2 comments.
| File | Description |
|---|---|
| HMCLCore/src/main/java/org/jackhuang/hmcl/download/LibraryAnalyzer.java | Adds new getLibraries() method to return all detected library types |
| HMCLCore/src/main/java/org/jackhuang/hmcl/mod/mcbbs/McbbsModpackExportTask.java | Replaces explicit library type checks with loop over getLibraries() |
| HMCLCore/src/main/java/org/jackhuang/hmcl/launch/DefaultLauncher.java | Replaces explicit mod loader checks with loop over getModLoaders(), adds ModLoaderType import |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| for (LibraryAnalyzer.LibraryType type : analyzer.getLibraries()) { | ||
| if (type == MINECRAFT) addons.add(new McbbsModpackManifest.Addon(MINECRAFT.getPatchId(), gameVersion)); | ||
| else addons.add(new McbbsModpackManifest.Addon(type.getPatchId(), analyzer.getVersion(type).orElseThrow())); | ||
| } |
There was a problem hiding this comment.
This refactoring introduces multiple critical issues:
-
Potential runtime exceptions: Using
orElseThrow()can cause exceptions even whenhas(type)is true, becausegetVersion()can return an empty Optional if the version string is null in the internal map. -
Changed behavior - includes unintended library types: The old code explicitly handled specific library types (MINECRAFT, FORGE, CLEANROOM, NEO_FORGE, LITELOADER, OPTIFINE, FABRIC, QUILT, LEGACY_FABRIC). The new code includes ALL library types, which means it will also add FABRIC_API, QUILT_API, LEGACY_FABRIC_API, and BOOTSTRAP_LAUNCHER to the manifest. These were not included before and could break the modpack format.
To fix these issues:
- Replace
orElseThrow()with safe handling like filtering out libraries without versions first, or using a default value - Filter library types to only include the relevant ones that should be in the manifest, similar to the explicit list in the old code
…er.java Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
No description provided.