public Node getChild(Category subcategory) { return new Node(subcategory.getSubcategories(), subcategory.getPackages(), subcategory.getName(), this); }
public GUIImpl getFirstPage(Category category) { List<GUIImpl> guis = categoryMenus.get(category.getId()); if (guis == null) return null; return Iterables.getFirst(guis, null); }
@Override public int compareTo(Category o) { Objects.requireNonNull(o, "category"); return Integer.compare(order, o.getOrder()); }
private static int calculatePages(Category category) { int pagesWithSubcats = (int) Math.ceil(category.getSubcategories().size() / 9D); int pagesWithPackages = (int) Math.ceil(category.getPackages().size() / 36D); return Math.max(pagesWithSubcats, pagesWithPackages); }
if (!category.getSubcategories().isEmpty()) { subcatPartition = Lists.partition(category.getSubcategories(), 45); if (subcatPartition.size() - 1 >= page) { List<Category> subcats = subcatPartition.get(page); Category subcat = subcats.get(i); String gui_item = subcats.get(i).getGui_item(); inventory.setItem(i, withName(material, ChatColor.YELLOW + subcat.getName(), variant)); List<List<Package>> packagePartition = Lists.partition(category.getPackages(), 36); int base = subcatPartition.isEmpty() ? 0 : 9;
String gui_item = category.getGui_item(); inventory.setItem(inventory.firstEmpty(), withName(material, ChatColor.YELLOW + category.getName(), variant));
public GUIImpl(Integer parentId, int page, Category category) { this.inventory = Bukkit.createInventory(null, calculateSize(category, page), GUIUtil.trimName("Buycraft: " + category.getName())); this.parentId = parentId; this.page = page; update(category); }
public void update() { if (plugin.getApiClient() == null || plugin.getServerInformation() == null) { plugin.getLogger().warning("No secret key available (or no server information), so can't update inventories."); return; } Listing listing = plugin.getListingUpdateTask().getListing(); if (listing == null) { plugin.getLogger().warning("No listing found, so can't update inventories."); return; } List<Integer> foundIds = new ArrayList<>(); for (Category category : listing.getCategories()) { foundIds.add(category.getId()); for (Category category1 : category.getSubcategories()) { foundIds.add(category1.getId()); } } for (Iterator<Map.Entry<Integer, List<GUIImpl>>> it = categoryMenus.entrySet().iterator(); it.hasNext(); ) { Map.Entry<Integer, List<GUIImpl>> next = it.next(); if (!foundIds.contains(next.getKey())) { for (GUIImpl gui : next.getValue()) { gui.destroy(); } it.remove(); } } for (Category category : listing.getCategories()) { doUpdate(null, category); } }
private int calculateSize(Category category, int page) { // TODO: Calculate this amount based on no of packages int needed = 45; // bottom row if (!category.getSubcategories().isEmpty()) { int pagesWithSubcats = (int) Math.ceil(category.getSubcategories().size() / 9D); if (pagesWithSubcats >= page) { // more pages exist needed += 9; } } // if we show subcategories, we can't show as many pages return needed; }
public void order() { Collections.sort(categories); for (Category category : categories) { category.order(); } } }
private Package doSearch(int id, Category category) { for (Package aPackage : category.getPackages()) { if (aPackage.getId() == id) return aPackage; } for (Category sub : category.getSubcategories()) { Package p = doSearch(id, sub); if (p != null) return p; } return null; } }
private void sendPaginatedMessage(Node node, CommandSource source) { PaginationService paginationService = Sponge.getServiceManager().provide(PaginationService.class).get(); PaginationList.Builder builder = paginationService.builder(); List<Text> contents = node.getSubcategories().stream() .map(category -> Text.builder("> " + category.getName()).color(TextColors.GRAY).onClick(TextActions.executeCallback(commandSource -> { if (commandSource instanceof Player) { sendPaginatedMessage(node.getChild(category), source); } })).build()).collect(Collectors.toList()); for (Package p : node.getPackages()) { contents.add(Text.builder(p.getName()).color(TextColors.WHITE).append(Text.builder(" - ").color(TextColors.GRAY).build()) .append(Text.builder("$x".replace("$", plugin.getServerInformation().getAccount().getCurrency().getSymbol()) .replace("x", "" + p.getEffectivePrice())).color(TextColors.GREEN).build()) .onClick(TextActions.executeCallback(commandSource -> { if (commandSource instanceof Player) { plugin.getPlatform().executeAsync(new SendCheckoutLinkTask(plugin, p.getId(), (Player) commandSource)); } })).build()); } builder.title(Text.builder(plugin.getI18n().get("sponge_listing")).color(TextColors.AQUA).build()).contents(contents).padding(Text.of("-")).sendTo(source); }
private void doUpdate(Category parent, Category category) { List<GUIImpl> pages = categoryMenus.get(category.getId()); if (pages == null) { pages = new ArrayList<>(); categoryMenus.put(category.getId(), pages); for (int i = 0; i < calculatePages(category); i++) { GUIImpl gui = new GUIImpl(parent != null ? parent.getId() : null, i, category); plugin.getServer().getPluginManager().registerEvents(gui, plugin); pages.add(gui); int toAdd = -toRemove; for (int i = 0; i < toAdd; i++) { GUIImpl gui = new GUIImpl(parent != null ? parent.getId() : null, pages.size(), category); plugin.getServer().getPluginManager().registerEvents(gui, plugin); pages.add(gui); if (gui.requiresResize(category)) { HandlerList.unregisterAll(gui); GUIImpl tmpGui = new GUIImpl(parent != null ? parent.getId() : null, i, category); plugin.getServer().getPluginManager().registerEvents(tmpGui, plugin); pages.set(i, tmpGui); for (Category category1 : category.getSubcategories()) { doUpdate(category, category1);
public void order() { Collections.sort(packages); if (subcategories != null) { Collections.sort(subcategories); for (Category category : subcategories) { category.order(); } } } }
if (displayName.startsWith(ChatColor.YELLOW.toString())) { for (final Category category1 : category.getSubcategories()) { if (category1.getName().equals(ChatColor.stripColor(displayName))) { final GUIImpl gui = getFirstPage(category1); if (gui == null) { for (Package aPackage : category.getPackages()) { if (aPackage.getName().equals(ChatColor.stripColor(displayName))) { GUIUtil.closeInventoryLater(player);
@Override public void run() { categoryMenus.get(category.getId()).get(page - 1).open(player); } });
@Override public void run() { categoryMenus.get(category.getId()).get(page + 1).open(player); } });