Récupérer et Parser des Placeholders
Avertissement
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!
Obtenir un PlaceholderParser
Les PlaceholderParsers sont stockés dans le registre Sponge, ce qui signifie qu’ils peuvent être obtenus de la même façon que n’importe quel autre CatalogType :
Sponge.getRegistry().getType(PlaceholderParser.class, id);
Astuce
N’oubliez pas que l’ID du PlaceholderParser
est de la forme pluginid:placeholderid
, par exemple sponge:name
.
Créer du Texte depuis un PlaceholderParser
A PlaceholderParser
requires a PlaceholderContext in order to generate an appropriate Component
object. PlaceholderContexts
can be created by using a PlaceholderContext.Builder obtained from the
PlaceholderContext#builder() method.
Ce builder permet de fournir le contexte facultatif suivant :
An associated object, allowing for the placeholder to modify its output (this will usually be a Player or other Audience)
Un string d’argument qu’un
PlaceholderParser
peut parser
Un PlaceholderContext
construit peut ensuite être fournit au ``PlaceholderParser` en utilisant PlaceholderParser#parse(PlaceholderContext).
Par exemple, si vous souhaitez inclure le nom d’un joueur en utilisant le parser sponge:name
, vous pouvez faire ceci :
Player player = ...;
// We know this exists
PlaceholderParser parser = PlaceholderParsers.NAME;
PlaceholderContext context = PlaceholderContext.builder()
.associatedObject(player)
.build();
Component text = parser.parse(context);
Si le nom du joueur est « SpongePlayer », le text retourné sera SpongePlayer
Inclure des Placeholders dans un Text
Placeholders can also be used in Component
and ComponentBuilder objects without parsing them
first. Sponge provides a PlaceholderComponent object that bundles a PlaceholderParser
and
PlaceholderContext
together.
To create a PlaceholderComponent
, use PlaceholderComponent#builder() and add the PlaceholderParser
and
PlaceholderContext
objects as appropriate. You can then use the built PlaceholderComponent
in the Component
objects.
If you wished to use the parser and context from the previous example in a Component
, you could write the following:
PlaceholderComponent placeholderText = PlaceholderComponent.builder().context(context).parser(parser).build();
Component result = Component.text("Hello! Your name is ")
.append(placeholderText)
.append(Component.text("!"));
Le texte dira « Hello! Your name is SpongePlayer! »
Note
A PlaceholderComponent
will be parsed as soon as it is added to a Component
or ComponentBuilder
using
Component#append(Component).