Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitdfda202

Browse files
committed
Make CommandFilter more extendable + fix AsyncTeleport not applying command cooldown
1 parent76e45fd commitdfda202

File tree

7 files changed

+90
-53
lines changed

7 files changed

+90
-53
lines changed

‎Essentials/src/main/java/com/earth2me/essentials/AsyncTeleport.java‎

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -258,13 +258,17 @@ private void teleport(final IUser teleportee, final ITarget target, final Trade
258258
delay =event.getDelay();
259259

260260
TradecashCharge =chargeFor;
261+
StringcooldownCommand =null;
261262

262263
if (chargeFor !=null) {
263264
chargeFor.isAffordableFor(teleportOwner,future);
264265
if (future.isCompletedExceptionally()) {
265266
return;
266267
}
267268

269+
// When cashCharge is being reassigned below, ensure the charge knows the command we should apply cooldown on
270+
cooldownCommand =chargeFor.getCommand();
271+
268272
//This code is to make sure that commandcosts are checked in the initial world, and not in the resulting world.
269273
if (!chargeFor.getCommandCost(teleportOwner).equals(BigDecimal.ZERO)) {
270274
//By converting a command cost to a regular cost, the command cost permission isn't checked when executing the charge after teleport.
@@ -281,7 +285,7 @@ private void teleport(final IUser teleportee, final ITarget target, final Trade
281285
}
282286
nowAsync(teleportee,target,cause,future);
283287
if (cashCharge !=null) {
284-
cashCharge.charge(teleportOwner,future);
288+
cashCharge.charge(teleportOwner,cooldownCommand,future);
285289
if (future.isCompletedExceptionally()) {
286290
return;
287291
}
@@ -305,13 +309,17 @@ private void teleportOther(final IUser teleporter, final IUser teleportee, final
305309
delay =event.getDelay();
306310

307311
TradecashCharge =chargeFor;
312+
StringcooldownCommand =null;
308313

309314
if (teleporter !=null &&chargeFor !=null) {
310315
chargeFor.isAffordableFor(teleporter,future);
311316
if (future.isCompletedExceptionally()) {
312317
return;
313318
}
314319

320+
// When cashCharge is being reassigned below, ensure the charge knows the command we should apply cooldown on
321+
cooldownCommand =chargeFor.getCommand();
322+
315323
//This code is to make sure that commandcosts are checked in the initial world, and not in the resulting world.
316324
if (!chargeFor.getCommandCost(teleporter).equals(BigDecimal.ZERO)) {
317325
//By converting a command cost to a regular cost, the command cost permission isn't checked when executing the charge after teleport.
@@ -332,7 +340,7 @@ private void teleportOther(final IUser teleporter, final IUser teleportee, final
332340

333341
nowAsync(teleportee,target,cause,future);
334342
if (teleporter !=null &&cashCharge !=null) {
335-
cashCharge.charge(teleporter,future);
343+
cashCharge.charge(teleporter,cooldownCommand,future);
336344
if (future.isCompletedExceptionally()) {
337345
return;
338346
}

‎Essentials/src/main/java/com/earth2me/essentials/CommandFilter.java‎

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,28 @@
11
packagecom.earth2me.essentials;
22

3+
importcom.google.common.base.Preconditions;
34
importnet.ess3.api.IUser;
45

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

9-
publicclassCommandFilter {
10+
publicabstractclassCommandFilter {
11+
12+
publicenumType {
13+
REGEX,
14+
ESS
15+
}
1016

1117
privatefinalStringname;
12-
privatefinalStringcommand;
1318
privatefinalPatternpattern;
1419
privatefinalIntegercooldown;
1520
privatefinalbooleanpersistentCooldown;
1621
privatefinalBigDecimalcost;
1722

18-
publicCommandFilter(Stringname,Stringcommand,Patternpattern,Integercooldown,booleanpersistentCooldown,BigDecimalcost) {
23+
publicCommandFilter(Stringname,Patternpattern,Integercooldown,booleanpersistentCooldown,BigDecimalcost) {
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
returnname;
2934
}
3035

31-
publicStringgetCommand() {
32-
returncommand;
33-
}
34-
35-
publicbooleanhasCommand() {
36-
returncommand !=null;
37-
}
38-
3936
publicPatterngetPattern() {
4037
returnpattern;
4138
}

‎Essentials/src/main/java/com/earth2me/essentials/CommandFilters.java‎

Lines changed: 19 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,9 @@
66

77
importjava.io.File;
88
importjava.math.BigDecimal;
9-
importjava.util.HashMap;
9+
importjava.util.ArrayList;
10+
importjava.util.EnumMap;
11+
importjava.util.List;
1012
importjava.util.Locale;
1113
importjava.util.Map;
1214
importjava.util.Objects;
@@ -19,7 +21,7 @@ public class CommandFilters implements IConf {
1921
privatefinalIEssentialsessentials;
2022
privatefinalEssentialsConfconfig;
2123
privateConfigurationSectionfilters;
22-
privateMap<String,CommandFilter>commandFilters;
24+
privateMap<CommandFilter.Type,List<CommandFilter>>commandFilters;
2325

2426
publicCommandFilters(finalIEssentialsessentials) {
2527
this.essentials =essentials;
@@ -50,16 +52,16 @@ private ConfigurationSection _getCommandFilterSection() {
5052
returnnull;
5153
}
5254

53-
privateMap<String,CommandFilter>_getCommandFilters() {
54-
finalMap<String,CommandFilter>commandFilters =newHashMap<>();
55+
privateMap<CommandFilter.Type,List<CommandFilter>>_getCommandFilters() {
56+
finalMap<CommandFilter.Type,List<CommandFilter>>commandFilters =newEnumMap<>(CommandFilter.Type.class);
5557
for (finalStringname :filters.getKeys(false)) {
5658
if (!filters.isConfigurationSection(name)) {
5759
EssentialsConf.LOGGER.warning("Invalid command filter '" +name +"'");
5860
continue;
5961
}
6062

6163
finalConfigurationSectionsection =Objects.requireNonNull(filters.getConfigurationSection(name));
62-
Patternpattern =section.isString("pattern") ?compileRegex(section.getString("pattern")) :null;
64+
finalPatternpattern =section.isString("pattern") ?compileRegex(section.getString("pattern")) :null;
6365
finalStringcommand =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
Integercooldown =section.getInt("cooldown", -1);
8178
if (cooldown <0) {
8279
cooldown =null;
@@ -88,7 +85,12 @@ private Map<String, CommandFilter> _getCommandFilters() {
8885
finalBigDecimalcost =EssentialsConf.toBigDecimal(section.getString("cost"),null);
8986

9087
finalStringlowerName =name.toLowerCase(Locale.ENGLISH);
91-
commandFilters.put(lowerName,newCommandFilter(lowerName,command,pattern,cooldown,persistentCooldown,cost));
88+
89+
if (pattern ==null) {
90+
commandFilters.computeIfAbsent(CommandFilter.Type.ESS,k ->newArrayList<>()).add(newEssCommandFilter(lowerName,command,compileRegex(command),cooldown,persistentCooldown,cost));
91+
}else {
92+
commandFilters.computeIfAbsent(CommandFilter.Type.REGEX,k ->newArrayList<>()).add(newRegexCommandFilter(lowerName,pattern,cooldown,persistentCooldown,cost));
93+
}
9294
}
9395
config.save();
9496
returncommandFilters;
@@ -116,28 +118,18 @@ public EssentialsConf getConfig() {
116118
returnconfig;
117119
}
118120

119-
publicCommandFiltergetFilterByName(finalStringname) {
120-
returncommandFilters.get(name.toLowerCase(Locale.ENGLISH));
121-
}
122-
123-
publicCommandFiltergetCommandCooldown(finalIUseruser,finalStringlabel,booleanessCommand) {
121+
publicCommandFiltergetCommandCooldown(finalIUseruser,finalStringlabel,CommandFilter.Typetype) {
124122
if (user.isAuthorized("essentials.commandcooldowns.bypass"))returnnull;
125-
returngetFilter(label,essCommand,filter ->filter.hasCooldown() && !user.isAuthorized("essentials.commandcooldowns.bypass." +filter.getName()));
123+
returngetFilter(label,type,filter ->filter.hasCooldown() && !user.isAuthorized("essentials.commandcooldowns.bypass." +filter.getName()));
126124
}
127125

128-
publicCommandFiltergetCommandCost(finalIUseruser,finalStringlabel,booleanessCommand) {
126+
publicCommandFiltergetCommandCost(finalIUseruser,finalStringlabel,CommandFilter.Typetype) {
129127
if (user.isAuthorized("essentials.nocommandcost.all"))returnnull;
130-
returngetFilter(label,essCommand,filter ->filter.hasCost() && !user.isAuthorized("essentials.nocommandcost." +filter.getName()));
128+
returngetFilter(label,type,filter ->filter.hasCost() && !user.isAuthorized("essentials.nocommandcost." +filter.getName()));
131129
}
132130

133-
privateCommandFiltergetFilter(finalStringlabel,booleanessCommand,Predicate<CommandFilter>filterPredicate) {
134-
for (CommandFilterfilter :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+
privateCommandFiltergetFilter(finalStringlabel,CommandFilter.Typetype,Predicate<CommandFilter>filterPredicate) {
132+
for (CommandFilterfilter :commandFilters.get(type)) {
141133
if (!filterPredicate.test(filter))continue;
142134

143135
finalbooleanmatches =filter.getPattern().matcher(label).matches();
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
packagecom.earth2me.essentials;
2+
3+
importjava.math.BigDecimal;
4+
importjava.util.regex.Pattern;
5+
6+
publicclassEssCommandFilterextendsCommandFilter {
7+
8+
privatefinalStringcommand;
9+
10+
publicEssCommandFilter(Stringname,Stringcommand,Patternpattern,Integercooldown,booleanpersistentCooldown,BigDecimalcost) {
11+
super(name,pattern,cooldown,persistentCooldown,cost);
12+
this.command =command;
13+
}
14+
15+
publicStringgetCommand() {
16+
returncommand;
17+
}
18+
}

‎Essentials/src/main/java/com/earth2me/essentials/EssentialsPlayerListener.java‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -603,18 +603,18 @@ public void onPlayerCommandPreprocess(final PlayerCommandPreprocessEvent event)
603603
}
604604

605605
if (!cooldownFound) {
606-
finalCommandFiltercooldownFilter =ess.getCommandFilters().getCommandCooldown(user,fullCommand,false);
606+
finalCommandFiltercooldownFilter =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-
finalCommandFiltercostFilter =ess.getCommandFilters().getCommandCost(user,fullCommand,false);
614+
finalCommandFiltercostFilter =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
finalBigDecimalcost =costFilter.getCost();
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
packagecom.earth2me.essentials;
2+
3+
importjava.math.BigDecimal;
4+
importjava.util.regex.Pattern;
5+
6+
publicclassRegexCommandFilterextendsCommandFilter {
7+
8+
publicRegexCommandFilter(Stringname,Patternpattern,Integercooldown,booleanpersistentCooldown,BigDecimalcost) {
9+
super(name,pattern,cooldown,persistentCooldown,cost);
10+
}
11+
}

‎Essentials/src/main/java/com/earth2me/essentials/Trade.java‎

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

296289
publicvoidcharge(finalIUseruser,finalCompletableFuture<Boolean>future) {
290+
charge(user,this.command,future);
291+
}
292+
293+
publicvoidcharge(finalIUseruser,finalStringcooldownCommand,finalCompletableFuture<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+
finalCommandFiltercooldownFilter =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+
publicStringgetCommand() {
353+
returncommand;
343354
}
344355

345356
publicBigDecimalgetMoney() {
@@ -369,7 +380,7 @@ public TradeType getType() {
369380
publicBigDecimalgetCommandCost(finalIUseruser) {
370381
BigDecimalcost =BigDecimal.ZERO;
371382
if (command !=null && !command.isEmpty()) {
372-
finalCommandFilterfilter =ess.getCommandFilters().getCommandCost(user,command.charAt(0) =='/' ?command.substring(1) :command,true);
383+
finalCommandFilterfilter =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
}elseif (fallbackTrade !=null) {

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp