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:
parent
9270423928
commit
ad91cda0c0
|
|
@ -61,4 +61,8 @@ public class ModifiableString {
|
||||||
return list;
|
return list;
|
||||||
}));
|
}));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void removeStringAtStart(String s) {
|
||||||
|
text = text.replaceText(TextReplacementConfig.builder().match("^" + s).replacement("").build());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,3 +1,4 @@
|
||||||
|
import com.alttd.chat.config.Config;
|
||||||
import com.alttd.chat.objects.ModifiableString;
|
import com.alttd.chat.objects.ModifiableString;
|
||||||
import net.kyori.adventure.text.Component;
|
import net.kyori.adventure.text.Component;
|
||||||
import net.kyori.adventure.text.minimessage.tag.resolver.Placeholder;
|
import net.kyori.adventure.text.minimessage.tag.resolver.Placeholder;
|
||||||
|
|
@ -20,6 +21,31 @@ public class ReverseTest {
|
||||||
assertEquals(expectedOutput, modifiableString.string());
|
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
|
@Test
|
||||||
public void testReverseStringWithTags() {
|
public void testReverseStringWithTags() {
|
||||||
String input = "<red>Hello how are</red> you <blue>doing today</blue>?";
|
String input = "<red>Hello how are</red> you <blue>doing today</blue>?";
|
||||||
|
|
|
||||||
|
|
@ -110,8 +110,12 @@ public class ChatListener implements Listener {
|
||||||
Set<Player> playersToPing = new HashSet<>();
|
Set<Player> playersToPing = new HashSet<>();
|
||||||
pingPlayers(playersToPing, modifiableString, player);
|
pingPlayers(playersToPing, modifiableString, player);
|
||||||
LocalDate now = LocalDate.now();
|
LocalDate now = LocalDate.now();
|
||||||
if (now.getMonth().equals(Month.APRIL) && now.getDayOfMonth() == 1 && !modifiableString.string().startsWith(Config.APRIL_FOOLS_RESET)) {
|
if (now.getMonth().equals(Month.APRIL) && now.getDayOfMonth() == 1) {
|
||||||
modifiableString.reverse();
|
if (modifiableString.string().startsWith(Config.APRIL_FOOLS_RESET + " ")) {
|
||||||
|
modifiableString.removeStringAtStart(Config.APRIL_FOOLS_RESET + " ");
|
||||||
|
} else {
|
||||||
|
modifiableString.reverse();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
input = render(player, modifiableString.component());
|
input = render(player, modifiableString.component());
|
||||||
for (Player receiver : receivers) {
|
for (Player receiver : receivers) {
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue
Block a user