Skip to content

Commit

Permalink
Added a tags configuration with a new system for checking colors and …
Browse files Browse the repository at this point in the history
…gradients
  • Loading branch information
DoubleNico committed Jul 15, 2023
1 parent 9cd6c45 commit 3a9fead
Show file tree
Hide file tree
Showing 16 changed files with 354 additions and 36 deletions.
7 changes: 6 additions & 1 deletion HypeGradients-API/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<parent>
<artifactId>HypeGradients</artifactId>
<groupId>me.doublenico</groupId>
<version>1.0.7</version>
<version>1.0.8</version>
</parent>
<artifactId>HypeGradients-API</artifactId>
<modelVersion>4.0.0</modelVersion>
Expand All @@ -31,5 +31,10 @@
<version>v2.0.3</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter</artifactId>
<version>5.8.2</version>
</dependency>
</dependencies>
</project>
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@
import java.util.regex.Pattern;

public class ChatGradient {
private final String gradientRegex = "<gradient:((?:(?=#[a-fA-F\\d]{6}[;>])#[a-fA-F\\d]{6}[^>]?)+)>(.+?)<.gradient>";
// regex given by @PerryPlaysMC and edited by me Thanks!
private final String gradientRegex = "<gradient:((?:(?=#[a-fA-F\\d]{6}[;>])#[a-fA-F\\d]{6}[^>]?)+)>(.+?)<\\/gradient>";
private String message;

public ChatGradient(String message) {
Expand All @@ -24,9 +25,8 @@ public String translateGradient() {
IDynamicConfiguration settings = ConfigurationManager.getInstance().getConfiguration("settings").getConfig();
if (settings == null) return new GradientLogger("Settings configuration is null").warn(message);
if (settings.getBoolean("colors", true)) this.message = (new ColorChat(this.message)).replaceColors();

this.message = ChatColor.translateAlternateColorCodes('&', this.message);
Pattern pattern = Pattern.compile(gradientRegex);
Pattern pattern = Pattern.compile(getGradientRegex());
Matcher matcher = pattern.matcher(this.message);
while (matcher.find()) {
String gradient = matcher.group(1);
Expand All @@ -40,7 +40,14 @@ public String translateGradient() {
continue;
}
List<CColor> gradients = new ArrayList<>();
for (String color : gradient.split(";"))
IDynamicConfiguration tags = ConfigurationManager.getInstance().getConfiguration("tags").getConfig();
String separator;
if (tags == null) {
new GradientLogger("Tags configuration is null, we will use default separator").warn();
separator = ";";
} else if (tags.getBoolean("gradient.useDefault", false)) separator = ";";
else separator = tags.getString("gradient.separator", ";");
for (String color : gradient.split(separator))
gradients.add(CColor.fromHex(color));
if (gradients.size() < 2) {
new GradientLogger("Gradient is not valid!").warn();
Expand All @@ -54,21 +61,40 @@ public String translateGradient() {
public boolean isGradient() {
if (this.message == null)
return false;
this.message = (new ColorChat(this.message)).replaceColors();
Pattern pattern = Pattern.compile(gradientRegex);
Matcher matcher = pattern.matcher(this.message);
String check = (new ColorChat(this.message)).replaceColors();
Pattern pattern = Pattern.compile(getGradientRegex());
Matcher matcher = pattern.matcher(check);
return matcher.find();
}

public boolean isGradientTeam() {
if (this.message == null)
return false;
this.message = (new ColorChat(this.message)).replaceColors();
Pattern pattern = Pattern.compile(gradientRegex);
Pattern pattern = Pattern.compile(getGradientRegex());
Matcher matcher = pattern.matcher(ChatColor.stripColor(this.message));
return matcher.find();
}

private String getGradientRegex() {
IDynamicConfiguration tags = ConfigurationManager.getInstance().getConfiguration("tags").getConfig();
if (tags == null)
return new GradientLogger("Tags configuration is null, we will use default regex").warn(gradientRegex);
if (tags.getBoolean("gradient.useDefault", false)) return gradientRegex;
String prefix = tags.getString("gradient.prefix", "<gradient:");
String prefixEnd = tags.getString("gradient.prefixEnd", ">");
String separator = tags.getString("gradient.separator", ";");
String suffix = tags.getString("gradient.suffix", "</gradient>");
char[] regexSymbols = {'.', '*', '+', '?', '^', '$', '[', ']', '-', '(', ')', '|', '/', '{', '}'};
for (char symbol : regexSymbols) {
prefix = prefix.replace(String.valueOf(symbol), "\\" + symbol);
prefixEnd = prefixEnd.replace(String.valueOf(symbol), "\\" + symbol);
separator = separator.replace(String.valueOf(symbol), "\\" + symbol);
suffix = suffix.replace(String.valueOf(symbol), "\\" + symbol);
}
return prefix + "((?:(?=#[a-fA-F\\d]{6}[" + separator + prefixEnd + "].)#[a-fA-F\\d]{6}[^" + prefixEnd + "]?)+)" + prefixEnd + "(.+?)" + suffix;
}

public String getMessage() {
return message;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,38 @@
import net.md_5.bungee.chat.ComponentSerializer;

public class ChatJson {
/**
* This variable represents a private final string message.
* <p>
* It is used to store a message that cannot be modified once assigned.
*/
private final String message;

/**
* Creates a ChatJson object with the given message.
*
* @param message The message to include in the ChatJson object.
*/
public ChatJson(String message) {
this.message = message;
}

/**
* Converts the message to JSON format.
*
* @return The message in JSON format. If the message is null, returns an empty string.
*/
public String convertToJson() {
if (this.message == null)
return "";
return ComponentSerializer.toString(TextComponent.fromLegacyText(this.message));
}

/**
* Converts the given message to a legacy string representation.
*
* @return the converted string representation of the message, If the message is null, returns an empty string
*/
public String convertToString() {
if (this.message == null)
return "";
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
package me.doublenico.hypegradients.api.chat;

import dev.dynamicstudios.json.data.util.CColor;
import dev.perryplaysmc.dynamicconfigurations.IDynamicConfiguration;
import dev.perryplaysmc.dynamicconfigurations.IDynamicConfigurationSection;
import me.doublenico.hypegradients.api.GradientLogger;
import me.doublenico.hypegradients.api.configuration.ConfigurationManager;
import org.bukkit.command.CommandSender;

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

public record ColorChat(String message) {

public String replaceColors() {
Expand All @@ -19,7 +23,45 @@ public String replaceColors() {
String color = section.getString(key);
if (color == null)
continue;
message = message.replaceAll("<" + key + ">", "#" + color);
IDynamicConfiguration tags = ConfigurationManager.getInstance().getConfiguration("tags").getConfig();
String replaced = message.replaceAll("<" + key + ">", "#" + color);
if (tags == null) {
new GradientLogger("Tags configuration is null using default system!").warn();
message = replaced;
continue;
}
if (tags.getBoolean("color.useDefault", true)) {
message = replaced;
continue;
}
String tag = tags.getString("color.tag");
if (tag == null) {
new GradientLogger("Tag is null using default system!").warn();
message = replaced;
continue;
}
if (!tag.contains("%tag%")) {
new GradientLogger("Tag does not contain %tag% using default system!").warn();
message = replaced;
continue;
}
String regexPattern = "(.*)%tag%(.*)";
Pattern pattern = Pattern.compile(regexPattern);
Matcher matcher = pattern.matcher(tag);
if (matcher.find()) {
String prefix = matcher.group(1);
String suffix = matcher.group(2);
message = message.replace(prefix + key + suffix, "#" + color);
}
/* String regexPattern = "(.*)" + key + "(.*)";
Pattern pattern = Pattern.compile(regexPattern);
Matcher matcher = pattern.matcher(message);
if (matcher.find()){
String prefix = matcher.group(1);
String suffix = matcher.group(2);
message = message.replaceAll(prefix + key + suffix, "#" + color);
}*/

}
return message;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package me.doublenico.hypegradients.api.chat;

import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.assertTrue;

public class ChatGradientTest {

@Test
void testFirstRegexCase() {
String regex = ".+?((?:(?=#[a-fA-F\\d]{6}[;\\]].)#[a-fA-F\\d]{6}[^\\]]?)+)](.+?)\\[\\/gradient\\]";
String test = "[gradient:#ff0000;#00ff00;#0000ff]Hello World![/gradient]";
assertTrue(test.matches(regex));
}

@Test
void testSecondRegexCase() {
String regex = ".+?((?:(?=#[a-fA-F\\d]{6}[;\\}].)#[a-fA-F\\d]{6}[^\\}]?)+)}(.+?)\\{\\/beep\\}";
String test = "{beep:#ff0000;#00ff00;#0000ff}Hello World!{/beep}";
assertTrue(test.matches(regex));
}

@Test
void testThirdRegexCase() {
String prefix = "<gradient:";
String prefixEnd = "]";
String separator = ";";
String suffix = "<\\/gradient>";
String regex = prefix + "((?:(?=#[a-fA-F\\d]{6}[" + separator + "\\" + prefixEnd + "].)#[a-fA-F\\d]{6}[^\\" + prefixEnd + "]?)+)" + prefixEnd + "(.+?)" + suffix;
String test = "<gradient:#ff0000;#00ff00;#0000ff]Hello World!</gradient>";
assertTrue(test.matches(regex));
}


}
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
package me.doublenico.hypegradients.api.chat;

import org.junit.jupiter.api.Test;

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

import static org.junit.jupiter.api.Assertions.assertEquals;

class ColorChatTest {

@Test
void testFirstChatTag() {
String message = "<red>";
String tag = message.replace("<", "").replace(">", "");
assertEquals("red", tag);
}

@Test
void testSecondChatTag() {
String message = "<red>";
String hex = "#ff0000";
String tag = message.replace("<", "").replace(">", "").replace("red", hex);
assertEquals("#ff0000", tag);
}

@Test
void testThirdChatTag() {
String message = "<color:red>";
String hex = "#ff0000";
String tag = message.replace("<color:", "").replace(">", "").replace("red", hex);
assertEquals("#ff0000", tag);
}

@Test
void testFourthChatTag() {
String message = "<color:red>";
String prefix = "<color:";
String suffix = ">";
String hex = "#ff0000";
String tag = message.replace(prefix, "").replace(suffix, "").replace("red", hex);
assertEquals("#ff0000", tag);
}

@Test
void testFifthChatTag() {
String message = "~red";
String prefix = "~";
String suffix = "";
String hex = "#ff0000";
String tag = message.replace(prefix, "").replace(suffix, "").replace("red", hex);
assertEquals("#ff0000", tag);
}

@Test
void testSixthChatTag() {
String configValue = "<%tag%>";
String message = configValue.replace("%tag%", "red");
String prefix = message.substring(0, 1);
String suffix = message.substring(message.length() - 1);
String tag = message.replace(prefix, "").replace(suffix, "");
assertEquals("red", tag);
}

@Test
void testSeventhChatTag() {
String message = "[color:%tag%]";
String tag = "%tag%";
String regexPattern = "(.*)\\" + tag + "(.*)";

Pattern pattern = Pattern.compile(regexPattern);

Matcher matcher = pattern.matcher(message);

if (matcher.find()) {
String prefix = matcher.group(1);
String suffix = matcher.group(2);

assertEquals("[color:", prefix);
assertEquals("]", suffix);
return;
}
System.out.println("No match found");
}

@Test
void testEighthColorTag() {
String message = "[%tag%]";
String tag = "%tag%";
String regexPattern = "(.*)" + tag + "(.*)";

Pattern pattern = Pattern.compile(regexPattern);

Matcher matcher = pattern.matcher(message);

if (matcher.find()) {
String prefix = matcher.group(1);
String suffix = matcher.group(2);

assertEquals("[", prefix);
assertEquals("]", suffix);
return;
}
System.out.println("No match found");
}
}
Loading

0 comments on commit 3a9fead

Please sign in to comment.