diff --git a/gradle.properties b/gradle.properties index 0fedb39856e..b4ed6bdcc60 100644 --- a/gradle.properties +++ b/gradle.properties @@ -1,3 +1,3 @@ groupid=ch.njol name=skript -version=2.2-dev26f \ No newline at end of file +version=2.2-dev27 \ No newline at end of file diff --git a/src/main/java/ch/njol/skript/SkriptConfig.java b/src/main/java/ch/njol/skript/SkriptConfig.java index ed0e7fb9fb2..49444e557b9 100644 --- a/src/main/java/ch/njol/skript/SkriptConfig.java +++ b/src/main/java/ch/njol/skript/SkriptConfig.java @@ -45,6 +45,7 @@ import ch.njol.skript.util.Task; import ch.njol.skript.util.Timespan; import ch.njol.skript.util.chat.ChatMessages; +import ch.njol.skript.util.chat.LinkParseMode; import ch.njol.util.Setter; /** @@ -190,12 +191,27 @@ public void set(Boolean t) { }); - public final static Option parseLinks = new Option("parse links in chat messages", false) - .setter(new Setter() { + public final static Option parseLinks = new Option("parse links in chat messages", "disabled") + .setter(new Setter() { @Override - public void set(Boolean t) { - ChatMessages.parseLinks = t; + public void set(String t) { + switch (t) { + case "false": + case "disabled": + ChatMessages.linkParseMode = LinkParseMode.DISABLED; + break; + case "true": + case "lenient": + ChatMessages.linkParseMode = LinkParseMode.LENIENT; + break; + case "strict": + ChatMessages.linkParseMode = LinkParseMode.STRICT; + break; + default: + ChatMessages.linkParseMode = LinkParseMode.DISABLED; + Skript.warning("Unknown link parse mode: " + t + ", please use disabled, strict or lenient"); + } } }); diff --git a/src/main/java/ch/njol/skript/util/chat/ChatMessages.java b/src/main/java/ch/njol/skript/util/chat/ChatMessages.java index db37d33f471..c0e41f2fd2e 100644 --- a/src/main/java/ch/njol/skript/util/chat/ChatMessages.java +++ b/src/main/java/ch/njol/skript/util/chat/ChatMessages.java @@ -61,10 +61,9 @@ public class ChatMessages { /** - * If anything that starts with http(s):// should be interpreted - * as a link (for old script compatibility). + * Link parse mode. */ - public static boolean parseLinks = false; + public static LinkParseMode linkParseMode = LinkParseMode.DISABLED; /** * Chat codes, see {@link ChatCode}. @@ -181,7 +180,7 @@ public static List parse(String msg) { VariableString varParam = null; // Attempt link parsing - if (parseLinks && c == 'h') { + if (linkParseMode == LinkParseMode.STRICT && c == 'h') { String rest = msg.substring(i); // Get rest of string String link = null; @@ -209,6 +208,48 @@ public static List parse(String msg) { i += link.length() - 1; // Skip link for all other parsing + // Add one MORE component (this comes after the link) + current = new MessageComponent(); + components.add(current); + continue; + } + } else if (linkParseMode == LinkParseMode.LENIENT && (i == 0 || chars[i - 1] == ' ')) { + // Lenient link parsing + String rest = msg.substring(i); // Get rest of string + + String link = null; + if (rest.contains(".")) { + link = rest.split(" ", 2)[0]; + } + + // Link found + if (link != null && !link.isEmpty()) { + // Insert protocol (aka guess it) if it isn't there + String url; + if (!link.startsWith("http://") && !link.startsWith("https://")) { + url = "http://" + link; // Hope that http -> https redirect works on target site... + } else { + url = link; + } + + // Take previous component, create new + String text = curStr.toString(); + curStr = new StringBuilder(); + assert text != null; + current.text = text; + + MessageComponent old = current; + current = new MessageComponent(); + copyStyles(old, current); + + components.add(current); + + // Make new component a link + ChatCode.open_url.updateComponent(current, url, null); // URL for client... + current.text = link; // ... and for player + + i += link.length() - 1; // Skip link for all other parsing + // Add one MORE component (this comes after the link) current = new MessageComponent(); components.add(current); diff --git a/src/main/java/ch/njol/skript/util/chat/LinkParseMode.java b/src/main/java/ch/njol/skript/util/chat/LinkParseMode.java new file mode 100644 index 00000000000..a254c07d567 --- /dev/null +++ b/src/main/java/ch/njol/skript/util/chat/LinkParseMode.java @@ -0,0 +1,41 @@ +/** + * This file is part of Skript. + * + * Skript is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * Skript is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with Skript. If not, see . + * + * + * Copyright 2011-2017 Peter Güttinger and contributors + */ +package ch.njol.skript.util.chat; + +/** + * Parse mode for links in chat messages. + */ +public enum LinkParseMode { + + /** + * Parses nothing automatically as a link. + */ + DISABLED, + + /** + * Parses everything that starts with http(s):// as a link. + */ + STRICT, + + /** + * Parses everything with "." as a link. + */ + LENIENT +} diff --git a/src/main/resources/config.sk b/src/main/resources/config.sk index 1ed8a87e22e..8da43cc7708 100644 --- a/src/main/resources/config.sk +++ b/src/main/resources/config.sk @@ -168,9 +168,11 @@ enable timings: false # Note that this requires Paper (https://paper.readthedocs.io/en/paper-1.11/) to work; on Bukkit/Spigot this option has no effect. # When false, timings are not enabled for scripts even if you're running Paper. -parse links in chat messages: false -# If Skript should attempt to parse anything that begins with "http://" or "https://" as links -# when parsing chat messages. Turning this on might make parsing texts slightly slower. +parse links in chat messages: disabled +# Controls how Skript will try to parse links in chat messages. +# If 'disabled' or 'false', no links will be automatically parsed +# When 'strict', everything starting with http(s):// will be parsed as link +# When 'lenient', everything that contains a dot will be interpreted as a link # ==== Variables ====