From b7ff0824b93c0046e1515c0baa4e77e044b5e93c Mon Sep 17 00:00:00 2001 From: Luck Date: Thu, 6 Dec 2018 14:02:41 +0000 Subject: [PATCH] Optimize context set manipulation --- .../java/me/lucko/luckperms/api/Contexts.java | 42 ++++++++++++++++++- .../bukkit/context/BukkitContextManager.java | 7 ++-- .../api/implementation/ApiConfiguration.java | 8 ++-- .../luckperms/common/config/ConfigKeys.java | 10 ++--- .../common/context/ContextManager.java | 2 +- .../nukkit/context/NukkitContextManager.java | 7 ++-- 6 files changed, 57 insertions(+), 19 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 c3b603499..02dae35bb 100644 --- a/api/src/main/java/me/lucko/luckperms/api/Contexts.java +++ b/api/src/main/java/me/lucko/luckperms/api/Contexts.java @@ -187,7 +187,7 @@ public class Contexts { } /** - * Gets the {@link ContextSet} which represent these {@link Contexts}. + * Gets the {@link ContextSet set of context pairs} for this {@link Contexts} instance. * * @return an immutable context from this instance * @since 2.13 @@ -197,7 +197,7 @@ public class Contexts { } /** - * Gets the set of {@link LookupSetting}s which represent these {@link Contexts}. + * Gets the set of {@link LookupSetting}s for this {@link Contexts} instance. * * @return the settings * @since 4.2 @@ -206,6 +206,44 @@ public class Contexts { return LookupSetting.createSetFromFlag(this.settingsFlag); } + /** + * Creates a copy of this {@link Contexts} instance, but with the + * {@link ContextSet set of context pairs} replaced by the given set. + * + * @param contextSet the context set + * @return a new contexts instance representing the new settings + * @since 4.4 + */ + public @NonNull Contexts setContexts(@NonNull ContextSet contextSet) { + ImmutableContextSet cs = Objects.requireNonNull(contextSet, "contextSet").makeImmutable(); + if (this.contextSet.equals(cs)) { + return this; + } + return new Contexts(Objects.requireNonNull(contextSet, "contextSet").makeImmutable(), this.settingsFlag); + } + + /** + * Creates a copy of this {@link Contexts} instance, but with the + * {@link LookupSetting}s replaced by the given settings. + * + * @param settings the lookup settings + * @return a new contexts instance representing the new settings + * @since 4.4 + */ + public @NonNull Contexts setSettings(@NonNull Set settings) { + Objects.requireNonNull(settings, "settings"); + + byte settingsFlag = LookupSetting.createFlag(settings); + if (this.settingsFlag == settingsFlag) { + return this; + } + if (this.contextSet.isEmpty() && DEFAULT_SETTINGS_FLAG == settingsFlag) { + return GLOBAL; + } + + return new Contexts(this.contextSet, settingsFlag); + } + /** * Gets if the given {@link LookupSetting} is set. * diff --git a/bukkit/src/main/java/me/lucko/luckperms/bukkit/context/BukkitContextManager.java b/bukkit/src/main/java/me/lucko/luckperms/bukkit/context/BukkitContextManager.java index 459579e31..c219ab211 100644 --- a/bukkit/src/main/java/me/lucko/luckperms/bukkit/context/BukkitContextManager.java +++ b/bukkit/src/main/java/me/lucko/luckperms/bukkit/context/BukkitContextManager.java @@ -99,12 +99,13 @@ public class BukkitContextManager extends ContextManager { @Override public Contexts formContexts(Player subject, ImmutableContextSet contextSet) { - EnumSet settings = this.plugin.getConfiguration().get(ConfigKeys.LOOKUP_SETTINGS); + Contexts contexts = this.plugin.getConfiguration().get(ConfigKeys.GLOBAL_CONTEXTS); if (subject.isOp()) { - settings = EnumSet.copyOf(settings); + EnumSet settings = EnumSet.copyOf(contexts.getSettings()); settings.add(LookupSetting.IS_OP); + contexts = contexts.setSettings(settings); } - return Contexts.of(contextSet, settings); + return contexts.setContexts(contextSet); } } diff --git a/common/src/main/java/me/lucko/luckperms/common/api/implementation/ApiConfiguration.java b/common/src/main/java/me/lucko/luckperms/common/api/implementation/ApiConfiguration.java index 761c2b03b..f161277bc 100644 --- a/common/src/main/java/me/lucko/luckperms/common/api/implementation/ApiConfiguration.java +++ b/common/src/main/java/me/lucko/luckperms/common/api/implementation/ApiConfiguration.java @@ -52,22 +52,22 @@ public class ApiConfiguration implements LPConfiguration { @Override public boolean getIncludeGlobalPerms() { - return this.handle.get(ConfigKeys.LOOKUP_SETTINGS).contains(LookupSetting.INCLUDE_NODES_SET_WITHOUT_SERVER); + return this.handle.get(ConfigKeys.GLOBAL_CONTEXTS).hasSetting(LookupSetting.INCLUDE_NODES_SET_WITHOUT_SERVER); } @Override public boolean getIncludeGlobalWorldPerms() { - return this.handle.get(ConfigKeys.LOOKUP_SETTINGS).contains(LookupSetting.INCLUDE_NODES_SET_WITHOUT_WORLD); + return this.handle.get(ConfigKeys.GLOBAL_CONTEXTS).hasSetting(LookupSetting.INCLUDE_NODES_SET_WITHOUT_WORLD); } @Override public boolean getApplyGlobalGroups() { - return this.handle.get(ConfigKeys.LOOKUP_SETTINGS).contains(LookupSetting.APPLY_PARENTS_SET_WITHOUT_SERVER); + return this.handle.get(ConfigKeys.GLOBAL_CONTEXTS).hasSetting(LookupSetting.APPLY_PARENTS_SET_WITHOUT_SERVER); } @Override public boolean getApplyGlobalWorldGroups() { - return this.handle.get(ConfigKeys.LOOKUP_SETTINGS).contains(LookupSetting.APPLY_PARENTS_SET_WITHOUT_WORLD); + return this.handle.get(ConfigKeys.GLOBAL_CONTEXTS).hasSetting(LookupSetting.APPLY_PARENTS_SET_WITHOUT_WORLD); } @Override 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 6e46588e2..fcf987fbd 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 @@ -29,7 +29,6 @@ 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.TemporaryMergeBehaviour; import me.lucko.luckperms.api.context.ContextSet; import me.lucko.luckperms.api.metastacking.DuplicateRemovalFunction; @@ -52,7 +51,6 @@ 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; @@ -99,10 +97,10 @@ public final class ConfigKeys { })); /** - * The lookup settings for contexts (care should be taken to not mutate this method) + * The default global contexts instance */ - public static final ConfigKey> LOOKUP_SETTINGS = customKey(c -> { - return EnumSet.copyOf(Contexts.of( + public static final ConfigKey GLOBAL_CONTEXTS = customKey(c -> { + return Contexts.of( ContextSet.empty(), c.getBoolean("include-global", true), c.getBoolean("include-global-world", true), @@ -110,7 +108,7 @@ public final class ConfigKeys { c.getBoolean("apply-global-groups", true), c.getBoolean("apply-global-world-groups", true), false - ).getSettings()); + ); }); /** diff --git a/common/src/main/java/me/lucko/luckperms/common/context/ContextManager.java b/common/src/main/java/me/lucko/luckperms/common/context/ContextManager.java index 0f40b0a05..f4ffb22e9 100644 --- a/common/src/main/java/me/lucko/luckperms/common/context/ContextManager.java +++ b/common/src/main/java/me/lucko/luckperms/common/context/ContextManager.java @@ -146,7 +146,7 @@ public abstract class ContextManager { * @return a contexts instance */ public Contexts formContexts(ImmutableContextSet contextSet) { - return Contexts.of(contextSet, this.plugin.getConfiguration().get(ConfigKeys.LOOKUP_SETTINGS)); + return this.plugin.getConfiguration().get(ConfigKeys.GLOBAL_CONTEXTS).setContexts(contextSet); } /** diff --git a/nukkit/src/main/java/me/lucko/luckperms/nukkit/context/NukkitContextManager.java b/nukkit/src/main/java/me/lucko/luckperms/nukkit/context/NukkitContextManager.java index 11cbe90a2..cba59bbe6 100644 --- a/nukkit/src/main/java/me/lucko/luckperms/nukkit/context/NukkitContextManager.java +++ b/nukkit/src/main/java/me/lucko/luckperms/nukkit/context/NukkitContextManager.java @@ -99,12 +99,13 @@ public class NukkitContextManager extends ContextManager { @Override public Contexts formContexts(Player subject, ImmutableContextSet contextSet) { - EnumSet settings = this.plugin.getConfiguration().get(ConfigKeys.LOOKUP_SETTINGS); + Contexts contexts = this.plugin.getConfiguration().get(ConfigKeys.GLOBAL_CONTEXTS); if (subject.isOp()) { - settings = EnumSet.copyOf(settings); + EnumSet settings = EnumSet.copyOf(contexts.getSettings()); settings.add(LookupSetting.IS_OP); + contexts = contexts.setSettings(settings); } - return Contexts.of(contextSet, settings); + return contexts.setContexts(contextSet); } }