Advertisement
Guest User

Untitled

a guest
Jun 27th, 2017
49
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.51 KB | None | 0 0
  1.  
  2. @RequiredArgsConstructor
  3. public class StringFolderGui extends BaseGui{
  4.  
  5. private final String title;
  6.  
  7. private final int[] inventoryMap;
  8. private final VisualLink[] links;
  9.  
  10.  
  11. @Override
  12. public void onClick(InventoryClickEvent event) {
  13. final int clickedId = inventoryMap[event.getSlot()];
  14. if(clickedId >= 0)
  15. links[clickedId].link.run((Player) event.getWhoClicked());
  16. }
  17.  
  18. @Override
  19. protected Inventory render() {
  20. Inventory inv = Bukkit.createInventory(null, inventoryMap.length);
  21. ItemStack[] contents = inv.getContents();
  22.  
  23. for(int i = 0; i < inventoryMap.length; i++) {
  24. final int itemId = inventoryMap[i];
  25. if(itemId < 0)
  26. continue;
  27. contents[i] = links[itemId].item;
  28. }
  29.  
  30. inv.setContents(contents);
  31. return inv;
  32. }
  33.  
  34. @Data
  35. public static class VisualLink {
  36. public final Link link;
  37. public final ItemStack item;
  38.  
  39. public static VisualLink of(Link link, ItemStack item) {
  40. return new VisualLink(link, item);
  41. }
  42. }
  43.  
  44. public static Builder builder() {
  45. return new Builder();
  46. }
  47.  
  48.  
  49. public static class Builder {
  50. private String title = "Gui";
  51. private String map = "";
  52. private Map<Character, VisualLink> links = new HashMap<>();
  53.  
  54. public Builder title(@NonNull String title) {
  55. this.title = title;
  56. return this;
  57. }
  58.  
  59. public Builder map(@NonNull String map) {
  60. if(!isValid(map.length()))
  61. throw new IllegalArgumentException("Invalid map length: " + map.length());
  62. this.map = map;
  63. return this;
  64. }
  65.  
  66. private boolean isValid(int length) {
  67. return length % 9 == 0 && length > 0 && length <= GuiSize.DOUBLE.size();
  68. }
  69.  
  70. public Builder addLink(char c, VisualLink link) {
  71. links.put(c, link);
  72. return this;
  73. }
  74.  
  75. public Builder addLink(char c, Link link, ItemStack item) {
  76. links.put(c, VisualLink.of(link, item));
  77. return this;
  78. }
  79.  
  80. public Builder addLink(char c, Link link, Material display, String name, String... lores) {
  81. links.put(c, VisualLink.of(link, GuiUtils.itemStack(display, name, lores)));
  82. return this;
  83. }
  84.  
  85. public StringFolderGui build() {
  86. VisualLink[] clinks = new VisualLink[links.size()];
  87. char[] linksbychar = new char[links.size()];
  88. {//compile clinks and linksbychar
  89. int index = 0;
  90.  
  91. for (Map.Entry<Character, VisualLink> e : links.entrySet()) {
  92. clinks[index] = e.getValue();
  93. linksbychar[index] = e.getKey();
  94. index++;
  95. }
  96. }
  97.  
  98. int[] cmap = new int[map.length()];
  99. {//compile cmap
  100. for (int index = 0; index < cmap.length; index++) {
  101. final char c = map.charAt(index);
  102. int id = -1;
  103. for (int i = 0; i < clinks.length; i++) {
  104. if (linksbychar[i] == c) {
  105. id = i;
  106. break;
  107. }
  108. }
  109. cmap[index] = id;
  110. }
  111. }
  112.  
  113. return new StringFolderGui(title, cmap, clinks);
  114. }
  115. }
  116. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement