mirror of
https://github.com/EssentialsX/Essentials.git
synced 2024-09-28 23:57:27 +02:00
Reduce permission check calls in PlayerCommandSendEvent (#3026)
This reduces permissions checks by only checking each command once, not checking each command for each alias. The impact of this is a near-tenfold reduction in permission checks during this listener, which should slightly improve performance when using LuckPerms (and provide significant improvements on older inefficient permissions plugins).
This commit is contained in:
parent
8069370b8c
commit
0bafbc3184
@ -48,6 +48,24 @@ public class EssentialsPlayerListener implements Listener {
|
|||||||
this.ess = parent;
|
this.ess = parent;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private static boolean isEntityPickupEvent() {
|
||||||
|
try {
|
||||||
|
Class.forName("org.bukkit.event.entity.EntityPickupItemEvent");
|
||||||
|
return true;
|
||||||
|
} catch (ClassNotFoundException ignored) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private static boolean isCommandSendEvent() {
|
||||||
|
try {
|
||||||
|
Class.forName("org.bukkit.event.player.PlayerCommandSendEvent");
|
||||||
|
return true;
|
||||||
|
} catch (ClassNotFoundException ignored) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
public void registerEvents() {
|
public void registerEvents() {
|
||||||
ess.getServer().getPluginManager().registerEvents(this, ess);
|
ess.getServer().getPluginManager().registerEvents(this, ess);
|
||||||
|
|
||||||
@ -788,24 +806,6 @@ public class EssentialsPlayerListener implements Listener {
|
|||||||
user.updateActivityOnInteract(true);
|
user.updateActivityOnInteract(true);
|
||||||
}
|
}
|
||||||
|
|
||||||
private static boolean isEntityPickupEvent() {
|
|
||||||
try {
|
|
||||||
Class.forName("org.bukkit.event.entity.EntityPickupItemEvent");
|
|
||||||
return true;
|
|
||||||
} catch (ClassNotFoundException ignored) {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private static boolean isCommandSendEvent() {
|
|
||||||
try {
|
|
||||||
Class.forName("org.bukkit.event.player.PlayerCommandSendEvent");
|
|
||||||
return true;
|
|
||||||
} catch (ClassNotFoundException ignored) {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private final class PickupListenerPre1_12 implements Listener {
|
private final class PickupListenerPre1_12 implements Listener {
|
||||||
@EventHandler(priority = EventPriority.LOW, ignoreCancelled = true)
|
@EventHandler(priority = EventPriority.LOW, ignoreCancelled = true)
|
||||||
public void onPlayerPickupItem(final org.bukkit.event.player.PlayerPickupItemEvent event) {
|
public void onPlayerPickupItem(final org.bukkit.event.player.PlayerPickupItemEvent event) {
|
||||||
@ -833,13 +833,25 @@ public class EssentialsPlayerListener implements Listener {
|
|||||||
public void onCommandSend(final PlayerCommandSendEvent event) {
|
public void onCommandSend(final PlayerCommandSendEvent event) {
|
||||||
User user = ess.getUser(event.getPlayer());
|
User user = ess.getUser(event.getPlayer());
|
||||||
|
|
||||||
ArrayList<String> removedCmds = new ArrayList<>(event.getCommands());
|
Set<PluginCommand> checked = new HashSet<>();
|
||||||
|
Set<PluginCommand> toRemove = new HashSet<>();
|
||||||
|
|
||||||
event.getCommands().removeIf(str -> shouldHideFromUser(str, user));
|
event.getCommands().removeIf(label -> {
|
||||||
|
if (isEssentialsCommand(label)) {
|
||||||
|
PluginCommand command = ess.getServer().getPluginCommand(label);
|
||||||
|
if (!checked.contains(command)) {
|
||||||
|
checked.add(command);
|
||||||
|
if (!user.isAuthorized(command.getName().equals("r") ? "essentials.msg" : "essentials." + command.getName())) {
|
||||||
|
toRemove.add(command);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return toRemove.contains(command);
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
});
|
||||||
|
|
||||||
if (ess.getSettings().isDebug()) {
|
if (ess.getSettings().isDebug()) {
|
||||||
removedCmds.removeAll(event.getCommands());
|
ess.getLogger().info("Removed commands: " + toRemove.toString());
|
||||||
ess.getLogger().info("Removed commands: " + removedCmds.toString());
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -848,15 +860,13 @@ public class EssentialsPlayerListener implements Listener {
|
|||||||
* - The command is a plugin command
|
* - The command is a plugin command
|
||||||
* - The plugin command is from Essentials
|
* - The plugin command is from Essentials
|
||||||
* - There is no known alternative OR the alternative is overridden by Essentials
|
* - There is no known alternative OR the alternative is overridden by Essentials
|
||||||
* - The user is not allowed to perform the given Essentials command
|
|
||||||
*/
|
*/
|
||||||
private boolean shouldHideFromUser(String commandLabel, User user) {
|
private boolean isEssentialsCommand(String label) {
|
||||||
PluginCommand command = ess.getServer().getPluginCommand(commandLabel);
|
PluginCommand command = ess.getServer().getPluginCommand(label);
|
||||||
|
|
||||||
return command != null
|
return command != null
|
||||||
&& command.getPlugin() == ess
|
&& command.getPlugin() == ess
|
||||||
&& (ess.getSettings().isCommandOverridden(commandLabel) || (ess.getAlternativeCommandsHandler().getAlternative(commandLabel) == null))
|
&& (ess.getSettings().isCommandOverridden(label) || (ess.getAlternativeCommandsHandler().getAlternative(label) == null));
|
||||||
&& !user.isAuthorized(command.getName().equals("r") ? "essentials.msg" : "essentials." + command.getName());
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user