Skip to content

Commit 177bc1e

Browse files
100% Stabilized & Reworked
1 parent a147600 commit 177bc1e

File tree

1 file changed

+122
-62
lines changed

1 file changed

+122
-62
lines changed

src/main/java/me/flame/menus/menu/layout/MenuLayoutBuilder.java

Lines changed: 122 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -3,27 +3,58 @@
33
import me.flame.menus.items.MenuItem;
44
import me.flame.menus.menu.Menu;
55
import me.flame.menus.menu.PaginatedMenu;
6+
67
import me.flame.menus.modifiers.Modifier;
7-
import org.jetbrains.annotations.ApiStatus;
88
import org.jetbrains.annotations.Contract;
99
import org.jetbrains.annotations.NotNull;
1010

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+
*/
1437
@SuppressWarnings("unused")
15-
public class MenuLayoutBuilder {
38+
public final class MenuLayoutBuilder {
1639
@NotNull
17-
public final Map<Character, MenuItem> itemMap;
40+
private final Map<Character, MenuItem> itemMap;
1841

19-
private final MenuItem[] items = new MenuItem[54];
20-
21-
private int size;
42+
@NotNull
43+
private String[] patterns;
2244

23-
private static final int MAX_ROW_SIZE = 9;
45+
private int rows;
2446

2547
MenuLayoutBuilder(@NotNull Map<Character, MenuItem> itemMap) {
2648
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;
2758
}
2859

2960
@NotNull
@@ -33,68 +64,51 @@ public static MenuLayoutBuilder bind(Map<Character, MenuItem> itemMap) {
3364
}
3465

3566
/**
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
3971
*/
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-
"\nFix: 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);
5376

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;
5981

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);
6484

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-
"\nLikely a letter not in the bound map." +
75-
"\nMap: " + itemMap + "\nFix: Add or Change the letter."
76-
);
85+
if (menuItem != null) menu.setItem(i, menuItem);
7786
}
78-
items[i] = item;
79-
}
8087

88+
return menu;
89+
}
8190
/**
8291
* Creates a menu with the given title and populates it with items.
8392
*
8493
* @param title the title of the menu
8594
* @return the created menu
8695
*/
87-
public Menu createMenu(String title) {
88-
if (size >= 54) {
89-
throw new IllegalArgumentException(
90-
"Too many rows. \nAmount of rows:" + size / 9 +
91-
"\nFix: 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;
95102
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);
97110
}
111+
98112
return menu;
99113
}
100114

@@ -106,11 +120,47 @@ public Menu createMenu(String title) {
106120
* @return the created paginated menu
107121
*/
108122
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);
110152

153+
int size = ((rows * 9) + 9) - 10;
111154
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);
113162
}
163+
114164
return menu;
115165
}
116166

@@ -121,11 +171,21 @@ public PaginatedMenu createPaginated(String title, int pages) {
121171
* @return the created paginated menu
122172
*/
123173
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);
125177

178+
int size = ((rows * 9) + 9) - 10;
126179
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);
128187
}
188+
129189
return menu;
130190
}
131191
}

0 commit comments

Comments
 (0)