001/*
002 * PlotSquared, a land and world management plugin for Minecraft.
003 * Copyright (C) IntellectualSites <https://intellectualsites.com>
004 * Copyright (C) IntellectualSites team and contributors
005 *
006 * This program is free software: you can redistribute it and/or modify
007 * it under the terms of the GNU General Public License as published by
008 * the Free Software Foundation, either version 3 of the License, or
009 * (at your option) any later version.
010 *
011 * This program is distributed in the hope that it will be useful,
012 * but WITHOUT ANY WARRANTY; without even the implied warranty of
013 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
014 * GNU General Public License for more details.
015 *
016 * You should have received a copy of the GNU General Public License
017 * along with this program.  If not, see <https://www.gnu.org/licenses/>.
018 */
019package com.plotsquared.bukkit.util;
020
021import com.intellectualsites.annotations.NotPublic;
022import com.plotsquared.core.PlotSquared;
023
024import java.io.IOException;
025import java.nio.file.Files;
026import java.nio.file.Path;
027import java.nio.file.Paths;
028import java.util.stream.Stream;
029
030/**
031 * This is a helper class which replaces older syntax no longer supported by MiniMessage with replacements in messages_%.json.
032 * MiniMessage changed the syntax between major releases. To warrant a smooth upgrade, we attempt to replace any occurrences
033 * while loading PlotSquared.
034 *
035 * @since 7.0.0
036 */
037@NotPublic
038public class TranslationUpdateManager {
039
040    public static void upgradeTranslationFile() throws IOException {
041        String suggestCommand = "suggest_command";
042        String suggestCommandReplacement = "run_command";
043        String minHeight = "minHeight";
044        String minheightReplacement = "minheight";
045        String maxHeight = "maxHeight";
046        String maxheightReplacement = "maxheight";
047
048        try (Stream<Path> paths = Files.walk(Paths.get(PlotSquared.platform().getDirectory().toPath().resolve("lang").toUri()))) {
049            paths
050                    .filter(Files::isRegularFile)
051                    .filter(p -> p.getFileName().toString().matches("messages_[a-z]{2}\\.json"))
052                    .forEach(p -> {
053                        replaceInFile(p, suggestCommand, suggestCommandReplacement);
054                        replaceInFile(p, minHeight, minheightReplacement);
055                        replaceInFile(p, maxHeight, maxheightReplacement);
056                    });
057        }
058    }
059
060    private static void replaceInFile(Path path, String searchText, String replacementText) {
061        try {
062            String content = Files.readString(path);
063            if (content.contains(searchText)) {
064                content = content.replaceAll(searchText, replacementText);
065                Files.writeString(path, content);
066            }
067        } catch (IOException e) {
068            e.printStackTrace();
069        }
070    }
071
072}