Search in sources :

Example 16 with TextColor

use of org.spongepowered.api.text.format.TextColor 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;
}
Also used : TranslatableText(org.spongepowered.api.text.TranslatableText) TextRepresentable(org.spongepowered.api.text.TextRepresentable) SelectorText(org.spongepowered.api.text.SelectorText) Translation(org.spongepowered.api.text.translation.Translation) Optional(java.util.Optional) ScoreText(org.spongepowered.api.text.ScoreText) LiteralText(org.spongepowered.api.text.LiteralText) ScoreText(org.spongepowered.api.text.ScoreText) SelectorText(org.spongepowered.api.text.SelectorText) Text(org.spongepowered.api.text.Text) TranslatableText(org.spongepowered.api.text.TranslatableText) TextStyle(org.spongepowered.api.text.format.TextStyle) TextFormat(org.spongepowered.api.text.format.TextFormat) TextStyle(org.spongepowered.api.text.format.TextStyle) TextColor(org.spongepowered.api.text.format.TextColor) LiteralText(org.spongepowered.api.text.LiteralText)

Example 17 with TextColor

use of org.spongepowered.api.text.format.TextColor in project Nucleus by NucleusPowered.

the class NameUtil method getName.

/**
 * Gets the display name from a {@link User} as Sponge sees it.
 *
 * @param player The {@link User} to get the data from.
 * @return The {@link Text}
 */
public Text getName(User player) {
    Preconditions.checkNotNull(player);
    TextColor tc = getNameColour(player);
    TextStyle ts = getNameStyle(player);
    Optional<Text> dname = player.getPlayer().map(x -> x.get(Keys.DISPLAY_NAME).orElse(null));
    Text.Builder tb = null;
    if (plugin != null && plugin.isModuleLoaded(NicknameModule.ID)) {
        Optional<ModularUserService> userService = plugin.getUserDataManager().get(player);
        if (userService.isPresent()) {
            Optional<Text> n = userService.get().get(NicknameUserDataModule.class).getNicknameWithPrefix();
            if (n.isPresent()) {
                tb = n.get().toBuilder();
            }
        }
    } else if (dname.isPresent()) {
        tb = dname.get().toBuilder();
    }
    if (tb == null) {
        tb = Text.builder(player.getName());
    }
    tb.onHover(TextActions.showText(Nucleus.getNucleus().getMessageProvider().getTextMessageWithFormat("name.hover.ign", player.getName()))).build();
    if (tc != TextColors.NONE && tb.getColor() == TextColors.NONE) {
        List<Text> lt = tb.getChildren();
        if (lt.isEmpty() || lt.get(0).getColor().equals(TextColors.NONE)) {
            tb.color(tc);
        }
    }
    if (!ts.isEmpty()) {
        tb.style(ts);
    }
    return tb.build();
}
Also used : TextStyle(org.spongepowered.api.text.format.TextStyle) Text(org.spongepowered.api.text.Text) TextColor(org.spongepowered.api.text.format.TextColor) ModularUserService(io.github.nucleuspowered.nucleus.dataservices.modular.ModularUserService) NicknameUserDataModule(io.github.nucleuspowered.nucleus.modules.nickname.datamodules.NicknameUserDataModule)

Example 18 with TextColor

use of org.spongepowered.api.text.format.TextColor in project Nucleus by NucleusPowered.

the class ListPowertoolCommand method from.

private Text from(final PowertoolUserDataModule inu, Player src, String powertool, List<String> commands) {
    Optional<ItemType> oit = Sponge.getRegistry().getType(ItemType.class, powertool);
    MessageProvider mp = plugin.getMessageProvider();
    // Create the click actions.
    ClickAction viewAction = TextActions.executeCallback(pl -> Util.getPaginationBuilder(src).title(mp.getTextMessageWithFormat("command.powertool.ind.header", powertool)).padding(Text.of(TextColors.GREEN, "-")).contents(commands.stream().map(x -> Text.of(TextColors.YELLOW, x)).collect(Collectors.toList())).sendTo(src));
    ClickAction deleteAction = TextActions.executeCallback(pl -> {
        inu.clearPowertool(powertool);
        pl.sendMessage(mp.getTextMessageWithFormat("command.powertool.removed", powertool));
    });
    TextColor tc = oit.map(itemType -> TextColors.YELLOW).orElse(TextColors.GRAY);
    // id - [View] - [Delete]
    return Text.builder().append(Text.of(tc, powertool)).append(Text.of(" - ")).append(Text.builder(mp.getMessageWithFormat("standard.view")).color(TextColors.YELLOW).onClick(viewAction).build()).append(Text.of(" - ")).append(Text.builder(mp.getMessageWithFormat("standard.delete")).color(TextColors.DARK_RED).onClick(deleteAction).build()).build();
}
Also used : NoModifiers(io.github.nucleuspowered.nucleus.internal.annotations.command.NoModifiers) CommandResult(org.spongepowered.api.command.CommandResult) ClickAction(org.spongepowered.api.text.action.ClickAction) TextActions(org.spongepowered.api.text.action.TextActions) Nucleus(io.github.nucleuspowered.nucleus.Nucleus) RegisterCommand(io.github.nucleuspowered.nucleus.internal.annotations.command.RegisterCommand) Sponge(org.spongepowered.api.Sponge) NonnullByDefault(org.spongepowered.api.util.annotation.NonnullByDefault) PowertoolUserDataModule(io.github.nucleuspowered.nucleus.modules.powertool.datamodules.PowertoolUserDataModule) Collectors(java.util.stream.Collectors) RunAsync(io.github.nucleuspowered.nucleus.internal.annotations.RunAsync) List(java.util.List) AbstractCommand(io.github.nucleuspowered.nucleus.internal.command.AbstractCommand) MessageProvider(io.github.nucleuspowered.nucleus.internal.messages.MessageProvider) CommandContext(org.spongepowered.api.command.args.CommandContext) Text(org.spongepowered.api.text.Text) TextColor(org.spongepowered.api.text.format.TextColor) Map(java.util.Map) Optional(java.util.Optional) Util(io.github.nucleuspowered.nucleus.Util) Player(org.spongepowered.api.entity.living.player.Player) Permissions(io.github.nucleuspowered.nucleus.internal.annotations.command.Permissions) ItemType(org.spongepowered.api.item.ItemType) TextColors(org.spongepowered.api.text.format.TextColors) MessageProvider(io.github.nucleuspowered.nucleus.internal.messages.MessageProvider) ClickAction(org.spongepowered.api.text.action.ClickAction) ItemType(org.spongepowered.api.item.ItemType) TextColor(org.spongepowered.api.text.format.TextColor)

