Skip to content

Commit

Permalink
fix: Fixed Chat ReplaceCheckResult
Browse files Browse the repository at this point in the history
  • Loading branch information
4drian3d committed Jun 22, 2023
1 parent 4750001 commit 01120a8
Show file tree
Hide file tree
Showing 10 changed files with 59 additions and 68 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ private CapsCheck(int limit, ControlType controlType, CapsAlgorithm algorithm) {

if (surpassedLimit) {
if (controlType == ControlType.REPLACE) {
return CheckResult.modified(string.toLowerCase(Locale.ROOT));
return CheckResult.modified(type(), string.toLowerCase(Locale.ROOT));
} else {
return CheckResult.denied(type());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ private FloodCheck(final Pattern pattern, final ControlType controlType) {
if (controlType == ControlType.BLOCK) {
return CheckResult.denied(type());
} else {
return CheckResult.modified(matcher.replaceAll(match -> Character.toString(match.group().charAt(0))));
return CheckResult.modified(type(), matcher.replaceAll(match -> Character.toString(match.group().charAt(0))));
}
}
return CheckResult.allowed();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ private RegexCheck(ControlType controlType, Pattern... blockedWords) {
for (final Pattern pattern : patterns) {
replaced = pattern.matcher(replaced).replaceAll(RegexCheck::generateReplacement);
}
return CheckResult.modified(replaced);
return CheckResult.modified(type(), replaced);
} else {
return CheckResult.allowed();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import java.util.Set;
import java.util.function.Predicate;

import static io.github._4drian3d.chatregulator.api.utils.Commands.SPACE;
import static java.util.Objects.requireNonNull;

/**
Expand Down Expand Up @@ -42,7 +43,7 @@ public static boolean defaultCharTest(char c) {
return !(c <= 'þ');
}

private boolean charTest(char c) {
private boolean charTest(final char c) {
for (final char character : this.chars) {
if (character == c) {
return true;
Expand Down Expand Up @@ -70,9 +71,9 @@ private boolean charTest(char c) {
} else {
String replaced = string;
for (final char character : results) {
replaced = replaced.replace(character, ' ');
replaced = replaced.replace(character, SPACE);
}
return CheckResult.modified(replaced);
return CheckResult.modified(type(), replaced);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public sealed interface CheckResult {
* @return the result
*/
static @NotNull CheckResult denied(final @NotNull InfractionType type) {
return new DeniedCheckresult(type);
return new DeniedCheckResult(type);
}

/**
Expand All @@ -35,8 +35,8 @@ public sealed interface CheckResult {
* @param modifier the modified result
* @return the result
*/
static @NotNull CheckResult modified(final @NotNull String modifier) {
return new ReplaceCheckResult(requireNonNull(modifier));
static @NotNull CheckResult modified( final @NotNull InfractionType infractionType, final @NotNull String modifier) {
return new ReplaceCheckResult(requireNonNull(infractionType), requireNonNull(modifier));
}

/**
Expand Down Expand Up @@ -71,13 +71,7 @@ public boolean shouldModify() {
}
}

final class DeniedCheckresult implements CheckResult {
private final InfractionType infractionType;

private DeniedCheckresult(InfractionType type) {
this.infractionType = type;
}

record DeniedCheckResult(InfractionType infractionType) implements CheckResult, DetectedResult {
@Override
public boolean isAllowed() {
return false;
Expand All @@ -92,18 +86,9 @@ public boolean isDenied() {
public boolean shouldModify() {
return false;
}

public InfractionType infractionType() {
return this.infractionType;
}
}

final class ReplaceCheckResult implements CheckResult {
private final String modified;
private ReplaceCheckResult(final String modified) {
this.modified = modified;
}

record ReplaceCheckResult(InfractionType infractionType, String modified) implements CheckResult, DetectedResult {
@Override
public boolean isAllowed() {
return false;
Expand All @@ -123,4 +108,8 @@ public String replaced() {
return modified;
}
}

sealed interface DetectedResult {
InfractionType infractionType();
}
}
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
package io.github._4drian3d.chatregulator.plugin.utils;
package io.github._4drian3d.chatregulator.api.utils;

import java.util.Objects;

import org.jetbrains.annotations.NotNull;

import io.github._4drian3d.chatregulator.plugin.config.Configuration;

/**
* Replacer class
* String Replacer Utils
*/
public final class Replacer {
private Replacer() {
}
/**
* Converts a string with the first character converted to uppercase
*
Expand All @@ -28,11 +28,6 @@ public final class Replacer {
string.substring(1);
}

public static @NotNull String firstLetterUppercase(@NotNull final String string, Configuration config) {
if (!config.getFormatterConfig().setFirstLetterUppercase()) return string;
return firstLetterUppercase(string);
}

/**
* Add a dot at the end of a string
* <p>
Expand All @@ -50,15 +45,6 @@ public final class Replacer {
return string + ".";
}

public static String addFinalDot(final String string, Configuration config) {
return config.getFormatterConfig().setFinalDot()
? addFinalDot(string)
: string;
}

private Replacer() {
}

/**
* Applies a trailing dot and a leading capital letter to the specified string
*
Expand All @@ -68,8 +54,4 @@ private Replacer() {
public static @NotNull String applyFormat(final @NotNull String string) {
return firstLetterUppercase(addFinalDot(string));
}

public static @NotNull String applyFormat(final @NotNull String string, Configuration config) {
return firstLetterUppercase(addFinalDot(string, config), config);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,12 @@

import io.github._4drian3d.chatregulator.api.InfractionPlayer;
import io.github._4drian3d.chatregulator.api.checks.Check;
import io.github._4drian3d.chatregulator.api.enums.InfractionType;
import io.github._4drian3d.chatregulator.api.result.CheckResult;
import org.jetbrains.annotations.NotNull;

import java.util.Map;
import java.util.Objects;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.atomic.AtomicReference;

Expand All @@ -22,7 +25,7 @@ public static LazyDetection checks(final CheckProvider<? extends Check>... check

public @NotNull CompletableFuture<CheckResult> detect(final @NotNull InfractionPlayer player, final @NotNull String string) {
return CompletableFuture.supplyAsync(() -> {
final AtomicReference<String> modifiedString = new AtomicReference<>(string);
final AtomicReference<Map.Entry<InfractionType, String>> modifiedString = new AtomicReference<>();
for (final CheckProvider<? extends Check> provider : checks) {
final Check providedCheck = provider.provide(player);
if (providedCheck == null) {
Expand All @@ -38,11 +41,15 @@ public static LazyDetection checks(final CheckProvider<? extends Check>... check
}

if (result instanceof final CheckResult.ReplaceCheckResult replaceCheckResult) {
modifiedString.set(replaceCheckResult.replaced());
modifiedString.set(Map.entry(replaceCheckResult.infractionType(), replaceCheckResult.replaced()));
}
}
if (modifiedString.get().equals(string)) {
return CheckResult.modified(modifiedString.get());
final Map.Entry<InfractionType, String> finalResult = modifiedString.get();
if (finalResult == null) {
return CheckResult.allowed();
}
if (!Objects.equals(finalResult.getValue(), string)) {
return CheckResult.modified(finalResult.getKey(), finalResult.getValue());
}
return CheckResult.allowed();
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,14 @@
import io.github._4drian3d.chatregulator.plugin.impl.InfractionPlayerImpl;
import io.github._4drian3d.chatregulator.plugin.impl.PlayerManagerImpl;
import io.github._4drian3d.chatregulator.plugin.lazy.LazyDetection;
import io.github._4drian3d.chatregulator.plugin.utils.Replacer;
import io.github._4drian3d.chatregulator.api.utils.Replacer;
import io.github._4drian3d.chatregulator.plugin.config.Configuration;
import io.github._4drian3d.chatregulator.plugin.config.ConfigurationContainer;
import io.github._4drian3d.chatregulator.plugin.listener.RegulatorExecutor;
import org.checkerframework.checker.nullness.qual.Nullable;
import org.jetbrains.annotations.NotNull;
import org.slf4j.Logger;


public final class ChatListener implements RegulatorExecutor<PlayerChatEvent> {
@Inject
private ConfigurationContainer<Configuration> configurationContainer;
Expand Down Expand Up @@ -67,23 +67,23 @@ public final class ChatListener implements RegulatorExecutor<PlayerChatEvent> {
logger.error("An error occurred while checking chat", ex);
return CheckResult.allowed();
}).thenAccept(checkResult -> {
if (checkResult.isDenied()) {
final CheckResult.DeniedCheckresult deniedResult = (CheckResult.DeniedCheckresult) checkResult;
if (checkResult instanceof final CheckResult.DeniedCheckResult deniedResult) {
eventManager.fireAndForget(new ChatInfractionEvent(player, deniedResult.infractionType(), checkResult, event.getMessage()));
player.onDenied(deniedResult, event.getMessage());
player.onDetected(deniedResult, event.getMessage());
event.setResult(ChatResult.denied());
} else {
String finalMessage = event.getMessage();
if (checkResult instanceof final CheckResult.ReplaceCheckResult replaceResult) {
finalMessage = replaceResult.replaced();
}
} else if (checkResult instanceof final CheckResult.ReplaceCheckResult replaceResult) {
String finalMessage = replaceResult.replaced();

final Configuration configuration = configurationContainer.get();
if (configuration.getFormatterConfig().enabled()) {
finalMessage = Replacer.applyFormat(finalMessage, configuration);
event.setResult(ChatResult.message(finalMessage));
finalMessage = applyFormat(finalMessage, configuration);
}
player.getChain(SourceType.CHAT).executed(event.getMessage());
eventManager.fireAndForget(new ChatInfractionEvent(player, replaceResult.infractionType(), checkResult, event.getMessage()));
player.onDetected(replaceResult, event.getMessage());
event.setResult(ChatResult.message(finalMessage));
} else {
player.getChain(SourceType.CHAT).executed(event.getMessage());
}
}).exceptionally(ex -> {
logger.error("An error occurred while setting chat result", ex);
Expand All @@ -101,4 +101,17 @@ public Class<PlayerChatEvent> eventClass() {
public PostOrder postOrder() {
return PostOrder.EARLY;
}

public static @NotNull String applyFormat(final @NotNull String string, Configuration config) {
return firstLetterUppercase(addFinalDot(string, config), config);
}
public static @NotNull String firstLetterUppercase(@NotNull final String string, Configuration config) {
if (!config.getFormatterConfig().setFirstLetterUppercase()) return string;
return Replacer.firstLetterUppercase(string);
}
public static String addFinalDot(final String string, Configuration config) {
return config.getFormatterConfig().setFinalDot()
? Replacer.addFinalDot(string)
: string;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -102,9 +102,9 @@ private boolean checkIfCanCheck(final String command) {
if (ex != null) {
logger.error("An error occurred while calculating command result", ex);
} else {
if (result instanceof final CheckResult.DeniedCheckresult deniedResult) {
if (result instanceof final CheckResult.DeniedCheckResult deniedResult) {
eventManager.fireAndForget(new CommandInfractionEvent(infractionPlayer, deniedResult.infractionType(), result, event.getCommand()));
infractionPlayer.onDenied(deniedResult, event.getCommand());
infractionPlayer.onDetected(deniedResult, event.getCommand());
event.setResult(CommandExecuteEvent.CommandResult.denied());
return null;
}
Expand All @@ -114,7 +114,6 @@ private boolean checkIfCanCheck(final String command) {
event.setResult(CommandExecuteEvent.CommandResult.command(replacedCommand));
} else {
infractionPlayer.getChain(SourceType.COMMAND).executed(event.getCommand());
event.setResult(CommandExecuteEvent.CommandResult.allowed());
}
}
return null;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package io.github._4drian3d.chatregulator.modules;

import io.github._4drian3d.chatregulator.plugin.utils.Replacer;
import io.github._4drian3d.chatregulator.api.utils.Replacer;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;

Expand Down

0 comments on commit 01120a8

Please sign in to comment.