/
CommandListener.java
129 lines (115 loc) · 4.65 KB
/
CommandListener.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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
package me.dreamerzero.chatregulator.listener.command;
import com.velocitypowered.api.event.Continuation;
import com.velocitypowered.api.event.PostOrder;
import com.velocitypowered.api.event.Subscribe;
import com.velocitypowered.api.event.command.CommandExecuteEvent;
import com.velocitypowered.api.proxy.Player;
import org.jetbrains.annotations.ApiStatus.Internal;
import me.dreamerzero.chatregulator.ChatRegulator;
import me.dreamerzero.chatregulator.InfractionPlayer;
import me.dreamerzero.chatregulator.config.Configuration;
import me.dreamerzero.chatregulator.modules.checks.CommandCheck;
import me.dreamerzero.chatregulator.modules.checks.SyntaxCheck;
import me.dreamerzero.chatregulator.result.Result;
import me.dreamerzero.chatregulator.utils.CommandUtils;
import me.dreamerzero.chatregulator.utils.GeneralUtils.EventBundle;
import me.dreamerzero.chatregulator.wrapper.event.CommandWrapper;
import me.dreamerzero.chatregulator.wrapper.event.EventWrapper;
import me.dreamerzero.chatregulator.enums.InfractionType;
import me.dreamerzero.chatregulator.enums.SourceType;
import static me.dreamerzero.chatregulator.utils.GeneralUtils.*;
import java.util.concurrent.atomic.AtomicReference;
/**
* Detections related to command execution by players
*/
@Internal
public final class CommandListener {
private final ChatRegulator plugin;
public CommandListener(ChatRegulator plugin){
this.plugin = plugin;
}
/**
* Listener for command detections
* @param event the command event
* @param continuation the event cycle
*/
@Subscribe(order = PostOrder.FIRST)
public void onCommand(CommandExecuteEvent event, Continuation continuation){
if (!(event.getCommandSource() instanceof final Player player)
|| !event.getResult().isAllowed()
) {
continuation.resume();
return;
}
final InfractionPlayer infractionPlayer = InfractionPlayer.get(player);
final EventWrapper<CommandExecuteEvent> wrapper = new CommandWrapper(event, continuation);
if(this.blockedCommands(infractionPlayer, event.getCommand(), wrapper)
|| this.syntax(infractionPlayer, event.getCommand(), wrapper)
|| !this.checkIfCanCheck(event.getCommand())
) {
return;
}
final AtomicReference<String> command = new AtomicReference<>(event.getCommand());
if(unicode(infractionPlayer, command, wrapper, plugin)
|| caps(infractionPlayer, command, wrapper, plugin)
|| flood(infractionPlayer, command, wrapper, plugin)
|| regular(infractionPlayer, command, wrapper, plugin)
|| spam(infractionPlayer, command, wrapper, plugin)
) {
return;
}
infractionPlayer.lastCommand(command.get());
continuation.resume();
}
private boolean checkIfCanCheck(final String command){
for(final String cmd : Configuration.getConfig().getCommandsChecked()){
if(CommandUtils.isStartingString(command, cmd))
return true;
}
return false;
}
private boolean syntax(InfractionPlayer player, String string, EventWrapper<?> event) {
if(allowedPlayer(player.getPlayer(), InfractionType.SYNTAX)
&& checkAndCall(
new EventBundle(
player,
string,
InfractionType.SYNTAX,
SyntaxCheck.createCheck(string).exceptionallyAsync(e -> {
plugin.getLogger().error("An Error ocurred on Syntax Check", e);
return new Result("", false);
}).join(),
SourceType.COMMAND
),
plugin
)
){
event.cancel();
event.resume();
return true;
}
return false;
}
private boolean blockedCommands(InfractionPlayer player, String string, EventWrapper<?> event) {
if(allowedPlayer(player.getPlayer(), InfractionType.BCOMMAND)
&& checkAndCall(
new EventBundle(
player,
string,
InfractionType.BCOMMAND,
CommandCheck.createCheck(string).exceptionallyAsync(e -> {
plugin.getLogger().error("An Error ocurred on Blocked Commands Check", e);
return new Result("", false);
}).join(),
SourceType.COMMAND
),
plugin
)
) {
event.cancel();
event.resume();
return true;
}
return false;
}
}