Add method to remove string at start

A new method `removeStringAtStart` has been added to the `ModifiableString` class. It replaces the starting string in a given text. Also, a condition has been modified in the `ChatListener` class to invoke this new method when the input string starts with the `APRIL_FOOLS_RESET` string. Additional unit tests were created to validate these changes.
This commit is contained in:
Teriuihi 2024-03-24 17:12:55 +01:00
parent 9270423928
commit ad91cda0c0
3 changed files with 36 additions and 2 deletions

View File

@ -61,4 +61,8 @@ public class ModifiableString {
return list;
}));
}
public void removeStringAtStart(String s) {
text = text.replaceText(TextReplacementConfig.builder().match("^" + s).replacement("").build());
}
}

View File

@ -1,3 +1,4 @@
import com.alttd.chat.config.Config;
import com.alttd.chat.objects.ModifiableString;
import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.minimessage.tag.resolver.Placeholder;
@ -20,6 +21,31 @@ public class ReverseTest {
assertEquals(expectedOutput, modifiableString.string());
}
@Test
public void testRemoveKeyword() {
String input = "Hello how are you doing today?";
MiniMessage miniMessage = MiniMessage.miniMessage();
ModifiableString modifiableString = new ModifiableString(miniMessage.deserialize(Config.APRIL_FOOLS_RESET + " " + input));
modifiableString.removeStringAtStart(Config.APRIL_FOOLS_RESET + " ");
assertEquals(input, modifiableString.string());
}
@Test
public void testRemoveKeywordWithTags() {
MiniMessage miniMessage = MiniMessage.miniMessage();
String input = "<blue>" + Config.APRIL_FOOLS_RESET + " <red>Hello how are</red> you <blue>doing today</blue>?</blue>";
String expectedOutput = "Hello how are you doing today?";
Component deserialize = miniMessage.deserialize(input);
ModifiableString modifiableString = new ModifiableString(deserialize);
modifiableString.removeStringAtStart(Config.APRIL_FOOLS_RESET + " ");
assertEquals(expectedOutput, modifiableString.string());
}
@Test
public void testReverseStringWithTags() {
String input = "<red>Hello how are</red> you <blue>doing today</blue>?";

View File

@ -110,8 +110,12 @@ public class ChatListener implements Listener {
Set<Player> playersToPing = new HashSet<>();
pingPlayers(playersToPing, modifiableString, player);
LocalDate now = LocalDate.now();
if (now.getMonth().equals(Month.APRIL) && now.getDayOfMonth() == 1 && !modifiableString.string().startsWith(Config.APRIL_FOOLS_RESET)) {
modifiableString.reverse();
if (now.getMonth().equals(Month.APRIL) && now.getDayOfMonth() == 1) {
if (modifiableString.string().startsWith(Config.APRIL_FOOLS_RESET + " ")) {
modifiableString.removeStringAtStart(Config.APRIL_FOOLS_RESET + " ");
} else {
modifiableString.reverse();
}
}
input = render(player, modifiableString.component());
for (Player receiver : receivers) {