Skip to content

Commit

Permalink
fix: Fixed IndexOutOfBoundException in SpamCheck
Browse files Browse the repository at this point in the history
  • Loading branch information
4drian3d committed May 17, 2023
1 parent 76e727c commit 2126eaa
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,29 +18,27 @@
*/
public final class SpamCheck implements Check {
private final SourceType type;
private final int similarLimit;

private SpamCheck(final @NotNull SourceType type){
this.type = Objects.requireNonNull(type);
private SpamCheck(final @NotNull SourceType type, int similarLimit) {
this.type = requireNonNull(type);
this.similarLimit = similarLimit;
}

@Override
public @NotNull CheckResult check(@NotNull InfractionPlayer player, @NotNull String string) {
public @NotNull CheckResult check(final @NotNull InfractionPlayer player, final @NotNull String string) {
final StringChain chain = player.getChain(type);
final int originalSize = chain.size();
int size = originalSize;
if (size % 2 != 0) {
size--;
}
if (size == 0) {
final int size = chain.size();
if (size < similarLimit) {
return CheckResult.allowed();
}
for (int i = 0; i < size; i++) {
for (int i = 0; i + 1 < size; i++) {
if (!chain.index(i).equalsIgnoreCase(chain.index(i + 1))) {
return CheckResult.allowed();
}
}

if (chain.index(originalSize - 1).equalsIgnoreCase(string)) {
if (chain.last().equalsIgnoreCase(string)) {
return CheckResult.denied(type());
} else {
return CheckResult.allowed();
Expand All @@ -56,8 +54,9 @@ public static Builder builder() {
return new Builder();
}

public static class Builder implements AbstractBuilder<SpamCheck> {
public static final class Builder implements AbstractBuilder<SpamCheck> {
private SourceType source;
private int similarLimit;

Builder() {}

Expand All @@ -67,10 +66,16 @@ public Builder source(SourceType source){
return this;
}

@Required
public Builder similarLimit(int limit) {
this.similarLimit = limit;
return this;
}

@Override
public @NotNull SpamCheck build(){
requireNonNull(source);
return new SpamCheck(source);
return new SpamCheck(source, Math.max(2, similarLimit));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,16 @@ public Iterator<String> iterator() {
}

public void executed(final String string) {
if (checksContainer != null && checksContainer.get().getSpamConfig().getSimilarStringCount() <= queue.size()) {
queue.removeFirst();
if (checksContainer != null) {
final int similarCount = checksContainer.get().getSpamConfig().getSimilarStringCount();
final int size = queue.size();
if (similarCount < size) {
while (similarCount < queue.size() + 1) {
queue.removeFirst();
}
} else if (similarCount == size) {
queue.removeFirst();
}
}
queue.add(string);
lastExecuted.set(Instant.now());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ private CheckProvider<SpamCheck> commandSpam(ConfigurationContainer<Checks> conf
if (infractionPlayer.isAllowed(InfractionType.SPAM) && configuration.isEnabled(InfractionType.SPAM)) {
return SpamCheck.builder()
.source(SourceType.COMMAND)
.similarLimit(configuration.getSpamConfig().getSimilarStringCount())
.build();
}
return null;
Expand All @@ -110,6 +111,7 @@ private CheckProvider<SpamCheck> chatSpam(ConfigurationContainer<Checks> configu
if (infractionPlayer.isAllowed(InfractionType.SPAM) && configuration.isEnabled(InfractionType.SPAM)) {
return SpamCheck.builder()
.source(SourceType.CHAT)
.similarLimit(configuration.getSpamConfig().getSimilarStringCount())
.build();
}
return null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,18 +13,20 @@
class SpamTest {
@Test
@DisplayName("Spam Test")
void chatTest(){
void chatTest() {
InfractionPlayerImpl player = TestsUtils.dummyPlayer();
StringChainImpl chatChain = player.getChain(SourceType.CHAT);
final String string = "holaaaaaaaa";

chatChain.executed("holaaaaaaaa");
chatChain.executed("holaaaaaaaa");
chatChain.executed("holaaaaaaaa");
chatChain.executed("holaaaaaaaa");
chatChain.executed("holaaaaaaaa");
chatChain.executed(string);
chatChain.executed(string);
chatChain.executed(string);
chatChain.executed(string);
chatChain.executed(string);

SpamCheck check = SpamCheck.builder().source(SourceType.CHAT).build();
SpamCheck.Builder builder = SpamCheck.builder().source(SourceType.CHAT);

assertTrue(check.check(player, "holaaaaaaaa").isDenied());
assertTrue(builder.similarLimit(5).build().check(player, string).isDenied());
assertTrue(builder.similarLimit(6).build().check(player, string).isAllowed());
}
}

0 comments on commit 2126eaa

Please sign in to comment.