|
| 1 | +package utils; |
| 2 | + |
| 3 | +import com.michealharker.saraswati.irc.MircColors; |
| 4 | + |
| 5 | +/** |
| 6 | + * Helper class to convert messages (colours and formatting) |
| 7 | + * between (m)IRC and Minecraft. |
| 8 | + * @author Fuchs |
| 9 | + * |
| 10 | + */ |
| 11 | +public class IRCMinecraftConverter { |
| 12 | + |
| 13 | + /** |
| 14 | + * Converts an IRC message to Minecraft by replacing formatting |
| 15 | + * with the corresponding (m)IRC variant. The non existing |
| 16 | + * k (garbage) and m (strikethrough) are marked. |
| 17 | + * @param message message to be converted |
| 18 | + * @return original message with colours replaced |
| 19 | + */ |
| 20 | + public static String convIRCtoMinecraft(String message) { |
| 21 | + |
| 22 | + if(message != null) { |
| 23 | + message = escapeMinecraftColour(message); |
| 24 | + message = mIRCtoMinecraftColourify(message); |
| 25 | + } |
| 26 | + |
| 27 | + return message; |
| 28 | + |
| 29 | + } |
| 30 | + |
| 31 | + /** |
| 32 | + * Converts a Minecraft message to IRC by replacing formatting |
| 33 | + * with the corresponding Minecraft variant. The non-printable |
| 34 | + * § character is replaced with the look-alike ⨕ to preserve |
| 35 | + * meaning as good as possible. |
| 36 | + * @param message message to be converted |
| 37 | + * @return original message with colours replaced |
| 38 | + */ |
| 39 | + public static String convMinecraftToIRC(String message) { |
| 40 | + |
| 41 | + if(message != null) { |
| 42 | + // IRC colours don't have to be stripped as they can't be typed anyway |
| 43 | + message = minecraftTomIRCcolourify(message); |
| 44 | + } |
| 45 | + |
| 46 | + return message; |
| 47 | + } |
| 48 | + |
| 49 | + /**if(message == null) { |
| 50 | + return message; |
| 51 | + } |
| 52 | + * Helper method to replace Minecraft colours with (m)IRC colours, |
| 53 | + * taken from com.michealharker.saraswati.irc |
| 54 | + * @param message message to convert |
| 55 | + * @return message with (m)IRC Colours |
| 56 | + */ |
| 57 | + public static String minecraftTomIRCcolourify(String message) { |
| 58 | + |
| 59 | + // TODO: Do some state tracking |
| 60 | + // The marks for non-existing IRC modes are not perfect, |
| 61 | + // as they would only last until the next reset, §r. |
| 62 | + // To catch that, regex instead of a simple replace would |
| 63 | + // be needed, which is not warranted at this point. |
| 64 | + |
| 65 | + return message// |
| 66 | + .replace("§0", MircColors.BLACK)// |
| 67 | + .replace("§1", MircColors.BLUE)// |
| 68 | + .replace("§2", MircColors.GREEN)// |
| 69 | + .replace("§3", MircColors.LIGHT_CYAN)// |
| 70 | + .replace("§4", MircColors.LIGHT_RED)// |
| 71 | + .replace("§5", MircColors.PURPLE)// |
| 72 | + .replace("§6", MircColors.ORANGE)// |
| 73 | + .replace("§7", MircColors.LIGHT_GRAY)// |
| 74 | + .replace("§8", MircColors.GRAY)// |
| 75 | + .replace("§9", MircColors.LIGHT_BLUE)// |
| 76 | + .replace("§a", MircColors.LIGHT_GREEN)// |
| 77 | + .replace("§b", MircColors.LIGHT_CYAN)// |
| 78 | + .replace("§c", MircColors.LIGHT_RED)// |
| 79 | + .replace("§d", MircColors.PINK)// |
| 80 | + .replace("§e", MircColors.YELLOW)// |
| 81 | + .replace("§f", MircColors.WHITE)// |
| 82 | + .replace("§l", MircColors.BOLD)// |
| 83 | + .replace("§n", MircColors.UNDERLINE)// |
| 84 | + .replace("§o", MircColors.ITALIC)// |
| 85 | + .replace("§r", MircColors.NORMAL)// |
| 86 | + .replace("§k", "-§k-") // mark random garbage |
| 87 | + .replace("§m", "-§m-"); // mark strikethrough |
| 88 | + } |
| 89 | + |
| 90 | + |
| 91 | + /** |
| 92 | + * Helper method to replace (m)IRC colours with Minecraft colours, |
| 93 | + * modified from com.michealharker.saraswati.irc |
| 94 | + * @param message message to convert |
| 95 | + * @return message with (m)IRC colours |
| 96 | + */ |
| 97 | + public static String mIRCtoMinecraftColourify(String message) { |
| 98 | + |
| 99 | + // TODO: Do some state tracking |
| 100 | + // In Minecraft if a colour code is used after formatting, |
| 101 | + // the formatting code will be disabled after the colour code point. |
| 102 | + // Therefore, when using a colour code together with formatting, |
| 103 | + // it has to be ensured the colour code is used first |
| 104 | + // and to reuse the formatting code when changing colours. |
| 105 | + // Also (m)IRC resets a format on using it twice, e.g. %Bbold%B normal, |
| 106 | + // whilst Minecraft in this case just continues in bold. |
| 107 | + |
| 108 | + return message// |
| 109 | + .replace(MircColors.WHITE, "§f")// |
| 110 | + .replace(MircColors.BLACK, "§0")// |
| 111 | + .replace(MircColors.BLUE, "§1")// |
| 112 | + .replace(MircColors.GREEN, "§2")// |
| 113 | + .replace(MircColors.LIGHT_CYAN, "§3")// |
| 114 | + .replace(MircColors.LIGHT_RED, "§4")// |
| 115 | + .replace(MircColors.PURPLE, "§5")// |
| 116 | + .replace(MircColors.ORANGE, "§6")// |
| 117 | + .replace(MircColors.LIGHT_GRAY, "§7")// |
| 118 | + .replace(MircColors.GRAY, "§8")// |
| 119 | + .replace(MircColors.LIGHT_BLUE, "§9")// |
| 120 | + .replace(MircColors.LIGHT_GREEN, "§a")// |
| 121 | + .replace(MircColors.LIGHT_CYAN, "§b")// |
| 122 | + .replace(MircColors.LIGHT_RED, "§c")// |
| 123 | + .replace(MircColors.PINK, "§d")// |
| 124 | + .replace(MircColors.YELLOW, "§e")// |
| 125 | + .replace(MircColors.BOLD, "§l")// |
| 126 | + .replace(MircColors.UNDERLINE, "§n")// |
| 127 | + .replace(MircColors.ITALIC, "§o") |
| 128 | + .replace(MircColors.NORMAL, "§r"); |
| 129 | + } |
| 130 | + |
| 131 | + /*** |
| 132 | + * Helper message to replace the non-valid § in minecraft |
| 133 | + * with the look-alike ⨕ character to preserve meaning. |
| 134 | + * Might be extended in the future for other things that are valid on IRC |
| 135 | + * but not in Minecraft. |
| 136 | + * @param message Message to escape |
| 137 | + * @return message with replaced § characters. |
| 138 | + */ |
| 139 | + public static String escapeMinecraftColour(String message) { |
| 140 | + // Closest lookalike accepted by Minecraft |
| 141 | + // and unlikely to be used, integral around point. |
| 142 | + return message.replace("§", "⨕"); |
| 143 | + } |
| 144 | + |
| 145 | +} |
0 commit comments