mirror of
https://github.com/LuckPerms/LuckPerms.git
synced 2024-11-30 22:53:27 +01:00
Implement accumulation of static contexts from a file
This commit is contained in:
parent
2749563f5d
commit
0ec19a8fee
@ -22,8 +22,6 @@
|
|||||||
|
|
||||||
package me.lucko.luckperms.api.context;
|
package me.lucko.luckperms.api.context;
|
||||||
|
|
||||||
import java.util.Map;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Calculates whether contexts are applicable to {@link T}
|
* Calculates whether contexts are applicable to {@link T}
|
||||||
*
|
*
|
||||||
@ -43,13 +41,4 @@ public interface ContextCalculator<T> {
|
|||||||
*/
|
*/
|
||||||
MutableContextSet giveApplicableContext(T subject, MutableContextSet accumulator);
|
MutableContextSet giveApplicableContext(T subject, MutableContextSet accumulator);
|
||||||
|
|
||||||
/**
|
|
||||||
* Checks to see if a context is applicable to a subject
|
|
||||||
*
|
|
||||||
* @param subject the subject to check against
|
|
||||||
* @param context the context to check for
|
|
||||||
* @return true if met, or false if not. If this calculator does not calculate the given context, return false.
|
|
||||||
*/
|
|
||||||
boolean isContextApplicable(T subject, Map.Entry<String, String> context);
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -47,7 +47,7 @@ import me.lucko.luckperms.common.config.ConfigKeys;
|
|||||||
import me.lucko.luckperms.common.config.LuckPermsConfiguration;
|
import me.lucko.luckperms.common.config.LuckPermsConfiguration;
|
||||||
import me.lucko.luckperms.common.constants.Permission;
|
import me.lucko.luckperms.common.constants.Permission;
|
||||||
import me.lucko.luckperms.common.contexts.ContextManager;
|
import me.lucko.luckperms.common.contexts.ContextManager;
|
||||||
import me.lucko.luckperms.common.contexts.ServerCalculator;
|
import me.lucko.luckperms.common.contexts.StaticCalculator;
|
||||||
import me.lucko.luckperms.common.core.UuidCache;
|
import me.lucko.luckperms.common.core.UuidCache;
|
||||||
import me.lucko.luckperms.common.core.model.User;
|
import me.lucko.luckperms.common.core.model.User;
|
||||||
import me.lucko.luckperms.common.dependencies.Dependency;
|
import me.lucko.luckperms.common.dependencies.Dependency;
|
||||||
@ -273,7 +273,7 @@ public class LPBukkitPlugin extends JavaPlugin implements LuckPermsPlugin {
|
|||||||
contextManager = new ContextManager<>();
|
contextManager = new ContextManager<>();
|
||||||
worldCalculator = new WorldCalculator(this);
|
worldCalculator = new WorldCalculator(this);
|
||||||
contextManager.registerCalculator(worldCalculator);
|
contextManager.registerCalculator(worldCalculator);
|
||||||
contextManager.registerCalculator(new ServerCalculator<>(getConfiguration()));
|
contextManager.registerCalculator(new StaticCalculator<>(getConfiguration()));
|
||||||
|
|
||||||
// Provide vault support
|
// Provide vault support
|
||||||
tryVaultHook(false);
|
tryVaultHook(false);
|
||||||
@ -560,6 +560,7 @@ public class LPBukkitPlugin extends JavaPlugin implements LuckPermsPlugin {
|
|||||||
MutableContextSet set = MutableContextSet.create();
|
MutableContextSet set = MutableContextSet.create();
|
||||||
set.add("server", getConfiguration().get(ConfigKeys.SERVER));
|
set.add("server", getConfiguration().get(ConfigKeys.SERVER));
|
||||||
set.add("world", s);
|
set.add("world", s);
|
||||||
|
set.addAll(configuration.getStaticContexts().getContextSet());
|
||||||
return set.makeImmutable();
|
return set.makeImmutable();
|
||||||
})
|
})
|
||||||
.collect(Collectors.toList())
|
.collect(Collectors.toList())
|
||||||
@ -574,6 +575,7 @@ public class LPBukkitPlugin extends JavaPlugin implements LuckPermsPlugin {
|
|||||||
MutableContextSet set = MutableContextSet.create();
|
MutableContextSet set = MutableContextSet.create();
|
||||||
set.add("server", getConfiguration().get(ConfigKeys.VAULT_SERVER));
|
set.add("server", getConfiguration().get(ConfigKeys.VAULT_SERVER));
|
||||||
set.add("world", s);
|
set.add("world", s);
|
||||||
|
set.addAll(configuration.getStaticContexts().getContextSet());
|
||||||
return set.makeImmutable();
|
return set.makeImmutable();
|
||||||
})
|
})
|
||||||
.collect(Collectors.toList())
|
.collect(Collectors.toList())
|
||||||
|
@ -33,8 +33,6 @@ import me.lucko.luckperms.common.plugin.LuckPermsPlugin;
|
|||||||
|
|
||||||
import org.bukkit.entity.Player;
|
import org.bukkit.entity.Player;
|
||||||
|
|
||||||
import java.util.Map;
|
|
||||||
|
|
||||||
@RequiredArgsConstructor
|
@RequiredArgsConstructor
|
||||||
public class WorldCalculator implements ContextCalculator<Player> {
|
public class WorldCalculator implements ContextCalculator<Player> {
|
||||||
private static final String WORLD_KEY = "world";
|
private static final String WORLD_KEY = "world";
|
||||||
@ -43,7 +41,8 @@ public class WorldCalculator implements ContextCalculator<Player> {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public MutableContextSet giveApplicableContext(Player subject, MutableContextSet accumulator) {
|
public MutableContextSet giveApplicableContext(Player subject, MutableContextSet accumulator) {
|
||||||
String world = getWorld(subject);
|
String world = subject.getWorld().getName();
|
||||||
|
world = plugin.getConfiguration().get(ConfigKeys.WORLD_REWRITES).getOrDefault(world, world);
|
||||||
|
|
||||||
if (world != null) {
|
if (world != null) {
|
||||||
accumulator.add(Maps.immutableEntry(WORLD_KEY, world));
|
accumulator.add(Maps.immutableEntry(WORLD_KEY, world));
|
||||||
@ -51,19 +50,4 @@ public class WorldCalculator implements ContextCalculator<Player> {
|
|||||||
|
|
||||||
return accumulator;
|
return accumulator;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
|
||||||
public boolean isContextApplicable(Player subject, Map.Entry<String, String> context) {
|
|
||||||
if (!context.getKey().equals(WORLD_KEY)) {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
String world = getWorld(subject);
|
|
||||||
return world != null && world.equals(context.getValue());
|
|
||||||
}
|
|
||||||
|
|
||||||
private String getWorld(Player player) {
|
|
||||||
String world = player.getWorld().getName();
|
|
||||||
return plugin.getConfiguration().get(ConfigKeys.WORLD_REWRITES).getOrDefault(world, world);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
@ -29,8 +29,6 @@ import me.lucko.luckperms.api.context.MutableContextSet;
|
|||||||
|
|
||||||
import net.md_5.bungee.api.connection.ProxiedPlayer;
|
import net.md_5.bungee.api.connection.ProxiedPlayer;
|
||||||
|
|
||||||
import java.util.Map;
|
|
||||||
|
|
||||||
public class BackendServerCalculator implements ContextCalculator<ProxiedPlayer> {
|
public class BackendServerCalculator implements ContextCalculator<ProxiedPlayer> {
|
||||||
private static final String WORLD_KEY = "world";
|
private static final String WORLD_KEY = "world";
|
||||||
|
|
||||||
@ -48,14 +46,4 @@ public class BackendServerCalculator implements ContextCalculator<ProxiedPlayer>
|
|||||||
|
|
||||||
return accumulator;
|
return accumulator;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
|
||||||
public boolean isContextApplicable(ProxiedPlayer subject, Map.Entry<String, String> context) {
|
|
||||||
if (!context.getKey().equals(WORLD_KEY)) {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
String server = getServer(subject);
|
|
||||||
return server != null && server.equals(context.getValue());
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
@ -38,7 +38,7 @@ import me.lucko.luckperms.common.commands.sender.Sender;
|
|||||||
import me.lucko.luckperms.common.config.ConfigKeys;
|
import me.lucko.luckperms.common.config.ConfigKeys;
|
||||||
import me.lucko.luckperms.common.config.LuckPermsConfiguration;
|
import me.lucko.luckperms.common.config.LuckPermsConfiguration;
|
||||||
import me.lucko.luckperms.common.contexts.ContextManager;
|
import me.lucko.luckperms.common.contexts.ContextManager;
|
||||||
import me.lucko.luckperms.common.contexts.ServerCalculator;
|
import me.lucko.luckperms.common.contexts.StaticCalculator;
|
||||||
import me.lucko.luckperms.common.core.UuidCache;
|
import me.lucko.luckperms.common.core.UuidCache;
|
||||||
import me.lucko.luckperms.common.core.model.User;
|
import me.lucko.luckperms.common.core.model.User;
|
||||||
import me.lucko.luckperms.common.dependencies.Dependency;
|
import me.lucko.luckperms.common.dependencies.Dependency;
|
||||||
@ -217,7 +217,7 @@ public class LPBungeePlugin extends Plugin implements LuckPermsPlugin {
|
|||||||
contextManager = new ContextManager<>();
|
contextManager = new ContextManager<>();
|
||||||
BackendServerCalculator serverCalculator = new BackendServerCalculator();
|
BackendServerCalculator serverCalculator = new BackendServerCalculator();
|
||||||
contextManager.registerCalculator(serverCalculator);
|
contextManager.registerCalculator(serverCalculator);
|
||||||
contextManager.registerCalculator(new ServerCalculator<>(configuration));
|
contextManager.registerCalculator(new StaticCalculator<>(configuration));
|
||||||
|
|
||||||
// register with the LP API
|
// register with the LP API
|
||||||
getLog().info("Registering API...");
|
getLog().info("Registering API...");
|
||||||
|
@ -41,6 +41,9 @@ public abstract class AbstractConfiguration implements LuckPermsConfiguration {
|
|||||||
@Getter
|
@Getter
|
||||||
private final LPConfigurationDelegate delegate = new LPConfigurationDelegate(this);
|
private final LPConfigurationDelegate delegate = new LPConfigurationDelegate(this);
|
||||||
|
|
||||||
|
@Getter
|
||||||
|
private final StaticContextsFile staticContexts = new StaticContextsFile(this);
|
||||||
|
|
||||||
@SuppressWarnings("unchecked")
|
@SuppressWarnings("unchecked")
|
||||||
@Override
|
@Override
|
||||||
public <T> T get(ConfigKey<T> key) {
|
public <T> T get(ConfigKey<T> key) {
|
||||||
@ -50,6 +53,7 @@ public abstract class AbstractConfiguration implements LuckPermsConfiguration {
|
|||||||
@Override
|
@Override
|
||||||
public void loadAll() {
|
public void loadAll() {
|
||||||
ConfigKeys.getAllKeys().forEach(cache::get);
|
ConfigKeys.getAllKeys().forEach(cache::get);
|
||||||
|
staticContexts.reload();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@ -60,7 +64,7 @@ public abstract class AbstractConfiguration implements LuckPermsConfiguration {
|
|||||||
cache.invalidateAll(toInvalidate);
|
cache.invalidateAll(toInvalidate);
|
||||||
|
|
||||||
loadAll();
|
loadAll();
|
||||||
|
staticContexts.reload();
|
||||||
getPlugin().getApiProvider().getEventFactory().handleConfigReload();
|
getPlugin().getApiProvider().getEventFactory().handleConfigReload();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -34,6 +34,8 @@ public interface LuckPermsConfiguration {
|
|||||||
|
|
||||||
LuckPermsPlugin getPlugin();
|
LuckPermsPlugin getPlugin();
|
||||||
|
|
||||||
|
StaticContextsFile getStaticContexts();
|
||||||
|
|
||||||
void init();
|
void init();
|
||||||
|
|
||||||
void reload();
|
void reload();
|
||||||
|
@ -0,0 +1,72 @@
|
|||||||
|
package me.lucko.luckperms.common.config;
|
||||||
|
|
||||||
|
import lombok.Getter;
|
||||||
|
import lombok.RequiredArgsConstructor;
|
||||||
|
|
||||||
|
import com.google.common.collect.ImmutableSetMultimap;
|
||||||
|
import com.google.gson.Gson;
|
||||||
|
import com.google.gson.GsonBuilder;
|
||||||
|
import com.google.gson.JsonArray;
|
||||||
|
import com.google.gson.JsonElement;
|
||||||
|
import com.google.gson.JsonObject;
|
||||||
|
|
||||||
|
import me.lucko.luckperms.api.context.ImmutableContextSet;
|
||||||
|
|
||||||
|
import java.io.BufferedReader;
|
||||||
|
import java.io.BufferedWriter;
|
||||||
|
import java.io.File;
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.nio.charset.StandardCharsets;
|
||||||
|
import java.nio.file.Files;
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
|
@RequiredArgsConstructor
|
||||||
|
public class StaticContextsFile {
|
||||||
|
private final LuckPermsConfiguration configuration;
|
||||||
|
|
||||||
|
@Getter
|
||||||
|
private ImmutableContextSet contextSet = ImmutableContextSet.empty();
|
||||||
|
|
||||||
|
public void reload() {
|
||||||
|
File file = new File(configuration.getPlugin().getConfigDirectory(), "static-contexts.json");
|
||||||
|
if (!file.exists()) {
|
||||||
|
try (BufferedWriter writer = Files.newBufferedWriter(file.toPath(), StandardCharsets.UTF_8)) {
|
||||||
|
JsonObject template = new JsonObject();
|
||||||
|
template.add("context", new JsonObject());
|
||||||
|
new GsonBuilder().setPrettyPrinting().create().toJson(template, writer);
|
||||||
|
} catch (IOException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
contextSet = ImmutableContextSet.empty();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
try (BufferedReader reader = Files.newBufferedReader(file.toPath(), StandardCharsets.UTF_8)) {
|
||||||
|
JsonObject data = new Gson().fromJson(reader, JsonObject.class);
|
||||||
|
|
||||||
|
if (!data.has("context") || !data.get("context").isJsonObject()) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
JsonObject contexts = data.get("context").getAsJsonObject();
|
||||||
|
ImmutableSetMultimap.Builder<String, String> map = ImmutableSetMultimap.builder();
|
||||||
|
|
||||||
|
for (Map.Entry<String, JsonElement> e : contexts.entrySet()) {
|
||||||
|
JsonElement val = e.getValue();
|
||||||
|
if (val.isJsonArray()) {
|
||||||
|
JsonArray vals = val.getAsJsonArray();
|
||||||
|
for (JsonElement element : vals) {
|
||||||
|
map.put(e.getKey(), element.getAsString());
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
map.put(e.getKey(), val.getAsString());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
contextSet = ImmutableContextSet.fromMultimap(map.build());
|
||||||
|
} catch (IOException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -29,10 +29,8 @@ import me.lucko.luckperms.api.context.MutableContextSet;
|
|||||||
import me.lucko.luckperms.common.config.ConfigKeys;
|
import me.lucko.luckperms.common.config.ConfigKeys;
|
||||||
import me.lucko.luckperms.common.config.LuckPermsConfiguration;
|
import me.lucko.luckperms.common.config.LuckPermsConfiguration;
|
||||||
|
|
||||||
import java.util.Map;
|
|
||||||
|
|
||||||
@AllArgsConstructor
|
@AllArgsConstructor
|
||||||
public class ServerCalculator<T> implements ContextCalculator<T> {
|
public class StaticCalculator<T> implements ContextCalculator<T> {
|
||||||
private final LuckPermsConfiguration config;
|
private final LuckPermsConfiguration config;
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@ -41,11 +39,10 @@ public class ServerCalculator<T> implements ContextCalculator<T> {
|
|||||||
if (!server.equals("global")) {
|
if (!server.equals("global")) {
|
||||||
accumulator.add("server", server);
|
accumulator.add("server", server);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
accumulator.addAll(config.getStaticContexts().getContextSet());
|
||||||
|
|
||||||
return accumulator;
|
return accumulator;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
|
||||||
public boolean isContextApplicable(T subject, Map.Entry<String, String> context) {
|
|
||||||
return context.getKey().equals("server") && config.get(ConfigKeys.SERVER).equalsIgnoreCase(context.getValue());
|
|
||||||
}
|
|
||||||
}
|
}
|
@ -39,7 +39,7 @@ import me.lucko.luckperms.common.config.ConfigKeys;
|
|||||||
import me.lucko.luckperms.common.config.LuckPermsConfiguration;
|
import me.lucko.luckperms.common.config.LuckPermsConfiguration;
|
||||||
import me.lucko.luckperms.common.constants.Permission;
|
import me.lucko.luckperms.common.constants.Permission;
|
||||||
import me.lucko.luckperms.common.contexts.ContextManager;
|
import me.lucko.luckperms.common.contexts.ContextManager;
|
||||||
import me.lucko.luckperms.common.contexts.ServerCalculator;
|
import me.lucko.luckperms.common.contexts.StaticCalculator;
|
||||||
import me.lucko.luckperms.common.core.UuidCache;
|
import me.lucko.luckperms.common.core.UuidCache;
|
||||||
import me.lucko.luckperms.common.core.model.User;
|
import me.lucko.luckperms.common.core.model.User;
|
||||||
import me.lucko.luckperms.common.dependencies.DependencyManager;
|
import me.lucko.luckperms.common.dependencies.DependencyManager;
|
||||||
@ -265,7 +265,7 @@ public class LPSpongePlugin implements LuckPermsPlugin {
|
|||||||
cachedStateManager = new CachedStateManager(this);
|
cachedStateManager = new CachedStateManager(this);
|
||||||
|
|
||||||
contextManager = new ContextManager<>();
|
contextManager = new ContextManager<>();
|
||||||
contextManager.registerCalculator(new ServerCalculator<>(configuration));
|
contextManager.registerCalculator(new StaticCalculator<>(configuration));
|
||||||
contextManager.registerCalculator(new WorldCalculator());
|
contextManager.registerCalculator(new WorldCalculator());
|
||||||
|
|
||||||
// register the PermissionService with Sponge
|
// register the PermissionService with Sponge
|
||||||
|
@ -32,7 +32,6 @@ import org.spongepowered.api.service.context.Context;
|
|||||||
import org.spongepowered.api.service.permission.Subject;
|
import org.spongepowered.api.service.permission.Subject;
|
||||||
|
|
||||||
import java.util.HashSet;
|
import java.util.HashSet;
|
||||||
import java.util.Map;
|
|
||||||
import java.util.Set;
|
import java.util.Set;
|
||||||
|
|
||||||
@AllArgsConstructor
|
@AllArgsConstructor
|
||||||
@ -47,10 +46,4 @@ public class SpongeCalculatorLink implements ContextCalculator<Subject> {
|
|||||||
accumulator.addAll(Util.convertContexts(contexts));
|
accumulator.addAll(Util.convertContexts(contexts));
|
||||||
return accumulator;
|
return accumulator;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
|
||||||
public boolean isContextApplicable(Subject subject, Map.Entry<String, String> context) {
|
|
||||||
Context c = new Context(context.getKey(), context.getValue());
|
|
||||||
return calculator.matches(c, subject);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
@ -32,8 +32,6 @@ import org.spongepowered.api.entity.living.player.Player;
|
|||||||
import org.spongepowered.api.service.context.Context;
|
import org.spongepowered.api.service.context.Context;
|
||||||
import org.spongepowered.api.service.permission.Subject;
|
import org.spongepowered.api.service.permission.Subject;
|
||||||
|
|
||||||
import java.util.Map;
|
|
||||||
|
|
||||||
@RequiredArgsConstructor
|
@RequiredArgsConstructor
|
||||||
public class WorldCalculator implements ContextCalculator<Subject> {
|
public class WorldCalculator implements ContextCalculator<Subject> {
|
||||||
|
|
||||||
@ -49,15 +47,4 @@ public class WorldCalculator implements ContextCalculator<Subject> {
|
|||||||
return accumulator;
|
return accumulator;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
|
||||||
public boolean isContextApplicable(Subject subject, Map.Entry<String, String> context) {
|
|
||||||
CommandSource source = subject.getCommandSource().orElse(null);
|
|
||||||
if (source == null || !(source instanceof Player)) {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
Player p = ((Player) source);
|
|
||||||
return context.getKey().equals(Context.WORLD_KEY) && p.getWorld().getName().equals(context.getValue());
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user