|
| 1 | +/* |
| 2 | + * MIT License |
| 3 | + * |
| 4 | + * Copyright (c) 2018 Aeternum Network |
| 5 | + * |
| 6 | + * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 7 | + * of this software and associated documentation files (the "Software"), to deal |
| 8 | + * in the Software without restriction, including without limitation the rights |
| 9 | + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 10 | + * copies of the Software, and to permit persons to whom the Software is |
| 11 | + * furnished to do so, subject to the following conditions: |
| 12 | + * |
| 13 | + * The above copyright notice and this permission notice shall be included in all |
| 14 | + * copies or substantial portions of the Software. |
| 15 | + * |
| 16 | + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 17 | + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 18 | + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 19 | + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 20 | + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 21 | + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 22 | + * SOFTWARE. |
| 23 | + */ |
| 24 | + |
| 25 | +package dev.hypera.ultrastaffchat.utils; |
| 26 | + |
| 27 | +import net.md_5.bungee.api.ChatColor; |
| 28 | +import net.md_5.bungee.api.plugin.Listener; |
| 29 | + |
| 30 | +import java.util.ArrayList; |
| 31 | +import java.util.List; |
| 32 | +import java.util.regex.Matcher; |
| 33 | +import java.util.regex.Pattern; |
| 34 | + |
| 35 | +public class MD_ implements Listener { |
| 36 | + |
| 37 | + public static String parseMarkdown(String message) { |
| 38 | + String translated = message; |
| 39 | + |
| 40 | + translated = replaceWith(translated, "(?<!\\\\)\\*\\*", ChatColor.COLOR_CHAR + "z", ChatColor.COLOR_CHAR + "Z"); |
| 41 | + translated = replaceWith(translated, "(?<!\\\\)\\*", ChatColor.COLOR_CHAR + "x", ChatColor.COLOR_CHAR + "X"); |
| 42 | + translated = replaceWith(translated, "(?<!\\\\)__", ChatColor.COLOR_CHAR + "v", ChatColor.COLOR_CHAR + "V"); |
| 43 | + translated = replaceWith(translated, "(?<!\\\\)_", ChatColor.COLOR_CHAR + "q", ChatColor.COLOR_CHAR + "Q"); |
| 44 | + translated = replaceWith(translated, "(?<!\\\\)~~", ChatColor.COLOR_CHAR + "m", ChatColor.COLOR_CHAR + "M"); |
| 45 | + translated = replaceWith(translated, "(?<!\\\\)~", ChatColor.COLOR_CHAR + "w", ChatColor.COLOR_CHAR + "W"); |
| 46 | + |
| 47 | + translated = translated.replace("\\*", "*").replace("\\_", "_").replace("\\~", "~"); |
| 48 | + |
| 49 | + return parseParts(translated).toString(); |
| 50 | + } |
| 51 | + |
| 52 | + private static StringBuilder parseParts(String translated) { |
| 53 | + String partForPart = (" " + translated); |
| 54 | + String[] parts = partForPart.split("" + ChatColor.COLOR_CHAR); |
| 55 | + StringBuilder builder = new StringBuilder(); |
| 56 | + for (String part : parts) { |
| 57 | + if (part.isEmpty()) { |
| 58 | + continue; |
| 59 | + } |
| 60 | + parsePart(part, builder); |
| 61 | + } |
| 62 | + return builder; |
| 63 | + } |
| 64 | + |
| 65 | + private static void parsePart(String part, StringBuilder builder) { |
| 66 | + char colorCharacter = part.charAt(0); |
| 67 | + ChatColor color = ChatColor.getByChar(colorCharacter); |
| 68 | + |
| 69 | + String colors = getLastColors(builder.toString()); |
| 70 | + if (color != null) { |
| 71 | + StringBuilder colorBuilder = new StringBuilder(); |
| 72 | + for (String cc : colors.split(ChatColor.COLOR_CHAR + "")) { |
| 73 | + if (cc.isEmpty()) { |
| 74 | + continue; |
| 75 | + } |
| 76 | + if (isFormat(ChatColor.getByChar(cc.charAt(0)))) { |
| 77 | + colorBuilder.append(ChatColor.COLOR_CHAR + cc); |
| 78 | + } |
| 79 | + } |
| 80 | + builder.append(color + colorBuilder.toString()); |
| 81 | + } else { |
| 82 | + colors = parseColours(colorCharacter, builder, colors); |
| 83 | + } |
| 84 | + if (part.length() > 1) { |
| 85 | + builder.append(part.substring(1)); |
| 86 | + } |
| 87 | + } |
| 88 | + |
| 89 | + private static String parseColours(char colorCharacter, StringBuilder builder, String c) { |
| 90 | + String colors = c; |
| 91 | + if (colorCharacter == 'z') builder.append(ChatColor.BOLD); |
| 92 | + else if (colorCharacter == 'x') builder.append(ChatColor.ITALIC); |
| 93 | + else if (colorCharacter == 'v') builder.append(ChatColor.UNDERLINE); |
| 94 | + else if (colorCharacter == 'q') builder.append(ChatColor.ITALIC); |
| 95 | + else if (colorCharacter == 'm') builder.append(ChatColor.STRIKETHROUGH); |
| 96 | + else if (colorCharacter == 'w') builder.append(ChatColor.MAGIC); |
| 97 | + else if (colorCharacter == 'Z') colors = colors.replace(ChatColor.BOLD.toString(), ""); |
| 98 | + else if (colorCharacter == 'X') colors = colors.replace(ChatColor.ITALIC.toString(), ""); |
| 99 | + else if (colorCharacter == 'V') colors = colors.replace(ChatColor.UNDERLINE.toString(), ""); |
| 100 | + else if (colorCharacter == 'Q') colors = colors.replace(ChatColor.ITALIC.toString(), ""); |
| 101 | + else if (colorCharacter == 'M') colors = colors.replace(ChatColor.STRIKETHROUGH.toString(), ""); |
| 102 | + else if (colorCharacter == 'W') colors = colors.replace(ChatColor.MAGIC.toString(), ""); |
| 103 | + if (Character.isUpperCase(colorCharacter)) builder.append(ChatColor.RESET + colors); |
| 104 | + return colors; |
| 105 | + } |
| 106 | + |
| 107 | + private static String replaceWith(String message, String quot, String pre, String suf) { |
| 108 | + String part = message; |
| 109 | + for (String str : getMatches(message, quot + "(.+?)" + quot)) { |
| 110 | + part = part.replaceFirst(quot + Pattern.quote(str) + quot, pre + str + suf); |
| 111 | + } |
| 112 | + return part; |
| 113 | + } |
| 114 | + |
| 115 | + public static List<String> getMatches(String string, String regex) { |
| 116 | + Pattern pattern = Pattern.compile(regex); |
| 117 | + Matcher matcher = pattern.matcher(string); |
| 118 | + List<String> matches = new ArrayList<String>(); |
| 119 | + while (matcher.find()) { |
| 120 | + matches.add(matcher.group(1)); |
| 121 | + } |
| 122 | + return matches; |
| 123 | + } |
| 124 | + |
| 125 | + private static boolean isFormat(ChatColor color) { |
| 126 | + return color == ChatColor.MAGIC || color == ChatColor.BOLD || color == ChatColor.STRIKETHROUGH || color == ChatColor.ITALIC || color == ChatColor.UNDERLINE; |
| 127 | + } |
| 128 | + |
| 129 | + private static String getLastColors(String input) { |
| 130 | + String result = ""; |
| 131 | + int length = input.length(); |
| 132 | + |
| 133 | + // Search backwards from the end as it is faster |
| 134 | + for (int index = length - 1; index > -1; index--) { |
| 135 | + char section = input.charAt(index); |
| 136 | + if (section == ChatColor.COLOR_CHAR && index < length - 1) { |
| 137 | + char c = input.charAt(index + 1); |
| 138 | + ChatColor color = ChatColor.getByChar(c); |
| 139 | + |
| 140 | + if (color != null) { |
| 141 | + result = color.toString() + result; |
| 142 | + |
| 143 | + // Once we find a color or reset we can stop searching |
| 144 | + if (!isFormat(color) || color.equals(ChatColor.RESET)) { |
| 145 | + break; |
| 146 | + } |
| 147 | + } |
| 148 | + } |
| 149 | + } |
| 150 | + |
| 151 | + return result; |
| 152 | + } |
| 153 | + |
| 154 | +} |
0 commit comments