Retrieving and Parsing Placeholders
Предупреждение
Эти документы были написаны для SpongeAPI 7 и, вероятно, устаревшие. Если вы чувствуете, что вы можете помочь обновить их, пожалуйста, отправьте PR!
Obtaining a PlaceholderParser
PlaceholderParsers are stored in the Sponge registry, meaning they can be obtained the same way as any other CatalogType:
Sponge.getRegistry().getType(PlaceholderParser.class, id);
Совет
Remember that the PlaceholderParser ID is of the form pluginid:placeholderid, for example sponge:name.
Creating Text from a PlaceholderParser
PlaceholderParser требует PlaceholderContext для создания соответствующего ему объекта Component. PlaceholderContexts можно создать с помощью :javadoc:«PlaceholderContext.Builder», полученного вызовом метода :javadoc:«PlaceholderContext#builder()».
The builder allows for the following optional context to be provided:
Дополнительный объект, позволяющий плейсхолдеру изменять результирующий текст (обычно это Player или иной объект Audience)
An argument string that a
PlaceholderParsercan parse
A built PlaceholderContext can then be supplied to the PlaceholderParser by using
PlaceholderParser#parse(PlaceholderContext).
For example, if you wish to include a player’s name using the sponge:name parser, you could do the following:
Player player = ...;
// We know this exists
PlaceholderParser parser = PlaceholderParsers.NAME;
PlaceholderContext context = PlaceholderContext.builder()
.associatedObject(player)
.build();
Component text = parser.parse(context);
If the player name is «SpongePlayer», the returned text will say SpongePlayer
Including Placeholders in Text
Плейсхолдеры также могут быть использованы в объектах Component и ComponentBuilder до их парсинга. Sponge предоставляет :javadoc:объект PlaceholderComponent, который связывает PlaceholderParser и PlaceholderContext.
Чтобы создать PlaceholderComponent воспользуйтесь PlaceholderComponent#builder() и соответственно добавьте PlaceholderParser и PlaceholderContext. Вы можете использовать созданный PlaceholderComponent внутри``Component`` объектов.
Если вы хотите использовать парсер и контекст из предыдущего примера в Component, вы можете воспользоваться следующим кодом:
PlaceholderComponent placeholderText = PlaceholderComponent.builder().context(context).parser(parser).build();
Component result = Component.text("Hello! Your name is ")
.append(placeholderText)
.append(Component.text("!"));
The text will say «Hello! Your name is SpongePlayer!»
Примечание
PlaceholderComponent будет обработан, как только он будет добавлен в Component или ComponentBuilder, используя Component#append(Component).