2626import org .jackhuang .hmcl .ui .Controllers ;
2727import org .jackhuang .hmcl .ui .FXUtils ;
2828import org .jackhuang .hmcl .ui .ListPageBase ;
29+ import org .jackhuang .hmcl .util .StringUtils ;
2930import org .jackhuang .hmcl .util .io .FileUtils ;
3031import org .jackhuang .hmcl .util .javafx .MappedObservableList ;
32+ import org .jetbrains .annotations .NotNull ;
3133
3234import java .io .IOException ;
3335import java .nio .file .Path ;
3436import java .util .List ;
37+ import java .util .Locale ;
3538import java .util .Objects ;
39+ import java .util .function .Predicate ;
40+ import java .util .regex .Pattern ;
3641
37- import static org .jackhuang .hmcl .util .logging .Logger .LOG ;
3842import static org .jackhuang .hmcl .util .i18n .I18n .i18n ;
43+ import static org .jackhuang .hmcl .util .logging .Logger .LOG ;
3944
4045public final class DatapackListPage extends ListPageBase <DatapackListPageSkin .DatapackInfoObject > {
4146 private final Path worldDir ;
@@ -47,17 +52,15 @@ public DatapackListPage(WorldManagePage worldManagePage) {
4752 datapack = new Datapack (worldDir .resolve ("datapacks" ));
4853 datapack .loadFromDir ();
4954
50- setItems (MappedObservableList .create (datapack .getInfo (), DatapackListPageSkin .DatapackInfoObject ::new ));
55+ setItems (MappedObservableList .create (datapack .getPacks (), DatapackListPageSkin .DatapackInfoObject ::new ));
5156
5257 FXUtils .applyDragListener (this , it -> Objects .equals ("zip" , FileUtils .getExtension (it )),
5358 mods -> mods .forEach (this ::installSingleDatapack ), this ::refresh );
5459 }
5560
5661 private void installSingleDatapack (Path datapack ) {
5762 try {
58- Datapack zip = new Datapack (datapack );
59- zip .loadFromZip ();
60- zip .installTo (worldDir );
63+ this .datapack .installPack (datapack );
6164 } catch (IOException | IllegalArgumentException e ) {
6265 LOG .warning ("Unable to parse datapack file " + datapack , e );
6366 }
@@ -83,8 +86,9 @@ public void add() {
8386 chooser .getExtensionFilters ().setAll (new FileChooser .ExtensionFilter (i18n ("datapack.extension" ), "*.zip" ));
8487 List <Path > res = FileUtils .toPaths (chooser .showOpenMultipleDialog (Controllers .getStage ()));
8588
86- if (res != null )
89+ if (res != null ) {
8790 res .forEach (this ::installSingleDatapack );
91+ }
8892
8993 datapack .loadFromDir ();
9094 }
@@ -97,20 +101,49 @@ void removeSelected(ObservableList<DatapackListPageSkin.DatapackInfoObject> sele
97101 datapack .deletePack (pack );
98102 } catch (IOException e ) {
99103 // Fail to remove mods if the game is running or the datapack is absent.
100- LOG .warning ("Failed to delete datapack " + pack );
104+ LOG .warning ("Failed to delete datapack \" " + pack . getId () + " \" " , e );
101105 }
102106 });
103107 }
104108
105109 void enableSelected (ObservableList <DatapackListPageSkin .DatapackInfoObject > selectedItems ) {
106110 selectedItems .stream ()
107111 .map (DatapackListPageSkin .DatapackInfoObject ::getPackInfo )
108- .forEach (info -> info .setActive (true ));
112+ .forEach (pack -> pack .setActive (true ));
109113 }
110114
111115 void disableSelected (ObservableList <DatapackListPageSkin .DatapackInfoObject > selectedItems ) {
112116 selectedItems .stream ()
113117 .map (DatapackListPageSkin .DatapackInfoObject ::getPackInfo )
114- .forEach (info -> info .setActive (false ));
118+ .forEach (pack -> pack .setActive (false ));
119+ }
120+
121+ void openDataPackFolder () {
122+ FXUtils .openFolder (datapack .getPath ());
123+ }
124+
125+ @ NotNull Predicate <DatapackListPageSkin .DatapackInfoObject > updateSearchPredicate (String queryString ) {
126+ if (queryString .isBlank ()) {
127+ return dataPack -> true ;
128+ }
129+
130+ final Predicate <String > stringPredicate ;
131+ if (queryString .startsWith ("regex:" )) {
132+ try {
133+ Pattern pattern = Pattern .compile (StringUtils .substringAfter (queryString , "regex:" ));
134+ stringPredicate = s -> s != null && pattern .matcher (s ).find ();
135+ } catch (Exception e ) {
136+ return dataPack -> false ;
137+ }
138+ } else {
139+ String lowerCaseFilter = queryString .toLowerCase (Locale .ROOT );
140+ stringPredicate = s -> s != null && s .toLowerCase (Locale .ROOT ).contains (lowerCaseFilter );
141+ }
142+
143+ return dataPack -> {
144+ String id = dataPack .getPackInfo ().getId ();
145+ String description = dataPack .getPackInfo ().getDescription ().toString ();
146+ return stringPredicate .test (id ) || stringPredicate .test (description );
147+ };
115148 }
116149}
0 commit comments