1
0
mirror of https://github.com/lucko/LuckPerms.git synced 2025-09-08 21:30:55 +02:00

Add config option to disable built-in contexts

This commit is contained in:
Luck
2021-03-01 11:18:21 +00:00
parent bf0ac1a867
commit e7f2a8d713
17 changed files with 197 additions and 59 deletions

View File

@@ -190,7 +190,7 @@ public class LPBukkitPlugin extends AbstractLuckPermsPlugin {
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);
}

View File

@@ -51,6 +51,8 @@ import org.bukkit.event.player.PlayerGameModeChangeEvent;
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,
@@ -69,41 +71,58 @@ 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();
@@ -111,16 +130,22 @@ public class BukkitPlayerCalculator implements ContextCalculator<Player>, Listen
@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());
}
}
}

View File

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