refactor: Allow for nullable NodeGroup

This commit is contained in:
Ben Woo 2023-03-28 17:15:15 +08:00
parent 90a3388728
commit 5cc5e6828f
No known key found for this signature in database
GPG Key ID: FB2A3645536E12C8
4 changed files with 47 additions and 16 deletions

View File

@ -34,8 +34,9 @@ public class MVCoreConfig implements MVConfig {
MVCoreConfig(@NotNull MultiverseCore core, @NotNull PluginManager pluginManager) {
this.configPath = Path.of(core.getDataFolder().getPath(), CONFIG_FILENAME);
this.configNodes = new MVCoreConfigNodes(pluginManager);
this.configHandle = CommentedYamlConfigHandle.builder(configPath, configNodes.getNodes())
this.configHandle = CommentedYamlConfigHandle.builder(configPath)
.logger(Logging.getLogger())
.nodes(configNodes.getNodes())
.migrator(ConfigMigrator.builder(configNodes.VERSION)
.addVersionMigrator(VersionMigrator.builder(5.0)
.addAction(MoveMigratorAction.of("multiverse-configuration.enforceaccess", "world.enforce-access"))

View File

@ -14,11 +14,11 @@ import org.jetbrains.annotations.Nullable;
public class CommentedYamlConfigHandle extends FileConfigHandle<CommentedConfiguration> {
public static @NotNull Builder builder(@NotNull Path configPath, @NotNull NodeGroup nodes) {
return new Builder(configPath, nodes);
public static @NotNull Builder builder(@NotNull Path configPath) {
return new Builder(configPath);
}
protected CommentedYamlConfigHandle(@NotNull Path configPath, @Nullable Logger logger, @NotNull NodeGroup nodes, @Nullable ConfigMigrator migrator) {
protected CommentedYamlConfigHandle(@NotNull Path configPath, @Nullable Logger logger, @Nullable NodeGroup nodes, @Nullable ConfigMigrator migrator) {
super(configPath, logger, nodes, migrator);
}
@ -30,6 +30,10 @@ public class CommentedYamlConfigHandle extends FileConfigHandle<CommentedConfigu
@Override
protected void setUpNodes() {
if (nodes == null || nodes.isEmpty()) {
return;
}
CommentedConfiguration oldConfig = config;
this.config = new CommentedConfiguration(configPath, logger);
@ -56,8 +60,8 @@ public class CommentedYamlConfigHandle extends FileConfigHandle<CommentedConfigu
public static class Builder extends FileConfigHandle.Builder<CommentedConfiguration, Builder> {
protected Builder(@NotNull Path configPath, @NotNull NodeGroup nodes) {
super(configPath, nodes);
protected Builder(@NotNull Path configPath) {
super(configPath);
}
@Override

View File

@ -20,12 +20,12 @@ abstract class FileConfigHandle<C extends FileConfiguration> {
protected final @NotNull Path configPath;
protected final @NotNull File configFile;
protected final @Nullable Logger logger;
protected final @NotNull NodeGroup nodes;
protected final @Nullable NodeGroup nodes;
protected final @Nullable ConfigMigrator migrator;
protected C config;
protected FileConfigHandle(@NotNull Path configPath, @Nullable Logger logger, @NotNull NodeGroup nodes, @Nullable ConfigMigrator migrator) {
protected FileConfigHandle(@NotNull Path configPath, @Nullable Logger logger, @Nullable NodeGroup nodes, @Nullable ConfigMigrator migrator) {
this.configPath = configPath;
this.configFile = configPath.toFile();
this.logger = logger;
@ -158,12 +158,11 @@ abstract class FileConfigHandle<C extends FileConfiguration> {
protected @NotNull Path configPath;
protected @Nullable Logger logger;
protected @NotNull NodeGroup nodes;
protected @Nullable NodeGroup nodes;
protected @Nullable ConfigMigrator migrator;
protected Builder(@NotNull Path configPath, @NotNull NodeGroup nodes) {
protected Builder(@NotNull Path configPath) {
this.configPath = configPath;
this.nodes = nodes;
}
/**
@ -177,11 +176,22 @@ abstract class FileConfigHandle<C extends FileConfiguration> {
return self();
}
/**
* Sets the logger.
*
* @param plugin The plugin to get the logger from.
* @return The builder.
*/
public B logger(Plugin plugin) {
this.logger = plugin.getLogger();
return self();
}
public B nodes(@Nullable NodeGroup nodes) {
this.nodes = nodes;
return self();
}
/**
* Sets the migrator.
*

View File

@ -14,14 +14,17 @@ import org.jetbrains.annotations.Nullable;
public class YamlConfigHandle extends FileConfigHandle<YamlConfiguration> {
public static @NotNull Builder<? extends Builder> builder(@NotNull Path configPath, @NotNull NodeGroup nodes) {
return new Builder<>(configPath, nodes);
public static @NotNull Builder<? extends Builder> builder(@NotNull Path configPath) {
return new Builder<>(configPath);
}
protected YamlConfigHandle(@NotNull Path configPath, @Nullable Logger logger, @NotNull NodeGroup nodes, @Nullable ConfigMigrator migrator) {
protected YamlConfigHandle(@NotNull Path configPath, @Nullable Logger logger, @Nullable NodeGroup nodes, @Nullable ConfigMigrator migrator) {
super(configPath, logger, nodes, migrator);
}
/**
* {@inheritDoc}
*/
@Override
protected boolean loadConfigObject() {
config = new YamlConfiguration();
@ -33,8 +36,15 @@ public class YamlConfigHandle extends FileConfigHandle<YamlConfiguration> {
return true;
}
/**
* {@inheritDoc}
*/
@Override
protected void setUpNodes() {
if (nodes == null || nodes.isEmpty()) {
return;
}
YamlConfiguration oldConfig = config;
config = new YamlConfiguration();
nodes.forEach(node -> {
@ -44,6 +54,9 @@ public class YamlConfigHandle extends FileConfigHandle<YamlConfiguration> {
});
}
/**
* {@inheritDoc}
*/
@Override
public boolean save() {
try {
@ -56,10 +69,13 @@ public class YamlConfigHandle extends FileConfigHandle<YamlConfiguration> {
public static class Builder<B extends Builder<B>> extends FileConfigHandle.Builder<YamlConfiguration, B> {
protected Builder(@NotNull Path configPath, @NotNull NodeGroup nodes) {
super(configPath, nodes);
protected Builder(@NotNull Path configPath) {
super(configPath);
}
/**
* {@inheritDoc}
*/
@Override
public @NotNull YamlConfigHandle build() {
return new YamlConfigHandle(configPath, logger, nodes, migrator);