1
1
package xyz .theprogramsrc .supercoreapi .spigot .guis ;
2
2
3
- import org .bukkit .ChatColor ;
4
3
import org .bukkit .entity .Player ;
5
4
import xyz .theprogramsrc .supercoreapi .global .translations .Base ;
6
5
import xyz .theprogramsrc .supercoreapi .spigot .dialog .Dialog ;
7
6
import 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 ;
8
9
import xyz .theprogramsrc .supercoreapi .spigot .guis .objects .GUIRows ;
9
10
import xyz .theprogramsrc .supercoreapi .spigot .items .SimpleItem ;
10
11
11
- import java .util .ArrayList ;
12
+ import java .util .Arrays ;
12
13
import java .util .List ;
13
14
import java .util .stream .Collectors ;
14
15
15
16
public abstract class BrowserGUI <OBJ > extends GUI {
16
17
17
18
private String searchTerm = null ;
18
19
public int maxItemsPerPage = 36 ;
20
+ public boolean backEnabled = false ;
19
21
private int page = 0 ;
20
22
21
23
/**
@@ -32,39 +34,33 @@ protected GUIRows getRows() {
32
34
}
33
35
34
36
@ 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 );
48
46
}
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 ++;
49
58
}
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
64
59
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 );
66
62
if (this .searchTerm == null ){
67
- new Dialog (this .getPlayer ()){
63
+ this . addButton ( new GUIButton ( 49 , this . getPreloadedItems (). getSearchItem (), a -> new Dialog (this .getPlayer ()){
68
64
69
65
@ Override
70
66
public String getTitle () {
@@ -88,32 +84,33 @@ public boolean onResult(String playerInput) {
88
84
}
89
85
}.setRecall (player ->{
90
86
this .page = 0 ;
91
- this .open ( );
92
- });
87
+ this .getSpigotTasks (). runTask ( this :: open );
88
+ }))) ;
93
89
}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
+ }));
97
108
}
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
- }
113
109
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
+ }
117
114
}
118
115
119
116
/**
@@ -133,5 +130,9 @@ public boolean onResult(String playerInput) {
133
130
* Executed when the back button is clicked
134
131
* @param clickAction The ClickAction
135
132
*/
136
- public abstract void onBack (ClickAction clickAction );
133
+ public void onBack (ClickAction clickAction ){
134
+
135
+ }
136
+
137
+
137
138
}
0 commit comments