2013-12-02 22:21:46 +01:00
|
|
|
From 92c15b1f9c59b5c5898fead8e68c604eeeb800d9 Mon Sep 17 00:00:00 2001
|
2013-12-01 11:35:33 +01: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-02 12:27:23 +01:00
|
|
|
index cc8e715..377653b 100644
|
2013-12-01 11:35:33 +01: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;
|
|
|
|
@@ -29,6 +33,7 @@ public final class CraftChatMessage {
|
|
|
|
private ChatModifier modifier = new ChatModifier();
|
|
|
|
private StringBuilder builder = new StringBuilder();
|
|
|
|
private final IChatBaseComponent[] output;
|
|
|
|
+ private static final Pattern url = Pattern.compile("^(?:(https?)://)?([-\\w_\\.]{2,}\\.[a-z]{2,4})(/\\S*)?$");
|
|
|
|
|
|
|
|
private FromString(String message) {
|
|
|
|
if (message == null) {
|
2013-12-01 21:36:51 +01:00
|
|
|
@@ -38,6 +43,8 @@ public final class CraftChatMessage {
|
|
|
|
list.add(currentChatComponent);
|
2013-12-01 11:35:33 +01:00
|
|
|
|
|
|
|
EnumChatFormat format = null;
|
|
|
|
+ Matcher matcher = url.matcher(message);
|
|
|
|
+ int lastWord = 0;
|
|
|
|
|
|
|
|
for (int i = 0; i < message.length(); i++) {
|
|
|
|
char currentChar = message.charAt(i);
|
2013-12-02 12:27:23 +01:00
|
|
|
@@ -78,6 +85,30 @@ public final class CraftChatMessage {
|
2013-12-01 11:35:33 +01:00
|
|
|
}
|
2013-12-01 21:36:51 +01:00
|
|
|
currentChatComponent = null;
|
2013-12-01 11:35:33 +01:00
|
|
|
} else {
|
|
|
|
+ if (currentChar == ' ' || i == message.length() - 1) {
|
|
|
|
+ Matcher urlMatcher = matcher.region(lastWord, i == message.length() - 1 ? message.length() : i);
|
|
|
|
+ lastWord = i + 1;
|
|
|
|
+ if (urlMatcher.find()) {
|
|
|
|
+ String fullUrl = urlMatcher.group(0);
|
|
|
|
+ String protocol = urlMatcher.group(1);
|
|
|
|
+ String url = urlMatcher.group(2);
|
2013-12-02 12:27:23 +01:00
|
|
|
+ String path = urlMatcher.group(3);
|
2013-12-01 11:35:33 +01:00
|
|
|
+ builder.delete(builder.length() - fullUrl.length() + (i == message.length() - 1 ? 1 : 0), builder.length());
|
|
|
|
+ if (builder.length() > 0) {
|
|
|
|
+ appendNewComponent();
|
|
|
|
+ }
|
|
|
|
+ builder.append(fullUrl);
|
2013-12-02 12:27:23 +01:00
|
|
|
+ ChatClickable link = new ChatClickable(EnumClickAction.OPEN_URL,
|
|
|
|
+ (protocol!=null?protocol:"http") + "://" + url + (path!=null?path:""));
|
2013-12-01 11:35:33 +01:00
|
|
|
+ modifier.a(link);
|
|
|
|
+ appendNewComponent();
|
|
|
|
+ modifier.a((ChatClickable) null);
|
|
|
|
+ if (i == message.length() - 1) {
|
2013-12-01 21:36:51 +01:00
|
|
|
+ appendNewComponent();
|
2013-12-01 11:35:33 +01:00
|
|
|
+ break;
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
builder.append(currentChar);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
--
|
2013-12-02 22:21:46 +01:00
|
|
|
1.8.3.2
|
2013-12-01 11:35:33 +01:00
|
|
|
|