-
Notifications
You must be signed in to change notification settings - Fork 0
Fix builtin command ownership classification #131
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
3a2ccf4
Expose plugin-wide command ownership lookup
remdui 093bb14
Expose ServerFeatures command label ownership
remdui 2d3b480
Exclude ServerFeatures-owned commands from builtin discovery
remdui 1c67a90
Use ServerFeatures ownership during builtin discovery
remdui b19abde
Add builtin command ownership regressions
remdui 3cf7eaa
Restore Brigadier warning text
remdui fdcaee2
Potential fix for pull request finding 'Array index out of bounds'
remdui File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
133 changes: 133 additions & 0 deletions
133
...c/serverfeatures/features/builtincommandblocker/internal/BuiltinCommandOwnershipTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,133 @@ | ||
| package nl.hauntedmc.serverfeatures.features.builtincommandblocker.internal; | ||
|
|
||
| import org.bukkit.command.Command; | ||
| import org.bukkit.command.CommandSender; | ||
| import org.bukkit.command.PluginIdentifiableCommand; | ||
| import org.bukkit.plugin.Plugin; | ||
| import org.jetbrains.annotations.NotNull; | ||
| import org.junit.jupiter.api.Test; | ||
|
|
||
| import java.util.EnumSet; | ||
| import java.util.LinkedHashMap; | ||
| import java.util.Map; | ||
| import java.util.Set; | ||
|
|
||
| import static org.junit.jupiter.api.Assertions.assertEquals; | ||
| import static org.junit.jupiter.api.Assertions.assertTrue; | ||
| import static org.mockito.Mockito.mock; | ||
| import static org.mockito.Mockito.when; | ||
|
|
||
| class BuiltinCommandOwnershipTest { | ||
|
|
||
| @Test | ||
| void serverFeaturesOwnedVanillaWrapperIsNotClassifiedAsMinecraft() { | ||
| Command command = new FakeVanillaCommandWrapper("autopickup"); | ||
| Map<String, Command> commands = registrations( | ||
| "autopickup", command, | ||
| "minecraft:autopickup", command | ||
| ); | ||
|
|
||
| BuiltinCommandSnapshot snapshot = BuiltinCommandDiscovery.discover( | ||
| commands, | ||
| Map.of(), | ||
| allBlocked(), | ||
| Set.of("autopickup")::contains | ||
| ); | ||
|
|
||
| assertTrue(snapshot.blockedCommands().isEmpty()); | ||
| assertEquals(0, snapshot.detectedSources().get("minecraft")); | ||
| } | ||
|
|
||
| @Test | ||
| void namespacedBuiltinFallbackRemainsBlockableWhenPlainLabelIsOwned() { | ||
| Command command = new FakeVanillaCommandWrapper("restart"); | ||
|
|
||
| BuiltinCommandSnapshot snapshot = BuiltinCommandDiscovery.discover( | ||
| registrations("minecraft:restart", command), | ||
| Map.of(), | ||
| allBlocked(), | ||
| Set.of("restart")::contains | ||
| ); | ||
|
|
||
| assertEquals(Set.of("minecraft:restart"), snapshot.blockedCommands()); | ||
| assertEquals(1, snapshot.detectedSources().get("minecraft")); | ||
| } | ||
|
|
||
| @Test | ||
| void pluginIdentifiableCommandWinsOverBuiltinNamespaceHeuristics() { | ||
| Plugin plugin = mock(Plugin.class); | ||
| when(plugin.getName()).thenReturn("ExamplePlugin"); | ||
| Command command = new PluginOwnedCommand("friends", plugin); | ||
|
|
||
| BuiltinCommandSnapshot snapshot = BuiltinCommandDiscovery.discover( | ||
| registrations( | ||
| "friends", command, | ||
| "minecraft:friends", command | ||
| ), | ||
| allBlocked() | ||
| ); | ||
|
|
||
| assertTrue(snapshot.blockedCommands().isEmpty()); | ||
| } | ||
|
|
||
| private static BuiltinCommandBlockerSettings allBlocked() { | ||
| return new BuiltinCommandBlockerSettings( | ||
| EnumSet.allOf(BuiltinCommandSource.class), | ||
| true, | ||
| false, | ||
| Set.of() | ||
| ); | ||
| } | ||
|
|
||
| private static Map<String, Command> registrations(Object... entries) { | ||
| if (entries.length % 2 != 0) { | ||
| throw new IllegalArgumentException("registrations requires key/value pairs"); | ||
| } | ||
|
|
||
| LinkedHashMap<String, Command> result = new LinkedHashMap<>(); | ||
| for (int index = 0; index < entries.length; index += 2) { | ||
| result.put((String) entries[index], (Command) entries[index + 1]); | ||
| } | ||
| return result; | ||
| } | ||
|
|
||
| private static class FakeVanillaCommandWrapper extends Command { | ||
|
|
||
| private FakeVanillaCommandWrapper(String name) { | ||
| super(name); | ||
| } | ||
|
|
||
| @Override | ||
| public boolean execute( | ||
| @NotNull CommandSender sender, | ||
| @NotNull String commandLabel, | ||
| @NotNull String @NotNull [] args | ||
| ) { | ||
| return true; | ||
| } | ||
| } | ||
|
|
||
| private static final class PluginOwnedCommand extends Command implements PluginIdentifiableCommand { | ||
|
|
||
| private final Plugin plugin; | ||
|
|
||
| private PluginOwnedCommand(String name, Plugin plugin) { | ||
| super(name); | ||
| this.plugin = plugin; | ||
| } | ||
|
|
||
| @Override | ||
| public @NotNull Plugin getPlugin() { | ||
| return plugin; | ||
| } | ||
|
|
||
| @Override | ||
| public boolean execute( | ||
| @NotNull CommandSender sender, | ||
| @NotNull String commandLabel, | ||
| @NotNull String @NotNull [] args | ||
| ) { | ||
| return true; | ||
| } | ||
| } | ||
| } | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.