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;
}
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);
}
}
}
}
}
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;
}
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;
}
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;
}
Aggregations