Search in sources :

Example 66 with ITextComponent

use of net.minecraft.util.text.ITextComponent in project SpongeCommon by SpongePowered.

the class LegacyTexts method parseComponent.

public static TextComponentString parseComponent(TextComponentString component, char code) {
    String text = component.getText();
    int next = text.lastIndexOf(code, text.length() - 2);
    List<ITextComponent> parsed = null;
    if (next >= 0) {
        parsed = new ArrayList<>();
        TextComponentString current = null;
        boolean reset = false;
        int pos = text.length();
        do {
            TextFormatting format = parseFormat(text.charAt(next + 1));
            if (format != null) {
                int from = next + 2;
                if (from != pos) {
                    if (current != null) {
                        if (reset) {
                            parsed.add(current);
                            current.getStyle().setParentStyle(component.getStyle());
                            reset = false;
                            current = new TextComponentString("");
                        } else {
                            TextComponentString old = current;
                            current = new TextComponentString("");
                            current.appendSibling(old);
                        }
                    } else {
                        current = new TextComponentString("");
                    }
                    current.text = text.substring(from, pos);
                } else if (current == null) {
                    current = new TextComponentString("");
                }
                reset |= applyStyle(current.getStyle(), format);
                pos = next;
            }
            next = text.lastIndexOf(code, next - 1);
        } while (next != -1);
        if (current != null) {
            parsed.add(current);
            current.getStyle().setParentStyle(component.getStyle());
        }
        Collections.reverse(parsed);
        text = pos > 0 ? text.substring(0, pos) : "";
        if (component.getSiblings().isEmpty()) {
            TextComponentString newComponent = new TextComponentString(text);
            newComponent.getSiblings().addAll(parsed);
            newComponent.setStyle(component.getStyle());
            return newComponent;
        }
    } else if (component.getSiblings().isEmpty()) {
        return component;
    }
    TextComponentString newComponent = new TextComponentString(text);
    if (parsed != null) {
        newComponent.getSiblings().addAll(parsed);
    }
    newComponent.setStyle(component.getStyle());
    for (ITextComponent child : component.getSiblings()) {
        if (child instanceof TextComponentString) {
            child = parseComponent((TextComponentString) child, code);
        } else {
            child = child.createCopy();
        }
        newComponent.appendSibling(child);
    }
    return newComponent;
}
Also used : ITextComponent(net.minecraft.util.text.ITextComponent) TextFormatting(net.minecraft.util.text.TextFormatting) TextComponentString(net.minecraft.util.text.TextComponentString) TextComponentString(net.minecraft.util.text.TextComponentString)

Example 67 with ITextComponent

use of net.minecraft.util.text.ITextComponent in project Charset by CharsetMC.

the class CharsetTweakChat method onServerChat.

@SubscribeEvent
public void onServerChat(ServerChatEvent event) {
    if (!EntityUtils.isPlayerFake(event.getPlayer())) {
        ITextComponent message = event.getComponent();
        if (message instanceof TextComponentTranslation && ((TextComponentTranslation) message).getFormatArgs().length == 2 && "chat.type.text".equals(((TextComponentTranslation) message).getKey())) {
            TextComponentTranslation messageTr = (TextComponentTranslation) message;
            Object playerMessage = messageTr.getFormatArgs()[1];
            if (playerMessage instanceof TextComponentString) {
                String playerMsgStr = ((TextComponentString) playerMessage).getText();
                for (int i = 0; i < playerMsgStr.length() - 1; i++) {
                    if (enableGreentext) {
                        if (playerMsgStr.charAt(i) == '>' && (i == 0 || Character.isWhitespace(playerMsgStr.codePointAt(i - 1))) && Character.isLetterOrDigit(playerMsgStr.codePointAt(i + 1))) {
                            playerMsgStr = playerMsgStr.substring(0, i) + TextFormatting.GREEN + playerMsgStr.substring(i);
                            break;
                        }
                    }
                    if (enableColoredChat) {
                        if (playerMsgStr.charAt(i) == '&') {
                            String comp = "\u00a7" + playerMsgStr.charAt(i + 1);
                            for (TextFormatting formatting : TextFormatting.values()) {
                                if (comp.equals(formatting.toString())) {
                                    playerMsgStr = playerMsgStr.substring(0, i) + "\u00a7" + playerMsgStr.substring(i + 1);
                                    break;
                                }
                            }
                            break;
                        }
                    }
                }
                boolean isShout = false;
                if (enableChatRadius && chatRadiusEnableShout && playerMsgStr.startsWith("!")) {
                    isShout = true;
                    playerMsgStr = playerMsgStr.substring(1);
                }
                ITextComponent msgResult = new TextComponentTranslation(messageTr.getKey(), messageTr.getFormatArgs()[0], new TextComponentString(playerMsgStr));
                if (isShout) {
                    msgResult = new TextComponentTranslation("chat.charset.shout", shoutPrefix, msgResult);
                }
                if (enableChatRadius) {
                    event.setCanceled(true);
                    for (EntityPlayer player : event.getPlayer().getServerWorld().playerEntities) {
                        if (chatRadius <= 0 || player.getDistance(event.getPlayer()) <= chatRadius) {
                            player.sendMessage(msgResult);
                        }
                    }
                } else {
                    event.setComponent(msgResult);
                }
            }
        }
    }
}
Also used : TextComponentTranslation(net.minecraft.util.text.TextComponentTranslation) ITextComponent(net.minecraft.util.text.ITextComponent) TextFormatting(net.minecraft.util.text.TextFormatting) EntityPlayer(net.minecraft.entity.player.EntityPlayer) TextComponentString(net.minecraft.util.text.TextComponentString) TextComponentString(net.minecraft.util.text.TextComponentString) SubscribeEvent(net.minecraftforge.fml.common.eventhandler.SubscribeEvent)

