Skip to content

Commit eefb99e

Browse files
committed
Make CommandFilter more extendable + fix AsyncTeleport not applying command cooldown
1 parent 76e45fd commit eefb99e

File tree

7 files changed

+82
-53
lines changed

7 files changed

+82
-53
lines changed

Diff for: Essentials/src/main/java/com/earth2me/essentials/AsyncTeleport.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -281,7 +281,7 @@ private void teleport(final IUser teleportee, final ITarget target, final Trade
281281
}
282282
nowAsync(teleportee, target, cause, future);
283283
if (cashCharge != null) {
284-
cashCharge.charge(teleportOwner, future);
284+
cashCharge.charge(teleportOwner, chargeFor.getCommand(), future);
285285
if (future.isCompletedExceptionally()) {
286286
return;
287287
}
@@ -332,7 +332,7 @@ private void teleportOther(final IUser teleporter, final IUser teleportee, final
332332

333333
nowAsync(teleportee, target, cause, future);
334334
if (teleporter != null && cashCharge != null) {
335-
cashCharge.charge(teleporter, future);
335+
cashCharge.charge(teleporter, chargeFor.getCommand(), future);
336336
if (future.isCompletedExceptionally()) {
337337
return;
338338
}

Diff for: Essentials/src/main/java/com/earth2me/essentials/CommandFilter.java

+9-12
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,28 @@
11
package com.earth2me.essentials;
22

3+
import com.google.common.base.Preconditions;
34
import net.ess3.api.IUser;
45

56
import java.math.BigDecimal;
67
import java.util.Date;
78
import java.util.regex.Pattern;
89

9-
public class CommandFilter {
10+
public abstract class CommandFilter {
11+
12+
public enum Type {
13+
REGEX,
14+
ESS
15+
}
1016

1117
private final String name;
12-
private final String command;
1318
private final Pattern pattern;
1419
private final Integer cooldown;
1520
private final boolean persistentCooldown;
1621
private final BigDecimal cost;
1722

18-
public CommandFilter(String name, String command, Pattern pattern, Integer cooldown, boolean persistentCooldown, BigDecimal cost) {
23+
public CommandFilter(String name, Pattern pattern, Integer cooldown, boolean persistentCooldown, BigDecimal cost) {
24+
Preconditions.checkNotNull(pattern);
1925
this.name = name;
20-
this.command = command;
2126
this.pattern = pattern;
2227
this.cooldown = cooldown;
2328
this.persistentCooldown = persistentCooldown;
@@ -28,14 +33,6 @@ public String getName() {
2833
return name;
2934
}
3035

31-
public String getCommand() {
32-
return command;
33-
}
34-
35-
public boolean hasCommand() {
36-
return command != null;
37-
}
38-
3936
public Pattern getPattern() {
4037
return pattern;
4138
}

Diff for: Essentials/src/main/java/com/earth2me/essentials/CommandFilters.java

+19-27
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,9 @@
66

77
import java.io.File;
88
import java.math.BigDecimal;
9-
import java.util.HashMap;
9+
import java.util.ArrayList;
10+
import java.util.EnumMap;
11+
import java.util.List;
1012
import java.util.Locale;
1113
import java.util.Map;
1214
import 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();
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package com.earth2me.essentials;
2+
3+
import java.math.BigDecimal;
4+
import java.util.regex.Pattern;
5+
6+
public class EssCommandFilter extends CommandFilter {
7+
8+
private final String command;
9+
10+
public EssCommandFilter(String name, String command, Pattern pattern, Integer cooldown, boolean persistentCooldown, BigDecimal cost) {
11+
super(name, pattern, cooldown, persistentCooldown, cost);
12+
this.command = command;
13+
}
14+
15+
public String getCommand() {
16+
return command;
17+
}
18+
}

Diff for: Essentials/src/main/java/com/earth2me/essentials/EssentialsPlayerListener.java

+4-4
Original file line numberDiff line numberDiff line change
@@ -603,18 +603,18 @@ public void onPlayerCommandPreprocess(final PlayerCommandPreprocessEvent event)
603603
}
604604

605605
if (!cooldownFound) {
606-
final CommandFilter cooldownFilter = ess.getCommandFilters().getCommandCooldown(user, fullCommand, false);
606+
final CommandFilter cooldownFilter = ess.getCommandFilters().getCommandCooldown(user, fullCommand, CommandFilter.Type.REGEX);
607607
if (cooldownFilter != null) {
608608
if (ess.getSettings().isDebug()) {
609-
ess.getLogger().info("Applying " + cooldownFilter.getCooldown() + "ms cooldown on /" + fullCommand + " for" + user.getName() + ".");
609+
ess.getLogger().info("Applying " + cooldownFilter.getCooldown() + "ms cooldown on /" + fullCommand + " for " + user.getName() + ".");
610610
}
611611
cooldownFilter.applyCooldownTo(user);
612612
}
613613

614-
final CommandFilter costFilter = ess.getCommandFilters().getCommandCost(user, fullCommand, false);
614+
final CommandFilter costFilter = ess.getCommandFilters().getCommandCost(user, fullCommand, CommandFilter.Type.REGEX);
615615
if (costFilter != null) {
616616
if (ess.getSettings().isDebug()) {
617-
ess.getLogger().info("Applying a cost of " + costFilter.getCost() + " on /" + fullCommand + " for" + user.getName() + ".");
617+
ess.getLogger().info("Applying a cost of " + costFilter.getCost() + " on /" + fullCommand + " for " + user.getName() + ".");
618618
}
619619

620620
final BigDecimal cost = costFilter.getCost();
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package com.earth2me.essentials;
2+
3+
import java.math.BigDecimal;
4+
import java.util.regex.Pattern;
5+
6+
public class RegexCommandFilter extends CommandFilter {
7+
8+
public RegexCommandFilter(String name, Pattern pattern, Integer cooldown, boolean persistentCooldown, BigDecimal cost) {
9+
super(name, pattern, cooldown, persistentCooldown, cost);
10+
}
11+
}

Diff for: Essentials/src/main/java/com/earth2me/essentials/Trade.java

+19-8
Original file line numberDiff line numberDiff line change
@@ -283,17 +283,14 @@ public void charge(final IUser user) throws ChargeException {
283283
} catch (final ExecutionException e) {
284284
throw (ChargeException) e.getCause();
285285
}
286-
} else {
287-
if (command != null && !command.isEmpty()) {
288-
final CommandFilter cooldownFilter = ess.getCommandFilters().getCommandCooldown(user, command, true);
289-
if (cooldownFilter != null) {
290-
cooldownFilter.applyCooldownTo(user);
291-
}
292-
}
293286
}
294287
}
295288

296289
public void charge(final IUser user, final CompletableFuture<Boolean> future) {
290+
charge(user, this.command, future);
291+
}
292+
293+
public void charge(final IUser user, final String cooldownCommand, final CompletableFuture<Boolean> future) {
297294
if (ess.getSettings().isDebug()) {
298295
ess.getLogger().log(Level.INFO, "attempting to charge user " + user.getName());
299296
}
@@ -340,6 +337,20 @@ public void charge(final IUser user, final CompletableFuture<Boolean> future) {
340337
if (ess.getSettings().isDebug()) {
341338
ess.getLogger().log(Level.INFO, "charge user " + user.getName() + " completed");
342339
}
340+
341+
if (cooldownCommand != null && !cooldownCommand.isEmpty()) {
342+
final CommandFilter cooldownFilter = ess.getCommandFilters().getCommandCooldown(user, cooldownCommand, CommandFilter.Type.ESS);
343+
if (cooldownFilter != null) {
344+
if (ess.getSettings().isDebug()) {
345+
ess.getLogger().info("Applying " + cooldownFilter.getCooldown() + "ms cooldown on /" + cooldownCommand + " for " + user.getName() + ".");
346+
}
347+
cooldownFilter.applyCooldownTo(user);
348+
}
349+
}
350+
}
351+
352+
public String getCommand() {
353+
return command;
343354
}
344355

345356
public BigDecimal getMoney() {
@@ -369,7 +380,7 @@ public TradeType getType() {
369380
public BigDecimal getCommandCost(final IUser user) {
370381
BigDecimal cost = BigDecimal.ZERO;
371382
if (command != null && !command.isEmpty()) {
372-
final CommandFilter filter = ess.getCommandFilters().getCommandCost(user, command.charAt(0) == '/' ? command.substring(1) : command, true);
383+
final CommandFilter filter = ess.getCommandFilters().getCommandCost(user, command.charAt(0) == '/' ? command.substring(1) : command, CommandFilter.Type.ESS);
373384
if (filter != null && filter.getCost().signum() != 0) {
374385
cost = filter.getCost();
375386
} else if (fallbackTrade != null) {

0 commit comments

Comments
 (0)