Texte erstellen

The Text API is used to create formatted text, which can be sent to players in chat messages, and can also be used in places such as books and signs.

Unformatierter Text

In den meisten Fällen benötigt man nur unformatierten Text. Unformatierter Text muss nicht mit einem Text-Builder erstellt werden und ist die simpelste Form der Texterstellung.

Beispiel:

import org.spongepowered.api.text.Text;

Text unformattedText = Text.of("Hey! This is unformatted text!");

Der oben gezeigte Beispielcode gibt ungefärbten, unformatierten test ohne text actions zurück.

Text Builder

Das Text-Builder Interface erlaubt dir das Erstellen von formatiertem Text mit Hilfe von Bausteinen (engl. building-block style).

Tipp

Read this Wikipedia article for help understanding the purpose of the builder pattern in software design.

Farben

Ein möglicher Verwendungszweck ist das Hinzufügen von Farbe zu einem Text, wie unten dargestellt.

Beispiel: Farbiger Text

import org.spongepowered.api.text.format.TextColors;

Text coloredText = Text.builder("Woot! Golden text is golden.").color(TextColors.GOLD).build();

Any color specified within the TextColors class can be used when coloring text. Multiple colors can be used in text by appending additional texts with different colors:

Beispiel: Mehrfarbiger Text

Text multiColoredText = Text.builder("Sponges are ").color(TextColors.YELLOW).append(
        Text.builder("invincible!").color(TextColors.RED).build()).build();

Styling

Der Builder kann außerdem für die Textformatierung, also zum Beispiel Unterstreichung, Kursivdruck etc, genutzt werden.

Beispiel: Gestylter Text

import org.spongepowered.api.text.format.TextStyles;

Text styledText = Text.builder("Yay! Styled text!").style(TextStyles.ITALIC).build();

Genau wie schon bei den Farben gezeigt, kann auch die Textformatierung Verkettet werden.

Beispiel: Mehrfarbiger Text

Text multiStyledText = Text.builder("I'm italicized! ").style(TextStyles.ITALIC)
        .append(Text.builder("I'm bold!").style(TextStyles.BOLD).build()).build();

Coloring & Styling Shortcut

The Text#of(Object…) method provides a simple way to add color and styling to your text in a much more concise way.

Example: Color & Style Shortcut

Text colorAndStyleText = Text.of(TextColors.RED, TextStyles.ITALIC, "Shortcuts for the win!");

Text Aktionen

The text builder also offers the ability to create actions for text. Any action specified within the TextActions class can be used when creating text actions for text. The method below is a small example of what text actions can do.

Beispiel: Text mit einer Aktion

import org.spongepowered.api.text.action.TextActions;

Text clickableText = Text.builder("Click here!").onClick(TextActions.runCommand("tell Spongesquad I'm ready!")).build();

In der oben gezeigten Methode kann der Spieler auf den „Click here!“ Text drücken, um einen Befehl auszuführen.

Bemerkung

Some text actions, such as TextActions#changePage(int), can only be used with book items.

Tipp

Just like with colors, multiple actions can be appended to text. Text actions can even be used in tandem with colors because of the builder pattern interface.

Selektoren

Ziel-Selektoren werden genutzt, um bestimmte Spieler oder Entitys auszuwählen, die eine bestimmte Bedingung erfüllen. Ziel-Selektoren habe teilweise Nutzen in Plugins für Minispiele, werden aber für viele Anwendungsbereiche benötigt.

Tipp

Read this Minecraft wiki article for help understanding what target selectors are in Minecraft, and how to use them.

To use selectors in text, you must use the Selector.Builder interface. This is illustrated in the example below.

Example: Selector-generated Text

import org.spongepowered.api.text.selector.Selector;

Text adventurers = Text.builder("These players are in adventure mode: ").append(
        Text.of(Selector.parse("@a[m=2]"))
).build();

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 Text will be returned containing the usernames of every online player who is in adventure mode.