Skip to content

Commit

Permalink
feat: Implement percentage caps limit
Browse files Browse the repository at this point in the history
resolves #28
  • Loading branch information
4drian3d committed May 17, 2023
1 parent b6fdfc9 commit 76e727c
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,34 +2,47 @@

import io.github._4drian3d.chatregulator.api.InfractionPlayer;
import io.github._4drian3d.chatregulator.api.annotations.Required;
import io.github._4drian3d.chatregulator.api.enums.CapsAlgorithm;
import io.github._4drian3d.chatregulator.api.enums.ControlType;
import io.github._4drian3d.chatregulator.api.enums.InfractionType;
import io.github._4drian3d.chatregulator.api.result.CheckResult;
import net.kyori.adventure.builder.AbstractBuilder;
import org.jetbrains.annotations.NotNull;

import java.util.Locale;
import java.util.Objects;

import static java.util.Objects.requireNonNull;

/**
* Check for compliance with uppercase character limit in a string
*/
public final class CapsCheck implements Check {
private final int limit;
private final ControlType controlType;
private final CapsAlgorithm algorithm;

private CapsCheck(int limit, ControlType controlType){
private CapsCheck(int limit, ControlType controlType, CapsAlgorithm algorithm) {
this.limit = limit;
this.controlType = controlType;
this.algorithm = algorithm;
}

@Override
public @NotNull CheckResult check(@NotNull InfractionPlayer player, @NotNull String string) {
boolean aboveLimit = Objects.requireNonNull(string)
final long caps = requireNonNull(string)
.chars()
.filter(Character::isUpperCase)
.count() >= this.limit;
if (aboveLimit) {
.count();
final boolean surpassedLimit = switch (algorithm) {
case AMOUNT -> caps >= this.limit;
case PERCENTAGE -> {
final double length = string.length();
final double percentageLimit = (length / 100) * this.limit;
yield caps >= percentageLimit;
}
};

if (surpassedLimit) {
if (controlType == ControlType.REPLACE) {
return CheckResult.modified(string.toLowerCase(Locale.ROOT));
} else {
Expand All @@ -56,7 +69,8 @@ public static CapsCheck.Builder builder(){
/**Caps Check Builder */
public static class Builder implements AbstractBuilder<CapsCheck> {
private int limit;
private ControlType controlType;
private ControlType controlType = ControlType.BLOCK;
private CapsAlgorithm algorithm = CapsAlgorithm.AMOUNT;
Builder() {}

/**
Expand All @@ -75,9 +89,14 @@ public Builder controlType(ControlType controlType) {
return this;
}

public Builder algorithm(CapsAlgorithm algorithm) {
this.algorithm = algorithm;
return this;
}

@Override
public @NotNull CapsCheck build(){
return new CapsCheck(limit, controlType);
return new CapsCheck(limit, controlType, algorithm);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package io.github._4drian3d.chatregulator.api.enums;

public enum CapsAlgorithm {
PERCENTAGE,
AMOUNT
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
package io.github._4drian3d.chatregulator.plugin.config;

import io.github._4drian3d.chatregulator.api.enums.ControlType;
import io.github._4drian3d.chatregulator.api.enums.DetectionMode;
import io.github._4drian3d.chatregulator.api.enums.InfractionType;
import io.github._4drian3d.chatregulator.api.enums.WarningType;
import io.github._4drian3d.chatregulator.api.enums.*;
import org.jetbrains.annotations.Nullable;
import org.spongepowered.configurate.objectmapping.ConfigSerializable;
import org.spongepowered.configurate.objectmapping.meta.Comment;
Expand Down Expand Up @@ -392,7 +389,17 @@ public static class Caps implements Warning, Toggleable, Controllable, Executabl
@Setting(value = "warning-type")
private WarningType warningType = WarningType.MESSAGE;

@Comment("Sets the maximum limit of caps in a sentence")
@Comment("""
Select caps detection algorithm
AMOUNT
|- In case the provided string has a specific amount of capital letters,
it will be detected
PERCENTAGE
|- In case the provided string has a higher percentage of capital letters than specified,
it will be detected""")
private CapsAlgorithm algorithm = CapsAlgorithm.AMOUNT;

@Comment("Sets the uppercase limit in a sentence according to the selected detection algorithm")
private int limit = 5;

@Comment("Commands to be executed in the caps module")
Expand All @@ -413,6 +420,10 @@ public ControlType getControlType(){
return this.controlType;
}

public CapsAlgorithm getAlgorithm() {
return algorithm;
}

public int limit(){
return this.limit;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ private CheckProvider<CapsCheck> caps(final ConfigurationContainer<Checks> confi
return CapsCheck.builder()
.limit(configuration.getCapsConfig().limit())
.controlType(configuration.getCapsConfig().getControlType())
.algorithm(configuration.getCapsConfig().getAlgorithm())
.build();
}
return null;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package io.github._4drian3d.chatregulator.modules.checks;

import io.github._4drian3d.chatregulator.api.InfractionPlayer;
import io.github._4drian3d.chatregulator.api.checks.CapsCheck;
import io.github._4drian3d.chatregulator.api.enums.CapsAlgorithm;
import io.github._4drian3d.chatregulator.api.enums.ControlType;
import io.github._4drian3d.chatregulator.api.result.CheckResult;
import io.github._4drian3d.chatregulator.plugin.config.Checks;
Expand All @@ -25,6 +27,7 @@ void capsTest(){
CapsCheck check = CapsCheck.builder()
.limit(5)
.controlType(ControlType.REPLACE)
.algorithm(CapsAlgorithm.AMOUNT)
.build();
CheckResult result = check.check(TestsUtils.dummyPlayer(), original);
assertTrue(result.shouldModify());
Expand All @@ -41,9 +44,21 @@ void realTest(@TempDir Path path){
CapsCheck check = CapsCheck.builder()
.limit(config.getCapsConfig().limit())
.controlType(ControlType.REPLACE)
.algorithm(CapsAlgorithm.AMOUNT)
.build();
CheckResult result = check.check(TestsUtils.dummyPlayer(), message);
CheckResult.ReplaceCheckResult replaceResult = assertInstanceOf(CheckResult.ReplaceCheckResult.class, result);
assertEquals("aaaaaaaaaa", replaceResult.replaced());
}

@Test
void percentageAlgorithm() {
String message = "AAaaaaaaaaaaaaaaaaaa";

var builder = CapsCheck.builder().algorithm(CapsAlgorithm.PERCENTAGE).controlType(ControlType.BLOCK);
InfractionPlayer player = TestsUtils.dummyPlayer();

assertTrue(builder.limit(11).build().check(player, message).isAllowed());
assertTrue(builder.limit(10).build().check(player, message).isDenied());
}
}

0 comments on commit 76e727c

Please sign in to comment.