66
77import java .io .File ;
88import java .math .BigDecimal ;
9- import java .util .HashMap ;
9+ import java .util .ArrayList ;
10+ import java .util .EnumMap ;
11+ import java .util .List ;
1012import java .util .Locale ;
1113import java .util .Map ;
1214import java .util .Objects ;
@@ -19,7 +21,7 @@ public class CommandFilters implements IConf {
1921 private final IEssentials essentials ;
2022 private final EssentialsConf config ;
2123 private ConfigurationSection filters ;
22- private Map <String , CommandFilter > commandFilters ;
24+ private Map <CommandFilter . Type , List < CommandFilter > > commandFilters ;
2325
2426 public CommandFilters (final IEssentials essentials ) {
2527 this .essentials = essentials ;
@@ -50,16 +52,16 @@ private ConfigurationSection _getCommandFilterSection() {
5052 return null ;
5153 }
5254
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 );
5557 for (final String name : filters .getKeys (false )) {
5658 if (!filters .isConfigurationSection (name )) {
5759 EssentialsConf .LOGGER .warning ("Invalid command filter '" + name + "'" );
5860 continue ;
5961 }
6062
6163 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 ;
6365 final String command = section .getString ("command" );
6466
6567 if (pattern == null && command == null ) {
@@ -72,11 +74,6 @@ private Map<String, CommandFilter> _getCommandFilters() {
7274 continue ;
7375 }
7476
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-
8077 Integer cooldown = section .getInt ("cooldown" , -1 );
8178 if (cooldown < 0 ) {
8279 cooldown = null ;
@@ -88,7 +85,12 @@ private Map<String, CommandFilter> _getCommandFilters() {
8885 final BigDecimal cost = EssentialsConf .toBigDecimal (section .getString ("cost" ), null );
8986
9087 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+ }
9294 }
9395 config .save ();
9496 return commandFilters ;
@@ -116,28 +118,18 @@ public EssentialsConf getConfig() {
116118 return config ;
117119 }
118120
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 ) {
124122 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 ()));
126124 }
127125
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 ) {
129127 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 ()));
131129 }
132130
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 )) {
141133 if (!filterPredicate .test (filter )) continue ;
142134
143135 final boolean matches = filter .getPattern ().matcher (label ).matches ();
0 commit comments