Skip to content

Commit

Permalink
perf: Optimized SpamCheck
Browse files Browse the repository at this point in the history
  • Loading branch information
4drian3d committed May 18, 2023
1 parent fc787ac commit 2e09079
Showing 1 changed file with 18 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,9 @@
import io.github._4drian3d.chatregulator.api.result.CheckResult;
import net.kyori.adventure.builder.AbstractBuilder;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Range;

import java.util.Objects;
import java.util.Iterator;

import static java.util.Objects.requireNonNull;

Expand All @@ -32,17 +33,23 @@ private SpamCheck(final @NotNull SourceType type, int similarLimit) {
if (size < similarLimit) {
return CheckResult.allowed();
}
for (int i = 0; i + 1 < size; i++) {
if (!chain.index(i).equalsIgnoreCase(chain.index(i + 1))) {

final Iterator<String> it = chain.iterator();
String actual;
String previous = null;
while(it.hasNext()) {
actual = it.next();
if (previous != null && !actual.equalsIgnoreCase(previous)) {
return CheckResult.allowed();
}
previous = actual;
}

if (chain.last().equalsIgnoreCase(string)) {
return CheckResult.denied(type());
} else {
return CheckResult.allowed();
}
if (chain.last().equalsIgnoreCase(string)) {
return CheckResult.denied(type());
} else {
return CheckResult.allowed();
}
}

@Override
Expand All @@ -61,13 +68,13 @@ public static final class Builder implements AbstractBuilder<SpamCheck> {
Builder() {}

@Required
public Builder source(SourceType source){
public Builder source(final @NotNull SourceType source){
this.source = source;
return this;
}

@Required
public Builder similarLimit(int limit) {
public Builder similarLimit(final @Range(from = 2, to = 255) int limit) {
this.similarLimit = limit;
return this;
}
Expand Down

0 comments on commit 2e09079

Please sign in to comment.