3
3
import me .flame .menus .items .MenuItem ;
4
4
import me .flame .menus .menu .Menu ;
5
5
import me .flame .menus .menu .PaginatedMenu ;
6
+
6
7
import me .flame .menus .modifiers .Modifier ;
7
- import org .jetbrains .annotations .ApiStatus ;
8
8
import org .jetbrains .annotations .Contract ;
9
9
import org .jetbrains .annotations .NotNull ;
10
10
11
- import java .util .*;
12
-
13
- @ ApiStatus .Experimental
11
+ import java .util .EnumSet ;
12
+ import java .util .Map ;
13
+
14
+ /**
15
+ * Complex and Fast builder to build (paginated) menus from a list of strings or a so-called pattern.
16
+ * <p>
17
+ * Example usage:
18
+ * <pre>{@code
19
+ * Map<Character, MenuItem> menuItems = ImmutableMap.of(
20
+ * 'X', ItemBuilder.of(Material.STONE).buildItem();
21
+ * 'K', ItemBuilder.of(Material.WHITE_STAINED_GLASS_PANE).buildItem();
22
+ * );
23
+ * Menu menu = MenuLayoutBuilder.bind(menuItems)
24
+ * .pattern(
25
+ * "KKKKKKKKK"
26
+ * "KXX XXK"
27
+ * "KX XK"
28
+ * "KX XK"
29
+ * "KXX XXK"
30
+ * "KKKKKKKKK"
31
+ * )
32
+ * .createMenu("Awesome");
33
+ * }</pre>
34
+ * @author FlameyosFlow
35
+ * @since 1.2.0. Stabilized at 1.5.0
36
+ */
14
37
@ SuppressWarnings ("unused" )
15
- public class MenuLayoutBuilder {
38
+ public final class MenuLayoutBuilder {
16
39
@ NotNull
17
- public final Map <Character , MenuItem > itemMap ;
40
+ private final Map <Character , MenuItem > itemMap ;
18
41
19
- private final MenuItem [] items = new MenuItem [54 ];
20
-
21
- private int size ;
42
+ @ NotNull
43
+ private String [] patterns ;
22
44
23
- private static final int MAX_ROW_SIZE = 9 ;
45
+ private int rows ;
24
46
25
47
MenuLayoutBuilder (@ NotNull Map <Character , MenuItem > itemMap ) {
26
48
this .itemMap = itemMap ;
49
+ this .patterns = null ;
50
+ this .rows = 0 ;
51
+ }
52
+
53
+
54
+ public MenuLayoutBuilder pattern (String @ NotNull ... patterns ) {
55
+ this .patterns = patterns ;
56
+ this .rows = patterns .length ;
57
+ return this ;
27
58
}
28
59
29
60
@ NotNull
@@ -33,68 +64,51 @@ public static MenuLayoutBuilder bind(Map<Character, MenuItem> itemMap) {
33
64
}
34
65
35
66
/**
36
- * Add a new row to the menu.
37
- * @param string the string to add (can be partially empty)
38
- * @return the object for chaining
67
+ * Creates a menu with the given title and populates it with items.
68
+ *
69
+ * @param title the title of the menu
70
+ * @return the created menu
39
71
*/
40
- public MenuLayoutBuilder row (@ NotNull String string ) {
41
- int stringLength = size + string .length ();
42
- if (stringLength > 9 ) {
43
- throw new IllegalArgumentException (
44
- "Too many strings (Temporary.. maybe?), length = "
45
- + stringLength + "\n String = " + string +
46
- "\n Fix: Reduce the amount of letters to 9 letters in total."
47
- );
48
- }
49
-
50
- for (int i = size ; i < stringLength ; i ++) {
51
- addItem (i , size >= 54 ? ' ' : string .charAt (i ));
52
- }
72
+ public Menu createMenu (String title ) {
73
+ if (patterns == null )
74
+ throw new IllegalStateException ("No patterns specified. Use the pattern() method before creating the menu." );
75
+ Menu menu = Menu .create (title , rows );
53
76
54
- if (stringLength < 9 ) {
55
- Arrays .fill (items , stringLength , stringLength + 9 , null );
56
- }
57
- return this ;
58
- }
77
+ int size = ((rows * 9 ) + 9 ) - 10 ;
78
+ for (int i = 0 ; i < size ; i ++) {
79
+ int row = i / 9 ;
80
+ int col = i % 9 ;
59
81
60
- public MenuLayoutBuilder pattern (@ NotNull String ... string ) {
61
- for (String s : string ) row (s );
62
- return this ;
63
- }
82
+ char item = patterns [row ].charAt (col );
83
+ MenuItem menuItem = itemMap .get (item );
64
84
65
- private void addItem (int i , char character ) {
66
- size ++;
67
-
68
- if (character == ' ' ) return ; // null by default
69
- MenuItem item = itemMap .get (character );
70
- if (item == null ) {
71
- items [i ] = null ;
72
- throw new IllegalArgumentException (
73
- "Unknown item: " + character +
74
- "\n Likely a letter not in the bound map." +
75
- "\n Map: " + itemMap + "\n Fix: Add or Change the letter."
76
- );
85
+ if (menuItem != null ) menu .setItem (i , menuItem );
77
86
}
78
- items [i ] = item ;
79
- }
80
87
88
+ return menu ;
89
+ }
81
90
/**
82
91
* Creates a menu with the given title and populates it with items.
83
92
*
84
93
* @param title the title of the menu
85
94
* @return the created menu
86
95
*/
87
- public Menu createMenu (String title ) {
88
- if (size >= 54 ) {
89
- throw new IllegalArgumentException (
90
- "Too many rows. \n Amount of rows:" + size / 9 +
91
- "\n Fix: Reduce the amount of letters to 9 letters in total."
92
- );
93
- }
94
- Menu menu = new Menu (Math .min (size / 9 , 6 ), title , EnumSet .noneOf (Modifier .class ));
96
+ public Menu createMenu (String title , EnumSet <Modifier > modifiers ) {
97
+ if (patterns == null )
98
+ throw new IllegalStateException ("No patterns specified. Use the pattern() method before creating the menu." );
99
+ Menu menu = Menu .create (title , rows , modifiers );
100
+
101
+ int size = ((rows * 9 ) + 9 ) - 10 ;
95
102
for (int i = 0 ; i < size ; i ++) {
96
- menu .setItem (i , items [i ]);
103
+ int row = i / 9 ;
104
+ int col = i % 9 ;
105
+
106
+ char item = patterns [row ].charAt (col );
107
+ MenuItem menuItem = itemMap .get (item );
108
+
109
+ if (menuItem != null ) menu .setItem (i , menuItem );
97
110
}
111
+
98
112
return menu ;
99
113
}
100
114
@@ -106,11 +120,47 @@ public Menu createMenu(String title) {
106
120
* @return the created paginated menu
107
121
*/
108
122
public PaginatedMenu createPaginated (String title , int pages ) {
109
- PaginatedMenu menu = PaginatedMenu .create (title , Math .min (size / 9 , 6 ), pages );
123
+ if (patterns == null )
124
+ throw new IllegalStateException ("No patterns specified. Use the pattern() method before creating the menu." );
125
+ PaginatedMenu menu = PaginatedMenu .create (title , rows , pages );
126
+
127
+ int size = ((rows * 9 ) + 9 ) - 10 ;
128
+ for (int i = 0 ; i < size ; i ++) {
129
+ int row = i / 9 ;
130
+ int col = i % 9 ;
131
+
132
+ char item = patterns [row ].charAt (col );
133
+ MenuItem menuItem = itemMap .get (item );
134
+
135
+ if (menuItem != null ) menu .setItem (i , menuItem );
136
+ }
137
+
138
+ return menu ;
139
+ }
140
+
141
+ /**
142
+ * Creates a paginated menu with the given title and populates it with items.
143
+ *
144
+ * @param title the title of the paginated menu
145
+ * @param pages the number of pages
146
+ * @return the created paginated menu
147
+ */
148
+ public PaginatedMenu createPaginated (String title , int pages , EnumSet <Modifier > modifiers ) {
149
+ if (patterns == null )
150
+ throw new IllegalStateException ("No patterns specified. Use the pattern() method before creating the menu." );
151
+ PaginatedMenu menu = PaginatedMenu .create (title , rows , pages , modifiers );
110
152
153
+ int size = ((rows * 9 ) + 9 ) - 10 ;
111
154
for (int i = 0 ; i < size ; i ++) {
112
- menu .setItem (i , items [i ]);
155
+ int row = i / 9 ;
156
+ int col = i % 9 ;
157
+
158
+ char item = patterns [row ].charAt (col );
159
+ MenuItem menuItem = itemMap .get (item );
160
+
161
+ if (menuItem != null ) menu .setItem (i , menuItem );
113
162
}
163
+
114
164
return menu ;
115
165
}
116
166
@@ -121,11 +171,21 @@ public PaginatedMenu createPaginated(String title, int pages) {
121
171
* @return the created paginated menu
122
172
*/
123
173
public PaginatedMenu createPaginated (String title ) {
124
- PaginatedMenu menu = PaginatedMenu .create (title , Math .min (size / 9 , 6 ), 3 );
174
+ if (patterns == null )
175
+ throw new IllegalStateException ("No patterns specified. Use the pattern() method before creating the menu." );
176
+ PaginatedMenu menu = PaginatedMenu .create (title , rows , 3 );
125
177
178
+ int size = ((rows * 9 ) + 9 ) - 10 ;
126
179
for (int i = 0 ; i < size ; i ++) {
127
- menu .setItem (i , items [i ]);
180
+ int row = i / 9 ;
181
+ int col = i % 9 ;
182
+
183
+ char item = patterns [row ].charAt (col );
184
+ MenuItem menuItem = itemMap .get (item );
185
+
186
+ if (menuItem != null ) menu .setItem (i , menuItem );
128
187
}
188
+
129
189
return menu ;
130
190
}
131
191
}
0 commit comments