refactor: Version migrator code

This commit is contained in:
Ben Woo 2023-03-16 11:18:31 +08:00
parent c76652f0a2
commit 9ce648d06a
No known key found for this signature in database
GPG Key ID: FB2A3645536E12C8
3 changed files with 54 additions and 11 deletions

View File

@ -2,21 +2,14 @@ package com.onarandombox.MultiverseCore.utils.settings;
import java.io.File;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.nio.file.Path;
import java.util.Arrays;
import java.util.List;
import java.util.logging.Logger;
import com.dumptruckman.minecraft.util.Logging;
import com.google.common.base.Strings;
import io.github.townyadvanced.commentedconfiguration.CommentedConfiguration;
import io.github.townyadvanced.commentedconfiguration.setting.CommentedNode;
import io.github.townyadvanced.commentedconfiguration.setting.TypedValueNode;
import io.github.townyadvanced.commentedconfiguration.setting.ValueNode;
import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.configuration.file.FileConfigurationOptions;
import org.bukkit.configuration.file.YamlConfigurationOptions;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

View File

@ -0,0 +1,46 @@
package com.onarandombox.MultiverseCore.utils.settings.migration;
import java.util.ArrayList;
import java.util.List;
import com.onarandombox.MultiverseCore.utils.settings.MVSettings;
import io.github.townyadvanced.commentedconfiguration.setting.TypedValueNode;
public class ConfigMigrator {
private final TypedValueNode<Double> versionNode;
private final List<VersionMigrator> versionMigrators;
public ConfigMigrator(TypedValueNode<Double> versionNode, List<VersionMigrator> versionMigrators) {
this.versionNode = versionNode;
this.versionMigrators = versionMigrators;
}
public void migrate(MVSettings settings) {
double versionNumber = settings.get(versionNode);
for (VersionMigrator versionMigrator : versionMigrators) {
if (versionNumber < versionMigrator.getVersion()) {
versionMigrator.migrate(settings);
}
}
}
public static class Builder {
private final TypedValueNode<Double> versionNode;
private final List<VersionMigrator> versionMigrators;
public Builder(TypedValueNode<Double> versionNode) {
this.versionNode = versionNode;
this.versionMigrators = new ArrayList<>();
}
public Builder addVersionMigrator(VersionMigrator versionMigrator) {
versionMigrators.add(versionMigrator);
return this;
}
public ConfigMigrator build() {
return new ConfigMigrator(versionNode, versionMigrators);
}
}
}

View File

@ -5,11 +5,11 @@ import java.util.List;
import com.onarandombox.MultiverseCore.utils.settings.MVSettings;
public class MigrateVersion {
public class VersionMigrator {
private final double version;
private final List<MigratorAction> actions;
protected MigrateVersion(double version, List<MigratorAction> actions) {
protected VersionMigrator(double version, List<MigratorAction> actions) {
this.version = version;
this.actions = actions;
}
@ -18,6 +18,10 @@ public class MigrateVersion {
actions.forEach(action -> action.migrate(settings));
}
public double getVersion() {
return version;
}
public static class Builder {
private final double version;
private final List<MigratorAction> actions = new ArrayList<>();
@ -31,8 +35,8 @@ public class MigrateVersion {
return this;
}
public MigrateVersion build() {
return new MigrateVersion(version, actions);
public VersionMigrator build() {
return new VersionMigrator(version, actions);
}
}
}