Skip to content

Commit

Permalink
feat: Added support for concatenating multiple check replacements
Browse files Browse the repository at this point in the history
  • Loading branch information
4drian3d committed Sep 15, 2023
1 parent 32a28b5 commit 2d0deaa
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ public boolean shouldModify() {
}
}

record ReplaceCheckResult(InfractionType infractionType, String modified) implements CheckResult, DetectedResult {
record ReplaceCheckResult(InfractionType infractionType, String replaced) implements CheckResult, DetectedResult {
@Override
public boolean isAllowed() {
return false;
Expand All @@ -103,10 +103,6 @@ public boolean isDenied() {
public boolean shouldModify() {
return true;
}

public String replaced() {
return modified;
}
}

sealed interface DetectedResult {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public static LazyDetection checks(final CheckProvider<? extends Check>... check
if (providedCheck == null) {
continue;
}
final CheckResult result = providedCheck.check(player, string);
final CheckResult result = providedCheck.check(player, modifiedOrDefault(modifiedString, string));
if (result.isAllowed()) {
continue;
}
Expand All @@ -41,7 +41,6 @@ public static LazyDetection checks(final CheckProvider<? extends Check>... check

if (result instanceof final CheckResult.ReplaceCheckResult replaceCheckResult) {
modifiedString.set(new InfractionDetection(replaceCheckResult.infractionType(), replaceCheckResult.replaced()));
break;
}
}
final InfractionDetection finalResult = modifiedString.get();
Expand All @@ -55,5 +54,13 @@ public static LazyDetection checks(final CheckProvider<? extends Check>... check
});
}

private @NotNull String modifiedOrDefault(final @NotNull AtomicReference<InfractionDetection> reference, final @NotNull String defaultValue) {
final InfractionDetection actualDetection = reference.get();
if (actualDetection == null) {
return defaultValue;
}
return actualDetection.modified;
}

private record InfractionDetection(@NotNull InfractionType infractionType, @NotNull String modified) {}
}
Original file line number Diff line number Diff line change
Expand Up @@ -106,9 +106,7 @@ private boolean checkIfCanCheck(final String command) {
this.eventManager.fireAndForget(new CommandInfractionEvent(infractionPlayer, deniedResult.infractionType(), result, event.getCommand()));
infractionPlayer.onDetection(deniedResult, event.getCommand());
event.setResult(CommandExecuteEvent.CommandResult.denied());
return null;
}
if (result instanceof final CheckResult.ReplaceCheckResult replaceResult) {
} else if (result instanceof final CheckResult.ReplaceCheckResult replaceResult) {
this.eventManager.fireAndForget(new CommandInfractionEvent(infractionPlayer, replaceResult.infractionType(), result, event.getCommand()));
final String replacedCommand = replaceResult.replaced();
infractionPlayer.getChain(SourceType.COMMAND).executed(replacedCommand);
Expand Down

0 comments on commit 2d0deaa

Please sign in to comment.