Example 68 with ITextComponent

use of net.minecraft.util.text.ITextComponent in project SpongeVanilla by SpongePowered.

the class ChatFormatter method format.

@Nullable
public static ITextComponent format(String s) {
    Matcher matcher = URL_PATTERN.matcher(s);
    if (!matcher.find()) {
        return null;
    }
    ITextComponent result = null;
    int pos = 0;
    do {
        int start = matcher.start();
        int end = matcher.end();
        String displayUrl = s.substring(start, end);
        String url = displayUrl;
        try {
            if (new URI(url).getScheme() == null) {
                url = DEFAULT_SCHEME + url;
            }
        } catch (URISyntaxException e) {
            // Invalid URL so just ignore it
            continue;
        }
        if (pos < start) {
            if (result == null) {
                result = new TextComponentString(s.substring(pos, start));
            } else {
                result.appendText(s.substring(pos, start));
            }
        }
        pos = end;
        ITextComponent link = new TextComponentString(displayUrl);
        link.getStyle().setClickEvent(new ClickEvent(ClickEvent.Action.OPEN_URL, url));
        if (result == null) {
            result = new TextComponentString("");
        }
        result.appendSibling(link);
    } while (matcher.find());
    // If there is something left, append the rest
    if (pos < s.length()) {
        if (result == null) {
            result = new TextComponentString(s.substring(pos));
        } else {
            result.appendText(s.substring(pos));
        }
    }
    return result;
}
Also used : Matcher(java.util.regex.Matcher) ClickEvent(net.minecraft.util.text.event.ClickEvent) ITextComponent(net.minecraft.util.text.ITextComponent) TextComponentString(net.minecraft.util.text.TextComponentString) URISyntaxException(java.net.URISyntaxException) URI(java.net.URI) TextComponentString(net.minecraft.util.text.TextComponentString) Nullable(javax.annotation.Nullable)

Example 69 with ITextComponent

use of net.minecraft.util.text.ITextComponent in project SpongeVanilla by SpongePowered.

the class ChatFormatter method formatChatComponent.

public static void formatChatComponent(TextComponentTranslation component) {
    String message = (String) component.getFormatArgs()[1];
    ITextComponent formatted = format(message);
    if (formatted == null) {
        return;
    }
    formatted.getStyle().setParentStyle(component.getStyle());
    component.getFormatArgs()[1] = formatted;
}
Also used : ITextComponent(net.minecraft.util.text.ITextComponent) TextComponentString(net.minecraft.util.text.TextComponentString)

Example 70 with ITextComponent

use of net.minecraft.util.text.ITextComponent in project Cavern2 by kegare.

the class MiningAssistToast method getDescription.

@Override
protected ITextComponent getDescription() {
    ITextComponent component = new TextComponentTranslation("cavern.miningassist.name");
    component.getStyle().setColor(TextFormatting.BLACK);
    return component;
}
Also used : TextComponentTranslation(net.minecraft.util.text.TextComponentTranslation) ITextComponent(net.minecraft.util.text.ITextComponent)

Aggregations

ITextComponent (net.minecraft.util.text.ITextComponent)116 TextComponentString (net.minecraft.util.text.TextComponentString)53 TextComponentTranslation (net.minecraft.util.text.TextComponentTranslation)43 EntityPlayer (net.minecraft.entity.player.EntityPlayer)20 ItemStack (net.minecraft.item.ItemStack)17 Style (net.minecraft.util.text.Style)17 ClickEvent (net.minecraft.util.text.event.ClickEvent)16 HoverEvent (net.minecraft.util.text.event.HoverEvent)9 ArrayList (java.util.ArrayList)8 BlockPos (net.minecraft.util.math.BlockPos)8 SubscribeEvent (net.minecraftforge.fml.common.eventhandler.SubscribeEvent)8 Minecraft (net.minecraft.client.Minecraft)5 StringTextComponent (net.minecraft.util.text.StringTextComponent)4 Text (org.spongepowered.api.text.Text)4 MagicBook (cavern.magic.MagicBook)3 SpecialMagic (cavern.magic.SpecialMagic)3 Matcher (java.util.regex.Matcher)3 TileEntity (net.minecraft.tileentity.TileEntity)3 SoundEvent (net.minecraft.util.SoundEvent)3 TextFormatting (net.minecraft.util.text.TextFormatting)3