Listas de Tabulaciones

Advertencia

These docs were written for SpongeAPI 7 and are likely out of date. If you feel like you can help update them, please submit a PR!

Tab lists are used in Minecraft to display the list of players currently on a server. SpongeAPI allows for manipulation of the tab list on a per-player basis.

Para obtener una TabList de jugador, simplemente necesita llamar al método Player#getTabList():

import org.spongepowered.api.entity.living.player.Player;
import org.spongepowered.api.entity.living.player.tab.TabList;

TabList tablist = player.getTabList();

Ahora que hemos obtenido la TabList, podemos modificar varios componentes de ella. Por ejemplo: para establecer el encabezado o pie de página de la TabList, simplemente necesitamos llamar sus métodos apropiados:

import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.format.NamedTextColor;

tablist.setHeader(Component.text("The tab list header", NamedTextColor.GOLD));
tablist.setFooter(Component.text("The tab list footer", NamedTextColor.RED));

We can call the TabList#setHeaderAndFooter(Component, Component) method if we want to alter both of them at once:

tablist.setHeaderAndFooter(Component.text("header"), Component.text("footer"));

Nota

If you are wanting to alter the tab list header and footer, it is recommended to use the setHeaderAndFooter() method over individually calling the TabList#setHeader(Component) and TabList#setFooter(Component) methods, as it only sends one packet instead of two separate packets for the header and the footer.

Entradas de la Lista de Tabulación

Ahora que hemos establecido el encabezado y el pie de página de la TabList, también podemos agregar nuestra propia entrada a la lista. Un ejemplo de esto se muestra a continuación:

import org.spongepowered.api.entity.living.player.gamemode.GameModes;
import org.spongepowered.api.entity.living.player.tab.TabListEntry;
import org.spongepowered.api.profile.GameProfile;

TabListEntry entry = TabListEntry.builder()
    .list(tablist)
    .gameMode(GameModes.SURVIVAL)
    .profile(gameProfile)
    .build();
tablist.addEntry(entry);

Now let’s break this down. We set the list associated with the TabListEntry to our specified TabList using the TabListEntry.Builder#list(TabList) method. We then set the game mode of our entry to GameModes#SURVIVAL. The game mode of our entry is used to determine various things. On the client, it is used to determine if a player is in creative or perhaps a spectator. If the game mode is spectator, then their name will also appear gray and italicized. We then need to specify the GameProfile that the entry is associated with. The GameProfile may be constructed using the GameProfile#of() method, or it can be obtained from a real profile, such as a player. For more information, see the Administrador de Perfiles del Juego article. To apply the entry to the tab list, we simply need to call the TabList#addEntry(TabListEntry) method.

Podemos desarrollar nuestro ejemplo básico para cosas específicas como el nombre para mostrar o el estado latente de la entrada:

TabListEntry entry = TabListEntry.builder()
    .list(tablist)
    .displayName(Component.text("Spongie"))
    .latency(0)
    .profile(gameProfile)
    .build();
tablist.addEntry(entry);

Here, we set the display name that our entry will appear under to Spongie using the TabListEntry.Builder#displayName(Component) method. We then set the latency for our TabListEntry to five bars. See the TabListEntry#setLatency(int) method for more information on how to specify other types of bars for our entry.

Modificación de Entradas Actuales

Utilizando la TabList, podemos obtener actualmente entradas en la TabList para nuestra propia modificación. Para obtener una entrada específica, utilice el método TabList#getEntry(UUID). Este método devolverá Optional.empty() si el UUID especificado no puede ser encontrado. Un ejemplo se muestra a continuación:

import java.util.Optional;

Optional<TabListEntry> optional = tablist.getEntry(uuid);
if (optional.isPresent()) {
    TabListEntry entry = optional.get();
}

Con esto, podemos utilizar los métodos en TabListEntry para modificar el modo de juego, la latencia y el nombre para mostrar de la entrada:

entry.setDisplayName(Component.text("Pretender Spongie"));
entry.setLatency(1000);
entry.setGameMode(GameModes.SPECTATOR);

As an alternative to getting entries, we can also remove a specified entry. We must simply call the TabList#removeEntry(UUID) method, specifying the UUID of the entry that we wish to remove. As with getEntry(UUID), this will return Optional.empty() if the specified UUID cannot be found.

Si no tenemos una entrada específica para modificar, entonces podemos iterar por medio de todas las TabListEntrys en una TabList. Solo necesitamos llamar al método TabList#getEntries() para obtener una Collection<TabListEntry> que podemos iterar.