|
2 | 2 |
|
3 | 3 | import me.flame.menus.items.MenuItem;
|
4 | 4 | import me.flame.menus.menu.Menu;
|
5 |
| -import me.flame.menus.menu.MenuFactory; |
6 |
| -import me.flame.menus.menu.Menus; |
7 | 5 | import me.flame.menus.menu.PaginatedMenu;
|
8 | 6 | import me.flame.menus.modifiers.Modifier;
|
| 7 | +import org.jetbrains.annotations.ApiStatus; |
9 | 8 |
|
10 | 9 | import java.util.*;
|
11 | 10 |
|
12 |
| -// changed |
13 |
| - |
| 11 | +@ApiStatus.Internal |
| 12 | +@ApiStatus.Experimental |
14 | 13 | public class MenuLayoutBuilderImpl implements MenuLayoutBuilder {
|
15 | 14 | public final Map<Character, MenuItem> itemMap;
|
16 | 15 | private final List<MenuItem> items = new ArrayList<>(54);
|
17 | 16 | private static final int MAX_ROW_SIZE = 8;
|
18 |
| - private static final MenuFactory FACTORY = Menus.getFactory(); |
19 | 17 |
|
20 |
| - public MenuLayoutBuilderImpl(Map<Character, MenuItem> itemMap) { |
| 18 | + MenuLayoutBuilderImpl(Map<Character, MenuItem> itemMap) { |
21 | 19 | this.itemMap = itemMap;
|
22 | 20 | }
|
23 | 21 |
|
24 | 22 | @Override
|
25 | 23 | public MenuLayoutBuilder row(String string) {
|
26 |
| - int stringsLength = string.length(); |
27 |
| - if (items.size() == 53) { |
28 |
| - throw new IllegalArgumentException("Attempted to add more than 54 (53 if considering 0 index) items (max inventory size)"); |
29 |
| - } |
30 |
| - if (stringsLength > MAX_ROW_SIZE) { |
| 24 | + int stringLength = items.size() + string.length(); |
| 25 | + if (stringLength > MAX_ROW_SIZE) { |
31 | 26 | throw new IllegalArgumentException("Too many strings (Temporary.. maybe?), length = "
|
32 |
| - + stringsLength + |
33 |
| - "\n String = " + string); |
| 27 | + + stringLength + |
| 28 | + "\n String = " + string + |
| 29 | + "\nFix: Reduce the amount of letters to 9 letters in total."); |
34 | 30 | }
|
35 | 31 |
|
36 |
| - char[] chars = string.toCharArray(); |
37 |
| - int index = 0; |
38 |
| - for (; index != stringsLength; index++) { |
39 |
| - if (items.size() == 53) { |
40 |
| - throw new IllegalArgumentException("Attempted to add more than 54 items (max inventory size)"); |
| 32 | + int index = items.size(); |
| 33 | + while (true) { |
| 34 | + if (items.get(53) != null) { |
| 35 | + throw new IllegalArgumentException( |
| 36 | + "Attempted to add more than 54 (53 if considering 0 index) items (max inventory size)" + |
| 37 | + "\nString = " + string + |
| 38 | + "\nFix: Remove a few rows." |
| 39 | + ); |
41 | 40 | }
|
42 |
| - char character = chars[index]; |
43 |
| - if (character == ' ') { |
44 |
| - items.add(null); |
45 |
| - continue; |
| 41 | + try { |
| 42 | + char character = string.charAt(index - items.size()); |
| 43 | + if (character == ' ') { |
| 44 | + items.set(index, null); |
| 45 | + } else { |
| 46 | + MenuItem item = itemMap.get(character); |
| 47 | + if (item == null) { |
| 48 | + throw new IllegalArgumentException("Unknown item: " + character + |
| 49 | + "\nLikely a letter not in the bound map." + |
| 50 | + "\nMap: " + itemMap + |
| 51 | + "\nFix: Add or Change the letter."); |
| 52 | + } |
| 53 | + items.set(index, item); |
| 54 | + } |
| 55 | + } catch (IndexOutOfBoundsException e) { |
| 56 | + break; |
46 | 57 | }
|
47 |
| - MenuItem item = itemMap.get(character); |
48 |
| - if (item == null) { |
49 |
| - throw new IllegalArgumentException("Unknown item: " + character + |
50 |
| - "\nLikely a letter not in the bound map." + |
51 |
| - "\nMap: " + itemMap); |
52 |
| - } |
53 |
| - items.add(item); |
54 | 58 | }
|
55 | 59 |
|
56 |
| - if (stringsLength < MAX_ROW_SIZE) |
57 |
| - for (; index != MAX_ROW_SIZE; index++) items.add(null); |
| 60 | + if (stringLength < MAX_ROW_SIZE) { |
| 61 | + for (; index != stringLength + MAX_ROW_SIZE; index++) { |
| 62 | + items.set(index, null); |
| 63 | + } |
| 64 | + } |
58 | 65 | return this;
|
59 | 66 | }
|
60 | 67 |
|
61 | 68 | @Override
|
62 | 69 | public Menu createMenu(String title) {
|
63 |
| - int i = 0, size = items.size(); |
64 |
| - Menu menu = FACTORY.createMenu(title, size / 9, EnumSet.noneOf(Modifier.class)); |
| 70 | + int size = items.size(); |
| 71 | + Menu menu = new Menu(size / 9, title, EnumSet.noneOf(Modifier.class)); |
65 | 72 |
|
66 |
| - for (; i < size; i++) |
| 73 | + for (int i = 0; i < size; i++) { |
67 | 74 | menu.setItem(i, items.get(i));
|
| 75 | + } |
68 | 76 | return menu;
|
69 | 77 | }
|
70 | 78 |
|
71 | 79 | @Override
|
72 | 80 | public PaginatedMenu createPaginated(String title, int pageSize) {
|
73 |
| - int i = 0, size = items.size(); |
74 |
| - PaginatedMenu menu = FACTORY.createPaginated(title, size / 9, pageSize); |
| 81 | + int size = items.size(); |
| 82 | + PaginatedMenu menu = new PaginatedMenu(size / 9, pageSize, title, EnumSet.noneOf(Modifier.class), true); |
75 | 83 |
|
76 |
| - for (; i < size; i++) |
| 84 | + for (int i = 0; i < size; i++) { |
77 | 85 | menu.setItem(i, items.get(i));
|
| 86 | + } |
78 | 87 | return menu;
|
79 | 88 | }
|
80 | 89 | }
|
0 commit comments