diff --git a/src/main/java/com/nguyenquyhy/spongediscord/SpongeDiscord.java b/src/main/java/com/nguyenquyhy/spongediscord/SpongeDiscord.java index 33396a7..f6cd596 100644 --- a/src/main/java/com/nguyenquyhy/spongediscord/SpongeDiscord.java +++ b/src/main/java/com/nguyenquyhy/spongediscord/SpongeDiscord.java @@ -5,6 +5,7 @@ import com.nguyenquyhy.spongediscord.logics.Config; import com.nguyenquyhy.spongediscord.logics.ConfigHandler; import com.nguyenquyhy.spongediscord.logics.LoginHandler; +import com.nguyenquyhy.spongediscord.utils.TextUtil; import org.apache.commons.lang3.StringUtils; import org.slf4j.Logger; import org.spongepowered.api.Game; @@ -142,6 +143,9 @@ public void onChat(MessageChannelEvent.Chat event) { if (StringUtils.isNotBlank(config.CHANNEL_ID)) { String plainString = event.getRawMessage().toPlain().trim(); if (StringUtils.isNotBlank(plainString) && !plainString.startsWith("/")) { + + plainString = TextUtil.formatMinecraftEmoji(plainString); + Optional player = event.getCause().first(Player.class); if (player.isPresent()) { UUID playerId = player.get().getUniqueId(); diff --git a/src/main/java/com/nguyenquyhy/spongediscord/logics/MessageHandler.java b/src/main/java/com/nguyenquyhy/spongediscord/logics/MessageHandler.java index b560590..392b8a2 100644 --- a/src/main/java/com/nguyenquyhy/spongediscord/logics/MessageHandler.java +++ b/src/main/java/com/nguyenquyhy/spongediscord/logics/MessageHandler.java @@ -27,10 +27,8 @@ public static void discordMessageReceived(IMessage message, CommandSource comman Config config = mod.getConfig(); if (message.getChannel().getID().equals(config.CHANNEL_ID) && !config.NONCE.equals(message.getNonce())) { - String content = message.getContent(); + String content = TextUtil.formatDiscordEmoji(message.getContent()); String author = message.getAuthor().getName(); -// Text formattedMessage = TextSerializers.FORMATTING_CODE.deserialize( -// String.format(config.MESSAGE_MINECRAFT_TEMPLATE.replace("%a", author), content)); Text formattedMessage = TextUtil.formatUrl(String.format(config.MESSAGE_MINECRAFT_TEMPLATE.replace("%a", author), content)); if (commandSource != null) { commandSource.sendMessage(formattedMessage); diff --git a/src/main/java/com/nguyenquyhy/spongediscord/utils/Emoji.java b/src/main/java/com/nguyenquyhy/spongediscord/utils/Emoji.java new file mode 100644 index 0000000..f83d5cc --- /dev/null +++ b/src/main/java/com/nguyenquyhy/spongediscord/utils/Emoji.java @@ -0,0 +1,34 @@ +package com.nguyenquyhy.spongediscord.utils; + +/** + * Created by Hy on 8/29/2016. + */ +public enum Emoji { + Smiley(":smiley:", ":)", "\ud83d\ude03"), + Smile(":smile:", ":D", "\ud83d\ude04"), + Joy(":joy:", ";D", "\ud83d\ude02"), + Laughing(":laughing:", "xD", "\ud83d\ude06"), + Frowning(":frowning:", ":(", "\ud83d\ude26"), + Sob(":sob:", ";(", "\ud83d\ude2d"), + TiredFace(":tired_face:", "x(", "\ud83d\ude2b"), + Wink(":wink:", ";)", "\ud83d\ude09"), + StuckOutTongue(":stuck_out_tongue:", ":P", "\ud83d\ude1b"), + StuckOutTongueWinkingEye(":stuck_out_tongue_winking_eye:", ";P", "\ud83d\ude1c"), + StuckOutTongueClosedEyes(":stuck_out_tongue_closed_eyes:", "xP", "\ud83d\ude1d"), + OpenMouth(":open_mouth:", ":O", "\ud83d\ude2e"), + DizzyFace(":dizzy_face:", "xO", "\ud83d\ude35"), + NeutralFace(":neutral_face:", ":|", "\ud83d\ude10"), + Sunglasses(":sunglasses:", "B)", "\ud83d\ude0e"), + Kissing(":kissing:", ":*", "\ud83d\ude17"), + Heart(":heart:", "<3", "\u2764"); + + public final String discordFormat; + public final String minecraftFormat; + public final String unicode; + + Emoji(String discordFormat, String minecraftFormat, String unicode) { + this.discordFormat = discordFormat; + this.minecraftFormat = minecraftFormat; + this.unicode = unicode; + } +} diff --git a/src/main/java/com/nguyenquyhy/spongediscord/utils/TextUtil.java b/src/main/java/com/nguyenquyhy/spongediscord/utils/TextUtil.java index dc1b7f4..f8014b8 100644 --- a/src/main/java/com/nguyenquyhy/spongediscord/utils/TextUtil.java +++ b/src/main/java/com/nguyenquyhy/spongediscord/utils/TextUtil.java @@ -24,7 +24,21 @@ public class TextUtil { private static final Pattern urlPattern = Pattern.compile("(?(^|\\s))(?(&[0-9a-flmnork])+)?(?(http(s)?://)?([A-Za-z0-9]+\\.)+[A-Za-z0-9]{2,}\\S*)", Pattern.CASE_INSENSITIVE); - public static final StyleTuple EMPTY = new StyleTuple(TextColors.NONE, TextStyles.NONE); + private static final StyleTuple EMPTY = new StyleTuple(TextColors.NONE, TextStyles.NONE); + + public static String formatDiscordEmoji(String message) { + for (Emoji emoji : Emoji.values()) { + message = message.replace(emoji.unicode, emoji.minecraftFormat); + } + return message; + } + + public static String formatMinecraftEmoji(String message) { + for (Emoji emoji : Emoji.values()) { + message = message.replace(emoji.minecraftFormat, emoji.discordFormat); + } + return message; + } public static Text formatUrl(String message) { Preconditions.checkNotNull(message, "message");