Skip to content

PaginatedList Example

Hempfest edited this page Dec 5, 2021 · 7 revisions

This section will soon be updated with the brand new AbstractPaginatedCollection object

	private PaginatedList<OfflinePlayer> getTestList(Player p) {
		Message msg = Message.form(p);
		List<OfflinePlayer> list = new LinkedList<>();
		Collections.addAll(list, Bukkit.getOfflinePlayers());
		return new PaginatedList<>(list)
				.limit(10)
				.compare((o1, o2) -> Long.compare(o2.getLastPlayed(), o1.getLastPlayed()))
				.start((pagination, page, max) -> p.sendMessage("--------------------"))
				.finish((pagination, page, max) -> {
					p.sendMessage("--------------------");
					TextLib component = TextLib.getInstance();
					int next = page + 1;
					int last = Math.max(page - 1, 1);
					List<BaseComponent> toSend = new LinkedList<>();
					if (page == 1) {
						if (page == max) {
							toSend.add(component.textHoverable("", "&8« ", "You are on the first page already."));
							toSend.add(component.textHoverable("&f<&7" + page + "&f/&7" + max + "&f>", "", ""));
							toSend.add(component.textHoverable("", " &8»", "You are already on the last page."));
							msg.build(toSend.toArray(new BaseComponent[0]));
							return;
						}
						toSend.add(component.textHoverable("", "&8« ", "You are on the first page already."));
						toSend.add(component.textHoverable("&f<&7" + page + "&f/&7" + max + "&f>", "", ""));
						toSend.add(component.execute(() -> t.get(next), component.textHoverable("", " &3»", "")));
						msg.build(toSend.toArray(new BaseComponent[0]));
						return;
					}
					if (page == max) {
						toSend.add(component.execute(() -> t.get(last), component.textHoverable("", "&3« ", "")));
						toSend.add(component.textHoverable("&f<&7" + page + "&f/&7" + max + "&f>", "", ""));
						toSend.add(component.textHoverable("", " &8»", "You are already on the last page."));
						msg.build(toSend.toArray(new BaseComponent[0]));
						return;
					}
					if (next <= max) {
						toSend.add(component.execute(() -> t.get(last), component.textHoverable("", "&3« ", "")));
						toSend.add(component.textHoverable("&f<&7" + page + "&f/&7" + max + "&f>", "", ""));
						toSend.add(component.execute(() -> t.get(next), component.textHoverable("", " &3»", "")));
						msg.build(toSend.toArray(new BaseComponent[0]));
					}
				}).decorate((pagination, player, page, max, placement) -> msg.send("&7#&f" + placement + " &6" + player.getName() + " &f&l: &e" + new Date(player.getLastPlayed()).toInstant().atZone(ZoneId.systemDefault()).toLocalDate().toString())).get(1);
	}

The above example will provide you with this layout. You only ever need to call the first page and internal programming will take care of taking players to next possible pages.

Clone this wiki locally