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 String usedGrants = "usedGrants"; 048 String usedGrantsReplacement = "used_grants"; 049 String remainingGrants = "remainingGrants"; 050 String rremainingGrantsReplacement = "remaining_grants"; 051 052 try (Stream<Path> paths = Files.walk(Paths.get(PlotSquared.platform().getDirectory().toPath().resolve("lang").toUri()))) { 053 paths 054 .filter(Files::isRegularFile) 055 .filter(p -> p.getFileName().toString().matches("messages_[a-z]{2}\\.json")) 056 .forEach(p -> { 057 replaceInFile(p, suggestCommand, suggestCommandReplacement); 058 replaceInFile(p, minHeight, minheightReplacement); 059 replaceInFile(p, maxHeight, maxheightReplacement); 060 replaceInFile(p, usedGrants, usedGrantsReplacement); 061 replaceInFile(p, remainingGrants, rremainingGrantsReplacement); 062 }); 063 } 064 } 065 066 private static void replaceInFile(Path path, String searchText, String replacementText) { 067 try { 068 String content = Files.readString(path); 069 if (content.contains(searchText)) { 070 content = content.replaceAll(searchText, replacementText); 071 Files.writeString(path, content); 072 } 073 } catch (IOException e) { 074 e.printStackTrace(); 075 } 076 } 077 078}