Podział strony serwisu

Wskazówka

For a basic understanding of services, make sure you read Usługi first.

PaginationService służy do dzielenia zawartości na poszczególne strony. Serwis dostarcza PaginationList.Builder, w którym możesz określić atrybuty, takie jak tytuł, zawartość, nagłówek i wypełnienie. Jest to funkcja specyficzna dla Sponge.

Pagination List Builder

First obtain an instance of a PaginationList.Builder:

import org.spongepowered.api.service.pagination.PaginationList;

PaginationList.Builder builder = PaginationList.builder();

Istnieją dwa sposoby na określanie zawartości list stronicowanych:

  • Z Iterable<Component>

import net.kyori.adventure.text.Component;

import java.util.ArrayList;
import java.util.List;

List<Component> contents = new ArrayList<>();
contents.add(Component.text("Item 1"));
contents.add(Component.text("Item 2"));
contents.add(Component.text("Item 3"));

builder.contents(contents);

Informacja

If the Iterable is a List, then bidirectional navigation is supported. Otherwise, only forwards navigation is supported.

  • Z tablicą Component-ów

builder.contents(Component.text("Item 1"), Component.text("Item 2"), Component.text("Item 3"));

You can also specify various other components of a paginated list, such as a title, header, footer, and padding. The diagram below shows which component is displayed in each part of the paginated list. In the following diagram, the padding string is shown as the letter p.

pppppppppppppppppppppppp Title pppppppppppppppppppppppp
Header
Item 1
Item 2
Item 3
...
ppppppppppppppppppppppp < 2/3 > ppppppppppppppppppppppp
Footer

To achieve the preceding output, we might use the following builder pattern:

builder.title(Component.text("Title"))
    .contents(Component.text("Item 1"), Component.text("Item 2"), Component.text("Item 3"))
    .header(Component.text("Header"))
    .footer(Component.text("Footer"))
    .padding(Component.text("p"));

Informacja

With the exception of contents, all components of the paginated list are optional. However, a title is strongly recommended.

Na koniec, aby wysłać paginowaną listę do gracza lub innego obiektu Audience, użyj PaginationList.Builder#sendTo(Audience)

And that’s it! To recap, a fully functional paginated list could be generated and sent to a previously defined msgReceiver using the following code:

PaginationList.builder()
    .title(Component.text("Title"))
    .contents(Component.text("Item 1"), Component.text("Item 2"), Component.text("Item 3"))
    .header(Component.text("Header"))
    .footer(Component.text("Footer"))
    .padding(Component.text("p"))
    .sendTo(msgReceiver);