Skip to content

Commit

Permalink
v3.1.0
Browse files Browse the repository at this point in the history
* Replace PlaceholderAPI stuff after replacing prefixes. Fixes #119

* Add first time joining message. Fixes #113

* Fix rgb color codes not being replaced. Fixes #120

* Update dependencies in pom.xml
  • Loading branch information
TheJeterLP authored Jul 8, 2023
1 parent 01a9699 commit cc5e918
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 53 deletions.
8 changes: 4 additions & 4 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<name>ChatEx</name>
<inceptionYear>2022</inceptionYear>

<version>3.0.1</version>
<version>3.1.0</version>
<description>ChatManagement plugin for Bukkit</description>
<url>https://www.spigotmc.org/resources/chatex-continued.71041/</url>

Expand Down Expand Up @@ -91,7 +91,7 @@
<dependency>
<groupId>org.spigotmc</groupId>
<artifactId>spigot-api</artifactId>
<version>1.19.3-R0.1-SNAPSHOT</version>
<version>1.20.1-R0.1-SNAPSHOT</version>
<scope>provided</scope>
</dependency>
<dependency>
Expand Down Expand Up @@ -121,13 +121,13 @@
<dependency>
<groupId>org.purpurmc.purpur</groupId>
<artifactId>purpur-api</artifactId>
<version>1.19.2-R0.1-SNAPSHOT</version>
<version>1.20.1-R0.1-SNAPSHOT</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.bstats</groupId>
<artifactId>bstats-bukkit</artifactId>
<version>3.0.1-SNAPSHOT</version>
<version>3.0.2</version>
<scope>compile</scope>
</dependency>
<dependency>
Expand Down
8 changes: 7 additions & 1 deletion src/main/java/de/jeter/chatex/PlayerListener.java
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,13 @@ public class PlayerListener implements Listener {
@EventHandler(priority = EventPriority.LOWEST)
public void onJoin(PlayerJoinEvent e) {
if (Config.CHANGE_JOIN_AND_QUIT.getBoolean()) {
String msg = Locales.PLAYER_JOIN.getString(e.getPlayer());
String msg;
if(e.getPlayer().hasPlayedBefore()) {
msg = Locales.PLAYER_JOIN.getString(e.getPlayer());
} else {
msg = Locales.PLAYER_JOIN_FIRST_TIME.getString(e.getPlayer());
}

e.setJoinMessage(Utils.replacePlayerPlaceholders(e.getPlayer(), msg));
}

Expand Down
1 change: 1 addition & 0 deletions src/main/java/de/jeter/chatex/utils/Locales.java
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ public enum Locales {
COMMAND_RESULT_WRONG_USAGE("Messages.CommandResult.WrongUsage", "&c[ERROR] &7Wrong usage! Please type &6%cmd help&7!"),
ANTI_SPAM_DENIED("Messages.AntiSpam.Denied", "&e[AntiSpam] &7You are not allowed to spam! Please wait another &e%time% &7seconds!"),
PLAYER_JOIN("Messages.Player.Join", "%prefix%displayname%suffix &ejoined the game!"),
PLAYER_JOIN_FIRST_TIME("Messages.Player.JoinFirstTime", "%prefix%displayname%suffix &ejoined the server for the first time!"),
PLAYER_KICK("Messages.Player.Kick", "%prefix%displayname%suffix &ewas kicked from the game!"),
PLAYER_QUIT("Messages.Player.Quit", "%prefix%displayname%suffix &eleft the game!"),
NO_LISTENING_PLAYERS("Messages.Chat.NoOneListens", "&cNo players are near you to hear you talking! Use the ranged mode to chat."),
Expand Down
54 changes: 12 additions & 42 deletions src/main/java/de/jeter/chatex/utils/RGBColors.java
Original file line number Diff line number Diff line change
Expand Up @@ -93,49 +93,19 @@ private static boolean isNotSupported() {
}

public static String translateGradientCodes(String message) {
Pattern pattern = Pattern.compile("&g#([A-Fa-f0-9]{6})#([A-Fa-f0-9]{6})#([^&]+)&g");
Matcher matcher = pattern.matcher(message);
//for all matches
while (matcher.find()) {
String hexColor1 = "#" + matcher.group(1);
String hexColor2 = "#" + matcher.group(2);
String text = matcher.group(3);
message = message.replace(matcher.group(0), gradientText(text, hexColor1, hexColor2)+ ChatColor.RESET);
final Pattern hexPattern = Pattern.compile("#([A-Fa-f0-9]{6})");
Matcher matcher = hexPattern.matcher(message);
StringBuffer buffer = new StringBuffer(message.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 message;
}

public static String gradientText(String text, String hexColor1, String hexColor2) {
StringBuilder result = new StringBuilder();
int length = text.length();
int[][] rgbValues = new int[2][3];
float[][] colorSteps = new float[2][3];
int[] currentRGB = new int[3];

rgbValues[0][0] = Integer.parseInt(hexColor1.substring(1, 3), 16);
rgbValues[0][1] = Integer.parseInt(hexColor1.substring(3, 5), 16);
rgbValues[0][2] = Integer.parseInt(hexColor1.substring(5, 7), 16);

rgbValues[1][0] = Integer.parseInt(hexColor2.substring(1, 3), 16);
rgbValues[1][1] = Integer.parseInt(hexColor2.substring(3, 5), 16);
rgbValues[1][2] = Integer.parseInt(hexColor2.substring(5, 7), 16);

for (int i = 0; i < 3; i++) {
colorSteps[0][i] = (float)(rgbValues[1][i] - rgbValues[0][i]) / length;
currentRGB[i] = rgbValues[0][i];
}

for (int i = 0; i < length; i++) {
String hexColor = String.format("#%02x%02x%02x", currentRGB[0], currentRGB[1], currentRGB[2]);
result.append("&" + hexColor + text.charAt(i));

// Update RGB values for next character
for (int j = 0; j < 3; j++) {
currentRGB[j] += colorSteps[0][j];
}
}

return result.toString();
return matcher.appendTail(buffer).toString();
}

}
13 changes: 7 additions & 6 deletions src/main/java/de/jeter/chatex/utils/Utils.java
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,13 @@ public static String replacePlayerPlaceholders(Player player, String format) {
}
String result = format;

result = result.replace("%displayname", player.getDisplayName());
result = result.replace("%prefix", PluginManager.getInstance().getPrefix(player));
result = result.replace("%suffix", PluginManager.getInstance().getSuffix(player));
result = result.replace("%player", player.getName());
result = result.replace("%world", player.getWorld().getName());
result = result.replace("%group", PluginManager.getInstance().getGroupNames(player).length > 0 ? PluginManager.getInstance().getGroupNames(player)[0] : "none");

if (HookManager.checkPlaceholderAPI()) {
LogHelper.debug("PlaceholderAPI is installed! Replacing...");
result = PlaceholderAPI.setPlaceholders(player, result);
Expand All @@ -71,12 +78,6 @@ public static String replacePlayerPlaceholders(Player player, String format) {
result = result.replace("%afk", "");
}

result = result.replace("%displayname", player.getDisplayName());
result = result.replace("%prefix", PluginManager.getInstance().getPrefix(player));
result = result.replace("%suffix", PluginManager.getInstance().getSuffix(player));
result = result.replace("%player", player.getName());
result = result.replace("%world", player.getWorld().getName());
result = result.replace("%group", PluginManager.getInstance().getGroupNames(player).length > 0 ? PluginManager.getInstance().getGroupNames(player)[0] : "none");
result = replaceColors(result);

return result;
Expand Down

0 comments on commit cc5e918

Please # to comment.