Skip to content

Commit 00484e0

Browse files
committed
Squashed commit of the following:
commit cf01355 Author: lokka30 <lokka30@protonmail.com> Date: Wed May 21 18:07:12 2025 +0800 build: set ver 1.1.7 commit 63dcf71 Author: lokka30 <lokka30@protonmail.com> Date: Wed May 21 18:06:26 2025 +0800 plugin-bukkit: begin working on TabCompleteLitener commit 2350ad6 Author: lokka30 <lokka30@protonmail.com> Date: Sat Feb 8 11:35:57 2025 +0800 build: set ver 1.1.6 commit 4bbb561 Author: lokka30 <lokka30@protonmail.com> Date: Sat Feb 8 11:35:45 2025 +0800 Settings: fix debug category loader casting incorrectly commit 86f8352 Author: lokka30 <lokka30@protonmail.com> Date: Sat Feb 8 11:16:42 2025 +0800 CmdBlocking: add suggestion arg testing and debug logging commit ed01203 Author: lokka30 <lokka30@protonmail.com> Date: Mon Jan 27 10:29:56 2025 +0800 TestCmdEvaluation: add testing for case sensitivity; flip equals/same args
1 parent a453781 commit 00484e0

File tree

11 files changed

+163
-56
lines changed

11 files changed

+163
-56
lines changed

blackwidowlib/pom.xml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,11 @@
2424
<parent>
2525
<groupId>io.github.arcaneplugins.blackwidow</groupId>
2626
<artifactId>blackwidow</artifactId>
27-
<version>1.1.5</version>
27+
<version>1.1.7</version>
2828
</parent>
2929

3030
<artifactId>blackwidowlib</artifactId>
31-
<version>1.1.5</version>
31+
<version>1.1.7</version>
3232
<build>
3333
<plugins>
3434
<plugin>

blackwidowlib/src/test/java/io/github/arcaneplugins/blackwidow/lib/cmdblocking/TestCmdEvaluation.java

Lines changed: 80 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -21,18 +21,40 @@
2121
import org.junit.jupiter.api.Assertions;
2222
import org.junit.jupiter.api.Test;
2323

24-
import java.util.Arrays;
2524
import java.util.Collection;
2625
import java.util.Collections;
2726
import java.util.EnumSet;
27+
import java.util.LinkedList;
2828
import java.util.function.BiFunction;
2929
import java.util.function.Consumer;
3030
import java.util.function.Function;
3131
import java.util.function.Supplier;
3232
import java.util.regex.Pattern;
3333

