-
-
Notifications
You must be signed in to change notification settings - Fork 1k
Asynchronously filter commands on Paper when possible #4460
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 1 commit
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
be2ba00
Use Paper async event to filter commands when available
mdcfe 6caa2fa
Merge branch '2.x' into feature/paper-async-command-send
JRoy 7228b38
Merge branch '2.x' into feature/paper-async-command-send
JRoy 03a4ac4
Apply suggestions from code review
JRoy 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
30 changes: 30 additions & 0 deletions
30
providers/BaseProviders/src/main/java/net/ess3/provider/CommandSendListenerProvider.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,30 @@ | ||
| package net.ess3.provider; | ||
|
|
||
| import org.bukkit.entity.Player; | ||
| import org.bukkit.event.Listener; | ||
|
|
||
| import java.util.function.Function; | ||
| import java.util.function.Predicate; | ||
|
|
||
| /** | ||
| * A provider for 1.13+ command send listeners. | ||
| * <p> | ||
| * Note to maintainers: this doesn't extend {@link ProviderListener} because it doesn't make sense here. | ||
| */ | ||
| public abstract class CommandSendListenerProvider implements Provider, Listener { | ||
| private final Filter commandFilter; | ||
|
|
||
| protected CommandSendListenerProvider(Filter commandFilter) { | ||
| this.commandFilter = commandFilter; | ||
| } | ||
|
|
||
| protected final Predicate<String> filter(final Player player) { | ||
| return commandFilter.apply(player); | ||
| } | ||
|
|
||
| /** | ||
| * A function that returns a predicate to test whether commands should be hidden from the given player. | ||
| */ | ||
| public interface Filter extends Function<Player, Predicate<String>> { | ||
| } | ||
| } |
25 changes: 25 additions & 0 deletions
25
...roviders/src/main/java/net/ess3/provider/providers/BukkitCommandSendListenerProvider.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,25 @@ | ||
| package net.ess3.provider.providers; | ||
|
|
||
| import net.ess3.provider.CommandSendListenerProvider; | ||
| import org.bukkit.event.EventHandler; | ||
| import org.bukkit.event.EventPriority; | ||
| import org.bukkit.event.player.PlayerCommandSendEvent; | ||
|
|
||
| import java.util.function.Predicate; | ||
|
|
||
| public class BukkitCommandSendListenerProvider extends CommandSendListenerProvider { | ||
| public BukkitCommandSendListenerProvider(Filter commandFilter) { | ||
| super(commandFilter); | ||
| } | ||
|
|
||
| @EventHandler(priority = EventPriority.NORMAL) | ||
| public void onCommandSend(PlayerCommandSendEvent event) { | ||
| final Predicate<String> filter = filter(event.getPlayer()); | ||
| event.getCommands().removeIf(filter); | ||
| } | ||
|
|
||
| @Override | ||
| public String getDescription() { | ||
| return "Bukkit synchronous command send listener"; | ||
| } | ||
| } |
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
35 changes: 35 additions & 0 deletions
35
...rProvider/src/main/java/net/ess3/provider/providers/PaperCommandSendListenerProvider.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,35 @@ | ||
| package net.ess3.provider.providers; | ||
|
|
||
| import com.destroystokyo.paper.event.brigadier.AsyncPlayerSendCommandsEvent; | ||
| import com.mojang.brigadier.tree.CommandNode; | ||
| import net.ess3.provider.CommandSendListenerProvider; | ||
| import org.bukkit.event.EventHandler; | ||
| import org.bukkit.event.EventPriority; | ||
|
|
||
| import java.util.Collection; | ||
| import java.util.function.Predicate; | ||
|
|
||
| public class PaperCommandSendListenerProvider extends CommandSendListenerProvider { | ||
|
|
||
| public PaperCommandSendListenerProvider(Filter commandFilter) { | ||
| super(commandFilter); | ||
| } | ||
|
|
||
| @EventHandler(priority = EventPriority.NORMAL) | ||
| public void onAsyncCommandSend(@SuppressWarnings("deprecation") AsyncPlayerSendCommandsEvent<?> event) { | ||
| if (!event.isAsynchronous() && event.hasFiredAsync()) { | ||
| // this has already fired once async | ||
| return; | ||
| } | ||
|
|
||
| final Collection<? extends CommandNode<?>> children = event.getCommandNode().getChildren(); | ||
| final Predicate<String> filter = filter(event.getPlayer()); | ||
|
|
||
| children.removeIf(node -> filter.test(node.getName())); | ||
| } | ||
|
|
||
| @Override | ||
| public String getDescription() { | ||
| return "Paper async Brigadier command send listener"; | ||
| } | ||
| } |
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
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.