001/*
002 * PermissionsEx
003 * Copyright (C) zml and PermissionsEx contributors
004 *
005 * Licensed under the Apache License, Version 2.0 (the "License");
006 * you may not use this file except in compliance with the License.
007 * You may obtain a copy of the License at
008 *
009 *    http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017package ca.stellardrift.permissionsex.impl.config;
018
019import org.spongepowered.configurate.transformation.ConfigurationTransformation;
020import org.spongepowered.configurate.transformation.MoveStrategy;
021import org.spongepowered.configurate.transformation.TransformAction;
022
023import static org.spongepowered.configurate.NodePath.path;
024
025public final class ConfigTransformations {
026
027    private ConfigTransformations() {
028        throw new AssertionError();
029    }
030
031    /**
032     * Create a configuration transformation that converts the Bukkit/1.x global configuration structure to the new v2.x configuration structure.
033     * @return A transformation that converts a 1.x-style configuration to a 2.x-style configuration
034     */
035    private static ConfigurationTransformation initialToZero() {
036        return ConfigurationTransformation.builder()
037                        .moveStrategy(MoveStrategy.MERGE)
038                        .addAction(path("permissions"), (inputPath, valueAtPath) -> new Object[0])
039                        .addAction(path("permissions", "backend"), (inputPath, valueAtPath) -> p("default-backend"))
040                        .addAction(path("permissions", "allowOps"), TransformAction.remove())
041                        .addAction(path("permissions", "basedir"), TransformAction.remove())
042                        .addAction(path("updater"), (inputPath, valueAtPath) -> {
043                            valueAtPath.node("enabled").set(valueAtPath.raw());
044                            valueAtPath.node("always-update").from(valueAtPath.parent().node("alwaysUpdate"));
045                            valueAtPath.parent().node("alwaysUpdate").set(null);
046                            return null;
047                        })
048                        .build();
049    }
050
051    public static ConfigurationTransformation versions() {
052        return ConfigurationTransformation.versionedBuilder()
053                .addVersion(0, initialToZero())
054                .build();
055    }
056
057    private static Object[] p(Object... path) {
058        return path;
059    }
060}