papermc/CraftBukkit-Patches/0066-Fix-links-in-chat.patch

105 lines
4.5 KiB
Diff
Raw Normal View History

2013-12-11 18:09:41 +00:00
From e36d608f7f0a8fc2ea06b381311a394f8d6ed123 Mon Sep 17 00:00:00 2001
2013-12-01 10:35:33 +00:00
From: Thinkofdeath <purggames@gmail.com>
Date: Sun, 1 Dec 2013 10:33:55 +0000
Subject: [PATCH] Fix links in chat
diff --git a/src/main/java/org/bukkit/craftbukkit/util/CraftChatMessage.java b/src/main/java/org/bukkit/craftbukkit/util/CraftChatMessage.java
2013-12-11 18:09:41 +00:00
index cc8e715..5607df1 100644
2013-12-01 10:35:33 +00:00
--- a/src/main/java/org/bukkit/craftbukkit/util/CraftChatMessage.java
+++ b/src/main/java/org/bukkit/craftbukkit/util/CraftChatMessage.java
@@ -3,11 +3,15 @@ package org.bukkit.craftbukkit.util;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
import net.minecraft.server.ChatComponentText;
import net.minecraft.server.ChatModifier;
import net.minecraft.server.EnumChatFormat;
import net.minecraft.server.IChatBaseComponent;
+import net.minecraft.server.ChatClickable;
+import net.minecraft.server.EnumClickAction;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.ImmutableMap.Builder;
2013-12-04 14:41:40 +00:00
@@ -29,6 +33,8 @@ public final class CraftChatMessage {
2013-12-01 10:35:33 +00:00
private ChatModifier modifier = new ChatModifier();
private StringBuilder builder = new StringBuilder();
private final IChatBaseComponent[] output;
2013-12-04 14:41:40 +00:00
+ private static final Pattern url = Pattern.compile("^(\u00A7.)*?((?:(https?)://)?([-\\w_\\.]{2,}\\.[a-z]{2,4})(/\\S*?)?)(\u00A7.)*?$");
+ private int lastWord = 0;
2013-12-01 10:35:33 +00:00
private FromString(String message) {
if (message == null) {
2013-12-09 10:38:44 +00:00
@@ -38,10 +44,14 @@ public final class CraftChatMessage {
2013-12-01 20:36:51 +00:00
list.add(currentChatComponent);
2013-12-01 10:35:33 +00:00
EnumChatFormat format = null;
+ Matcher matcher = url.matcher(message);
2013-12-04 14:41:40 +00:00
+ lastWord = 0;
2013-12-01 10:35:33 +00:00
for (int i = 0; i < message.length(); i++) {
char currentChar = message.charAt(i);
2013-12-04 14:41:40 +00:00
if (currentChar == '\u00A7' && (i < (message.length() - 1)) && (format = formatMap.get(message.charAt(i + 1))) != null) {
2013-12-09 10:38:44 +00:00
+ checkUrl(matcher, message, i);
+ lastWord++;
2013-12-04 14:41:40 +00:00
if (builder.length() > 0) {
appendNewComponent();
}
2013-12-11 18:09:41 +00:00
@@ -73,11 +83,18 @@ public final class CraftChatMessage {
}
i++;
} else if (currentChar == '\n') {
+ checkUrl(matcher, message, i);
+ lastWord = i + 1;
if (builder.length() > 0) {
appendNewComponent();
2013-12-01 10:35:33 +00:00
}
2013-12-01 20:36:51 +00:00
currentChatComponent = null;
2013-12-01 10:35:33 +00:00
} else {
+ if (currentChar == ' ' || i == message.length() - 1) {
2013-12-09 10:38:44 +00:00
+ if (checkUrl(matcher, message, i)) {
2013-12-04 14:41:40 +00:00
+ break;
2013-12-01 10:35:33 +00:00
+ }
+ }
builder.append(currentChar);
}
}
2013-12-11 18:09:41 +00:00
@@ -89,6 +106,31 @@ public final class CraftChatMessage {
2013-12-04 14:41:40 +00:00
output = list.toArray(new IChatBaseComponent[0]);
}
2013-12-09 10:38:44 +00:00
+ private boolean checkUrl(Matcher matcher, String message, int i) {
2013-12-04 14:41:40 +00:00
+ Matcher urlMatcher = matcher.region(lastWord, i == message.length() - 1 ? message.length() : i);
2013-12-09 10:38:44 +00:00
+ lastWord = i + 1;
2013-12-04 14:41:40 +00:00
+ if (urlMatcher.find()) {
+ String fullUrl = urlMatcher.group(2);
+ String protocol = urlMatcher.group(3);
+ String url = urlMatcher.group(4);
+ String path = urlMatcher.group(5);
+ builder.delete(builder.length() - fullUrl.length() + (i == message.length() - 1 ? 1 : 0), builder.length());
+ if (builder.length() > 0) {
+ appendNewComponent();
+ }
+ builder.append(fullUrl);
+ ChatClickable link = new ChatClickable(EnumClickAction.OPEN_URL,
+ (protocol!=null?protocol:"http") + "://" + url + (path!=null?path:""));
+ modifier.a(link);
+ appendNewComponent();
+ modifier.a((ChatClickable) null);
+ if (i == message.length() - 1) {
+ return true;
+ }
+ }
+ return false;
+ }
+
private void appendNewComponent() {
IChatBaseComponent addition = new ChatComponentText(builder.toString()).setChatModifier(modifier);
builder = new StringBuilder();
2013-12-01 10:35:33 +00:00
--
2013-12-09 10:38:44 +00:00
1.8.4.msysgit.0
2013-12-01 10:35:33 +00:00