use of net.minecraft.network.chat.ClickEvent in project SpongeCommon by SpongePowered.
the class MetadataPanel method newChatWithLinks.
/*
* Credit: MinecraftForge
* Changes: Set ichat to link if ichat is null
*/
public static Component newChatWithLinks(final String string, final boolean allowMissingHeader) {
// Includes ipv4 and domain pattern
// Matches an ip (xx.xxx.xx.xxx) or a domain (something.com) with or
// without a protocol or path.
MutableComponent ichat = null;
final Matcher matcher = MetadataPanel.URL_PATTERN.matcher(string);
int lastEnd = 0;
// Find all urls
while (matcher.find()) {
final int start = matcher.start();
final int end = matcher.end();
// Append the previous left overs.
final String part = string.substring(lastEnd, start);
if (part.length() > 0) {
if (ichat == null) {
ichat = new TextComponent(part);
} else {
ichat.append(part);
}
}
lastEnd = end;
String url = string.substring(start, end);
final MutableComponent link = new TextComponent(url);
try {
// Add schema so client doesn't crash.
if ((new URI(url)).getScheme() == null) {
if (!allowMissingHeader) {
if (ichat == null) {
ichat = new TextComponent(url);
} else {
ichat.append(url);
}
continue;
}
url = "http://" + url;
}
} catch (final URISyntaxException e) {
// Bad syntax bail out!
if (ichat == null) {
ichat = new TextComponent(url);
} else {
ichat.append(url);
}
continue;
}
// Set the click event and append the link.
final ClickEvent click = new ClickEvent(ClickEvent.Action.OPEN_URL, url);
link.withStyle(style -> style.withClickEvent(click).withUnderlined(true).withColor(ChatFormatting.BLUE));
ichat = ichat == null ? link : ichat.append(link);
}
// Append the rest of the message.
final String end = string.substring(lastEnd);
if (ichat == null) {
ichat = new TextComponent(end);
} else if (end.length() > 0) {
ichat.append(string.substring(lastEnd));
}
return ichat;
}
use of net.minecraft.network.chat.ClickEvent in project SpongeCommon by SpongePowered.
the class StyleMixin method bridge$asAdventure.
@Override
@SuppressWarnings("ConstantConditions")
public net.kyori.adventure.text.format.Style bridge$asAdventure() {
if (this.bridge$adventure == null) {
final net.kyori.adventure.text.format.Style.Builder builder = net.kyori.adventure.text.format.Style.style();
final Style $this = (Style) (Object) this;
final StyleAccessor $access = (StyleAccessor) this;
// font
builder.font(SpongeAdventure.asAdventure($access.accessor$font()));
// color
builder.color(SpongeAdventure.asAdventure($this.getColor()));
// decorations
builder.decoration(TextDecoration.OBFUSCATED, TextDecoration.State.byBoolean($access.accessor$obfuscated()));
builder.decoration(TextDecoration.BOLD, TextDecoration.State.byBoolean($access.accessor$bold()));
builder.decoration(TextDecoration.STRIKETHROUGH, TextDecoration.State.byBoolean($access.accessor$strikethrough()));
builder.decoration(TextDecoration.UNDERLINED, TextDecoration.State.byBoolean($access.accessor$underlined()));
builder.decoration(TextDecoration.ITALIC, TextDecoration.State.byBoolean($access.accessor$italic()));
// events
final HoverEvent hoverEvent = $this.getHoverEvent();
if (hoverEvent != null) {
builder.hoverEvent(SpongeAdventure.asAdventure(hoverEvent));
}
final ClickEvent clickEvent = $this.getClickEvent();
if (clickEvent != null) {
builder.clickEvent(net.kyori.adventure.text.event.ClickEvent.clickEvent(SpongeAdventure.asAdventure(clickEvent.getAction()), clickEvent.getValue()));
}
// insertion
builder.insertion($this.getInsertion());
this.bridge$adventure = builder.build();
}
return this.bridge$adventure;
}
use of net.minecraft.network.chat.ClickEvent in project SpongeCommon by SpongePowered.
the class ChatFormatter method format.
@Nullable
public static Component format(final String s) {
final Matcher matcher = ChatFormatter.URL_PATTERN.matcher(s);
if (!matcher.find()) {
return null;
}
MutableComponent result = null;
int pos = 0;
do {
final int start = matcher.start();
final int end = matcher.end();
final String displayUrl = s.substring(start, end);
String url = displayUrl;
try {
if (new URI(url).getScheme() == null) {
url = ChatFormatter.DEFAULT_SCHEME + url;
}
} catch (final URISyntaxException e) {
// Invalid URL so just ignore it
continue;
}
if (pos < start) {
if (result == null) {
result = new TextComponent(s.substring(pos, start));
} else {
result.append(s.substring(pos, start));
}
}
pos = end;
final MutableComponent link = new TextComponent(displayUrl);
link.setStyle(link.getStyle().withClickEvent(new ClickEvent(ClickEvent.Action.OPEN_URL, url)));
if (result == null) {
result = new TextComponent("");
}
result.append(link);
} while (matcher.find());
// If there is something left, append the rest
if (pos < s.length()) {
if (result == null) {
result = new TextComponent(s.substring(pos));
} else {
result.append(s.substring(pos));
}
}
return result;
}
Aggregations