Refactor config handling - towards #100

This commit is contained in:
Luck
2017-01-21 15:36:13 +00:00
Unverified
parent 7430013cc3
commit b7cf0e6bc7
42 changed files with 814 additions and 494 deletions
@@ -33,6 +33,7 @@ import me.lucko.luckperms.common.calculators.PermissionProcessor;
import me.lucko.luckperms.common.calculators.processors.MapProcessor;
import me.lucko.luckperms.common.calculators.processors.RegexProcessor;
import me.lucko.luckperms.common.calculators.processors.WildcardProcessor;
import me.lucko.luckperms.common.config.ConfigKeys;
import me.lucko.luckperms.common.core.model.User;
@AllArgsConstructor
@@ -43,10 +44,10 @@ public class BungeeCalculatorFactory extends AbstractCalculatorFactory {
public PermissionCalculator build(Contexts contexts, User user) {
ImmutableList.Builder<PermissionProcessor> processors = ImmutableList.builder();
processors.add(new MapProcessor());
if (plugin.getConfiguration().isApplyingWildcards()) {
if (plugin.getConfiguration().get(ConfigKeys.APPLYING_WILDCARDS)) {
processors.add(new WildcardProcessor());
}
if (plugin.getConfiguration().isApplyingRegex()) {
if (plugin.getConfiguration().get(ConfigKeys.APPLYING_REGEX)) {
processors.add(new RegexProcessor());
}
@@ -40,7 +40,7 @@ class BungeeCommand extends Command implements TabExecutor {
private final CommandManager manager;
BungeeCommand(LPBungeePlugin plugin, CommandManager manager) {
super("luckpermsbungee", null, "bperms", "lpb", "bpermissions", "bp", "bperm");
super("luckpermsbungee", null, "bperms", "lpb", "bpermissions", "bperm");
this.plugin = plugin;
this.manager = manager;
}
@@ -49,7 +49,7 @@ class BungeeCommand extends Command implements TabExecutor {
public void execute(CommandSender sender, String[] args) {
manager.onCommand(
plugin.getSenderFactory().wrap(sender),
"bperms",
"lpb",
Util.stripQuotes(Splitter.on(Patterns.COMMAND_SEPARATOR).omitEmptyStrings().splitToList(Joiner.on(' ').join(args)))
);
}
@@ -22,6 +22,8 @@
package me.lucko.luckperms.bungee;
import lombok.RequiredArgsConstructor;
import me.lucko.luckperms.common.config.AbstractConfiguration;
import net.md_5.bungee.config.Configuration;
@@ -38,20 +40,18 @@ import java.util.Map;
import java.util.Optional;
import java.util.stream.Collectors;
class BungeeConfig extends AbstractConfiguration<LPBungeePlugin> {
@RequiredArgsConstructor
public class BungeeConfig extends AbstractConfiguration {
private final LPBungeePlugin plugin;
private Configuration configuration;
BungeeConfig(LPBungeePlugin plugin) {
super(plugin, "bungee", false, "flatfile");
}
@SuppressWarnings("ResultOfMethodCallIgnored")
private File makeFile(String file) throws IOException {
File cfg = new File(getPlugin().getDataFolder(), file);
File cfg = new File(plugin.getDataFolder(), file);
if (!cfg.exists()) {
getPlugin().getDataFolder().mkdir();
try (InputStream is = getPlugin().getResourceAsStream(file)) {
plugin.getDataFolder().mkdir();
try (InputStream is = plugin.getResourceAsStream(file)) {
Files.copy(is, cfg.toPath());
}
}
@@ -60,7 +60,7 @@ class BungeeConfig extends AbstractConfiguration<LPBungeePlugin> {
}
@Override
protected void init() {
public void init() {
try {
configuration = ConfigurationProvider.getProvider(YamlConfiguration.class).load(makeFile("config.yml"));
} catch (IOException e) {
@@ -69,27 +69,27 @@ class BungeeConfig extends AbstractConfiguration<LPBungeePlugin> {
}
@Override
protected String getString(String path, String def) {
public String getString(String path, String def) {
return configuration.getString(path, def);
}
@Override
protected int getInt(String path, int def) {
public int getInt(String path, int def) {
return configuration.getInt(path, def);
}
@Override
protected boolean getBoolean(String path, boolean def) {
public boolean getBoolean(String path, boolean def) {
return configuration.getBoolean(path, def);
}
@Override
protected List<String> getList(String path, List<String> def) {
public List<String> getList(String path, List<String> def) {
return Optional.ofNullable(configuration.getStringList(path)).orElse(def);
}
@Override
protected List<String> getObjectList(String path, List<String> def) {
public List<String> getObjectList(String path, List<String> def) {
Configuration section = configuration.getSection(path);
if (section == null) {
return def;
@@ -99,7 +99,7 @@ class BungeeConfig extends AbstractConfiguration<LPBungeePlugin> {
}
@Override
protected Map<String, String> getMap(String path, Map<String, String> def) {
public Map<String, String> getMap(String path, Map<String, String> def) {
Map<String, String> map = new HashMap<>();
Configuration section = configuration.getSection(path);
if (section == null) {
@@ -24,6 +24,7 @@ package me.lucko.luckperms.bungee;
import me.lucko.luckperms.api.Contexts;
import me.lucko.luckperms.api.event.events.UserFirstLoginEvent;
import me.lucko.luckperms.common.config.ConfigKeys;
import me.lucko.luckperms.common.constants.Message;
import me.lucko.luckperms.common.core.UuidCache;
import me.lucko.luckperms.common.core.model.User;
@@ -75,11 +76,11 @@ public class BungeeListener extends AbstractListener implements Listener {
Contexts contexts = new Contexts(
plugin.getContextManager().getApplicableContext(player),
plugin.getConfiguration().isIncludingGlobalPerms(),
plugin.getConfiguration().isIncludingGlobalWorldPerms(),
plugin.getConfiguration().get(ConfigKeys.INCLUDING_GLOBAL_PERMS),
plugin.getConfiguration().get(ConfigKeys.INCLUDING_GLOBAL_WORLD_PERMS),
true,
plugin.getConfiguration().isApplyingGlobalGroups(),
plugin.getConfiguration().isApplyingGlobalWorldGroups(),
plugin.getConfiguration().get(ConfigKeys.APPLYING_GLOBAL_GROUPS),
plugin.getConfiguration().get(ConfigKeys.APPLYING_GLOBAL_WORLD_GROUPS),
false
);
@@ -97,7 +98,7 @@ public class BungeeListener extends AbstractListener implements Listener {
final UuidCache cache = plugin.getUuidCache();
final PendingConnection c = e.getConnection();
if (!cache.isOnlineMode()) {
if (!plugin.getConfiguration().get(ConfigKeys.ONLINE_MODE)) {
UUID uuid = plugin.getStorage().getUUID(c.getName()).join();
if (uuid != null) {
cache.addToCache(c.getUniqueId(), uuid);
@@ -126,7 +127,7 @@ public class BungeeListener extends AbstractListener implements Listener {
} else {
// Setup defaults for the user
boolean save = false;
for (Rule rule : plugin.getConfiguration().getDefaultAssignments()) {
for (Rule rule : plugin.getConfiguration().get(ConfigKeys.DEFAULT_ASSIGNMENTS)) {
if (rule.apply(user)) {
save = true;
}
@@ -36,6 +36,7 @@ import me.lucko.luckperms.common.caching.handlers.CachedStateManager;
import me.lucko.luckperms.common.calculators.CalculatorFactory;
import me.lucko.luckperms.common.commands.CommandManager;
import me.lucko.luckperms.common.commands.sender.Sender;
import me.lucko.luckperms.common.config.ConfigKeys;
import me.lucko.luckperms.common.config.LPConfiguration;
import me.lucko.luckperms.common.contexts.ContextManager;
import me.lucko.luckperms.common.contexts.ServerCalculator;
@@ -116,6 +117,8 @@ public class LPBungeePlugin extends Plugin implements LuckPermsPlugin {
getLog().info("Loading configuration...");
configuration = new BungeeConfig(this);
configuration.init();
configuration.loadAll();
Set<StorageType> storageTypes = StorageFactory.getRequiredTypes(this, StorageType.H2);
DependencyManager.loadDependencies(this, storageTypes);
@@ -127,11 +130,11 @@ public class LPBungeePlugin extends Plugin implements LuckPermsPlugin {
storage = StorageFactory.getInstance(this, StorageType.H2);
// initialise redis
if (getConfiguration().isRedisEnabled()) {
if (getConfiguration().get(ConfigKeys.REDIS_ENABLED)) {
getLog().info("Loading redis...");
redisMessaging = new RedisMessaging(this);
try {
redisMessaging.init(getConfiguration().getRedisAddress(), getConfiguration().getRedisPassword());
redisMessaging.init(getConfiguration().get(ConfigKeys.REDIS_ADDRESS), getConfiguration().get(ConfigKeys.REDIS_PASSWORD));
getLog().info("Loaded redis successfully...");
} catch (Exception e) {
getLog().info("Couldn't load redis...");
@@ -170,7 +173,7 @@ public class LPBungeePlugin extends Plugin implements LuckPermsPlugin {
// load internal managers
getLog().info("Loading internal permission managers...");
uuidCache = new UuidCache(getConfiguration().isOnlineMode());
uuidCache = new UuidCache(this);
userManager = new GenericUserManager(this);
groupManager = new GenericGroupManager(this);
trackManager = new GenericTrackManager();
@@ -182,7 +185,7 @@ public class LPBungeePlugin extends Plugin implements LuckPermsPlugin {
BackendServerCalculator serverCalculator = new BackendServerCalculator();
getProxy().getPluginManager().registerListener(this, serverCalculator);
contextManager.registerCalculator(serverCalculator);
contextManager.registerCalculator(new ServerCalculator<>(getConfiguration().getServer()));
contextManager.registerCalculator(new ServerCalculator<>(configuration));
// register with the LP API
getLog().info("Registering API...");
@@ -190,7 +193,7 @@ public class LPBungeePlugin extends Plugin implements LuckPermsPlugin {
ApiHandler.registerProvider(apiProvider);
// schedule update tasks
int mins = getConfiguration().getSyncTime();
int mins = getConfiguration().get(ConfigKeys.SYNC_TIME);
if (mins > 0) {
getProxy().getScheduler().schedule(this, new UpdateTask(this), mins, mins, TimeUnit.MINUTES);
}
@@ -267,11 +270,11 @@ public class LPBungeePlugin extends Plugin implements LuckPermsPlugin {
}
return new Contexts(
getContextManager().getApplicableContext(player),
getConfiguration().isIncludingGlobalPerms(),
getConfiguration().isIncludingGlobalWorldPerms(),
getConfiguration().get(ConfigKeys.INCLUDING_GLOBAL_PERMS),
getConfiguration().get(ConfigKeys.INCLUDING_GLOBAL_WORLD_PERMS),
true,
getConfiguration().isApplyingGlobalGroups(),
getConfiguration().isApplyingGlobalWorldGroups(),
getConfiguration().get(ConfigKeys.APPLYING_GLOBAL_GROUPS),
getConfiguration().get(ConfigKeys.APPLYING_GLOBAL_WORLD_GROUPS),
false
);
}
@@ -312,12 +315,12 @@ public class LPBungeePlugin extends Plugin implements LuckPermsPlugin {
public Set<Contexts> getPreProcessContexts(boolean op) {
Set<ContextSet> c = new HashSet<>();
c.add(ContextSet.empty());
c.add(ContextSet.singleton("server", getConfiguration().getServer()));
c.add(ContextSet.singleton("server", getConfiguration().get(ConfigKeys.SERVER)));
c.addAll(getProxy().getServers().values().stream()
.map(ServerInfo::getName)
.map(s -> {
MutableContextSet set = MutableContextSet.create();
set.add("server", getConfiguration().getServer());
set.add("server", getConfiguration().get(ConfigKeys.SERVER));
set.add("world", s);
return set.makeImmutable();
})
@@ -327,11 +330,11 @@ public class LPBungeePlugin extends Plugin implements LuckPermsPlugin {
return c.stream()
.map(set -> new Contexts(
set,
getConfiguration().isIncludingGlobalPerms(),
getConfiguration().isIncludingGlobalWorldPerms(),
getConfiguration().get(ConfigKeys.INCLUDING_GLOBAL_PERMS),
getConfiguration().get(ConfigKeys.INCLUDING_GLOBAL_WORLD_PERMS),
true,
getConfiguration().isApplyingGlobalGroups(),
getConfiguration().isApplyingGlobalWorldGroups(),
getConfiguration().get(ConfigKeys.APPLYING_GLOBAL_GROUPS),
getConfiguration().get(ConfigKeys.APPLYING_GLOBAL_WORLD_GROUPS),
false
))
.collect(Collectors.toSet());