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