Skip to content

Commit

Permalink
Add RGB support for 1.16 and BungeeCord
Browse files Browse the repository at this point in the history
  • Loading branch information
mnewt00 committed May 17, 2021
1 parent fc16d09 commit 38ff716
Show file tree
Hide file tree
Showing 9 changed files with 52 additions and 8 deletions.
2 changes: 1 addition & 1 deletion BetterStaffChat-bungeecord/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
<parent>
<groupId>dev.austech</groupId>
<artifactId>BetterStaffChat</artifactId>
<version>1.0.1</version>
<version>1.0.2</version>
</parent>

<artifactId>BetterStaffChat-bungeecord</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ public final class BetterStaffChatBungeeCord extends Plugin {
public void onEnable() {
instance = this;

TextUtil.init(false, this);
Config.load();

if (getConfig().getBoolean("check-for-updates"))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ public void onPlayerChat(ChatEvent event) {
@EventHandler
public void onPlayerPostLogin(PostLoginEvent event) {
if (event.getPlayer().hasPermission("betterstaffchat.messages.join") && !BetterStaffChatBungeeCord.getInstance().getConfig().getString("staffchat.join").equals("")) {
if (event.getPlayer().getServer() == null) return;
BetterStaffChatBungeeCord.getInstance().getProxy().getScheduler().schedule(BetterStaffChatBungeeCord.getInstance(), () -> {
String message = TextUtil.colorize(
BetterStaffChatBungeeCord.getInstance().getConfig().getString("staffchat.join")
Expand All @@ -94,6 +95,7 @@ public void onPlayerPostLogin(PostLoginEvent event) {
@EventHandler
public void onPlayerDisconnect(PlayerDisconnectEvent event) {
if (event.getPlayer().hasPermission("betterstaffchat.messages.leave") && !BetterStaffChatBungeeCord.getInstance().getConfig().getString("staffchat.leave").equals("")) {
if (event.getPlayer().getServer() == null) return;
BetterStaffChatBungeeCord.getInstance().getProxy().getScheduler().schedule(BetterStaffChatBungeeCord.getInstance(), () -> {
String message = TextUtil.colorize(
BetterStaffChatBungeeCord.getInstance().getConfig().getString("staffchat.leave")
Expand Down
2 changes: 1 addition & 1 deletion BetterStaffChat-common/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
<parent>
<groupId>dev.austech</groupId>
<artifactId>BetterStaffChat</artifactId>
<version>1.0.1</version>
<version>1.0.2</version>
</parent>

<artifactId>BetterStaffChat-common</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,16 +22,54 @@
import net.md_5.bungee.api.ChatColor;
import net.md_5.bungee.api.chat.TextComponent;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

@UtilityClass
public final class TextUtil {
private String serverVersion = "";

public void init(boolean spigot, Object plugin) {
if (!spigot) {
serverVersion = "new enough";
} else {
try {
serverVersion = (String) plugin.getClass().getMethod("getVersion").invoke(plugin);
} catch (Exception exception) {
exception.printStackTrace();
}
}
}

/**
* Colors a specific message using the ChatColor API.
* @param string input
* @return Colored string
*/
public String colorize(String string) {
return ChatColor.translateAlternateColorCodes('&', string);
if (serverVersion.isEmpty()) {
throw new IllegalArgumentException("Server version has not been set.");
}
if (serverVersion.equals("new enough") || serverVersion.contains("1.16") || serverVersion.contains("1.17")) // TODO: Make this dynamic later
return ChatColor.translateAlternateColorCodes('&', colorizeRgb(string));
else return ChatColor.translateAlternateColorCodes('&', string);
}

/* Pulled right from https://www.spigotmc.org/threads/hex-chat-class.449300/ */
public String colorizeRgb(String string) {
final Pattern hexPattern = Pattern.compile("&#" + "([A-Fa-f0-9]{6})" + "");
Matcher matcher = hexPattern.matcher(string);
StringBuffer buffer = new StringBuffer(string.length() + 4 * 8);
while (matcher.find())
{
String group = matcher.group(1);
matcher.appendReplacement(buffer, ChatColor.COLOR_CHAR + "x"
+ ChatColor.COLOR_CHAR + group.charAt(0) + ChatColor.COLOR_CHAR + group.charAt(1)
+ ChatColor.COLOR_CHAR + group.charAt(2) + ChatColor.COLOR_CHAR + group.charAt(3)
+ ChatColor.COLOR_CHAR + group.charAt(4) + ChatColor.COLOR_CHAR + group.charAt(5)
);
}
return matcher.appendTail(buffer).toString();
}

public TextComponent colorizeToComponent(String string) {
Expand Down
2 changes: 1 addition & 1 deletion BetterStaffChat-package/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
<parent>
<artifactId>BetterStaffChat</artifactId>
<groupId>dev.austech</groupId>
<version>1.0.1</version>
<version>1.0.2</version>
</parent>
<modelVersion>4.0.0</modelVersion>

Expand Down
2 changes: 1 addition & 1 deletion BetterStaffChat-spigot/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
<parent>
<groupId>dev.austech</groupId>
<artifactId>BetterStaffChat</artifactId>
<version>1.0.1</version>
<version>1.0.2</version>
</parent>

<artifactId>BetterStaffChat-spigot</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@
import lombok.Setter;
import net.dv8tion.jda.api.JDABuilder;
import net.dv8tion.jda.api.entities.Activity;
import net.md_5.bungee.api.chat.TextComponent;
import org.bukkit.Bukkit;
import org.bukkit.command.CommandSender;
import org.bukkit.configuration.file.FileConfiguration;
Expand All @@ -47,7 +46,6 @@
import java.io.File;
import java.util.ArrayList;
import java.util.UUID;
import java.util.concurrent.TimeUnit;

public final class BetterStaffChatSpigot extends JavaPlugin {

Expand All @@ -62,6 +60,7 @@ public final class BetterStaffChatSpigot extends JavaPlugin {
public void onEnable() {
instance = this;

TextUtil.init(true, this);
Config.load();

if (getConfig().getBoolean("check-for-updates"))
Expand Down Expand Up @@ -134,6 +133,10 @@ public void logPrefixDebug(String string) {
if (getConfig().getBoolean("debug")) Bukkit.getConsoleSender().sendMessage("[BetterStaffChat] Debug - " + TextUtil.colorize(string));
}

public String getVersion() {
return Bukkit.getVersion();
}

@Override
public void onDisable() {
if (isDiscordEnabled()) (getJda()).shutdown();
Expand Down
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@

<groupId>dev.austech</groupId>
<artifactId>BetterStaffChat</artifactId>
<version>1.0.1</version>
<version>1.0.2</version>
<packaging>pom</packaging>

<properties>
Expand Down

0 comments on commit 38ff716

Please sign in to comment.