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