From 93d0c54687d4d6b3eb9ab537f11ca57303f03279 Mon Sep 17 00:00:00 2001 From: Luck Date: Tue, 10 Nov 2020 15:04:05 +0000 Subject: [PATCH] Cache some PermissionProcessor instances --- .../bukkit/calculator/BukkitCalculatorFactory.java | 10 ++++++++-- .../lucko/luckperms/bukkit/calculator/OpProcessor.java | 8 +++++++- .../nukkit/calculator/NukkitCalculatorFactory.java | 10 ++++++++-- .../lucko/luckperms/nukkit/calculator/OpProcessor.java | 8 +++++++- 4 files changed, 30 insertions(+), 6 deletions(-) diff --git a/bukkit/src/main/java/me/lucko/luckperms/bukkit/calculator/BukkitCalculatorFactory.java b/bukkit/src/main/java/me/lucko/luckperms/bukkit/calculator/BukkitCalculatorFactory.java index c3f8de994..effe61b20 100644 --- a/bukkit/src/main/java/me/lucko/luckperms/bukkit/calculator/BukkitCalculatorFactory.java +++ b/bukkit/src/main/java/me/lucko/luckperms/bukkit/calculator/BukkitCalculatorFactory.java @@ -45,8 +45,14 @@ import net.luckperms.api.query.QueryOptions; public class BukkitCalculatorFactory implements CalculatorFactory { private final LPBukkitPlugin plugin; + private final DefaultsProcessor opDefaultsProcessor; + private final DefaultsProcessor nonOpDefaultsProcessor; + public BukkitCalculatorFactory(LPBukkitPlugin plugin) { this.plugin = plugin; + + this.opDefaultsProcessor = new DefaultsProcessor(this.plugin, true); + this.nonOpDefaultsProcessor = new DefaultsProcessor(this.plugin, false); } @Override @@ -73,11 +79,11 @@ public class BukkitCalculatorFactory implements CalculatorFactory { boolean op = queryOptions.option(BukkitContextManager.OP_OPTION).orElse(false); if (metadata.getHolderType() == HolderType.USER && this.plugin.getConfiguration().get(ConfigKeys.APPLY_BUKKIT_DEFAULT_PERMISSIONS)) { - processors.add(new DefaultsProcessor(this.plugin, op)); + processors.add(op ? this.opDefaultsProcessor : this.nonOpDefaultsProcessor); } if (op) { - processors.add(new OpProcessor()); + processors.add(OpProcessor.INSTANCE); } return new PermissionCalculator(this.plugin, metadata, processors.build()); diff --git a/bukkit/src/main/java/me/lucko/luckperms/bukkit/calculator/OpProcessor.java b/bukkit/src/main/java/me/lucko/luckperms/bukkit/calculator/OpProcessor.java index 6ff30b01e..7c22bab2f 100644 --- a/bukkit/src/main/java/me/lucko/luckperms/bukkit/calculator/OpProcessor.java +++ b/bukkit/src/main/java/me/lucko/luckperms/bukkit/calculator/OpProcessor.java @@ -34,9 +34,15 @@ import net.luckperms.api.util.Tristate; * Permission Processor which is added for opped users, to simply return true if * no other processors match. */ -public class OpProcessor implements PermissionProcessor { +public final class OpProcessor implements PermissionProcessor { private static final TristateResult TRUE_RESULT = new TristateResult.Factory(OpProcessor.class).result(Tristate.TRUE); + public static final OpProcessor INSTANCE = new OpProcessor(); + + private OpProcessor() { + + } + @Override public TristateResult hasPermission(String permission) { return TRUE_RESULT; diff --git a/nukkit/src/main/java/me/lucko/luckperms/nukkit/calculator/NukkitCalculatorFactory.java b/nukkit/src/main/java/me/lucko/luckperms/nukkit/calculator/NukkitCalculatorFactory.java index 39abce2ea..f37d56959 100644 --- a/nukkit/src/main/java/me/lucko/luckperms/nukkit/calculator/NukkitCalculatorFactory.java +++ b/nukkit/src/main/java/me/lucko/luckperms/nukkit/calculator/NukkitCalculatorFactory.java @@ -45,8 +45,14 @@ import net.luckperms.api.query.QueryOptions; public class NukkitCalculatorFactory implements CalculatorFactory { private final LPNukkitPlugin plugin; + private final DefaultsProcessor opDefaultsProcessor; + private final DefaultsProcessor nonOpDefaultsProcessor; + public NukkitCalculatorFactory(LPNukkitPlugin plugin) { this.plugin = plugin; + + this.opDefaultsProcessor = new DefaultsProcessor(this.plugin, true); + this.nonOpDefaultsProcessor = new DefaultsProcessor(this.plugin, false); } @Override @@ -73,11 +79,11 @@ public class NukkitCalculatorFactory implements CalculatorFactory { boolean op = queryOptions.option(NukkitContextManager.OP_OPTION).orElse(false); if (metadata.getHolderType() == HolderType.USER && this.plugin.getConfiguration().get(ConfigKeys.APPLY_NUKKIT_DEFAULT_PERMISSIONS)) { - processors.add(new DefaultsProcessor(this.plugin, op)); + processors.add(op ? this.opDefaultsProcessor : this.nonOpDefaultsProcessor); } if (op) { - processors.add(new OpProcessor()); + processors.add(OpProcessor.INSTANCE); } return new PermissionCalculator(this.plugin, metadata, processors.build()); diff --git a/nukkit/src/main/java/me/lucko/luckperms/nukkit/calculator/OpProcessor.java b/nukkit/src/main/java/me/lucko/luckperms/nukkit/calculator/OpProcessor.java index 1d5b811b8..31f125763 100644 --- a/nukkit/src/main/java/me/lucko/luckperms/nukkit/calculator/OpProcessor.java +++ b/nukkit/src/main/java/me/lucko/luckperms/nukkit/calculator/OpProcessor.java @@ -34,9 +34,15 @@ import net.luckperms.api.util.Tristate; * Permission Processor which is added for opped users, to simply return true if * no other processors match. */ -public class OpProcessor implements PermissionProcessor { +public final class OpProcessor implements PermissionProcessor { private static final TristateResult TRUE_RESULT = new TristateResult.Factory(OpProcessor.class).result(Tristate.TRUE); + public static final OpProcessor INSTANCE = new OpProcessor(); + + private OpProcessor() { + + } + @Override public TristateResult hasPermission(String permission) { return TRUE_RESULT;