Skip to content

Commit

Permalink
Add config option to disable built-in contexts
Browse files Browse the repository at this point in the history
  • Loading branch information
lucko committed Mar 1, 2021
1 parent bf0ac1a commit e7f2a8d
Show file tree
Hide file tree
Showing 17 changed files with 197 additions and 59 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,7 @@ protected CalculatorFactory provideCalculatorFactory() {
protected void setupContextManager() {
this.contextManager = new BukkitContextManager(this);

BukkitPlayerCalculator playerCalculator = new BukkitPlayerCalculator(this);
BukkitPlayerCalculator playerCalculator = new BukkitPlayerCalculator(this, getConfiguration().get(ConfigKeys.DISABLED_CONTEXTS));
this.bootstrap.getServer().getPluginManager().registerEvents(playerCalculator, this.bootstrap.getLoader());
this.contextManager.registerCalculator(playerCalculator);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@
import org.bukkit.event.player.PlayerJoinEvent;
import org.checkerframework.checker.nullness.qual.NonNull;

import java.util.Set;

public class BukkitPlayerCalculator implements ContextCalculator<Player>, Listener {
private static final EnumNamer<GameMode> GAMEMODE_NAMER = new EnumNamer<>(
GameMode.class,
Expand All @@ -69,58 +71,81 @@ public class BukkitPlayerCalculator implements ContextCalculator<Player>, Listen

private final LPBukkitPlugin plugin;

public BukkitPlayerCalculator(LPBukkitPlugin plugin) {
private final boolean gamemode;
private final boolean world;
private final boolean dimensionType;

public BukkitPlayerCalculator(LPBukkitPlugin plugin, Set<String> disabled) {
this.plugin = plugin;
this.gamemode = !disabled.contains(DefaultContextKeys.GAMEMODE_KEY);
this.world = !disabled.contains(DefaultContextKeys.WORLD_KEY);
this.dimensionType = !disabled.contains(DefaultContextKeys.DIMENSION_TYPE_KEY);
}

@SuppressWarnings("ConstantConditions") // bukkit lies
@Override
public void calculate(@NonNull Player subject, @NonNull ContextConsumer consumer) {
GameMode mode = subject.getGameMode();
if (mode != null) {
consumer.accept(DefaultContextKeys.GAMEMODE_KEY, GAMEMODE_NAMER.name(mode));
if (this.gamemode) {
GameMode mode = subject.getGameMode();
if (mode != null) {
consumer.accept(DefaultContextKeys.GAMEMODE_KEY, GAMEMODE_NAMER.name(mode));
}
}

World world = subject.getWorld();
if (world != null) {
Environment environment = world.getEnvironment();
if (environment != null) {
if (this.dimensionType && environment != null) {
consumer.accept(DefaultContextKeys.DIMENSION_TYPE_KEY, DIMENSION_TYPE_NAMER.name(environment));
}
this.plugin.getConfiguration().get(ConfigKeys.WORLD_REWRITES).rewriteAndSubmit(world.getName(), consumer);
if (this.world) {
this.plugin.getConfiguration().get(ConfigKeys.WORLD_REWRITES).rewriteAndSubmit(world.getName(), consumer);
}
}
}

@Override
public ContextSet estimatePotentialContexts() {
ImmutableContextSet.Builder builder = new ImmutableContextSetImpl.BuilderImpl();
for (GameMode mode : GameMode.values()) {
builder.add(DefaultContextKeys.GAMEMODE_KEY, GAMEMODE_NAMER.name(mode));
if (this.gamemode) {
for (GameMode mode : GameMode.values()) {
builder.add(DefaultContextKeys.GAMEMODE_KEY, GAMEMODE_NAMER.name(mode));
}
}
for (Environment env : Environment.values()) {
builder.add(DefaultContextKeys.DIMENSION_TYPE_KEY, DIMENSION_TYPE_NAMER.name(env));
if (this.dimensionType) {
for (Environment env : Environment.values()) {
builder.add(DefaultContextKeys.DIMENSION_TYPE_KEY, DIMENSION_TYPE_NAMER.name(env));
}
}
for (World world : this.plugin.getBootstrap().getServer().getWorlds()) {
String worldName = world.getName();
if (Context.isValidValue(worldName)) {
builder.add(DefaultContextKeys.WORLD_KEY, worldName);
if (this.world) {
for (World world : this.plugin.getBootstrap().getServer().getWorlds()) {
String worldName = world.getName();
if (Context.isValidValue(worldName)) {
builder.add(DefaultContextKeys.WORLD_KEY, worldName);
}
}
}
return builder.build();
}

@EventHandler(priority = EventPriority.LOWEST)
public void onWorldChange(PlayerChangedWorldEvent e) {
this.plugin.getContextManager().signalContextUpdate(e.getPlayer());
if (this.world || this.dimensionType) {
this.plugin.getContextManager().signalContextUpdate(e.getPlayer());
}
}

@EventHandler(priority = EventPriority.LOWEST)
public void onPlayerJoinWorld(PlayerJoinEvent e) {
this.plugin.getContextManager().signalContextUpdate(e.getPlayer());
if (this.world || this.dimensionType) {
this.plugin.getContextManager().signalContextUpdate(e.getPlayer());
}
}

@EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true)
public void onGameModeChange(PlayerGameModeChangeEvent e) {
this.plugin.getContextManager().signalContextUpdate(e.getPlayer());
if (this.gamemode) {
this.plugin.getContextManager().signalContextUpdate(e.getPlayer());
}
}
}
5 changes: 5 additions & 0 deletions bukkit/src/main/resources/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -435,6 +435,11 @@ post-traversal-inheritance-sort: false
# entries in A are also in B.
context-satisfy-mode: at-least-one-value-per-key

# LuckPerms has a number of built-in contexts. These can be disabled by adding the context key to
# the list below.
disabled-contexts:
# - "world"

# +----------------------------------------------------------------------------------------------+ #
# | Permission resolution settings | #
# +----------------------------------------------------------------------------------------------+ #
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
import me.lucko.luckperms.common.api.LuckPermsApiProvider;
import me.lucko.luckperms.common.calculator.CalculatorFactory;
import me.lucko.luckperms.common.command.CommandManager;
import me.lucko.luckperms.common.config.ConfigKeys;
import me.lucko.luckperms.common.config.generic.adapter.ConfigurationAdapter;
import me.lucko.luckperms.common.dependencies.Dependency;
import me.lucko.luckperms.common.event.AbstractEventBus;
Expand All @@ -50,6 +51,7 @@
import me.lucko.luckperms.common.tasks.ExpireTemporaryTask;

import net.luckperms.api.LuckPerms;
import net.luckperms.api.context.DefaultContextKeys;
import net.luckperms.api.query.QueryOptions;
import net.md_5.bungee.api.plugin.Plugin;

Expand Down Expand Up @@ -141,11 +143,14 @@ protected CalculatorFactory provideCalculatorFactory() {
protected void setupContextManager() {
this.contextManager = new BungeeContextManager(this);

BungeePlayerCalculator playerCalculator = new BungeePlayerCalculator(this);
this.bootstrap.getProxy().getPluginManager().registerListener(this.bootstrap.getLoader(), playerCalculator);
this.contextManager.registerCalculator(playerCalculator);
Set<String> disabledContexts = getConfiguration().get(ConfigKeys.DISABLED_CONTEXTS);
if (!disabledContexts.contains(DefaultContextKeys.WORLD_KEY)) {
BungeePlayerCalculator playerCalculator = new BungeePlayerCalculator(this);
this.bootstrap.getProxy().getPluginManager().registerListener(this.bootstrap.getLoader(), playerCalculator);
this.contextManager.registerCalculator(playerCalculator);
}

if (this.bootstrap.getProxy().getPluginManager().getPlugin("RedisBungee") != null) {
if (!disabledContexts.contains("proxy") && this.bootstrap.getProxy().getPluginManager().getPlugin("RedisBungee") != null) {
this.contextManager.registerCalculator(new RedisBungeeCalculator());
}
}
Expand Down
5 changes: 5 additions & 0 deletions bungee/src/main/resources/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -443,6 +443,11 @@ post-traversal-inheritance-sort: false
# entries in A are also in B.
context-satisfy-mode: at-least-one-value-per-key

# LuckPerms has a number of built-in contexts. These can be disabled by adding the context key to
# the list below.
disabled-contexts:
# - "world"

# +----------------------------------------------------------------------------------------------+ #
# | Permission resolution settings | #
# +----------------------------------------------------------------------------------------------+ #
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,16 @@ private ConfigKeys() {}
return ContextSatisfyMode.AT_LEAST_ONE_VALUE_PER_KEY;
});

/**
* A set of disabled contexts
*/
public static final ConfigKey<Set<String>> DISABLED_CONTEXTS = notReloadable(key(c -> {
return c.getStringList("disabled-contexts", ImmutableList.of())
.stream()
.map(String::toLowerCase)
.collect(ImmutableCollectors.toSet());
}));

/**
* # If the servers own UUID cache/lookup facility should be used when there is no record for a player in the LuckPerms cache.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@

import me.lucko.luckperms.common.api.LuckPermsApiProvider;
import me.lucko.luckperms.common.calculator.CalculatorFactory;
import me.lucko.luckperms.common.config.ConfigKeys;
import me.lucko.luckperms.common.config.generic.adapter.ConfigurationAdapter;
import me.lucko.luckperms.common.dependencies.Dependency;
import me.lucko.luckperms.common.event.AbstractEventBus;
Expand Down Expand Up @@ -141,7 +142,7 @@ protected CalculatorFactory provideCalculatorFactory() {
protected void setupContextManager() {
this.contextManager = new FabricContextManager(this);

FabricPlayerCalculator playerCalculator = new FabricPlayerCalculator(this);
FabricPlayerCalculator playerCalculator = new FabricPlayerCalculator(this, getConfiguration().get(ConfigKeys.DISABLED_CONTEXTS));
playerCalculator.registerListeners();
this.contextManager.registerCalculator(playerCalculator);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
import org.checkerframework.checker.nullness.qual.NonNull;

import java.util.Optional;
import java.util.Set;

public class FabricPlayerCalculator implements ContextCalculator<ServerPlayerEntity> {
private static final EnumNamer<GameMode> GAMEMODE_NAMER = new EnumNamer<>(
Expand All @@ -55,8 +56,15 @@ public class FabricPlayerCalculator implements ContextCalculator<ServerPlayerEnt

private final LPFabricPlugin plugin;

public FabricPlayerCalculator(LPFabricPlugin plugin) {
private final boolean gamemode;
private final boolean world;
//private final boolean dimensionType;

public FabricPlayerCalculator(LPFabricPlugin plugin, Set<String> disabled) {
this.plugin = plugin;
this.gamemode = !disabled.contains(DefaultContextKeys.GAMEMODE_KEY);
this.world = !disabled.contains(DefaultContextKeys.WORLD_KEY);
//this.dimensionType = !disabled.contains(DefaultContextKeys.DIMENSION_TYPE_KEY);
}

public void registerListeners() {
Expand All @@ -66,27 +74,31 @@ public void registerListeners() {
@Override
public void calculate(@NonNull ServerPlayerEntity target, @NonNull ContextConsumer consumer) {
GameMode mode = target.interactionManager.getGameMode();
if (mode != null && mode != GameMode.NOT_SET) {
if (this.gamemode && mode != null && mode != GameMode.NOT_SET) {
consumer.accept(DefaultContextKeys.GAMEMODE_KEY, GAMEMODE_NAMER.name(mode));
}

// TODO: figure out dimension type context too
ServerWorld world = target.getServerWorld();
this.plugin.getConfiguration().get(ConfigKeys.WORLD_REWRITES).rewriteAndSubmit(getContextKey(world.getRegistryKey().getValue()), consumer);
if (this.world) {
this.plugin.getConfiguration().get(ConfigKeys.WORLD_REWRITES).rewriteAndSubmit(getContextKey(world.getRegistryKey().getValue()), consumer);
}
}

@Override
public ContextSet estimatePotentialContexts() {
ImmutableContextSet.Builder builder = new ImmutableContextSetImpl.BuilderImpl();

for (GameMode mode : GameMode.values()) {
builder.add(DefaultContextKeys.GAMEMODE_KEY, GAMEMODE_NAMER.name(mode));
if (this.gamemode) {
for (GameMode mode : GameMode.values()) {
builder.add(DefaultContextKeys.GAMEMODE_KEY, GAMEMODE_NAMER.name(mode));
}
}

// TODO: dimension type

Optional<MinecraftServer> server = this.plugin.getBootstrap().getServer();
if (server.isPresent()) {
if (this.world && server.isPresent()) {
Iterable<ServerWorld> worlds = server.get().getWorlds();
for (ServerWorld world : worlds) {
String worldName = getContextKey(world.getRegistryKey().getValue());
Expand All @@ -107,7 +119,9 @@ private static String getContextKey(Identifier key) {
}

private void onWorldChange(ServerWorld origin, ServerWorld destination, ServerPlayerEntity player) {
this.plugin.getContextManager().invalidateCache(player);
if (this.world) {
this.plugin.getContextManager().invalidateCache(player);
}
}

}
6 changes: 6 additions & 0 deletions fabric/src/main/resources/luckperms.conf
Original file line number Diff line number Diff line change
Expand Up @@ -446,6 +446,12 @@ post-traversal-inheritance-sort = false
# entries in A are also in B.
context-satisfy-mode = "at-least-one-value-per-key"

# LuckPerms has a number of built-in contexts. These can be disabled by adding the context key to
# the list below.
disabled-contexts = [
# "world"
]

# +----------------------------------------------------------------------------------------------+ #
# | Permission resolution settings | #
# +----------------------------------------------------------------------------------------------+ #
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ protected CalculatorFactory provideCalculatorFactory() {
protected void setupContextManager() {
this.contextManager = new NukkitContextManager(this);

NukkitPlayerCalculator playerCalculator = new NukkitPlayerCalculator(this);
NukkitPlayerCalculator playerCalculator = new NukkitPlayerCalculator(this, getConfiguration().get(ConfigKeys.DISABLED_CONTEXTS));
this.bootstrap.getServer().getPluginManager().registerEvents(playerCalculator, this.bootstrap.getLoader());
this.contextManager.registerCalculator(playerCalculator);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,37 +47,59 @@
import cn.nukkit.event.player.PlayerGameModeChangeEvent;
import cn.nukkit.level.Level;

import java.util.Set;

public class NukkitPlayerCalculator implements ContextCalculator<Player>, Listener {
private static final int[] KNOWN_GAMEMODES = {Player.SURVIVAL, Player.CREATIVE, Player.ADVENTURE, Player.SPECTATOR};
private static final int[] KNOWN_DIMENSION_TYPES = {Level.DIMENSION_OVERWORLD, Level.DIMENSION_NETHER};

private final LPNukkitPlugin plugin;

public NukkitPlayerCalculator(LPNukkitPlugin plugin) {
private final boolean gamemode;
private final boolean world;
private final boolean dimensionType;

public NukkitPlayerCalculator(LPNukkitPlugin plugin, Set<String> disabled) {
this.plugin = plugin;
this.gamemode = !disabled.contains(DefaultContextKeys.GAMEMODE_KEY);
this.world = !disabled.contains(DefaultContextKeys.WORLD_KEY);
this.dimensionType = !disabled.contains(DefaultContextKeys.DIMENSION_TYPE_KEY);
}

@Override
public void calculate(@NonNull Player subject, @NonNull ContextConsumer consumer) {
if (this.gamemode) {
consumer.accept(DefaultContextKeys.GAMEMODE_KEY, getGamemodeName(subject.getGamemode()));
}

Level level = subject.getLevel();
consumer.accept(DefaultContextKeys.GAMEMODE_KEY, getGamemodeName(subject.getGamemode()));
consumer.accept(DefaultContextKeys.DIMENSION_TYPE_KEY, getDimensionName(level.getDimension()));
this.plugin.getConfiguration().get(ConfigKeys.WORLD_REWRITES).rewriteAndSubmit(level.getName(), consumer);
if (this.dimensionType) {
consumer.accept(DefaultContextKeys.DIMENSION_TYPE_KEY, getDimensionName(level.getDimension()));
}
if (this.world) {
this.plugin.getConfiguration().get(ConfigKeys.WORLD_REWRITES).rewriteAndSubmit(level.getName(), consumer);
}
}

@Override
public ContextSet estimatePotentialContexts() {
ImmutableContextSet.Builder builder = new ImmutableContextSetImpl.BuilderImpl();
for (int mode : KNOWN_GAMEMODES) {
builder.add(DefaultContextKeys.GAMEMODE_KEY, getGamemodeName(mode));
if (this.gamemode) {
for (int mode : KNOWN_GAMEMODES) {
builder.add(DefaultContextKeys.GAMEMODE_KEY, getGamemodeName(mode));
}
}
for (int dim : KNOWN_DIMENSION_TYPES) {
builder.add(DefaultContextKeys.DIMENSION_TYPE_KEY, getDimensionName(dim));
if (this.dimensionType) {
for (int dim : KNOWN_DIMENSION_TYPES) {
builder.add(DefaultContextKeys.DIMENSION_TYPE_KEY, getDimensionName(dim));
}
}
for (Level world : this.plugin.getBootstrap().getServer().getLevels().values()) {
String worldName = world.getName();
if (Context.isValidValue(worldName)) {
builder.add(DefaultContextKeys.WORLD_KEY, worldName);
if (this.world) {
for (Level world : this.plugin.getBootstrap().getServer().getLevels().values()) {
String worldName = world.getName();
if (Context.isValidValue(worldName)) {
builder.add(DefaultContextKeys.WORLD_KEY, worldName);
}
}
}
return builder.build();
Expand All @@ -104,14 +126,16 @@ private static String getDimensionName(int dim) {

@EventHandler(priority = EventPriority.LOWEST)
public void onWorldChange(EntityLevelChangeEvent e) {
if (e.getEntity() instanceof Player) {
if ((this.world || this.dimensionType) && e.getEntity() instanceof Player) {
Player player = (Player) e.getEntity();
this.plugin.getContextManager().signalContextUpdate(player);
}
}

@EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true)
public void onGameModeChange(PlayerGameModeChangeEvent e) {
this.plugin.getContextManager().signalContextUpdate(e.getPlayer());
if (this.gamemode) {
this.plugin.getContextManager().signalContextUpdate(e.getPlayer());
}
}
}
Loading

0 comments on commit e7f2a8d

Please sign in to comment.