Daftar tab
Peringatan
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.
Untuk mendapatkan pemain TabList, anda hanya perlu menghubungi :javadoc:`Pemain#getTabList()` metode:
import org.spongepowered.api.entity.living.player.Player;
import org.spongepowered.api.entity.living.player.tab.TabList;
TabList tablist = player.getTabList();
Sekarang bahwa kita telah memperoleh TabList
, kita dapat memodifikasi beberapa komponen itu. Misalnya, untuk mengatur header atau footer TabList
, kita hanya perlu memanggil metode yang sesuai:
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"));
Catatan
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.
Tab Entri Daftar
Sekarang bahwa kita memiliki set header dan footer dari TabList
, kita juga dapat menambahkan entri ke daftar. contoh dari hal ini adalah yang ditunjukkan di bawah ini:
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 Manajer profil game article. To apply the entry to the
tab list, we simply need to call the TabList#addEntry(TabListEntry) method.
Kita dapat menyempurnakan dasar contoh kita dengan menentukan hal-hal seperti nama tampilan atau latency entri:
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.
Memodifikasi strategi saat ini
Menggunakan TabList
, kita dapat memperoleh entri saat ini di TabList
untuk kami modifikasi sendiri. Untuk mendapatkan entri tertentu, gunakan TabList#getEntry(UUID) metode. Metode ini akan kembali Optional.kosong() jika ditentukan UUID tidak dapat ditemukan. Contoh ditunjukkan di bawah ini:
import java.util.Optional;
Optional<TabListEntry> optional = tablist.getEntry(uuid);
if (optional.isPresent()) {
TabListEntry entry = optional.get();
}
Dengan ini, kita dapat menggunakan metode TabListEntry
untuk mengubah modus permainan, latency, dan nama tampilan entri:
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.
Jika kita tidak memiliki entri khusus untuk dimodifikasi , maka kita dapat iterate melalui semua `` TabListEntry`` s dalam `` TabList``. Kita hanya perlu memanggil metode : javadoc: TabList # getEntries () untuk mendapatkan `` Collection <TabListEntry> `` yang bisa kita iterasikan .