Merge pull request #77 from thinkofdeath/master

Fix color codes breaking urls
This commit is contained in:
md-5 2013-12-04 13:18:18 -08:00
commit 8d622d915b

View file

@ -1,11 +1,11 @@
From 92c15b1f9c59b5c5898fead8e68c604eeeb800d9 Mon Sep 17 00:00:00 2001 From 64706e7d04a86c6916f0cfe1decb0f5eaf2db38e Mon Sep 17 00:00:00 2001
From: Thinkofdeath <purggames@gmail.com> From: Thinkofdeath <purggames@gmail.com>
Date: Sun, 1 Dec 2013 10:33:55 +0000 Date: Sun, 1 Dec 2013 10:33:55 +0000
Subject: [PATCH] Fix links in chat 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 diff --git a/src/main/java/org/bukkit/craftbukkit/util/CraftChatMessage.java b/src/main/java/org/bukkit/craftbukkit/util/CraftChatMessage.java
index cc8e715..377653b 100644 index cc8e715..5d6ce54 100644
--- a/src/main/java/org/bukkit/craftbukkit/util/CraftChatMessage.java --- a/src/main/java/org/bukkit/craftbukkit/util/CraftChatMessage.java
+++ b/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; @@ -3,11 +3,15 @@ package org.bukkit.craftbukkit.util;
@ -24,35 +24,55 @@ index cc8e715..377653b 100644
import com.google.common.collect.ImmutableMap; import com.google.common.collect.ImmutableMap;
import com.google.common.collect.ImmutableMap.Builder; import com.google.common.collect.ImmutableMap.Builder;
@@ -29,6 +33,7 @@ public final class CraftChatMessage { @@ -29,6 +33,8 @@ public final class CraftChatMessage {
private ChatModifier modifier = new ChatModifier(); private ChatModifier modifier = new ChatModifier();
private StringBuilder builder = new StringBuilder(); private StringBuilder builder = new StringBuilder();
private final IChatBaseComponent[] output; private final IChatBaseComponent[] output;
+ private static final Pattern url = Pattern.compile("^(?:(https?)://)?([-\\w_\\.]{2,}\\.[a-z]{2,4})(/\\S*)?$"); + private static final Pattern url = Pattern.compile("^(\u00A7.)*?((?:(https?)://)?([-\\w_\\.]{2,}\\.[a-z]{2,4})(/\\S*?)?)(\u00A7.)*?$");
+ private int lastWord = 0;
private FromString(String message) { private FromString(String message) {
if (message == null) { if (message == null) {
@@ -38,6 +43,8 @@ public final class CraftChatMessage { @@ -38,10 +44,13 @@ public final class CraftChatMessage {
list.add(currentChatComponent); list.add(currentChatComponent);
EnumChatFormat format = null; EnumChatFormat format = null;
+ Matcher matcher = url.matcher(message); + Matcher matcher = url.matcher(message);
+ int lastWord = 0; + lastWord = 0;
for (int i = 0; i < message.length(); i++) { for (int i = 0; i < message.length(); i++) {
char currentChar = message.charAt(i); char currentChar = message.charAt(i);
@@ -78,6 +85,30 @@ public final class CraftChatMessage { if (currentChar == '\u00A7' && (i < (message.length() - 1)) && (format = formatMap.get(message.charAt(i + 1))) != null) {
+ checkUrl(matcher, message, i, false);
if (builder.length() > 0) {
appendNewComponent();
}
@@ -78,6 +87,11 @@ public final class CraftChatMessage {
} }
currentChatComponent = null; currentChatComponent = null;
} else { } else {
+ if (currentChar == ' ' || i == message.length() - 1) { + if (currentChar == ' ' || i == message.length() - 1) {
+ if (checkUrl(matcher, message, i, true)) {
+ break;
+ }
+ }
builder.append(currentChar);
}
}
@@ -89,6 +103,36 @@ public final class CraftChatMessage {
output = list.toArray(new IChatBaseComponent[0]);
}
+ private boolean checkUrl(Matcher matcher, String message, int i, boolean newWord) {
+ Matcher urlMatcher = matcher.region(lastWord, i == message.length() - 1 ? message.length() : i); + Matcher urlMatcher = matcher.region(lastWord, i == message.length() - 1 ? message.length() : i);
+ if (newWord) {
+ lastWord = i + 1; + lastWord = i + 1;
+ }
+ if (urlMatcher.find()) { + if (urlMatcher.find()) {
+ String fullUrl = urlMatcher.group(0); + String fullUrl = urlMatcher.group(2);
+ String protocol = urlMatcher.group(1); + String protocol = urlMatcher.group(3);
+ String url = urlMatcher.group(2); + String url = urlMatcher.group(4);
+ String path = urlMatcher.group(3); + String path = urlMatcher.group(5);
+ builder.delete(builder.length() - fullUrl.length() + (i == message.length() - 1 ? 1 : 0), builder.length()); + builder.delete(builder.length() - fullUrl.length() + (i == message.length() - 1 ? 1 : 0), builder.length());
+ if (builder.length() > 0) { + if (builder.length() > 0) {
+ appendNewComponent(); + appendNewComponent();
@ -63,15 +83,19 @@ index cc8e715..377653b 100644
+ modifier.a(link); + modifier.a(link);
+ appendNewComponent(); + appendNewComponent();
+ modifier.a((ChatClickable) null); + modifier.a((ChatClickable) null);
+ if (!newWord) { //Force new word to prevent double checking
+ lastWord = i + 1;
+ }
+ if (i == message.length() - 1) { + if (i == message.length() - 1) {
+ appendNewComponent(); + return true;
+ break;
+ } + }
+ } + }
+ return false;
+ } + }
builder.append(currentChar); +
} private void appendNewComponent() {
} IChatBaseComponent addition = new ChatComponentText(builder.toString()).setChatModifier(modifier);
builder = new StringBuilder();
-- --
1.8.3.2 1.8.4.msysgit.0