Fix same case rename

This commit is contained in:
NotAlexNoyle committed 2026-08-20 11:33:28 -04:00
1 parent 00c08e6eb5
commit a3993384d9
6 files changed
+117 -2

No files matched your search

+1
View File
@@ -91,6 +91,7 @@ dependencies {
compileOnly("com.github.TechFortress:GriefPrevention:17.0.0")
compileOnly("com.discordsrv:discordsrv:1.28.0")
implementation("com.google.code.gson:gson:2.10.1")
testImplementation("org.purpurmc.purpur:purpur-api:1.19.4-R0.1-SNAPSHOT")
testImplementation("org.junit.jupiter:junit-jupiter-api:5.11.3")
testRuntimeOnly("org.junit.jupiter:junit-jupiter-engine:5.11.3")
testImplementation("org.junit.jupiter:junit-jupiter-params:5.11.3")
@@ -199,6 +199,25 @@ public class Union implements Serializable, Comparable<Union> {
}
/**
* Renames the union. When the new name is just the tag in a different case, the
* displayed tag is re-cased to match, so a union can fix its capitalization
* even though the tag itself can never change.
*
* @param name the new name
*/
public void rename(String name) {
if (colorTag != null && Helper.cleanTag(name).equals(tag)) {
colorTag = ChatUtils.applyCase(colorTag, ChatUtils.stripColors(name));
}
this.name = name;
}
/**
* Returns the union's description
*
@@ -399,8 +399,13 @@ public class StaffCommands extends BaseCommand {
}
Union union = unionInput.getUnion();
union.setName(unionName);
union.rename(unionName);
storage.updateUnion(union);
for (UnionPlayer member : union.getOnlineMembers()) {
cm.updateDisplayName(member.toPlayer());
}
ChatBlock.sendMessageKey(sender, "you.have.successfully.renamed.the.union", unionName);
@@ -297,8 +297,14 @@ public final class ProposalManager {
union.addBb(proposer != null ? proposer.getName() : union.getName(),
lang("union.renamed.to.0", proposal.getTarget()));
union.setName(proposal.getTarget());
union.rename(proposal.getTarget());
plugin.getStorageManager().updateUnion(union);
for (UnionPlayer member : union.getOnlineMembers()) {
plugin.getUnionManager().updateDisplayName(member.toPlayer());
}
break;
}
@@ -89,6 +89,46 @@ public class ChatUtils {
}
/**
* Replaces the visible characters of a colored string with the characters of a
* plain one, keeping every color code in place. The plain string is expected to
* match the colored string's stripped text ignoring case; leftover characters
* on either side are kept as they are.
*
* @param colored the colored string to take the color codes from
* @param plain the plain string to take the characters from
* @return the colored string with the plain string's characters
*/
public static String applyCase(@NotNull String colored, @NotNull String plain) {
Pattern pattern = HEX_COLOR_SUPPORT ? HEX_STRIP_COLOR_PATTERN : STRIP_COLOR_PATTERN;
Matcher matcher = pattern.matcher(colored);
StringBuilder result = new StringBuilder();
int from = 0;
int at = 0;
while (matcher.find()) {
for (int i = from; i < matcher.start(); i++) {
result.append(at < plain.length() ? plain.charAt(at++) : colored.charAt(i));
}
result.append(matcher.group());
from = matcher.end();
}
for (int i = from; i < colored.length(); i++) {
result.append(at < plain.length() ? plain.charAt(at++) : colored.charAt(i));
}
return result.toString();
}
public static String getLastColorCode(String msg) {
if (msg.length() < 2) {
@@ -0,0 +1,44 @@
package net.trueog.unionsog.utils;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertEquals;
class ChatUtilsApplyCaseTest {
@Test
void recasesPlainTag() {
assertEquals("OGS", ChatUtils.applyCase("OGs", "OGS"));
}
@Test
void keepsLeadingColorCode() {
assertEquals("§bOGS", ChatUtils.applyCase("§bOGs", "OGS"));
}
@Test
void keepsEmbeddedColorCodes() {
assertEquals("§4O§6GS", ChatUtils.applyCase("§4O§6Gs", "OGS"));
}
@Test
void keepsHexColorCode() {
assertEquals("§x§f§f§0§0§0§0OGS", ChatUtils.applyCase("§x§f§f§0§0§0§0OGs", "OGS"));
}
@Test
void keepsLeftoverCharactersWhenLengthsDiffer() {
assertEquals("OGs", ChatUtils.applyCase("OGs", "OG"));
}
}