@@ -80,7 +80,18 @@ public void init()
8080 for (Feature f : features )
8181 if (f .getCategory () != null )
8282 windowMap .get (f .getCategory ()).add (new FeatureButton (f ));
83-
83+ // add favorites window entries (show favorites in the Favorites
84+ // category)
85+ for (Feature f : features )
86+ if (f instanceof net .wurstclient .hack .Hack
87+ && ((net .wurstclient .hack .Hack )f ).isFavorite ())
88+ windowMap .get (net .wurstclient .Category .FAVORITES )
89+ .add (new FeatureButton (f ));
90+ // ensure favourites window is sorted alphabetically
91+ Window favWindow = windowMap .get (net .wurstclient .Category .FAVORITES );
92+ if (favWindow != null )
93+ sortFavoritesWindow (favWindow );
94+
8495 windows .addAll (windowMap .values ());
8596
8697 Window uiSettings = new Window ("UI Settings" );
@@ -836,8 +847,94 @@ public void addPopup(Popup popup)
836847 popups .add (popup );
837848 }
838849
850+ /**
851+ * Add a feature to the Favorites window if not already present.
852+ */
853+ public void addFavoriteFeature (Feature feature )
854+ {
855+ String favTitle = net .wurstclient .Category .FAVORITES .getName ();
856+ for (Window window : windows )
857+ {
858+ if (window .getTitle ().equals (favTitle ))
859+ {
860+ // check existing
861+ for (int i = 0 ; i < window .countChildren (); i ++)
862+ {
863+ Component c = window .getChild (i );
864+ if (c instanceof net .wurstclient .clickgui .components .FeatureButton )
865+ {
866+ net .wurstclient .clickgui .components .FeatureButton fb =
867+ (net .wurstclient .clickgui .components .FeatureButton )c ;
868+ if (fb .getFeature ().getName ().equals (feature .getName ()))
869+ return ;
870+ }
871+ }
872+ window
873+ .add (new net .wurstclient .clickgui .components .FeatureButton (
874+ feature ));
875+ sortFavoritesWindow (window );
876+ return ;
877+ }
878+ }
879+ }
880+
881+ public void removeFavoriteFeature (Feature feature )
882+ {
883+ String favTitle = net .wurstclient .Category .FAVORITES .getName ();
884+ for (Window window : windows )
885+ {
886+ if (!window .getTitle ().equals (favTitle ))
887+ continue ;
888+ for (int i = window .countChildren () - 1 ; i >= 0 ; i --)
889+ {
890+ Component c = window .getChild (i );
891+ if (c instanceof net .wurstclient .clickgui .components .FeatureButton )
892+ {
893+ net .wurstclient .clickgui .components .FeatureButton fb =
894+ (net .wurstclient .clickgui .components .FeatureButton )c ;
895+ if (fb .getFeature ().getName ().equals (feature .getName ()))
896+ {
897+ window .remove (i );
898+ window .pack ();
899+ return ;
900+ }
901+ }
902+ }
903+ }
904+ }
905+
839906 public boolean isLeftMouseButtonPressed ()
840907 {
841908 return leftMouseButtonPressed ;
842909 }
910+
911+ /**
912+ * Sort the given favourites window's children alphabetically by feature
913+ * name.
914+ */
915+ private void sortFavoritesWindow (Window window )
916+ {
917+ if (window == null )
918+ return ;
919+ // collect children
920+ ArrayList <Component > all = new ArrayList <>();
921+ for (int i = 0 ; i < window .countChildren (); i ++)
922+ all .add (window .getChild (i ));
923+ // sort by feature name when possible
924+ all .sort ((c1 , c2 ) -> {
925+ String n1 = c1 instanceof FeatureButton
926+ ? ((FeatureButton )c1 ).getFeature ().getName ()
927+ : c1 .getClass ().getName ();
928+ String n2 = c2 instanceof FeatureButton
929+ ? ((FeatureButton )c2 ).getFeature ().getName ()
930+ : c2 .getClass ().getName ();
931+ return n1 .compareToIgnoreCase (n2 );
932+ });
933+ // remove all children and re-add in sorted order
934+ for (int i = window .countChildren () - 1 ; i >= 0 ; i --)
935+ window .remove (i );
936+ for (Component c : all )
937+ window .add (c );
938+ window .pack ();
939+ }
843940}
0 commit comments