Discord Syntax (#36)

dynmap_link
Etzelia 2019-10-03 17:58:55 +02:00 committed by Gitea
parent af7abe9ca7
commit 253402f4fe
4 changed files with 47 additions and 10 deletions

View File

@ -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" +

View File

@ -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<Pattern> 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));
}
}

View File

@ -92,4 +92,4 @@ disable-fire:
# Overrides view distance per world, format is [World Name]: [View Distance]
view-distances:
# world: 2
world: 2

View File

@ -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