Optimize context set manipulation

This commit is contained in:
Luck 2018-12-06 14:02:41 +00:00
parent 7b5917660c
commit b7ff0824b9
No known key found for this signature in database
GPG Key ID: EFA9B3EC5FD90F8B
6 changed files with 57 additions and 19 deletions

View File

@ -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<LookupSetting> 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.
*

View File

@ -99,12 +99,13 @@ public class BukkitContextManager extends ContextManager<Player> {
@Override
public Contexts formContexts(Player subject, ImmutableContextSet contextSet) {
EnumSet<LookupSetting> 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<LookupSetting> settings = EnumSet.copyOf(contexts.getSettings());
settings.add(LookupSetting.IS_OP);
contexts = contexts.setSettings(settings);
}
return Contexts.of(contextSet, settings);
return contexts.setContexts(contextSet);
}
}

View File

@ -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

View File

@ -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<EnumSet<LookupSetting>> LOOKUP_SETTINGS = customKey(c -> {
return EnumSet.copyOf(Contexts.of(
public static final ConfigKey<Contexts> 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());
);
});
/**

View File

@ -146,7 +146,7 @@ public abstract class ContextManager<T> {
* @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);
}
/**

View File

@ -99,12 +99,13 @@ public class NukkitContextManager extends ContextManager<Player> {
@Override
public Contexts formContexts(Player subject, ImmutableContextSet contextSet) {
EnumSet<LookupSetting> 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<LookupSetting> settings = EnumSet.copyOf(contexts.getSettings());
settings.add(LookupSetting.IS_OP);
contexts = contexts.setSettings(settings);
}
return Contexts.of(contextSet, settings);
return contexts.setContexts(contextSet);
}
}