Implement accumulation of static contexts from a file
This commit is contained in:
@@ -41,6 +41,9 @@ public abstract class AbstractConfiguration implements LuckPermsConfiguration {
|
||||
@Getter
|
||||
private final LPConfigurationDelegate delegate = new LPConfigurationDelegate(this);
|
||||
|
||||
@Getter
|
||||
private final StaticContextsFile staticContexts = new StaticContextsFile(this);
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
@Override
|
||||
public <T> T get(ConfigKey<T> key) {
|
||||
@@ -50,6 +53,7 @@ public abstract class AbstractConfiguration implements LuckPermsConfiguration {
|
||||
@Override
|
||||
public void loadAll() {
|
||||
ConfigKeys.getAllKeys().forEach(cache::get);
|
||||
staticContexts.reload();
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -60,7 +64,7 @@ public abstract class AbstractConfiguration implements LuckPermsConfiguration {
|
||||
cache.invalidateAll(toInvalidate);
|
||||
|
||||
loadAll();
|
||||
|
||||
staticContexts.reload();
|
||||
getPlugin().getApiProvider().getEventFactory().handleConfigReload();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -34,6 +34,8 @@ public interface LuckPermsConfiguration {
|
||||
|
||||
LuckPermsPlugin getPlugin();
|
||||
|
||||
StaticContextsFile getStaticContexts();
|
||||
|
||||
void init();
|
||||
|
||||
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();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
+4
-7
@@ -29,10 +29,8 @@ import me.lucko.luckperms.api.context.MutableContextSet;
|
||||
import me.lucko.luckperms.common.config.ConfigKeys;
|
||||
import me.lucko.luckperms.common.config.LuckPermsConfiguration;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
@AllArgsConstructor
|
||||
public class ServerCalculator<T> implements ContextCalculator<T> {
|
||||
public class StaticCalculator<T> implements ContextCalculator<T> {
|
||||
private final LuckPermsConfiguration config;
|
||||
|
||||
@Override
|
||||
@@ -41,11 +39,10 @@ public class ServerCalculator<T> implements ContextCalculator<T> {
|
||||
if (!server.equals("global")) {
|
||||
accumulator.add("server", server);
|
||||
}
|
||||
|
||||
accumulator.addAll(config.getStaticContexts().getContextSet());
|
||||
|
||||
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());
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user