/
CommandCheck.java
114 lines (98 loc) · 3.36 KB
/
CommandCheck.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
package me.dreamerzero.chatregulator.modules.checks;
import java.util.Arrays;
import java.util.Collection;
import java.util.HashSet;
import java.util.concurrent.CompletableFuture;
import org.jetbrains.annotations.NotNull;
import me.dreamerzero.chatregulator.config.Configuration;
import me.dreamerzero.chatregulator.enums.InfractionType;
import me.dreamerzero.chatregulator.result.Result;
import me.dreamerzero.chatregulator.utils.CommandUtils;
/**
* Check for verification of executed commands
*/
public final class CommandCheck implements ICheck {
private final Collection<String> blockedCommands;
private CommandCheck(){
this(Configuration.getBlacklist().getBlockedCommands());
}
private CommandCheck(Collection<String> blocledCommands){
this.blockedCommands = blocledCommands;
}
/**
* {@inheritDoc}
*
* @return A Result with the string with the command blocked and if the check was succesfull
*/
@Override
public CompletableFuture<Result> check(@NotNull String string) {
return CompletableFuture.supplyAsync(() -> {
for (final String blockedCommand : blockedCommands){
if (CommandUtils.isStartingString(string, blockedCommand)) {
return new Result(string, true);
}
}
return new Result(string, false);
});
}
@Override
public @NotNull InfractionType type() {
return InfractionType.BCOMMAND;
}
/**
* Create a new CommandCheck with default values
* @param string the string to check
* @return a CompletableFuture with the result of this check
*/
public static CompletableFuture<Result> createCheck(String string){
return new CommandCheck().check(string);
}
/**
* Create a new builder for CommandCheck
* @return a new CommandCheck.Builder
*/
public static CommandCheck.Builder builder(){
return new CommandCheck.Builder();
}
/**Command Check Builder */
public static class Builder {
private Collection<String> blockedCommands;
private Builder(){}
/**
* Set the blocked commands
* @param blockedCommands the blocked commands
* @return this
*/
public Builder blockedCommands(Collection<String> blockedCommands){
this.blockedCommands = blockedCommands;
return this;
}
/**
* Set the blocked commands
* @param blockedCommands the blocked commands
* @return this
*/
public Builder blockedCommands(String... blockedCommands){
this.blockedCommands = Arrays.asList(blockedCommands);
return this;
}
/**
* Adds a command to the blocked commands
* @param command the command to add
* @return this
*/
public Builder addBlockedCommand(String command){
if(this.blockedCommands == null)
this.blockedCommands = new HashSet<String>();
this.blockedCommands.add(command);
return this;
}
/**
* Build a new CommandCheck with the Builder values
* @return a new CommandCheck
*/
public CommandCheck build(){
return blockedCommands == null ? new CommandCheck() : new CommandCheck(blockedCommands);
}
}
}