11package xyz .theprogramsrc .supercoreapi .spigot .guis ;
22
3- import org .bukkit .ChatColor ;
43import org .bukkit .entity .Player ;
54import xyz .theprogramsrc .supercoreapi .global .translations .Base ;
65import xyz .theprogramsrc .supercoreapi .spigot .dialog .Dialog ;
76import xyz .theprogramsrc .supercoreapi .spigot .guis .action .ClickAction ;
7+ import xyz .theprogramsrc .supercoreapi .spigot .guis .events .GUIEvent ;
8+ import xyz .theprogramsrc .supercoreapi .spigot .guis .events .GUIOpenEvent ;
89import xyz .theprogramsrc .supercoreapi .spigot .guis .objects .GUIRows ;
910import xyz .theprogramsrc .supercoreapi .spigot .items .SimpleItem ;
1011
11- import java .util .ArrayList ;
12+ import java .util .Arrays ;
1213import java .util .List ;
1314import java .util .stream .Collectors ;
1415
1516public abstract class BrowserGUI <OBJ > extends GUI {
1617
1718 private String searchTerm = null ;
1819 public int maxItemsPerPage = 36 ;
20+ public boolean backEnabled = false ;
1921 private int page = 0 ;
2022
2123 /**
@@ -32,39 +34,33 @@ protected GUIRows getRows() {
3234 }
3335
3436 @ Override
35- public GUIButton [] getButtons () {
36- OBJ [] objs = this .getObjects ();
37- List <OBJ > objectsFound = new ArrayList <>();
38- for (OBJ obj : objs ) {
39- if (obj != null ){
40- if (this .searchTerm == null ){
41- objectsFound .add (obj );
42- }else {
43- GUIButton button = this .getButton (obj );
44- String name = ChatColor .stripColor (new SimpleItem (button .getItemStack ()).getDisplayName ()).toLowerCase ();
45- if (name .contains (ChatColor .stripColor (this .searchTerm .toLowerCase ()))){
46- objectsFound .add (obj );
47- }
37+ public void onEvent (GUIEvent event ) {
38+ if (event instanceof GUIOpenEvent ){
39+ this .clearButtons ();
40+ OBJ [] objs = this .getObjects ();
41+ List <OBJ > objectsFound = Arrays .stream (this .getObjects ()).filter (obj ->{
42+ if (this .searchTerm != null ){
43+ String itemName = this .getSuperUtils ().removeColor (new SimpleItem (this .getButton (obj ).getItemStack ()).getDisplayName ()).toLowerCase ();
44+ String search = this .getSuperUtils ().removeColor (this .searchTerm ).toLowerCase ();
45+ return itemName .contains (search );
4846 }
47+
48+ return true ;
49+ }).collect (Collectors .toList ());
50+ int index0 = this .page * this .maxItemsPerPage ;
51+ int index1 = Math .min (index0 + this .maxItemsPerPage , objectsFound .size ());
52+ int maxPages = (int )Math .round (Math .ceil ((double )objectsFound .size () / (double )maxItemsPerPage ));
53+ List <GUIButton > buttons = objectsFound .subList (index0 , index1 ).stream ().map (this ::getButton ).collect (Collectors .toList ());
54+ int i = 0 ;
55+ for (int o = 0 ; o < buttons .size (); ++o ){
56+ buttons .set (o , buttons .get (o ).setSlot (i ));
57+ i ++;
4958 }
50- }
51- int index0 = this .page * this .maxItemsPerPage ;
52- int index1 = Math .min (index0 + this .maxItemsPerPage , objectsFound .size ());
53- int maxPages = (int )Math .round (Math .ceil ((double )objectsFound .size () / (double )maxItemsPerPage ));
54- List <GUIButton > buttons1 = new ArrayList <>();
55- int slot = 0 ;
56- for (GUIButton b : objectsFound .subList (index0 , index1 ).stream ().map (this ::getButton ).collect (Collectors .toList ())) {
57- b .setSlot (slot );
58- buttons1 .add (b );
59- slot ++;
60- }
61- List <GUIButton > buttons = new ArrayList <>(buttons1 );
62- buttons1 .clear (); // Save memory
63- objectsFound .clear (); // Save memory
6459
65- buttons .add (new GUIButton (49 , this .searchTerm == null ? this .getPreloadedItems ().getSearchItem () : this .getPreloadedItems ().getEndSearchItem ()).setAction (a -> {
60+ this .addButtons (buttons );
61+ this .removeButtons (43 , 44 ,45 ,46 ,47 ,48 ,49 ,50 ,51 ,52 ,53 );
6662 if (this .searchTerm == null ){
67- new Dialog (this .getPlayer ()){
63+ this . addButton ( new GUIButton ( 49 , this . getPreloadedItems (). getSearchItem (), a -> new Dialog (this .getPlayer ()){
6864
6965 @ Override
7066 public String getTitle () {
@@ -88,32 +84,33 @@ public boolean onResult(String playerInput) {
8884 }
8985 }.setRecall (player ->{
9086 this .page = 0 ;
91- this .open ( );
92- });
87+ this .getSpigotTasks (). runTask ( this :: open );
88+ }))) ;
9389 }else {
94- this .searchTerm = null ;
95- this .page = 0 ;
96- this .open ();
90+ this .addButton (new GUIButton (49 , this .getPreloadedItems ().getEndSearchItem (), a ->{
91+ this .searchTerm = null ;
92+ this .page = 0 ;
93+ this .open ();
94+ }));
95+ }
96+
97+ if (this .page != 0 ){
98+ this .addButton (new GUIButton (52 , this .getPreloadedItems ().getPreviousItem ()).setAction (a -> {
99+ this .page -= 1 ;
100+ this .open ();
101+ }));
102+ }
103+ if (this .page +1 < maxPages ){
104+ this .addButton (new GUIButton (53 , this .getPreloadedItems ().getNextItem ()).setAction (a -> {
105+ this .page += 1 ;
106+ this .open ();
107+ }));
97108 }
98- }));
99- buttons .add (new GUIButton (45 , this .getPreloadedItems ().getBackItem ()).setAction (this ::onBack ));
100-
101- if (this .page != 0 ){
102- buttons .add (new GUIButton (52 , this .getPreloadedItems ().getPreviousItem ()).setAction (a -> {
103- this .page --;
104- this .open ();
105- }));
106- }
107- if (this .page +1 < maxPages ){
108- buttons .add (new GUIButton (53 , this .getPreloadedItems ().getNextItem ()).setAction (a -> {
109- this .page ++;
110- this .open ();
111- }));
112- }
113109
114- GUIButton [] guiButtons = new GUIButton [buttons .size ()];
115- guiButtons = buttons .toArray (guiButtons );
116- return guiButtons ;
110+ if (this .backEnabled ){
111+ this .addButton (new GUIButton (45 , this .getPreloadedItems ().getBackItem ()).setAction (this ::onBack ));
112+ }
113+ }
117114 }
118115
119116 /**
@@ -133,5 +130,9 @@ public boolean onResult(String playerInput) {
133130 * Executed when the back button is clicked
134131 * @param clickAction The ClickAction
135132 */
136- public abstract void onBack (ClickAction clickAction );
133+ public void onBack (ClickAction clickAction ){
134+
135+ }
136+
137+
137138}
0 commit comments