Skip to content

Commit 746d3fe

Browse files
committed
Convert Discord user colors to closest MC color
1 parent c50765d commit 746d3fe

File tree

10 files changed

+66
-16
lines changed

10 files changed

+66
-16
lines changed

1.10.2/src/main/resources/assets/discordchat/default.conf

+2-2
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,8 @@ discordchat {
4343
fromMC = "MC » <$1> $2"
4444

4545
# Format for a normal message from Discord to MC.
46-
# $1 will be replaced with the channel, $2 will be replaced with the sender's username, and $3 will be replaced with the message
47-
fromDiscord = "#$1 » <$2> $3"
46+
# $1 will be replaced with the channel, $2 will be replaced with the sender's username, $3 will be replaced with the message, and $4 will be replaced with the MC color (e.g. §3) closest to the Discord user's color.
47+
fromDiscord = "#$1 » <$4$2§f> $3"
4848

4949
# Format for a private message from MC to Discord.
5050
# $1 will be replaced with the sender's username and $2 will be replaced with the message.

1.11.2/src/main/resources/assets/discordchat/default.conf

+2-2
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,8 @@ discordchat {
4343
fromMC = "MC » <$1> $2"
4444

4545
# Format for a normal message from Discord to MC.
46-
# $1 will be replaced with the channel, $2 will be replaced with the sender's username, and $3 will be replaced with the message
47-
fromDiscord = "#$1 » <$2> $3"
46+
# $1 will be replaced with the channel, $2 will be replaced with the sender's username, $3 will be replaced with the message, and $4 will be replaced with the MC color (e.g. §3) closest to the Discord user's color.
47+
fromDiscord = "#$1 » <$4$2§f> $3"
4848

4949
# Format for a private message from MC to Discord.
5050
# $1 will be replaced with the sender's username and $2 will be replaced with the message.

1.12/src/main/resources/assets/discordchat/default.conf

+2-2
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,8 @@ discordchat {
4343
fromMC = "MC » <$1> $2"
4444

4545
# Format for a normal message from Discord to MC.
46-
# $1 will be replaced with the channel, $2 will be replaced with the sender's username, and $3 will be replaced with the message
47-
fromDiscord = "#$1 » <$2> $3"
46+
# $1 will be replaced with the channel, $2 will be replaced with the sender's username, $3 will be replaced with the message, and $4 will be replaced with the MC color (e.g. §3) closest to the Discord user's color.
47+
fromDiscord = "#$1 » <$4$2§f> $3"
4848

4949
# Format for a private message from MC to Discord.
5050
# $1 will be replaced with the sender's username and $2 will be replaced with the message.

1.7.10/src/main/resources/assets/discordchat/default.conf

+2-2
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,8 @@ discordchat {
4343
fromMC = "MC » <$1> $2"
4444

4545
# Format for a normal message from Discord to MC.
46-
# $1 will be replaced with the channel, $2 will be replaced with the sender's username, and $3 will be replaced with the message
47-
fromDiscord = "#$1 » <$2> $3"
46+
# $1 will be replaced with the channel, $2 will be replaced with the sender's username, $3 will be replaced with the message, and $4 will be replaced with the MC color (e.g. §3) closest to the Discord user's color.
47+
fromDiscord = "#$1 » <$4$2§f> $3"
4848

4949
# Format for a private message from MC to Discord.
5050
# $1 will be replaced with the sender's username and $2 will be replaced with the message.

1.8.9/src/main/resources/assets/discordchat/default.conf

+2-2
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,8 @@ discordchat {
4343
fromMC = "MC » <$1> $2"
4444

4545
# Format for a normal message from Discord to MC.
46-
# $1 will be replaced with the channel, $2 will be replaced with the sender's username, and $3 will be replaced with the message
47-
fromDiscord = "#$1 » <$2> $3"
46+
# $1 will be replaced with the channel, $2 will be replaced with the sender's username, $3 will be replaced with the message, and $4 will be replaced with the MC color (e.g. §3) closest to the Discord user's color.
47+
fromDiscord = "#$1 » <$4$2§f> $3"
4848

4949
# Format for a private message from MC to Discord.
5050
# $1 will be replaced with the sender's username and $2 will be replaced with the message.

core/src/main/java/net/shadowfacts/discordchat/api/IMessageFormatter.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ public interface IMessageFormatter {
77

88
String fromMC(String sender, String message);
99

10-
String fromDiscord(String channel, String sender, String message);
10+
String fromDiscord(String channel, String senderColor, String sender, String message);
1111

1212
String fromMCPrivate(String sender, String message);
1313

core/src/main/java/net/shadowfacts/discordchat/core/Listener.java

+5-1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@
99
import net.shadowfacts.discordchat.api.IMessageFormatter;
1010
import net.shadowfacts.discordchat.api.IMinecraftAdapter;
1111
import net.shadowfacts.discordchat.api.command.ICommandManager;
12+
import net.shadowfacts.discordchat.core.util.NearestMCColor;
13+
14+
import java.awt.*;
1215

1316
/**
1417
* @author shadowfacts
@@ -46,9 +49,10 @@ public void onGuildMessageReceived(GuildMessageReceivedEvent event) {
4649
commandManager.execute(raw.substring(config.getCommandPrefix().length()), event.getAuthor(), event.getChannel());
4750
} else {
4851
String channel = event.getChannel().getName();
52+
String color = NearestMCColor.get(event.getMember().getColor().getRGB());
4953
String author = event.getMember().getEffectiveName().replaceAll("\\.", "_");
5054
String message = EmojiParser.parseToAliases(event.getMessage().getContent());
51-
minecraftAdapter.sendMessage(formatter.fromDiscord(channel, author, message));
55+
minecraftAdapter.sendMessage(formatter.fromDiscord(channel, color, author, message));
5256
event.getMessage().getAttachments().forEach(it -> {
5357
minecraftAdapter.sendMessage("Attachment: " + it.getUrl());
5458
});

core/src/main/java/net/shadowfacts/discordchat/core/MessageFormatter.java

+3-2
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,12 @@ public String fromMC(String sender, String message) {
2323
}
2424

2525
@Override
26-
public String fromDiscord(String channel, String sender, String message) {
26+
public String fromDiscord(String channel, String senderColor, String sender, String message) {
2727
return config.getFromDiscordFormat()
2828
.replace("$1", channel)
2929
.replace("$2", sender)
30-
.replace("$3", message);
30+
.replace("$3", message)
31+
.replace("$4", senderColor);
3132
}
3233

3334
@Override
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package net.shadowfacts.discordchat.core.util;
2+
3+
import net.shadowfacts.shadowlib.util.NearestColor;
4+
5+
/**
6+
* @author shadowfacts
7+
*/
8+
public class NearestMCColor {
9+
10+
public static String get(int color) {
11+
return "§" + NearestColor.find(color, Color.values(), Color::getColor).character;
12+
}
13+
14+
private enum Color {
15+
BLACK(0x000000, '0'),
16+
DARK_BLUE(0x0000AA, '1'),
17+
DARK_GREEN(0x00AA00, '2'),
18+
DARK_AQUA(0x00AAAA, '3'),
19+
DARK_RED(0xAA0000, '4'),
20+
DARK_PURPLE(0xAA00AA, '5'),
21+
GOLD(0xFFAA00, '6'),
22+
GRAY(0xAAAAAA, '7'),
23+
DARK_GRAY(0x555555, '8'),
24+
BLUE(0x5555FF, '9'),
25+
GREEN(0x55FF55, 'a'),
26+
AQUA(0x55FFFF, 'b'),
27+
RED(0xFF5555, 'c'),
28+
LIGHT_PURPLE(0xFF55FF, 'd'),
29+
YELLOW(0xFFFF55, 'e'),
30+
WHITE(0xFFFFFF, 'f');
31+
32+
public final int color;
33+
public final char character;
34+
35+
Color(int color, char character) {
36+
this.color = color;
37+
this.character = character;
38+
}
39+
40+
public int getColor() {
41+
return color;
42+
}
43+
}
44+
45+
}

core/src/main/resources/assets/discordchat/default.conf

+2-2
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,8 @@ discordchat {
4343
fromMC = "MC » <$1> $2"
4444

4545
# Format for a normal message from Discord to MC.
46-
# $1 will be replaced with the channel, $2 will be replaced with the sender's username, and $3 will be replaced with the message
47-
fromDiscord = "#$1 » <$2> $3"
46+
# $1 will be replaced with the channel, $2 will be replaced with the sender's username, $3 will be replaced with the message, and $4 will be replaced with the MC color (e.g. §3) closest to the Discord user's color.
47+
fromDiscord = "#$1 » <$4$2§f> $3"
4848

4949
# Format for a private message from MC to Discord.
5050
# $1 will be replaced with the sender's username and $2 will be replaced with the message.

0 commit comments

Comments
 (0)