diff --git a/src/main/java/me/dreamerzero/chatregulator/listener/chat/ChatListener.java b/src/main/java/me/dreamerzero/chatregulator/listener/chat/ChatListener.java index bd30eb3f..81fe726f 100644 --- a/src/main/java/me/dreamerzero/chatregulator/listener/chat/ChatListener.java +++ b/src/main/java/me/dreamerzero/chatregulator/listener/chat/ChatListener.java @@ -13,12 +13,13 @@ import me.dreamerzero.chatregulator.InfractionPlayer; import me.dreamerzero.chatregulator.config.Configuration; import me.dreamerzero.chatregulator.modules.Replacer; -import me.dreamerzero.chatregulator.objects.AtomicString; import me.dreamerzero.chatregulator.wrapper.event.ChatWrapper; import me.dreamerzero.chatregulator.wrapper.event.EventWrapper; import static me.dreamerzero.chatregulator.utils.GeneralUtils.*; +import java.util.concurrent.atomic.AtomicReference; + /** * ChatRegulator's Chat Listener */ @@ -40,7 +41,7 @@ public void onChat(final PlayerChatEvent event, final Continuation continuation) return; } final Player player = event.getPlayer(); - final AtomicString message = new AtomicString(event.getMessage()); + final AtomicReference message = new AtomicReference(event.getMessage()); final InfractionPlayer infractor = InfractionPlayer.get(player); final EventWrapper wrapper = new ChatWrapper(event, continuation); diff --git a/src/main/java/me/dreamerzero/chatregulator/listener/command/CommandListener.java b/src/main/java/me/dreamerzero/chatregulator/listener/command/CommandListener.java index 21d780bb..a47011d9 100644 --- a/src/main/java/me/dreamerzero/chatregulator/listener/command/CommandListener.java +++ b/src/main/java/me/dreamerzero/chatregulator/listener/command/CommandListener.java @@ -13,7 +13,6 @@ import me.dreamerzero.chatregulator.config.Configuration; import me.dreamerzero.chatregulator.modules.checks.CommandCheck; import me.dreamerzero.chatregulator.modules.checks.SyntaxCheck; -import me.dreamerzero.chatregulator.objects.AtomicString; import me.dreamerzero.chatregulator.utils.CommandUtils; import me.dreamerzero.chatregulator.utils.GeneralUtils.EventBundle; import me.dreamerzero.chatregulator.wrapper.event.CommandWrapper; @@ -23,6 +22,8 @@ import static me.dreamerzero.chatregulator.utils.GeneralUtils.*; +import java.util.concurrent.atomic.AtomicReference; + /** * Detections related to command execution by players */ @@ -56,7 +57,7 @@ public void onCommand(CommandExecuteEvent event, Continuation continuation){ return; } - final AtomicString command = new AtomicString(event.getCommand()); + final AtomicReference command = new AtomicReference<>(event.getCommand()); if(unicode(infractionPlayer, command, wrapper, plugin) || caps(infractionPlayer, command, wrapper, plugin) diff --git a/src/main/java/me/dreamerzero/chatregulator/objects/AtomicString.java b/src/main/java/me/dreamerzero/chatregulator/objects/AtomicString.java deleted file mode 100644 index 7c43c8a9..00000000 --- a/src/main/java/me/dreamerzero/chatregulator/objects/AtomicString.java +++ /dev/null @@ -1,90 +0,0 @@ -package me.dreamerzero.chatregulator.objects; - -import java.util.Objects; - -import org.jetbrains.annotations.NotNull; -import org.jetbrains.annotations.Nullable; - -/** - * String container for chains usage - */ -public final class AtomicString { - private String string; - - /** - * Creates a new AtomicString containing a string - * @param string the string - */ - public AtomicString(final @NotNull String string){ - this.string = Objects.requireNonNull(string); - } - - /** - * Concatenates the specified string to the end of this string - * - * @param string the concat string - * @return the concatenated string of the specified string - */ - public String concatAndGet(final @NotNull String string){ - this.string = this.string.concat(Objects.requireNonNull(string)); - return this.string; - } - - /** - * Replace the value of this string - * @param string the new string - */ - public void set(final @NotNull String string){ - this.string = Objects.requireNonNull(string); - } - - /** - * Get the old value and set the new value of the string - * @param string the new string - * @return the old value and set the new value of the string - */ - public String getAndSet(final @NotNull String string){ - final String oldString = this.string; - this.string = Objects.requireNonNull(string); - return oldString; - } - - /** - * Set the new value of the string and return the new value - * @param string the new value - * @return the new value - */ - public String setAndGet(final @NotNull String string){ - this.string = Objects.requireNonNull(string); - return this.string; - } - - /** - * Obtain the container string - * @return the string - */ - public String get(){ - return this.string; - } - - @Override - public boolean equals(final @Nullable Object o){ - if(this == o) { - return true; - } - if(!(o instanceof final AtomicString that)){ - return false; - } - return Objects.equals(that.string, this.string); - } - - @Override - public int hashCode(){ - return Objects.hash(this.string); - } - - @Override - public String toString(){ - return this.string; - } -} diff --git a/src/main/java/me/dreamerzero/chatregulator/utils/GeneralUtils.java b/src/main/java/me/dreamerzero/chatregulator/utils/GeneralUtils.java index 90d43c39..9e0dff66 100644 --- a/src/main/java/me/dreamerzero/chatregulator/utils/GeneralUtils.java +++ b/src/main/java/me/dreamerzero/chatregulator/utils/GeneralUtils.java @@ -1,6 +1,7 @@ package me.dreamerzero.chatregulator.utils; import java.util.Objects; +import java.util.concurrent.atomic.AtomicReference; import com.velocitypowered.api.proxy.Player; @@ -17,7 +18,6 @@ import me.dreamerzero.chatregulator.modules.checks.InfractionCheck; import me.dreamerzero.chatregulator.modules.checks.SpamCheck; import me.dreamerzero.chatregulator.modules.checks.UnicodeCheck; -import me.dreamerzero.chatregulator.objects.AtomicString; import me.dreamerzero.chatregulator.result.IReplaceable; import me.dreamerzero.chatregulator.result.ReplaceableResult; import me.dreamerzero.chatregulator.result.Result; @@ -97,7 +97,7 @@ public static boolean checkAndCall(@NotNull EventBundle bundle, @NotNull ChatReg } private GeneralUtils(){} - public static boolean unicode(InfractionPlayer player, AtomicString string, EventWrapper event, ChatRegulator plugin) { + public static boolean unicode(InfractionPlayer player, AtomicReference string, EventWrapper event, ChatRegulator plugin) { return GeneralUtils.allowedPlayer(player.getPlayer(), InfractionType.UNICODE) && UnicodeCheck.createCheck(string.get()).thenApply(result -> { if(GeneralUtils.checkAndCall(new EventBundle(player, string.get(), InfractionType.UNICODE, result, event.source()), plugin)){ @@ -107,14 +107,15 @@ public static boolean unicode(InfractionPlayer player, AtomicString string, Even return true; } if(result instanceof final ReplaceableResult replaceableResult){ - event.setString(string.setAndGet(replaceableResult.replaceInfraction())); + string.set(replaceableResult.replaceInfraction()); + event.setString(string.get()); } } return false; }).join(); } - public static boolean caps(InfractionPlayer player, AtomicString string, EventWrapper event, ChatRegulator plugin) { + public static boolean caps(InfractionPlayer player, AtomicReference string, EventWrapper event, ChatRegulator plugin) { return GeneralUtils.allowedPlayer(player.getPlayer(), InfractionType.CAPS) && CapsCheck.createCheck(string.get()).thenApply(result -> { if(GeneralUtils.checkAndCall(new EventBundle(player, string.get(), InfractionType.CAPS, result, event.source()), plugin)){ @@ -123,16 +124,16 @@ public static boolean caps(InfractionPlayer player, AtomicString string, EventWr event.resume(); return true; } - if(result instanceof IReplaceable replaceable){ - String messageReplaced = replaceable.replaceInfraction(); - event.setString(string.setAndGet(messageReplaced)); + if(result instanceof final IReplaceable replaceable){ + string.set(replaceable.replaceInfraction()); + event.setString(string.get()); } } return false; }).join(); } - public static boolean flood(InfractionPlayer player, AtomicString string, EventWrapper event, ChatRegulator plugin) { + public static boolean flood(InfractionPlayer player, AtomicReference string, EventWrapper event, ChatRegulator plugin) { return GeneralUtils.allowedPlayer(player.getPlayer(), InfractionType.FLOOD) && FloodCheck.createCheck(string.get()).thenApply(result -> { if(GeneralUtils.checkAndCall(new EventBundle(player, string.get(), InfractionType.FLOOD, result, event.source()), plugin)) { @@ -141,15 +142,16 @@ public static boolean flood(InfractionPlayer player, AtomicString string, EventW event.resume(); return true; } - if(result instanceof IReplaceable replaceable){ - event.setString(string.setAndGet(replaceable.replaceInfraction())); + if(result instanceof final IReplaceable replaceable){ + string.set(replaceable.replaceInfraction()); + event.setString(string.get()); } } return false; }).join(); } - public static boolean regular(InfractionPlayer player, AtomicString string, EventWrapper event, ChatRegulator plugin) { + public static boolean regular(InfractionPlayer player, AtomicReference string, EventWrapper event, ChatRegulator plugin) { return GeneralUtils.allowedPlayer(player.getPlayer(), InfractionType.REGULAR) && InfractionCheck.createCheck(string.get()).thenApply(result -> { if(GeneralUtils.checkAndCall(new EventBundle(player, string.get(), InfractionType.REGULAR, result, event.source()), plugin)) { @@ -158,18 +160,18 @@ public static boolean regular(InfractionPlayer player, AtomicString string, Even event.resume(); return true; } - if(result instanceof IReplaceable replaceable){ - String messageReplaced = replaceable.replaceInfraction(); - event.setString(string.setAndGet(messageReplaced)); + if(result instanceof final IReplaceable replaceable){ + string.set(replaceable.replaceInfraction()); + event.setString(string.get()); } } return false; }).join(); } - public static boolean spam(InfractionPlayer player, AtomicString string, EventWrapper event, ChatRegulator plugin) { + public static boolean spam(InfractionPlayer player, AtomicReference string, EventWrapper event, ChatRegulator plugin) { if(GeneralUtils.allowedPlayer(player.getPlayer(), InfractionType.SPAM)) { - var result = SpamCheck.createCheck(player, string.get(), event.source()).join(); + Result result = SpamCheck.createCheck(player, string.get(), event.source()).join(); if(GeneralUtils.spamCheck(result, player) && GeneralUtils.callViolationEvent(new EventBundle(player, string.get(), InfractionType.SPAM, result, event.source()), plugin) ) { diff --git a/src/test/java/me/dreamerzero/chatregulator/objects/StringTest.java b/src/test/java/me/dreamerzero/chatregulator/objects/StringTest.java deleted file mode 100644 index fa8f1489..00000000 --- a/src/test/java/me/dreamerzero/chatregulator/objects/StringTest.java +++ /dev/null @@ -1,42 +0,0 @@ -package me.dreamerzero.chatregulator.objects; - -import static org.junit.jupiter.api.Assertions.assertEquals; - -import java.util.concurrent.CompletableFuture; - -import org.junit.jupiter.api.DisplayName; -import org.junit.jupiter.api.Test; - -public final class StringTest { - @Test - @DisplayName("AtomicString Basic Test") - void testString(){ - AtomicString astring = new AtomicString("test"); - assertEquals("test", astring.get()); - - assertEquals("testing", astring.concatAndGet("ing")); - - assertEquals("hello", astring.setAndGet("hello")); - - assertEquals("hello", astring.getAndSet("new hello")); - - assertEquals("new hello", astring.get()); - } - - @Test - @DisplayName(value = "AtomicString Concurrent") - void concurrentTest(){ - final AtomicString aString = new AtomicString("testing"); - CompletableFuture futureString = CompletableFuture.supplyAsync(() -> { - aString.set("full replaced string"); - return aString.get(); - }) - .thenApplyAsync(s -> aString.concatAndGet("nose")) - .thenApplyAsync(a -> aString.setAndGet("full new String")); - - String expected = futureString.join(); - String actual = aString.get(); - - assertEquals(expected, actual); - } -}