From 325ece3594e0f2c1cb305ff68b4b3958e1ec09f4 Mon Sep 17 00:00:00 2001 From: Etzelia Date: Wed, 2 Oct 2019 23:26:06 -0500 Subject: [PATCH] Add Discord syntax and fix some small bugs Signed-off-by: Etzelia --- .../etztech/qol/commands/ColorsCommand.java | 2 +- .../listeners/AsyncPlayerChatListener.java | 50 ++++++++++++++++--- src/main/resources/config.yml | 2 +- src/main/resources/plugin.yml | 3 ++ 4 files changed, 47 insertions(+), 10 deletions(-) diff --git a/src/main/java/xyz/etztech/qol/commands/ColorsCommand.java b/src/main/java/xyz/etztech/qol/commands/ColorsCommand.java index 7b5c79a..e30d65f 100644 --- a/src/main/java/xyz/etztech/qol/commands/ColorsCommand.java +++ b/src/main/java/xyz/etztech/qol/commands/ColorsCommand.java @@ -29,7 +29,7 @@ public class ColorsCommand implements CommandExecutor { ChatColor.DARK_GRAY + "\n&8 - Dark Gray" + ChatColor.BLUE + "\n&9 - Blue" + ChatColor.GREEN + "\n&a - Green" + - ChatColor.AQUA + "\n&b - Aqu" + + ChatColor.AQUA + "\n&b - Aqua" + ChatColor.RED + "\n&c - Red" + ChatColor.LIGHT_PURPLE + "\n&d - Light Purple" + ChatColor.YELLOW + "\n&e - Yellow" + diff --git a/src/main/java/xyz/etztech/qol/listeners/AsyncPlayerChatListener.java b/src/main/java/xyz/etztech/qol/listeners/AsyncPlayerChatListener.java index c5ecff6..08c7e2d 100644 --- a/src/main/java/xyz/etztech/qol/listeners/AsyncPlayerChatListener.java +++ b/src/main/java/xyz/etztech/qol/listeners/AsyncPlayerChatListener.java @@ -1,23 +1,28 @@ package xyz.etztech.qol.listeners; -import org.apache.commons.lang.StringUtils; -import org.bukkit.Bukkit; +import org.bukkit.ChatColor; import org.bukkit.entity.Player; import org.bukkit.event.EventHandler; import org.bukkit.event.EventPriority; import org.bukkit.event.Listener; import org.bukkit.event.player.AsyncPlayerChatEvent; -import org.bukkit.event.server.ServerListPingEvent; -import org.bukkit.util.CachedServerIcon; import xyz.etztech.qol.QoL; -import javax.imageio.ImageIO; -import java.awt.*; -import java.awt.image.BufferedImage; -import java.io.File; +import java.util.Arrays; +import java.util.List; +import java.util.regex.Matcher; +import java.util.regex.Pattern; public class AsyncPlayerChatListener implements Listener { + private final List discordSyntax = Arrays.asList( + Pattern.compile("__([^_]+)__"), // 0: __underline__ + Pattern.compile("_([^_]+)_"), // 1: _italic_ + Pattern.compile("\\*\\*([^*]+)\\*\\*"), // 2: **bold** + Pattern.compile("\\*([^*]+)\\*"), // 3: *italic* + Pattern.compile("~~([^~]+)~~") // 4: ~~strikethrough~~ + ); + private QoL plugin; public AsyncPlayerChatListener(QoL plugin) { @@ -36,6 +41,35 @@ public class AsyncPlayerChatListener implements Listener { if (QoL.getTimeout() && !player.hasPermission("qol.timeout.bypass")) { event.getRecipients().clear(); } + + // Discord syntax for font emphasis + if (player.hasPermission("qol.discord")) { + String chat = event.getMessage(); + Pattern syntax; + Matcher matcher; + for (int i = 0; i < discordSyntax.size(); i++) { + syntax = discordSyntax.get(i); + matcher = syntax.matcher(chat); + switch (i) { + case 0: + chat = matcher.replaceAll("&n$1&r"); + break; + case 1: + case 3: + chat = matcher.replaceAll("&o$1&r"); + break; + case 2: + chat = matcher.replaceAll("&l$1&r"); + break; + case 4: + chat = matcher.replaceAll("&m$1&r"); + break; + default: + break; + } + } + event.setMessage(ChatColor.translateAlternateColorCodes('&', chat)); + } } diff --git a/src/main/resources/config.yml b/src/main/resources/config.yml index 9fa67eb..aaa9b13 100644 --- a/src/main/resources/config.yml +++ b/src/main/resources/config.yml @@ -92,4 +92,4 @@ disable-fire: # Overrides view distance per world, format is [World Name]: [View Distance] view-distances: - # world: 2 + world: 2 diff --git a/src/main/resources/plugin.yml b/src/main/resources/plugin.yml index 10c41d1..ce6acc7 100644 --- a/src/main/resources/plugin.yml +++ b/src/main/resources/plugin.yml @@ -49,6 +49,9 @@ permissions: qol.admin: description: Ability to reload the plugin default: op + qol.discord: + description: Ability to use limited Discord syntax for chat formatting + default: op qol.priority: description: Allows a player on past the player cap default: op