11package xyz .theprogramsrc .supercoreapi .spigot .guis ;
22
3+ import java .util .Arrays ;
4+ import java .util .List ;
5+ import java .util .stream .Collectors ;
6+
37import org .bukkit .entity .Player ;
8+
49import xyz .theprogramsrc .supercoreapi .global .translations .Base ;
510import xyz .theprogramsrc .supercoreapi .spigot .dialog .Dialog ;
611import 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 ;
912import xyz .theprogramsrc .supercoreapi .spigot .guis .objects .GUIRows ;
1013import xyz .theprogramsrc .supercoreapi .spigot .items .SimpleItem ;
1114
12- import java .util .Arrays ;
13- import java .util .List ;
14- import java .util .stream .Collectors ;
15-
1615public abstract class BrowserGUI <OBJ > extends GUI {
1716
1817 private String searchTerm = null ;
@@ -34,82 +33,78 @@ protected GUIRows getRows() {
3433 }
3534
3635 @ Override
37- public void onEvent (GUIEvent event ) {
38- if (event instanceof GUIOpenEvent ){
39- this .clearButtons ();
40- List <OBJ > objectsFound = Arrays .stream (this .getObjects ()).filter (obj ->{
41- if (this .searchTerm != null ){
42- String itemName = this .getSuperUtils ().removeColor (new SimpleItem (this .getButton (obj ).getItemStack ()).getDisplayName ()).toLowerCase ();
43- String search = this .getSuperUtils ().removeColor (this .searchTerm ).toLowerCase ();
44- return itemName .contains (search );
36+ protected GUIButton [] getButtons () {
37+ List <OBJ > objectsFound = Arrays .stream (this .getObjects ()).filter (obj ->{
38+ if (this .searchTerm != null ){
39+ String itemName = this .getSuperUtils ().removeColor (new SimpleItem (this .getButton (obj ).getItemStack ()).getDisplayName ()).toLowerCase ();
40+ String search = this .getSuperUtils ().removeColor (this .searchTerm ).toLowerCase ();
41+ return itemName .contains (search );
42+ }
43+
44+ return true ;
45+ }).collect (Collectors .toList ());
46+ int index0 = this .page * this .maxItemsPerPage ;
47+ int index1 = Math .min (index0 + this .maxItemsPerPage , objectsFound .size ());
48+ int maxPages = (int )Math .round (Math .ceil ((double )objectsFound .size () / (double )maxItemsPerPage ));
49+ List <GUIButton > buttons = objectsFound .subList (index0 , index1 ).stream ().map (this ::getButton ).collect (Collectors .toList ());
50+ int i = 0 ;
51+ for (int o = 0 ; o < buttons .size (); ++o ){
52+ buttons .set (o , buttons .get (o ).setSlot (i ));
53+ i ++;
54+ }
55+
56+ if (this .searchTerm == null ){
57+ this .addButton (new GUIButton (49 , this .getPreloadedItems ().getSearchItem (), a -> new Dialog (this .getPlayer ()){
58+
59+ @ Override
60+ public String getTitle () {
61+ return Base .DIALOG_SEARCH_TITLE .toString ();
4562 }
4663
47- return true ;
48- }).collect (Collectors .toList ());
49- int index0 = this .page * this .maxItemsPerPage ;
50- int index1 = Math .min (index0 + this .maxItemsPerPage , objectsFound .size ());
51- int maxPages = (int )Math .round (Math .ceil ((double )objectsFound .size () / (double )maxItemsPerPage ));
52- List <GUIButton > buttons = objectsFound .subList (index0 , index1 ).stream ().map (this ::getButton ).collect (Collectors .toList ());
53- int i = 0 ;
54- for (int o = 0 ; o < buttons .size (); ++o ){
55- buttons .set (o , buttons .get (o ).setSlot (i ));
56- i ++;
57- }
64+ @ Override
65+ public String getSubtitle () {
66+ return Base .DIALOG_SEARCH_SUBTITLE .toString ();
67+ }
5868
59- this .addButtons (buttons );
60- this .removeButtons (43 , 44 ,45 ,46 ,47 ,48 ,49 ,50 ,51 ,52 ,53 );
61- if (this .searchTerm == null ){
62- this .addButton (new GUIButton (49 , this .getPreloadedItems ().getSearchItem (), a -> new Dialog (this .getPlayer ()){
63-
64- @ Override
65- public String getTitle () {
66- return Base .DIALOG_SEARCH_TITLE .toString ();
67- }
68-
69- @ Override
70- public String getSubtitle () {
71- return Base .DIALOG_SEARCH_SUBTITLE .toString ();
72- }
73-
74- @ Override
75- public String getActionbar () {
76- return Base .DIALOG_SEARCH_ACTIONBAR .toString ();
77- }
78-
79- @ Override
80- public boolean onResult (String playerInput ) {
81- BrowserGUI .this .searchTerm = playerInput ;
82- return true ;
83- }
84- }.setRecall (player ->{
85- this .page = 0 ;
86- this .getSpigotTasks ().runTask (this ::open );
87- })));
88- }else {
89- this .addButton (new GUIButton (49 , this .getPreloadedItems ().getEndSearchItem (), a ->{
90- this .searchTerm = null ;
91- this .page = 0 ;
92- this .open ();
93- }));
94- }
69+ @ Override
70+ public String getActionbar () {
71+ return Base .DIALOG_SEARCH_ACTIONBAR .toString ();
72+ }
9573
96- if (this .page != 0 ){
97- this .addButton (new GUIButton (52 , this .getPreloadedItems ().getPreviousItem ()).setAction (a -> {
98- this .page -= 1 ;
99- this .open ();
100- }));
101- }
102- if (this .page +1 < maxPages ){
103- this .addButton (new GUIButton (53 , this .getPreloadedItems ().getNextItem ()).setAction (a -> {
104- this .page += 1 ;
105- this .open ();
106- }));
107- }
74+ @ Override
75+ public boolean onResult (String playerInput ) {
76+ BrowserGUI .this .searchTerm = playerInput ;
77+ return true ;
78+ }
79+ }.setRecall (player ->{
80+ this .page = 0 ;
81+ this .getSpigotTasks ().runTask (this ::open );
82+ })));
83+ }else {
84+ buttons .add (new GUIButton (49 , this .getPreloadedItems ().getEndSearchItem (), a ->{
85+ this .searchTerm = null ;
86+ this .page = 0 ;
87+ this .open ();
88+ }));
89+ }
10890
109- if (this .backEnabled ){
110- this .addButton (new GUIButton (45 , this .getPreloadedItems ().getBackItem ()).setAction (this ::onBack ));
111- }
91+ if (this .page != 0 ){
92+ buttons .add (new GUIButton (52 , this .getPreloadedItems ().getPreviousItem ()).setAction (a -> {
93+ this .page -= 1 ;
94+ this .open ();
95+ }));
96+ }
97+ if (this .page +1 < maxPages ){
98+ buttons .add (new GUIButton (53 , this .getPreloadedItems ().getNextItem ()).setAction (a -> {
99+ this .page += 1 ;
100+ this .open ();
101+ }));
102+ }
103+
104+ if (this .backEnabled ){
105+ buttons .add (new GUIButton (45 , this .getPreloadedItems ().getBackItem ()).setAction (this ::onBack ));
112106 }
107+ return buttons .toArray (new GUIButton [0 ]);
113108 }
114109
115110 /**
0 commit comments