6
6
7
7
import java .io .File ;
8
8
import java .math .BigDecimal ;
9
- import java .util .HashMap ;
9
+ import java .util .ArrayList ;
10
+ import java .util .EnumMap ;
11
+ import java .util .List ;
10
12
import java .util .Locale ;
11
13
import java .util .Map ;
12
14
import java .util .Objects ;
@@ -19,7 +21,7 @@ public class CommandFilters implements IConf {
19
21
private final IEssentials essentials ;
20
22
private final EssentialsConf config ;
21
23
private ConfigurationSection filters ;
22
- private Map <String , CommandFilter > commandFilters ;
24
+ private Map <CommandFilter . Type , List < CommandFilter > > commandFilters ;
23
25
24
26
public CommandFilters (final IEssentials essentials ) {
25
27
this .essentials = essentials ;
@@ -50,16 +52,16 @@ private ConfigurationSection _getCommandFilterSection() {
50
52
return null ;
51
53
}
52
54
53
- private Map <String , CommandFilter > _getCommandFilters () {
54
- final Map <String , CommandFilter > commandFilters = new HashMap <>();
55
+ private Map <CommandFilter . Type , List < CommandFilter > > _getCommandFilters () {
56
+ final Map <CommandFilter . Type , List < CommandFilter >> commandFilters = new EnumMap <>(CommandFilter . Type . class );
55
57
for (final String name : filters .getKeys (false )) {
56
58
if (!filters .isConfigurationSection (name )) {
57
59
EssentialsConf .LOGGER .warning ("Invalid command filter '" + name + "'" );
58
60
continue ;
59
61
}
60
62
61
63
final ConfigurationSection section = Objects .requireNonNull (filters .getConfigurationSection (name ));
62
- Pattern pattern = section .isString ("pattern" ) ? compileRegex (section .getString ("pattern" )) : null ;
64
+ final Pattern pattern = section .isString ("pattern" ) ? compileRegex (section .getString ("pattern" )) : null ;
63
65
final String command = section .getString ("command" );
64
66
65
67
if (pattern == null && command == null ) {
@@ -72,11 +74,6 @@ private Map<String, CommandFilter> _getCommandFilters() {
72
74
continue ;
73
75
}
74
76
75
- // Compile the command as a regex if the pattern hasn't been set, so pattern is always available.
76
- if (pattern == null ) {
77
- pattern = compileRegex (command );
78
- }
79
-
80
77
Integer cooldown = section .getInt ("cooldown" , -1 );
81
78
if (cooldown < 0 ) {
82
79
cooldown = null ;
@@ -88,7 +85,12 @@ private Map<String, CommandFilter> _getCommandFilters() {
88
85
final BigDecimal cost = EssentialsConf .toBigDecimal (section .getString ("cost" ), null );
89
86
90
87
final String lowerName = name .toLowerCase (Locale .ENGLISH );
91
- commandFilters .put (lowerName , new CommandFilter (lowerName , command , pattern , cooldown , persistentCooldown , cost ));
88
+
89
+ if (pattern == null ) {
90
+ commandFilters .computeIfAbsent (CommandFilter .Type .ESS , k -> new ArrayList <>()).add (new EssCommandFilter (lowerName , command , compileRegex (command ), cooldown , persistentCooldown , cost ));
91
+ } else {
92
+ commandFilters .computeIfAbsent (CommandFilter .Type .REGEX , k -> new ArrayList <>()).add (new RegexCommandFilter (lowerName , pattern , cooldown , persistentCooldown , cost ));
93
+ }
92
94
}
93
95
config .save ();
94
96
return commandFilters ;
@@ -116,28 +118,18 @@ public EssentialsConf getConfig() {
116
118
return config ;
117
119
}
118
120
119
- public CommandFilter getFilterByName (final String name ) {
120
- return commandFilters .get (name .toLowerCase (Locale .ENGLISH ));
121
- }
122
-
123
- public CommandFilter getCommandCooldown (final IUser user , final String label , boolean essCommand ) {
121
+ public CommandFilter getCommandCooldown (final IUser user , final String label , CommandFilter .Type type ) {
124
122
if (user .isAuthorized ("essentials.commandcooldowns.bypass" )) return null ;
125
- return getFilter (label , essCommand , filter -> filter .hasCooldown () && !user .isAuthorized ("essentials.commandcooldowns.bypass." + filter .getName ()));
123
+ return getFilter (label , type , filter -> filter .hasCooldown () && !user .isAuthorized ("essentials.commandcooldowns.bypass." + filter .getName ()));
126
124
}
127
125
128
- public CommandFilter getCommandCost (final IUser user , final String label , boolean essCommand ) {
126
+ public CommandFilter getCommandCost (final IUser user , final String label , CommandFilter . Type type ) {
129
127
if (user .isAuthorized ("essentials.nocommandcost.all" )) return null ;
130
- return getFilter (label , essCommand , filter -> filter .hasCost () && !user .isAuthorized ("essentials.nocommandcost." + filter .getName ()));
128
+ return getFilter (label , type , filter -> filter .hasCost () && !user .isAuthorized ("essentials.nocommandcost." + filter .getName ()));
131
129
}
132
130
133
- private CommandFilter getFilter (final String label , boolean essCommand , Predicate <CommandFilter > filterPredicate ) {
134
- for (CommandFilter filter : commandFilters .values ()) {
135
- // When the label is an ess command, the filter must define a command entry.
136
- if (essCommand && !filter .hasCommand ()) continue ;
137
-
138
- // Same vice versa.
139
- if (!essCommand && filter .hasCommand ()) continue ;
140
-
131
+ private CommandFilter getFilter (final String label , CommandFilter .Type type , Predicate <CommandFilter > filterPredicate ) {
132
+ for (CommandFilter filter : commandFilters .get (type )) {
141
133
if (!filterPredicate .test (filter )) continue ;
142
134
143
135
final boolean matches = filter .getPattern ().matcher (label ).matches ();
0 commit comments