use of org.spongepowered.api.text.format.TextFormat in project SpongeAPI by SpongePowered.
the class Text method of.
/**
* Builds a {@link Text} from a given array of objects.
*
* <p>For instance, you can use this like
* <code>Text.of(TextColors.DARK_AQUA, "Hi", TextColors.AQUA, "Bye")</code>
* </p>
*
* <p>This will create the correct {@link Text} instance if the input object
* is the input for one of the {@link Text} types or convert the object to a
* string otherwise.</p>
*
* <p>For instances of type {@link TextRepresentable} (e.g. {@link Text},
* {@link Builder}, ...) the formatting of appended text has priority over
* the current formatting in the method, e.g. the following results in a
* green, then yellow and at the end green again {@link Text}:</p>
*
* <code>Text.of(TextColors.GREEN, "Hello ", Text.of(TextColors.YELLOW,
* "Spongie"), '!');</code>
*
* @param objects The object array
* @return The built text object
*/
public static Text of(Object... objects) {
// Shortcut for lonely TextRepresentables
if (objects.length == 1 && objects[0] instanceof TextRepresentable) {
return ((TextRepresentable) objects[0]).toText();
}
final Text.Builder builder = builder();
TextFormat format = TextFormat.NONE;
HoverAction<?> hoverAction = null;
ClickAction<?> clickAction = null;
ShiftClickAction<?> shiftClickAction = null;
boolean changedFormat = false;
for (Object obj : objects) {
// Text formatting + actions
if (obj instanceof TextFormat) {
changedFormat = true;
format = (TextFormat) obj;
} else if (obj instanceof TextColor) {
changedFormat = true;
format = format.color((TextColor) obj);
} else if (obj instanceof TextStyle) {
changedFormat = true;
format = format.style(obj.equals(TextStyles.RESET) ? TextStyles.NONE : format.getStyle().and((TextStyle) obj));
} else if (obj instanceof TextAction) {
changedFormat = true;
if (obj instanceof HoverAction) {
hoverAction = (HoverAction<?>) obj;
} else if (obj instanceof ClickAction) {
clickAction = (ClickAction<?>) obj;
} else if (obj instanceof ShiftClickAction) {
shiftClickAction = (ShiftClickAction<?>) obj;
} else {
// Unsupported TextAction
}
} else if (obj instanceof TextRepresentable) {
// Special content
changedFormat = false;
Text.Builder childBuilder = ((TextRepresentable) obj).toText().toBuilder();
// Merge format (existing format has priority)
childBuilder.format(format.merge(childBuilder.format));
// Overwrite text actions if *NOT* present
if (childBuilder.clickAction == null) {
childBuilder.clickAction = clickAction;
}
if (childBuilder.hoverAction == null) {
childBuilder.hoverAction = hoverAction;
}
if (childBuilder.shiftClickAction == null) {
childBuilder.shiftClickAction = shiftClickAction;
}
builder.append(childBuilder.build());
} else {
// Simple content
changedFormat = false;
Text.Builder childBuilder;
if (obj instanceof String) {
childBuilder = builder((String) obj);
} else if (obj instanceof Translation) {
childBuilder = builder((Translation) obj);
} else if (obj instanceof Translatable) {
childBuilder = builder(((Translatable) obj).getTranslation());
} else if (obj instanceof Selector) {
childBuilder = builder((Selector) obj);
} else if (obj instanceof Score) {
childBuilder = builder((Score) obj);
} else {
childBuilder = builder(String.valueOf(obj));
}
if (hoverAction != null) {
childBuilder.onHover(hoverAction);
}
if (clickAction != null) {
childBuilder.onClick(clickAction);
}
if (shiftClickAction != null) {
childBuilder.onShiftClick(shiftClickAction);
}
builder.append(childBuilder.format(format).build());
}
}
if (changedFormat) {
// Did the formatting change without being applied to something?
// Then just append an empty text with that formatting
final Text.Builder childBuilder = builder();
if (hoverAction != null) {
childBuilder.onHover(hoverAction);
}
if (clickAction != null) {
childBuilder.onClick(clickAction);
}
if (shiftClickAction != null) {
childBuilder.onShiftClick(shiftClickAction);
}
builder.append(childBuilder.format(format).build());
}
if (builder.children.size() == 1) {
// Single content, reduce Text depth
return builder.children.get(0);
}
return builder.build();
}
use of org.spongepowered.api.text.format.TextFormat in project SpongeAPI by SpongePowered.
the class TextTemplateConfigSerializer method parseArg.
private void parseArg(LiteralText source, List<Object> into) throws ObjectMappingException {
String name = unwrap(source.getContent());
boolean optional = this.root.getNode(NODE_ARGS, name, NODE_OPT).getBoolean();
Text defaultValue = this.root.getNode(NODE_ARGS, name, NODE_DEF_VAL).getValue(TypeToken.of(Text.class));
TextFormat format = source.getFormat();
into.add(TextTemplate.arg(name).format(format).optional(optional).defaultValue(defaultValue).build());
}
use of org.spongepowered.api.text.format.TextFormat in project core by CubeEngine.
the class HelpCommand method execute.
@Override
public boolean execute(CommandInvocation invocation) {
if (!(invocation.getCommandSource() instanceof CommandSource)) {
return false;
}
CommandDescriptor descriptor = helpTarget.getDescriptor();
CommandSource sender = (CommandSource) invocation.getCommandSource();
TextFormat formatGray = NONE.color(GRAY);
i18n.send(sender, formatGray, "Description: {input}", i18n.translate(sender, MessageType.NONE, descriptor.getDescription()).toPlain());
List<String> labels = new ArrayList<>(invocation.getLabels());
if (labels.isEmpty()) {
labels.add("");
}
if ("?".equals(labels.get(labels.size() - 1))) {
labels.remove(labels.size() - 1);
}
i18n.send(sender, formatGray, "Usage: {input}", descriptor.getUsage(invocation, labels.toArray(new String[labels.size()])));
sender.sendMessage(Text.of());
if (helpTarget instanceof DispatcherCommand) {
Set<CommandBase> commands = helpTarget.getCommands();
if (!commands.isEmpty() && (commands.size() != 1 || // is Empty ignoring HelpCommand
!(commands.iterator().next() instanceof HelpCommand))) {
i18n.send(sender, NEUTRAL, "The following sub-commands are available:");
sender.sendMessage(Text.of());
commands.stream().filter(command -> !(command instanceof HelpCommand || command instanceof AliasCommand && commands.contains(((AliasCommand) command).getTarget()))).filter(command -> !(command.getDescriptor() instanceof CubeCommandDescriptor && ((CubeCommandDescriptor) command.getDescriptor()).isCheckPerm() && !sender.hasPermission(((CubeCommandDescriptor) command.getDescriptor()).getPermission().getName()))).forEach(command -> sender.sendMessage(Text.of(YELLOW, command.getDescriptor().getName()).toBuilder().onClick(TextActions.runCommand("/" + (String.join(" ", labels) + " " + command.getDescriptor().getName()).trim() + " ?")).append(Text.of(WHITE, ": ", GRAY, i18n.translate(sender, TextFormat.NONE, command.getDescriptor().getDescription()))).build()));
sender.sendMessage(Text.of());
} else if (helpTarget instanceof ParametricContainerCommand) {
i18n.send(sender, MessageType.NEGATIVE, "No actions are available");
sender.sendMessage(Text.of());
}
}
/*
if (descriptor instanceof CubeDescriptor)
{
sender.sendTranslated(GRAY, "Detailed help: {input#link:color=INDIGO}", "http://engine.cubeisland.de/c/" + ((CubeDescriptor)descriptor).getModule().getInformation().getName().toLowerCase() + "/" + StringUtils.implode("/", labels));
}
*/
return true;
}
use of org.spongepowered.api.text.format.TextFormat in project LanternServer by LanternPowered.
the class LegacyTexts method toLegacy.
private static StringBuilder toLegacy(StringBuilder builder, Locale locale, Text text, char legacyChar, Style base, Style applied) {
if (legacyChar != 0) {
final TextFormat format = text.getFormat();
final TextColor color = format.getColor();
final TextStyle style = format.getStyle();
// Create a new style object
final Style newStyle = base.copyTo(new Style());
base = newStyle;
if (color != TextColors.NONE) {
newStyle.color = color == TextColors.RESET ? null : color;
}
style.isBold().ifPresent(value -> newStyle.bold = value);
style.isItalic().ifPresent(value -> newStyle.italic = value);
style.isObfuscated().ifPresent(value -> newStyle.obfuscated = value);
style.hasUnderline().ifPresent(value -> newStyle.underlined = value);
style.hasStrikethrough().ifPresent(value -> newStyle.strikethrough = value);
if ((applied.color != null && newStyle.color == null) || (applied.bold && !newStyle.bold) || (applied.italic && !newStyle.italic) || (applied.obfuscated && !newStyle.obfuscated) || (applied.underlined && !newStyle.underlined) || (applied.strikethrough && !newStyle.strikethrough)) {
builder.append(legacyChar).append(TextConstants.RESET);
if (newStyle.color != null) {
builder.append(legacyChar).append(((FormattingCodeHolder) newStyle.color).getCode());
}
if (newStyle.bold) {
builder.append(legacyChar).append(TextConstants.BOLD);
}
if (newStyle.italic) {
builder.append(legacyChar).append(TextConstants.ITALIC);
}
if (newStyle.obfuscated) {
builder.append(legacyChar).append(TextConstants.OBFUSCATED);
}
if (newStyle.underlined) {
builder.append(legacyChar).append(TextConstants.UNDERLINE);
}
if (newStyle.strikethrough) {
builder.append(legacyChar).append(TextConstants.STRIKETHROUGH);
}
} else {
if (applied.color != newStyle.color) {
builder.append(legacyChar).append(((FormattingCodeHolder) newStyle.color).getCode());
}
if (applied.bold != newStyle.bold) {
builder.append(legacyChar).append(TextConstants.BOLD);
}
if (applied.italic != newStyle.italic) {
builder.append(legacyChar).append(TextConstants.ITALIC);
}
if (applied.obfuscated != newStyle.obfuscated) {
builder.append(legacyChar).append(TextConstants.OBFUSCATED);
}
if (applied.underlined != newStyle.underlined) {
builder.append(legacyChar).append(TextConstants.UNDERLINE);
}
if (applied.strikethrough != newStyle.strikethrough) {
builder.append(legacyChar).append(TextConstants.STRIKETHROUGH);
}
}
newStyle.copyTo(applied);
}
if (text instanceof LiteralText) {
builder.append(((LiteralText) text).getContent());
} else if (text instanceof SelectorText) {
builder.append(((SelectorText) text).getSelector().toPlain());
} else if (text instanceof TranslatableText) {
final TranslatableText text0 = (TranslatableText) text;
final Translation translation = text0.getTranslation();
final ImmutableList<Object> args = text0.getArguments();
final Object[] args0 = new Object[args.size()];
for (int i = 0; i < args0.length; i++) {
Object object = args.get(i);
if (object instanceof Text || object instanceof Text.Builder || object instanceof TextRepresentable) {
if (object instanceof Text) {
// Ignore
} else if (object instanceof Text.Builder) {
object = ((Text.Builder) object).build();
} else {
object = ((TextRepresentable) object).toText();
}
args0[i] = toLegacy(new StringBuilder(), locale, (Text) object, legacyChar, base, applied).toString();
} else {
args0[i] = object;
}
}
builder.append(translation.get(locale, args0));
} else if (text instanceof ScoreText) {
final ScoreText text0 = (ScoreText) text;
final Optional<String> override = text0.getOverride();
if (override.isPresent()) {
builder.append(override.get());
} else {
builder.append(text0.getScore().getScore());
}
}
for (Text child : text.getChildren()) {
toLegacy(builder, locale, child, legacyChar, base, applied);
}
return builder;
}
Aggregations