Metin oluşturma
Formatted text can be created using Component factories as described in this section. The robust text API can be used in a variety of ways to combine styling, coloring, and actions.
Biçimlendirilmemiş Metin
Oftentimes, all you need is unformatted text. Unformatted text is the simplest form of text to create.
Örnek:
import net.kyori.adventure.text.Component;
Component unformattedText = Component.text("Hey! This is unformatted text!");
Aşağıda gösterilan bu kod alıntısı, renksiz, biçimlenmemiş ve text actions ile ayarlanmış metin dönecektir.
Formatted Text
The Component interface allows for the creation of formatted text in a “building-block” style.
Components in Adventure act like builders where any Component can be amended with colors, decorations, etc.
Components are immutable, so using these builder-like methods will create a copy each time.
There is no need to call .build()
at the end, unlike with Sponge’s previous Text API from API 7 and below.
Tüyo
Yazılım tasarımındaki, geliştirici modelinin amacını anlamak için bunu okuyun: Wikipedia article <https://en.wikipedia.org/wiki/Builder_pattern>.
Renkler
One usage of the text API is the addition of colors to text, as illustrated below.
Örnek: renkli metin
import net.kyori.adventure.text.format.NamedTextColor;
Component coloredText = Component.text("Woot! Golden text is golden.", NamedTextColor.GOLD);
Any Minecraft color specified within the NamedTextColor class can be used when coloring text, as well as full-RGB colors using TextColor. Multiple colors can be used in text by appending additional texts with different colors:
Örnek: Çok renkli Metin
import net.kyori.adventure.text.format.TextColor;
Component multiColoredText = Component.text("Sponges are ", NamedTextColor.YELLOW).append(
Component.text("invincible!", TextColor.color(0x5fb0ff)));
Stil
The API can also be used to style text, including underlining, italicizing, etc. This is called “decorating” in the Adventure library.
Örnek: Stillendirilmiş Yazı
import net.kyori.adventure.text.format.TextDecoration;
Component styledText = Component.text("Yay! Styled text!").decorate(TextDecoration.ITALIC);
Component shortcutText = Component.text("Shortcut for both!", NamedTextColor.GRAY, TextDecoration.UNDERLINE);
Renklerle olduğu gibi, birden fazla stil zincirleme bağlanarak, ayrı stillendirilmiş textlerle kullanılabilir.
Örnek: Çok stilli Metin
Component multiStyledText = Component.text("I'm italicized! ").decorate(TextDecoration.ITALIC)
.append(Component.text("I'm bold!").decorate(TextDecoration.BOLD));
Text Events
Components also offer the ability to create actions for text. Any action within the HoverEvent or ClickEvent classes can be used when creating text actions for text. Sponge provides additional actions in SpongeComponents. The method below is a small example of what text actions can do.
Example: Text with an Event
import net.kyori.adventure.text.event.ClickEvent;
Component clickableText = Component.text("Click here!")
.clickEvent(ClickEvent.runCommand("tell Spongesquad I'm ready!"));
Component callbackText = Component.text("Click here too!")
.clickEvent(SpongeComponents.executeCallback(
cause -> cause.audience().sendMessage(Component.text("You clicked!"))));
Yukarıdaki metotta, oyuncular belirtilmiş komutu çalıştırmak için “Buraya Tıkla!” yazısını tıklayabilirler.
Not
Some text actions, such as ClickEvent#changePage(int), can only be used with book items.
Tüyo
Aynı renkler gibi, birden fazla aksiyon metinlere uygulanabilir. Geliştirici Model Arayüzü sayesinde, metin aksiyonları renklerle birlikte kullanılabilir de.
Seçiciler
Hedef seçiciler, oyunları veya belli kriterlere uyan varlıkları hedef almak için kullanılır. Hedef seçiciler küçük eklentiler yaratırken kullanışlıdır ama çok geniş aralıkta uygulamaları vardır.
Tüyo
Read this Minecraft wiki article for help understanding what target selectors are in Minecraft, and how to use them.
To use selectors in text, there is a component factory for Selectors. This is illustrated in the example below.
Örnek: Seçici-üretilmiş Metin
Component adventurers = Component.text("These players are in adventure mode: ")
.append(Component.selector("@a[m=2]"));
In this example, the target selector @a[m=2]
is targeting every online player who is in adventure mode. When the
method is called, a Component will be returned with a selector of all the usernames of every online player who is in adventure mode.
To send this selector to players, the selector will have to be expanded to the real usernames using SpongeComponents#resolve
.
Text Builders
Mutable builder classes exist for every kind of Component
.
These are available should you need a mutable representation, and they are also used in a few convenience methods
throughout the library to expose parts of an existing Component for editing.
Example: Text Component builder
Component weBuiltThisText = Component.text()
.content("with Sponge and Flard")
.color(NamedTextColor.YELLOW)
.decorate(TextDecoration.ITALIC, TextDecoration.BOLD)
.build()
Example: Editing text with a StyleBuilder
Component limitedEdition = weBuiltThisText.style(
styleBuilder -> styleBuilder.decorate(TextDecoration.UNDERLINE));