Compare commits

...

6 Commits

Author SHA1 Message Date
Teriuihi 42736bd566 Update APRIL_FOOLS_RESET string in Config class
APRIL_FOOLS_RESET string in Config class has been updated from "reverse" to "esrever". This change updates the behavior of the April Fools feature in the chat application.
2024-03-31 14:53:36 +02:00
Teriuihi 157edbd6b6 Merge branch 'main' into april_fools 2024-03-31 13:37:42 +02:00
Teriuihi a7e029b0a1 Refactor April 1st date check in ChatListener
Removed the more complex date logic used to check for April 1st in the ChatListener class. This check checked if it was April 1st in any timezone. Replaced it with a simpler check using LocalDate's methods for month and day comparison. This only checks if it's April 1st in UTC. This aims to enhance code readability and simplify date handling.
2024-03-31 10:56:21 +02:00
Teriuihi ab98222f06 Refactor April 1st check in ChatListener class
A new method, `isWithinApril1st`, is introduced to simplify April 1st date check logic in the `ChatListener` class. This new method calculates the start and end of April 1st for all timezones in UTC and determines if the current time falls within this range.
2024-03-24 17:24:24 +01:00
Teriuihi ad91cda0c0 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.
2024-03-24 17:12:55 +01:00
Teriuihi 9270423928 Add reverse chat feature for April Fools' and corresponding tests
This commit includes a new feature that reverses the text speech of chat users on April 1st as an April Fools' prank. It also includes a reset option, configurable via a new parameter in the configuration file. Furthermore, it provides tests for the reverse string functionality, ensuring that it works correctly, even with complex strings that include tags.
2024-03-24 16:58:09 +01:00
5 changed files with 145 additions and 4 deletions

View File

@ -8,6 +8,9 @@ dependencies {
}
compileOnly("org.spongepowered:configurate-yaml:4.1.2") // Configurate
compileOnly("net.luckperms:api:5.3") // Luckperms
testImplementation("org.junit.jupiter:junit-jupiter-api:5.7.0")
testRuntimeOnly("org.junit.jupiter:junit-jupiter-engine:5.7.0")
testImplementation("com.alttd:Galaxy-API:1.19.2-R0.1-SNAPSHOT")
}
publishing {
@ -24,4 +27,8 @@ publishing {
credentials(PasswordCredentials::class)
}
}
}
tasks.test {
useJUnitPlatform()
}

View File

@ -543,4 +543,9 @@ public final class Config {
NICK_ALLOWED_COLOR_CODESLIST = getList("nicknames.allowed-color-codes", List.of("&0", "&1", "&2", "&3", "&4", "&5", "&6", "&7", "&8", "&9", "&a", "&b", "&c", "&d", "&e", "&f", "&r"));
NICK_CURRENT = getString("nicknames.messages.nick-current", NICK_CURRENT);
}
public static String APRIL_FOOLS_RESET = "esrever";
private static void aprilFools() {
APRIL_FOOLS_RESET = getString("april-fools.reset", APRIL_FOOLS_RESET);
}
}

View File

@ -1,12 +1,14 @@
package com.alttd.chat.objects;
import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.JoinConfiguration;
import net.kyori.adventure.text.TextComponent;
import net.kyori.adventure.text.TextReplacementConfig;
import net.kyori.adventure.text.serializer.plain.PlainTextComponentSerializer;
import org.intellij.lang.annotations.RegExp;
import javax.annotation.RegEx;
import java.util.regex.Pattern;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;
public class ModifiableString {
private Component text;
@ -30,4 +32,37 @@ public class ModifiableString {
public Component component() {
return text;
}
public void reverse() {
text = reverseComponent(text);
}
public Component reverseComponent(Component component) {
if (!(component instanceof TextComponent textComponent)) {
return Component.text("")
.append(Component.join(JoinConfiguration.noSeparators(), reverseChildren(component.children())));
}
String content = textComponent.content();
String reversedContent = new StringBuilder(content).reverse().toString();
List<Component> reversedChildren = reverseChildren(component.children());
return Component.text("")
.append(Component.join(JoinConfiguration.noSeparators(), reversedChildren)
.append(Component.text(reversedContent, component.style())));
}
public List<Component> reverseChildren(List<Component> children) {
return children.stream()
.map(this::reverseComponent)
.collect(Collectors.collectingAndThen(Collectors.toList(), list -> {
Collections.reverse(list);
return list;
}));
}
public void removeStringAtStart(String s) {
text = text.replaceText(TextReplacementConfig.builder().match("^" + s).replacement("").build());
}
}

View File

@ -0,0 +1,86 @@
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;
import net.kyori.adventure.text.minimessage.tag.resolver.TagResolver;
import net.kyori.adventure.text.serializer.plain.PlainTextComponentSerializer;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertEquals;
import net.kyori.adventure.text.minimessage.MiniMessage;
public class ReverseTest {
@Test
public void testReverseString() {
String input = "Hello how are you doing today?";
String expectedOutput = new StringBuilder(input).reverse().toString();
MiniMessage miniMessage = MiniMessage.miniMessage();
ModifiableString modifiableString = new ModifiableString(miniMessage.deserialize(input));
modifiableString.reverse();
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>?";
MiniMessage miniMessage = MiniMessage.miniMessage();
Component deserialize = miniMessage.deserialize(input);
ModifiableString modifiableString = new ModifiableString(deserialize);
String expectedOutput = new StringBuilder(PlainTextComponentSerializer.plainText().serialize(deserialize)).reverse().toString();
modifiableString.reverse();
assertEquals(expectedOutput, modifiableString.string());
}
@Test
public void complexTestReverseStringWithTags() {
String input = "<green><red>Hello <b>how</b> are</red> you <blue>doing today</blue><gold>?</gold></green>";
MiniMessage miniMessage = MiniMessage.miniMessage();
Component deserialize = miniMessage.deserialize(input);
ModifiableString modifiableString = new ModifiableString(deserialize);
String expectedOutput = new StringBuilder(PlainTextComponentSerializer.plainText().serialize(deserialize)).reverse().toString();
modifiableString.reverse();
assertEquals(expectedOutput, modifiableString.string());
}
@Test
public void extraComplexTestReverseStringWithTags() {
String input = "<gold>This <red>is</red> longer<green> <name> <red>Hello <b>how</b> are</red> you <test> <blue>doing <name> today</blue><gold>?</gold></green></gold>";
MiniMessage miniMessage = MiniMessage.miniMessage();
Component deserialize = miniMessage.deserialize(input, TagResolver.resolver(
Placeholder.component("name", miniMessage.deserialize("<red>Cool<blue><rainbow>_player_</rainbow>name</red>")),
Placeholder.parsed("test", "test replacement")
));
ModifiableString modifiableString = new ModifiableString(deserialize);
String expectedOutput = new StringBuilder(PlainTextComponentSerializer.plainText().serialize(deserialize)).reverse().toString();
modifiableString.reverse();
System.out.println(expectedOutput);
assertEquals(expectedOutput, modifiableString.string());
}
}

View File

@ -29,6 +29,7 @@ import org.bukkit.event.EventPriority;
import org.bukkit.event.Listener;
import org.jetbrains.annotations.NotNull;
import java.time.*;
import java.util.HashSet;
import java.util.Set;
import java.util.regex.Pattern;
@ -107,7 +108,14 @@ 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) {
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) {
receiver.sendMessage(input);