Skip to content

Commit 65df5b6

Browse files
committed
Add dependencies for colour support
1 parent fe6375d commit 65df5b6

2 files changed

Lines changed: 183 additions & 0 deletions

File tree

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package com.michealharker.saraswati.irc;
2+
3+
/**
4+
* Original by michealharker, modified to incldude a bit more
5+
* @author Fuchs
6+
*
7+
*/
8+
public final class MircColors {
9+
private MircColors() {
10+
}
11+
12+
public static final String WHITE = "\u000300";
13+
public static final String BLACK = "\u000301";
14+
public static final String BLUE = "\u000302";
15+
public static final String GREEN = "\u000303";
16+
public static final String LIGHT_RED = "\u000304";
17+
public static final String BROWN = "\u000305";
18+
public static final String PURPLE = "\u000306";
19+
public static final String ORANGE = "\u000307";
20+
public static final String YELLOW = "\u000308";
21+
public static final String LIGHT_GREEN = "\u000309";
22+
public static final String CYAN = "\u000310";
23+
public static final String LIGHT_CYAN = "\u000311";
24+
public static final String LIGHT_BLUE = "\u000312";
25+
public static final String PINK = "\u000313";
26+
public static final String GRAY = "\u000314";
27+
public static final String LIGHT_GRAY = "\u000315";
28+
29+
public static final String TRANSPARENT_BG = "\u000399";
30+
31+
public static final String BOLD = "\u0002";
32+
public static final String NORMAL = "\u000f";
33+
public static final String UNDERLINE = "\u001f";
34+
35+
public static final String ITALIC = "\u001d";
36+
public static final String INVERSE = "\u0016";
37+
38+
}
Lines changed: 145 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,145 @@
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

Comments
 (0)