From 163987910624286f0635f8c98a559bde59ef8f4f Mon Sep 17 00:00:00 2001 From: Luck Date: Sat, 12 May 2018 01:51:38 +0100 Subject: [PATCH] Reduce the number of config lookups required when calculating contexts --- .../java/me/lucko/luckperms/api/Contexts.java | 10 +++++- .../bukkit/contexts/BukkitContextManager.java | 19 ++++++----- .../bungee/contexts/BungeeContextManager.java | 11 +------ .../api/delegates/misc/ApiConfiguration.java | 9 ++--- .../luckperms/common/config/ConfigKeys.java | 33 +++++++++---------- .../contexts/AbstractContextManager.java | 10 +----- .../nukkit/contexts/NukkitContextManager.java | 19 ++++++----- .../sponge/contexts/SpongeContextManager.java | 11 +------ 8 files changed, 53 insertions(+), 69 deletions(-) diff --git a/api/src/main/java/me/lucko/luckperms/api/Contexts.java b/api/src/main/java/me/lucko/luckperms/api/Contexts.java index d1cfb472..35485f6e 100644 --- a/api/src/main/java/me/lucko/luckperms/api/Contexts.java +++ b/api/src/main/java/me/lucko/luckperms/api/Contexts.java @@ -130,6 +130,14 @@ public class Contexts { return new Contexts(contextSet.makeImmutable(), ImmutableSet.copyOf(settings)); } + private static EnumSet asEnumSet(Set settings) { + if (settings instanceof EnumSet) { + return ((EnumSet) settings); + } else { + return EnumSet.copyOf(settings); + } + } + /** * Creates a new {@link Contexts} instance. * @@ -141,7 +149,7 @@ public class Contexts { Objects.requireNonNull(contextSet, "contextSet"); Objects.requireNonNull(settings, "settings"); - EnumSet settingsCopy = EnumSet.copyOf(settings); + EnumSet settingsCopy = asEnumSet(settings); if (contextSet.isEmpty() && DEFAULT_SETTINGS.equals(settingsCopy)) { return GLOBAL; } diff --git a/bukkit/src/main/java/me/lucko/luckperms/bukkit/contexts/BukkitContextManager.java b/bukkit/src/main/java/me/lucko/luckperms/bukkit/contexts/BukkitContextManager.java index 810463bb..4da776c6 100644 --- a/bukkit/src/main/java/me/lucko/luckperms/bukkit/contexts/BukkitContextManager.java +++ b/bukkit/src/main/java/me/lucko/luckperms/bukkit/contexts/BukkitContextManager.java @@ -26,6 +26,7 @@ package me.lucko.luckperms.bukkit.contexts; import me.lucko.luckperms.api.Contexts; +import me.lucko.luckperms.api.LookupSetting; import me.lucko.luckperms.api.context.ImmutableContextSet; import me.lucko.luckperms.bukkit.LPBukkitPlugin; import me.lucko.luckperms.common.config.ConfigKeys; @@ -33,6 +34,8 @@ import me.lucko.luckperms.common.contexts.AbstractContextManager; import org.bukkit.entity.Player; +import java.util.EnumSet; + public class BukkitContextManager extends AbstractContextManager { public BukkitContextManager(LPBukkitPlugin plugin) { super(plugin, Player.class); @@ -40,14 +43,12 @@ public class BukkitContextManager extends AbstractContextManager { @Override public Contexts formContexts(Player subject, ImmutableContextSet contextSet) { - return Contexts.of( - contextSet, - this.plugin.getConfiguration().get(ConfigKeys.INCLUDING_GLOBAL_PERMS), - this.plugin.getConfiguration().get(ConfigKeys.INCLUDING_GLOBAL_WORLD_PERMS), - true, - this.plugin.getConfiguration().get(ConfigKeys.APPLYING_GLOBAL_GROUPS), - this.plugin.getConfiguration().get(ConfigKeys.APPLYING_GLOBAL_WORLD_GROUPS), - subject.isOp() - ); + EnumSet settings = this.plugin.getConfiguration().get(ConfigKeys.LOOKUP_SETTINGS); + if (subject.isOp()) { + settings = EnumSet.copyOf(settings); + settings.add(LookupSetting.IS_OP); + } + + return Contexts.of(contextSet, settings); } } diff --git a/bungee/src/main/java/me/lucko/luckperms/bungee/contexts/BungeeContextManager.java b/bungee/src/main/java/me/lucko/luckperms/bungee/contexts/BungeeContextManager.java index 57835bc3..3086e203 100644 --- a/bungee/src/main/java/me/lucko/luckperms/bungee/contexts/BungeeContextManager.java +++ b/bungee/src/main/java/me/lucko/luckperms/bungee/contexts/BungeeContextManager.java @@ -28,7 +28,6 @@ package me.lucko.luckperms.bungee.contexts; import me.lucko.luckperms.api.Contexts; import me.lucko.luckperms.api.context.ImmutableContextSet; import me.lucko.luckperms.bungee.LPBungeePlugin; -import me.lucko.luckperms.common.config.ConfigKeys; import me.lucko.luckperms.common.contexts.AbstractContextManager; import net.md_5.bungee.api.connection.ProxiedPlayer; @@ -40,14 +39,6 @@ public class BungeeContextManager extends AbstractContextManager @Override public Contexts formContexts(ProxiedPlayer subject, ImmutableContextSet contextSet) { - return Contexts.of( - contextSet, - this.plugin.getConfiguration().get(ConfigKeys.INCLUDING_GLOBAL_PERMS), - this.plugin.getConfiguration().get(ConfigKeys.INCLUDING_GLOBAL_WORLD_PERMS), - true, - this.plugin.getConfiguration().get(ConfigKeys.APPLYING_GLOBAL_GROUPS), - this.plugin.getConfiguration().get(ConfigKeys.APPLYING_GLOBAL_WORLD_GROUPS), - false - ); + return formContexts(contextSet); } } diff --git a/common/src/main/java/me/lucko/luckperms/common/api/delegates/misc/ApiConfiguration.java b/common/src/main/java/me/lucko/luckperms/common/api/delegates/misc/ApiConfiguration.java index 8e0f3874..6a5388b6 100644 --- a/common/src/main/java/me/lucko/luckperms/common/api/delegates/misc/ApiConfiguration.java +++ b/common/src/main/java/me/lucko/luckperms/common/api/delegates/misc/ApiConfiguration.java @@ -26,6 +26,7 @@ package me.lucko.luckperms.common.api.delegates.misc; import me.lucko.luckperms.api.LPConfiguration; +import me.lucko.luckperms.api.LookupSetting; import me.lucko.luckperms.common.config.ConfigKey; import me.lucko.luckperms.common.config.ConfigKeys; import me.lucko.luckperms.common.config.LuckPermsConfiguration; @@ -52,22 +53,22 @@ public class ApiConfiguration implements LPConfiguration { @Override public boolean getIncludeGlobalPerms() { - return this.handle.get(ConfigKeys.INCLUDING_GLOBAL_PERMS); + return this.handle.get(ConfigKeys.LOOKUP_SETTINGS).contains(LookupSetting.INCLUDE_NODES_SET_WITHOUT_SERVER); } @Override public boolean getIncludeGlobalWorldPerms() { - return this.handle.get(ConfigKeys.INCLUDING_GLOBAL_WORLD_PERMS); + return this.handle.get(ConfigKeys.LOOKUP_SETTINGS).contains(LookupSetting.INCLUDE_NODES_SET_WITHOUT_WORLD); } @Override public boolean getApplyGlobalGroups() { - return this.handle.get(ConfigKeys.APPLYING_GLOBAL_GROUPS); + return this.handle.get(ConfigKeys.LOOKUP_SETTINGS).contains(LookupSetting.APPLY_PARENTS_SET_WITHOUT_SERVER); } @Override public boolean getApplyGlobalWorldGroups() { - return this.handle.get(ConfigKeys.APPLYING_GLOBAL_WORLD_GROUPS); + return this.handle.get(ConfigKeys.LOOKUP_SETTINGS).contains(LookupSetting.APPLY_PARENTS_SET_WITHOUT_WORLD); } @Nonnull diff --git a/common/src/main/java/me/lucko/luckperms/common/config/ConfigKeys.java b/common/src/main/java/me/lucko/luckperms/common/config/ConfigKeys.java index 42099f6b..e0418711 100644 --- a/common/src/main/java/me/lucko/luckperms/common/config/ConfigKeys.java +++ b/common/src/main/java/me/lucko/luckperms/common/config/ConfigKeys.java @@ -28,6 +28,9 @@ package me.lucko.luckperms.common.config; import com.google.common.collect.ImmutableList; import com.google.common.collect.ImmutableMap; +import me.lucko.luckperms.api.Contexts; +import me.lucko.luckperms.api.LookupSetting; +import me.lucko.luckperms.api.context.ContextSet; import me.lucko.luckperms.api.metastacking.MetaStackDefinition; import me.lucko.luckperms.common.assignments.AssignmentRule; import me.lucko.luckperms.common.config.keys.AbstractKey; @@ -53,6 +56,7 @@ import java.lang.reflect.Field; import java.lang.reflect.Modifier; import java.util.ArrayList; import java.util.EnumMap; +import java.util.EnumSet; import java.util.LinkedHashMap; import java.util.List; import java.util.Map; @@ -89,24 +93,19 @@ public class ConfigKeys { })); /** - * If permissions without a server context should be included. + * The lookup settings for contexts (care should be taken to not mutate this method) */ - public static final ConfigKey INCLUDING_GLOBAL_PERMS = BooleanKey.of("include-global", true); - - /** - * If permissions without a world context should be included. - */ - public static final ConfigKey INCLUDING_GLOBAL_WORLD_PERMS = BooleanKey.of("include-global-world", true); - - /** - * If groups without a server context should be included. - */ - public static final ConfigKey APPLYING_GLOBAL_GROUPS = BooleanKey.of("apply-global-groups", true); - - /** - * If groups without a world context should be included. - */ - public static final ConfigKey APPLYING_GLOBAL_WORLD_GROUPS = BooleanKey.of("apply-global-world-groups", true); + public static final ConfigKey> LOOKUP_SETTINGS = AbstractKey.of(c -> { + return EnumSet.copyOf(Contexts.of( + ContextSet.empty(), + c.getBoolean("include-global", true), + c.getBoolean("include-global-world", true), + true, + c.getBoolean("apply-global-groups", true), + c.getBoolean("apply-global-world-groups", true), + false + ).getSettings()); + }); /** * # If the servers own UUID cache/lookup facility should be used when there is no record for a player in the LuckPerms cache. diff --git a/common/src/main/java/me/lucko/luckperms/common/contexts/AbstractContextManager.java b/common/src/main/java/me/lucko/luckperms/common/contexts/AbstractContextManager.java index e69dfeba..61dcdba4 100644 --- a/common/src/main/java/me/lucko/luckperms/common/contexts/AbstractContextManager.java +++ b/common/src/main/java/me/lucko/luckperms/common/contexts/AbstractContextManager.java @@ -140,15 +140,7 @@ public abstract class AbstractContextManager implements ContextManager { @Override public Contexts formContexts(ImmutableContextSet contextSet) { - return Contexts.of( - contextSet, - this.plugin.getConfiguration().get(ConfigKeys.INCLUDING_GLOBAL_PERMS), - this.plugin.getConfiguration().get(ConfigKeys.INCLUDING_GLOBAL_WORLD_PERMS), - true, - this.plugin.getConfiguration().get(ConfigKeys.APPLYING_GLOBAL_GROUPS), - this.plugin.getConfiguration().get(ConfigKeys.APPLYING_GLOBAL_WORLD_GROUPS), - false - ); + return Contexts.of(contextSet, this.plugin.getConfiguration().get(ConfigKeys.LOOKUP_SETTINGS)); } @Override diff --git a/nukkit/src/main/java/me/lucko/luckperms/nukkit/contexts/NukkitContextManager.java b/nukkit/src/main/java/me/lucko/luckperms/nukkit/contexts/NukkitContextManager.java index 8eab39aa..ccc605f8 100644 --- a/nukkit/src/main/java/me/lucko/luckperms/nukkit/contexts/NukkitContextManager.java +++ b/nukkit/src/main/java/me/lucko/luckperms/nukkit/contexts/NukkitContextManager.java @@ -26,6 +26,7 @@ package me.lucko.luckperms.nukkit.contexts; import me.lucko.luckperms.api.Contexts; +import me.lucko.luckperms.api.LookupSetting; import me.lucko.luckperms.api.context.ImmutableContextSet; import me.lucko.luckperms.common.config.ConfigKeys; import me.lucko.luckperms.common.contexts.AbstractContextManager; @@ -33,6 +34,8 @@ import me.lucko.luckperms.nukkit.LPNukkitPlugin; import cn.nukkit.Player; +import java.util.EnumSet; + public class NukkitContextManager extends AbstractContextManager { public NukkitContextManager(LPNukkitPlugin plugin) { super(plugin, Player.class); @@ -40,14 +43,12 @@ public class NukkitContextManager extends AbstractContextManager { @Override public Contexts formContexts(Player subject, ImmutableContextSet contextSet) { - return Contexts.of( - contextSet, - this.plugin.getConfiguration().get(ConfigKeys.INCLUDING_GLOBAL_PERMS), - this.plugin.getConfiguration().get(ConfigKeys.INCLUDING_GLOBAL_WORLD_PERMS), - true, - this.plugin.getConfiguration().get(ConfigKeys.APPLYING_GLOBAL_GROUPS), - this.plugin.getConfiguration().get(ConfigKeys.APPLYING_GLOBAL_WORLD_GROUPS), - subject.isOp() - ); + EnumSet settings = this.plugin.getConfiguration().get(ConfigKeys.LOOKUP_SETTINGS); + if (subject.isOp()) { + settings = EnumSet.copyOf(settings); + settings.add(LookupSetting.IS_OP); + } + + return Contexts.of(contextSet, settings); } } diff --git a/sponge/src/main/java/me/lucko/luckperms/sponge/contexts/SpongeContextManager.java b/sponge/src/main/java/me/lucko/luckperms/sponge/contexts/SpongeContextManager.java index 5af67d89..fefad79b 100644 --- a/sponge/src/main/java/me/lucko/luckperms/sponge/contexts/SpongeContextManager.java +++ b/sponge/src/main/java/me/lucko/luckperms/sponge/contexts/SpongeContextManager.java @@ -27,7 +27,6 @@ package me.lucko.luckperms.sponge.contexts; import me.lucko.luckperms.api.Contexts; import me.lucko.luckperms.api.context.ImmutableContextSet; -import me.lucko.luckperms.common.config.ConfigKeys; import me.lucko.luckperms.common.contexts.AbstractContextManager; import me.lucko.luckperms.sponge.LPSpongePlugin; @@ -40,14 +39,6 @@ public class SpongeContextManager extends AbstractContextManager { @Override public Contexts formContexts(Subject subject, ImmutableContextSet contextSet) { - return Contexts.of( - contextSet, - this.plugin.getConfiguration().get(ConfigKeys.INCLUDING_GLOBAL_PERMS), - this.plugin.getConfiguration().get(ConfigKeys.INCLUDING_GLOBAL_WORLD_PERMS), - true, - this.plugin.getConfiguration().get(ConfigKeys.APPLYING_GLOBAL_GROUPS), - this.plugin.getConfiguration().get(ConfigKeys.APPLYING_GLOBAL_WORLD_GROUPS), - false - ); + return formContexts(contextSet); } }