Example 19 with TextColor

use of org.spongepowered.api.text.format.TextColor in project Skree by Skelril.

the class PatientXInstance method sendAttackBroadcast.

protected void sendAttackBroadcast(String message, AttackSeverity severity) {
    TextColor color;
    switch(severity) {
        case INFO:
            color = TextColors.YELLOW;
            break;
        case ULTIMATE:
            color = TextColors.DARK_RED;
            break;
        default:
            color = TextColors.RED;
            break;
    }
    getPlayerMessageChannel(SPECTATOR).send(Text.of(color, message));
}
Also used : TextColor(org.spongepowered.api.text.format.TextColor)

Example 20 with TextColor

use of org.spongepowered.api.text.format.TextColor in project Skree by Skelril.

the class JungleRaidEffectListener method onPlayerInteract.

@Listener
public void onPlayerInteract(InteractBlockEvent.Secondary.MainHand event, @First Player player) {
    Optional<JungleRaidInstance> optInst = manager.getApplicableZone(player);
    if (!optInst.isPresent()) {
        return;
    }
    JungleRaidInstance inst = optInst.get();
    Optional<ItemStack> optStack = player.getItemInHand(HandTypes.MAIN_HAND);
    if (!optStack.isPresent()) {
        return;
    }
    ItemStack stack = optStack.get();
    if (stack.getItem() == ItemTypes.COMPASS) {
        event.setUseBlockResult(Tristate.FALSE);
        if (inst.getState() == JungleRaidState.IN_PROGRESS && inst.isFlagEnabled(JungleRaidFlag.ENHANCED_COMPASS)) {
            Set<Text> resultSet = new HashSet<>();
            for (Player aPlayer : inst.getPlayers(PlayerClassifier.PARTICIPANT)) {
                // Check validity
                if (player.equals(aPlayer)) {
                    continue;
                }
                // Check team
                if (inst.isFriendlyFire(player, aPlayer)) {
                    continue;
                }
                TextColor color = tf(player).canEntityBeSeen(tf(aPlayer)) ? TextColors.DARK_RED : TextColors.RED;
                resultSet.add(Text.of(color, aPlayer.getName(), " - ", player.getLocation().getPosition().distance(aPlayer.getLocation().getPosition())));
            }
            if (resultSet.isEmpty()) {
                player.sendMessage(Text.of(TextColors.RED, "No players found."));
            }
            player.sendMessage(Text.of(TextColors.YELLOW, "Player - Distance"));
            player.sendMessages(resultSet);
        } else if (inst.getState() == JungleRaidState.INITIALIZE) {
            player.setLocation(inst.getRandomLocation());
        }
    }
}
Also used : Player(org.spongepowered.api.entity.living.player.Player) Text(org.spongepowered.api.text.Text) TextColor(org.spongepowered.api.text.format.TextColor) ItemStack(org.spongepowered.api.item.inventory.ItemStack) ItemStackFactory.newItemStack(com.skelril.nitro.item.ItemStackFactory.newItemStack) HashSet(java.util.HashSet) Listener(org.spongepowered.api.event.Listener)

Aggregations

TextColor (org.spongepowered.api.text.format.TextColor)21 Text (org.spongepowered.api.text.Text)9 TextStyle (org.spongepowered.api.text.format.TextStyle)6 TextColors (org.spongepowered.api.text.format.TextColors)4 List (java.util.List)3 Map (java.util.Map)3 Optional (java.util.Optional)3 Collectors (java.util.stream.Collectors)3 LanternDisplaySlot (org.lanternpowered.server.scoreboard.LanternDisplaySlot)3 Sponge (org.spongepowered.api.Sponge)3 Player (org.spongepowered.api.entity.living.player.Player)3 ItemStack (org.spongepowered.api.item.inventory.ItemStack)3 Score (org.spongepowered.api.scoreboard.Score)3 IOException (java.io.IOException)2 Files (java.nio.file.Files)2 Path (java.nio.file.Path)2 ArrayList (java.util.ArrayList)2 HashMap (java.util.HashMap)2 Iterator (java.util.Iterator)2 Set (java.util.Set)2