Skip to content

Commit

Permalink
fix: Replaced String wrapper usage with AtomicReference
Browse files Browse the repository at this point in the history
  • Loading branch information
4drian3d committed Apr 25, 2022
1 parent 88fa73b commit e212558
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 152 deletions.
Expand Up @@ -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
*/
Expand All @@ -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<String> message = new AtomicReference<String>(event.getMessage());
final InfractionPlayer infractor = InfractionPlayer.get(player);
final EventWrapper<PlayerChatEvent> wrapper = new ChatWrapper(event, continuation);

Expand Down
Expand Up @@ -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;
Expand All @@ -23,6 +22,8 @@

import static me.dreamerzero.chatregulator.utils.GeneralUtils.*;

import java.util.concurrent.atomic.AtomicReference;

/**
* Detections related to command execution by players
*/
Expand Down Expand Up @@ -56,7 +57,7 @@ public void onCommand(CommandExecuteEvent event, Continuation continuation){
return;
}

final AtomicString command = new AtomicString(event.getCommand());
final AtomicReference<String> command = new AtomicReference<>(event.getCommand());

if(unicode(infractionPlayer, command, wrapper, plugin)
|| caps(infractionPlayer, command, wrapper, plugin)
Expand Down

This file was deleted.

34 changes: 18 additions & 16 deletions 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;

Expand All @@ -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;
Expand Down Expand Up @@ -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> 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)){
Expand All @@ -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> 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)){
Expand All @@ -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> 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)) {
Expand All @@ -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> 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)) {
Expand All @@ -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> 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)
) {
Expand Down
42 changes: 0 additions & 42 deletions src/test/java/me/dreamerzero/chatregulator/objects/StringTest.java

This file was deleted.

0 comments on commit e212558

Please sign in to comment.