Skip to content

Commit

Permalink
Transfer emoji
Browse files Browse the repository at this point in the history
  • Loading branch information
nguyenquyhy committed Aug 29, 2016
1 parent f9cb0e3 commit ab463b5
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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> player = event.getCause().first(Player.class);
if (player.isPresent()) {
UUID playerId = player.get().getUniqueId();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
34 changes: 34 additions & 0 deletions src/main/java/com/nguyenquyhy/spongediscord/utils/Emoji.java
Original file line number Diff line number Diff line change
@@ -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;
}
}
16 changes: 15 additions & 1 deletion src/main/java/com/nguyenquyhy/spongediscord/utils/TextUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,21 @@ public class TextUtil {
private static final Pattern urlPattern =
Pattern.compile("(?<first>(^|\\s))(?<colour>(&[0-9a-flmnork])+)?(?<url>(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");
Expand Down

0 comments on commit ab463b5

Please sign in to comment.