34+
import static java.util.Arrays.asList;
35+
import static java.util.Collections.singletonList;
36+
3437
public final class TestCmdEvaluation {
3538

39+
public TestCmdEvaluation() {
40+
TEST_CHAINS.addAll(
41+
asList(
42+
new Chain("0", true, Policy.DENY, asList("/cd reload *", "/"), false, EvalCause.setValues()),
43+
new Chain("1", true, Policy.ALLOW, singletonList("/cd reload"), false, EvalCause.setValues()),
44+
new Chain("2", true, Policy.DENY, singletonList("/cd"), false, EvalCause.setValues()),
45+
new Chain("3", true, Policy.DENY, asList(TEST_PLUGIN_CMDS), false, EvalCause.setValues()),
46+
new Chain("4", true, Policy.ALLOW, singletonList("/es version"), false, EvalCause.setValues()),
47+
new Chain("5", true, Policy.DENY, asList("/es give", "/es enchant"), false, EvalCause.setValues()),
48+
new Chain("6", true, Policy.ALLOW, singletonList("^(/heywhats(up)?(?:$|\\W)cool(beans)?(?:$|\\W).*)"), true, EvalCause.setValues()),
49+
new Chain("7", true, Policy.DENY, singletonList("^(/heywhats(up)?(?:$|\\W).*)"), true, EvalCause.setValues()),
50+
new Chain("8", false, Policy.DENY, singletonList("/thisshouldnotbedenied"), false, EvalCause.setValues()),
51+
new Chain("9", true, Policy.DENY, singletonList("/blocksuggestionsonly"), false, EnumSet.of(EvalCause.CMD_SUGGESTION)),
52+
new Chain("10", true, Policy.DENY, singletonList("/UPPERCASE"), false, EvalCause.setValues()),
53+
new Chain("11", true, Policy.DENY, singletonList("/suggestion argument"), false, Collections.singleton(EvalCause.CMD_SUGGESTION))
54+
)
55+
);
56+
}
57+
3658
// Various 'plugin checking' commands
3759
private static final String[] TEST_PLUGIN_CMDS = {
3860
"/plugins", "/pl", "/version", "/ver", "/icanhasbukkit",
@@ -41,26 +63,16 @@ public final class TestCmdEvaluation {
4163

4264
// Various programatically defined chains of rules, some regex ones provided at the bottom.
4365
// Chains should NOT contain 'donotuseinchains', doing so will make some tests fail (by design).
44-
private static final Collection<Chain> TEST_CHAINS = Arrays.asList(
45-
new Chain("0", true, Policy.DENY, Arrays.asList("/cd reload *", "/"), false, EvalCause.setValues()),
46-
new Chain("1", true, Policy.ALLOW, Collections.singletonList("/cd reload"), false, EvalCause.setValues()),
47-
new Chain("2", true, Policy.DENY, Collections.singletonList("/cd"), false, EvalCause.setValues()),
48-
new Chain("3", true, Policy.DENY, Arrays.asList(TEST_PLUGIN_CMDS), false, EvalCause.setValues()),
49-
new Chain("4", true, Policy.ALLOW, Collections.singletonList("/es version"), false, EvalCause.setValues()),
50-
new Chain("5", true, Policy.DENY, Arrays.asList("/es give", "/es enchant"), false, EvalCause.setValues()),
51-
new Chain("6", true, Policy.ALLOW, Collections.singletonList("^(/heywhats(up)?(?:$|\\W)cool(beans)?(?:$|\\W).*)"), true, EvalCause.setValues()),
52-
new Chain("7", true, Policy.DENY, Collections.singletonList("^(/heywhats(up)?(?:$|\\W).*)"), true, EvalCause.setValues()),
53-
new Chain("8", false, Policy.DENY, Collections.singletonList("/thisshouldnotbedenied"), false, EvalCause.setValues()),
54-
new Chain("9", true, Policy.DENY, Collections.singletonList("/blocksuggestionsonly"), false, EnumSet.of(EvalCause.CMD_SUGGESTION))
55-
);
66+
private static final Collection<Chain> TEST_CHAINS = new LinkedList<>();
5667

5768
// Commands to be tested which are expected to be evaluated with a DENY policy.
5869
public static final String[] TEST_CMDS_EXPECTING_DENY = {
5970
"/cd abc", "/cd reloada", "/cd reloa", "/cd reload abc",
6071
"/plugins", "/pl", "/version", "/ver", "/icanhasbukkit",
6172
"/about", "/?", "/help", "/ehelp", "/paper", "/spigot",
6273
"/es give", "/es enchant", "/cd", "/", "/:", "/hello:how",
63-
"/the:quick brown fox", "/bukkit:help",
74+
"/the:quick brown fox", "/bukkit:help", "/uppercase",
75+
"/UPPERCASE",
6476
// regex:
6577
"/heywhats",
6678
"/heywhats coolio",
@@ -162,11 +174,11 @@ public void testWildcardBaseCmd() {
162174
final BiFunction<String, Policy, Boolean> test = (cmd, policy) -> {
163175
final Evaluation eval = Evaluator.evaluate(
164176
"/hello",
165-
Collections.singletonList(new Chain(
177+
singletonList(new Chain(
166178
"wildcard-base-cmd",
167179
true,
168180
Policy.DENY,
169-
Collections.singletonList("/*"),
181+
singletonList("/*"),
170182
false,
171183
EvalCause.setValues()
172184
)),
@@ -195,11 +207,11 @@ public void testWildcardArgs() {
195207
final BiFunction<String, Policy, Boolean> test = (cmd, policy) -> {
196208
final Evaluation eval = Evaluator.evaluate(
197209
cmd,
198-
Collections.singletonList(new Chain(
210+
singletonList(new Chain(
199211
"wildcard-arg-cmd",
200212
true,
201213
policy,
202-
Collections.singletonList("/hello * world *"),
214+
singletonList("/hello * world *"),
203215
false,
204216
EvalCause.setValues()
205217
)),
@@ -233,7 +245,7 @@ public void testRulesExpectingDeny() {
233245
for (final String cmd : TEST_CMDS_EXPECTING_DENY) {
234246
final Evaluation eval = testEvaluation(cmd, true);
235247
Assertions.assertFalse(eval.dueToException(), "Exception not expected here");
236-
Assertions.assertEquals(eval.policy(), Policy.DENY, "Wrong policy evaluated for cmd='" +
248+
Assertions.assertEquals(Policy.DENY, eval.policy(), "Wrong policy evaluated for cmd='" +
237249
cmd + "'; description='" + eval.description() + "'");
238250
}
239251
}
@@ -249,7 +261,7 @@ public void testRulesExpectingAllow() {
249261
for (final String cmd : TEST_CMDS_EXPECTING_ALLOW) {
250262
final Evaluation eval = testEvaluation(cmd, true);
251263
Assertions.assertFalse(eval.dueToException(), "Exception not expected here");
252-
Assertions.assertEquals(eval.policy(), Policy.ALLOW, "Wrong policy evaluated for cmd='" +
264+
Assertions.assertEquals(Policy.ALLOW, eval.policy(), "Wrong policy evaluated for cmd='" +
253265
cmd + "'; description='" + eval.description() + "'");
254266
}
255267
}
@@ -269,7 +281,7 @@ public void testColonBlocking() {
269281

270282
Assertions.assertFalse(eval.dueToException(), "Exception not expected here");
271283
Assertions.assertTrue(eval.dueToColonInFirstArg(), "Colon in first arg expected to be cause");
272-
Assertions.assertEquals(eval.policy(), Policy.DENY,
284+
Assertions.assertEquals(Policy.DENY, eval.policy(),
273285
"Wrong policy evaluated for cmd='" + cmd + "'; description='" +
274286
eval.description() + "'");
275287
}
@@ -279,7 +291,7 @@ public void testColonBlocking() {
279291

280292
Assertions.assertFalse(eval.dueToException(), "Exception not expected here");
281293
Assertions.assertFalse(eval.dueToColonInFirstArg(), "Colon in first arg unexpected to be cause");
282-
Assertions.assertEquals(eval.policy(), Policy.ALLOW,
294+
Assertions.assertEquals(Policy.ALLOW, eval.policy(),
283295
"Wrong policy evaluated for cmd='" + cmd + "'; description='" +
284296
eval.description() + "'");
285297
}
@@ -291,7 +303,7 @@ public void testColonBlocking() {
291303

292304
Assertions.assertFalse(eval.dueToException(), "Exception not expected here");
293305
Assertions.assertFalse(eval.dueToColonInFirstArg(), "Colon in first arg unexpected to be cause");
294-
Assertions.assertEquals(eval.policy(), Policy.ALLOW,
306+
Assertions.assertEquals(Policy.ALLOW, eval.policy(),
295307
"Wrong policy evaluated for cmd='" + cmd + "'; description='" +
296308
eval.description() + "'");
297309
}
@@ -300,7 +312,7 @@ public void testColonBlocking() {
300312
final Evaluation eval = testEvaluation(cmd, false);
301313
Assertions.assertFalse(eval.dueToException(), "Exception not expected here");
302314
Assertions.assertFalse(eval.dueToColonInFirstArg(), "Colon in first arg unexpected to be cause");
303-
Assertions.assertEquals(eval.policy(), Policy.ALLOW,
315+
Assertions.assertEquals(Policy.ALLOW, eval.policy(),
304316
"Wrong policy evaluated for cmd='" + cmd + "'; description='" +
305317
eval.description() + "'");
306318
}
@@ -351,7 +363,7 @@ public void testDenyDefaultPolicy() {
351363
);
352364

353365
Assertions.assertFalse(eval.dueToException(), "Exception not expected here");
354-
Assertions.assertEquals(eval.policy(), Policy.DENY, "Wrong policy evaluated");
366+
Assertions.assertEquals(Policy.DENY, eval.policy(), "Wrong policy evaluated");
355367
}
356368

357369
/**
@@ -372,7 +384,7 @@ public void testExceptionDeniesCmd() {
372384
final Evaluation eval = testEvaluation("DoesNotHaveStartingSlash", true);
373385

374386
Assertions.assertTrue(eval.dueToException(), "Intentional exception was expected here");
375-
Assertions.assertEquals(eval.policy(), Policy.DENY, "Wrong security policy evaluated");
387+
Assertions.assertEquals(Policy.DENY, eval.policy(), "Wrong security policy evaluated");
376388

377389
Evaluator.SUPPRESS_EXCEPTION_MESSAGES.set(false);
378390
}
@@ -403,12 +415,51 @@ public void testEvalCauseFiltering() {
403415
final Evaluation evalOther = eval.apply(EvalCause.OTHER);
404416

405417
Assertions.assertTrue(evalExecution.dueToDefaultPolicy());
406-
Assertions.assertSame(evalExecution.policy(), Policy.ALLOW);
418+
Assertions.assertSame(Policy.ALLOW, evalExecution.policy());
407419

408420
Assertions.assertFalse(evalSuggestion.dueToDefaultPolicy()); // << notice
409-
Assertions.assertSame(evalSuggestion.policy(), Policy.DENY); // << notice
421+
Assertions.assertSame(Policy.DENY, evalSuggestion.policy()); // << notice
410422

411423
Assertions.assertTrue(evalOther.dueToDefaultPolicy());
412-
Assertions.assertSame(evalOther.policy(), Policy.ALLOW);
424+
Assertions.assertSame(Policy.ALLOW, evalOther.policy());
425+
}
426+
427+
/**
428+
* Tests that command suggestion filtering is working with arguments too.
429+
*
430+
* @author lokka30
431+
* @since 1.1.6
432+
*/
433+
@Test
434+
public void testSuggestionFilteringArgumentsWorks() {
435+
final Collection<String> cmdsShouldAllow = asList(
436+
"/suggestion argumento",
437+
"/suggestion argumen argument",
438+
"/suggestion"
439+
);
440+
final Collection<String> cmdsShouldDeny = asList(
441+
"/suggestion argument",
442+
"/suggestion argument test"
443+
);
444+
445+
cmdsShouldAllow.forEach(cmd -> Assertions.assertSame(Policy.ALLOW, Evaluator.evaluate(
446+
cmd,
447+
TEST_CHAINS,
448+
TEST_DEFAULT_POLICY,
449+
true,
450+
EvalCause.CMD_SUGGESTION,
451+
debugLogger,
452+
warningLogger
453+
).policy()));
454+
455+
cmdsShouldDeny.forEach(cmd -> Assertions.assertSame(Policy.DENY, Evaluator.evaluate(
456+
cmd,
457+
TEST_CHAINS,
458+
TEST_DEFAULT_POLICY,
459+
true,
460+
EvalCause.CMD_SUGGESTION,
461+
debugLogger,
462+
warningLogger
463+
).policy()));
413464
}
414465
}

blackwidowpluginbukkit/pom.xml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,11 @@
2424
<parent>
2525
<groupId>io.github.arcaneplugins.blackwidow</groupId>
2626
<artifactId>blackwidow</artifactId>
27-
<version>1.1.5</version>
27+
<version>1.1.7</version>
2828
</parent>
2929

3030
<artifactId>blackwidowpluginbukkit</artifactId>
31-
<version>1.1.5</version>
31+
<version>1.1.7</version>
3232

3333
<properties>
3434
<maven.compiler.source>21</maven.compiler.source>
@@ -47,7 +47,7 @@
4747
<dependency>
4848
<groupId>io.github.arcaneplugins.blackwidow</groupId>
4949
<artifactId>blackwidowlib</artifactId>
50-
<version>1.1.5</version>
50+
<version>1.1.7</version>
5151
<scope>compile</scope>
5252
</dependency>
5353
<dependency>

blackwidowpluginbukkit/src/main/java/io/github/arcaneplugins/blackwidow/plugin/bukkit/cfg/settings/Settings.java

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
import org.spongepowered.configurate.ConfigurateException;
2525

2626
import java.util.Collections;
27+
import java.util.LinkedList;
2728
import java.util.Objects;
2829

2930
public final class Settings extends YamlCfg {
@@ -58,11 +59,9 @@ protected void loadMore() {
5859
plugin().enabledDebugCategories().clear();
5960
plugin().enabledDebugCategories().addAll(
6061
Objects.requireNonNullElse(
61-
root()
62-
.node("debug-categories")
63-
.getList(DebugCategory.class),
64-
Collections.emptySet()
65-
)
62+
root().node("debug-categories").getList(String.class),
63+
new LinkedList<String>()
64+
).stream().map(DebugCategory::valueOf).toList()
6665
);
6766
} catch (final ConfigurateException ex) {
6867
throw new RuntimeException(ex.getMessage(), ex);

blackwidowpluginbukkit/src/main/java/io/github/arcaneplugins/blackwidow/plugin/bukkit/component/cmdblocking/CmdBlocker.java

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
import io.github.arcaneplugins.blackwidow.plugin.bukkit.BlackWidow;
2626
import io.github.arcaneplugins.blackwidow.plugin.bukkit.logic.Action;
2727
import io.github.arcaneplugins.blackwidow.plugin.bukkit.logic.Context;
28+
import io.github.arcaneplugins.blackwidow.plugin.bukkit.util.DebugCategory;
2829
import org.bukkit.entity.Player;
2930
import org.spongepowered.configurate.CommentedConfigurationNode;
3031

@@ -97,14 +98,22 @@ public Evaluation evalAndProcess(final Context context, final String cmd, final
9798
final Player player = context.player(false);
9899
if (player != null && player.isOp() && operatorsBypassCompletely()) {
99100
return new Evaluation(cmd, Policy.ALLOW, null, null, "Operators configured to bypass command blocking")
100-
.withDueToOperatorsBypassCmdBlocking(true);
101+
.withDueToOperatorsBypassCmdBlocking(true);
101102
}
102103

103104
// copy chains into a new LinkedHashSet, remove chains not applicable to context.
104105
final Collection<BukkitChain> applicableChains = new LinkedHashSet<>(chains());
105106
applicableChains.removeIf(chain -> chain.requirements().stream().anyMatch(req -> !req.validate(context)));
106107

107-
final Evaluation eval = Evaluator.evaluate(cmd, applicableChains, defaultBehaviourPolicy(), denyColonInFirstArgEnabled(), cause, (debugMsg) -> { /* do nothing */ }, (warnMsg) -> plugin().getLogger().warning(warnMsg.get()));
108+
final Evaluation eval = Evaluator.evaluate(
109+
cmd,
110+
applicableChains,
111+
defaultBehaviourPolicy(),
112+
denyColonInFirstArgEnabled(),
113+
cause,
114+
debugMsg -> plugin().debugLog(DebugCategory.CMD_DETECTION_ENGINE, debugMsg),
115+
warnMsg -> plugin().getLogger().warning(warnMsg.get())
116+
);
108117

109118
if (runActions && eval.dueToColonInFirstArg()) {
110119
denyColonInFirstArgActions().forEach(act -> act.run(context));

blackwidowpluginbukkit/src/main/java/io/github/arcaneplugins/blackwidow/plugin/bukkit/listener/ListenerManager.java

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,9 @@
2121
import io.github.arcaneplugins.blackwidow.plugin.bukkit.BlackWidow;
2222
import io.github.arcaneplugins.blackwidow.plugin.bukkit.listener.bukkit.PlayerCommandPreprocessListener;
2323
import io.github.arcaneplugins.blackwidow.plugin.bukkit.listener.bukkit.PlayerCommandSendListener;
24+
import io.github.arcaneplugins.blackwidow.plugin.bukkit.listener.bukkit.TabCompleteListener;
2425
import io.github.arcaneplugins.blackwidow.plugin.bukkit.listener.paper.AsyncPlayerCommandSendListener;
26+
import org.bukkit.Bukkit;
2527
import org.bukkit.event.Listener;
2628

2729
import java.util.Collection;
@@ -39,9 +41,8 @@ public ListenerManager(
3941
}
4042

4143
private void constructListeners() {
42-
listeners().add(
43-
new PlayerCommandPreprocessListener(plugin())
44-
);
44+
listeners().add(new PlayerCommandPreprocessListener(plugin()));
45+
listeners().add(new TabCompleteListener(plugin()));
4546

4647
listeners().add(
4748
plugin().usingPaper() && plugin().usePaperFeatures() ?
@@ -54,10 +55,7 @@ public void load() {
5455
plugin().getLogger().info("Loading listeners.");
5556
listeners().clear();
5657
constructListeners();
57-
58-
for (final Listener listener : listeners()) {
59-
plugin().getServer().getPluginManager().registerEvents(listener, plugin());
60-
}
58+
listeners().forEach(listener -> Bukkit.getPluginManager().registerEvents(listener, plugin()));
6159
}
6260

6361
public Collection<Listener> listeners() {

blackwidowpluginbukkit/src/main/java/io/github/arcaneplugins/blackwidow/plugin/bukkit/listener/bukkit/PlayerCommandPreprocessListener.java

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -40,13 +40,8 @@ public PlayerCommandPreprocessListener(
4040
this.plugin = plugin;
4141
}
4242

43-
@EventHandler(priority = EventPriority.LOWEST)
43+
@EventHandler(priority = EventPriority.LOWEST, ignoreCancelled = true)
4444
public void handle(final PlayerCommandPreprocessEvent event) {
45-
if (event.isCancelled()) {
46-
// Event is already cancelled - nothing to do!
47-
return;
48-
}
49-
5045
final Player player = event.getPlayer();
5146
final String msg = event.getMessage();
5247
final String msgWithSlash = msg.startsWith("/") ? msg : '/' + msg;

blackwidowpluginbukkit/src/main/java/io/github/arcaneplugins/blackwidow/plugin/bukkit/listener/bukkit/PlayerCommandSendListener.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
import io.github.arcaneplugins.blackwidow.plugin.bukkit.logic.Context;
2626
import org.bukkit.entity.Player;
2727
import org.bukkit.event.EventHandler;
28+
import org.bukkit.event.EventPriority;
2829
import org.bukkit.event.Listener;
2930
import org.bukkit.event.player.PlayerCommandSendEvent;
3031

@@ -38,7 +39,7 @@ public PlayerCommandSendListener(
3839
this.plugin = plugin;
3940
}
4041

41-
@EventHandler
42+
@EventHandler(ignoreCancelled = true, priority = EventPriority.LOWEST)
4243
public void handle(final PlayerCommandSendEvent event) {
4344
if (event.getCommands().isEmpty()) {
4445
// no commands to process - nothing to do here

0 commit comments

Comments
 (0)