Search in sources :

Example 1 with BaseConfig

use of org.spongepowered.common.config.inheritable.BaseConfig in project SpongeCommon by SpongePowered.

the class SpongeGameConfigs method createWorld.

public static InheritableConfigHandle<WorldConfig> createWorld(@Nullable final ResourceKey dimensionTypeKey, final ResourceKey world) {
    // Path format: config/sponge/worlds/<world-namespace>/<world-value>.conf
    final Path configPath = SpongeConfigs.getDirectory().resolve(Paths.get("worlds", world.namespace(), world.value() + ".conf"));
    if (dimensionTypeKey != null) {
        // Legacy config path: config/sponge/worlds/<dim-namespace>/<dim-value>/<world-name>/world.conf
        final String legacyName = SpongeGameConfigs.getLegacyWorldName(world);
        if (legacyName != null) {
            final Path legacyPath = SpongeConfigs.getDirectory().resolve(Paths.get("worlds", dimensionTypeKey.namespace(), SpongeGameConfigs.getLegacyValue(dimensionTypeKey), legacyName, "world.conf"));
            if (legacyPath.toFile().isFile() && !configPath.toFile().isFile()) {
                try {
                    Files.createDirectories(configPath.getParent());
                    Files.move(legacyPath, configPath);
                    final Path legacyParent = legacyPath.getParent();
                    try (final DirectoryStream<Path> str = Files.newDirectoryStream(legacyParent)) {
                        if (!str.iterator().hasNext()) {
                            Files.delete(legacyParent);
                        }
                    }
                } catch (final IOException ex) {
                    SpongeGameConfigs.LOGGER.error("Unable to migrate config for world {} from legacy location {}", world, legacyPath, ex);
                }
            }
        }
    }
    try {
        final InheritableConfigHandle<WorldConfig> config = new InheritableConfigHandle<>(WorldConfig.class, BaseConfig::transformation, SpongeConfigs.createLoader(configPath), SpongeGameConfigs.getGlobalInheritable());
        config.load();
        return config;
    } catch (final IOException ex) {
        SpongeGameConfigs.LOGGER.error("Unable to load configuration for world {}. Sponge will use a " + "fallback configuration with default values that will not save.", world, ex);
        return SpongeGameConfigs.createDetached();
    }
}
Also used : Path(java.nio.file.Path) InheritableConfigHandle(org.spongepowered.common.config.inheritable.InheritableConfigHandle) WorldConfig(org.spongepowered.common.config.inheritable.WorldConfig) IOException(java.io.IOException) BaseConfig(org.spongepowered.common.config.inheritable.BaseConfig)

Aggregations

IOException (java.io.IOException)1 Path (java.nio.file.Path)1 BaseConfig (org.spongepowered.common.config.inheritable.BaseConfig)1 InheritableConfigHandle (org.spongepowered.common.config.inheritable.InheritableConfigHandle)1 WorldConfig (org.spongepowered.common.config.inheritable.WorldConfig)1