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 // tag opening / closing characters are important, as the locale keys exist as well, which should not be replaced 059 String listInfoUnknown = "<info.unknown>"; 060 String listInfoUnknownReplacement = "<unknown>"; 061 String listInfoServer = "<info.server>"; 062 String listInfoServerReplacement = "<server>"; 063 String listInfoEveryone = "<info.everyone>"; 064 String listInfoEveryoneReplacement = "<everyone>"; 065 066 try (Stream<Path> paths = Files.walk(Paths.get(PlotSquared.platform().getDirectory().toPath().resolve("lang").toUri()))) { 067 paths 068 .filter(Files::isRegularFile) 069 .filter(p -> p.getFileName().toString().matches("messages_[a-z]{2}\\.json")) 070 .forEach(p -> { 071 replaceInFile(p, suggestCommand, suggestCommandReplacement); 072 replaceInFile(p, minHeight, minheightReplacement); 073 replaceInFile(p, maxHeight, maxheightReplacement); 074 replaceInFile(p, usedGrants, usedGrantsReplacement); 075 replaceInFile(p, remainingGrants, remainingGrantsReplacement); 076 replaceInFile(p, minimumRadius, minimumRadiusReplacement); 077 replaceInFile(p, maximumMoves, maximumMovesReplacement); 078 replaceInFile(p, userMove, userMoveReplacement); 079 replaceInFile(p, listInfoUnknown, listInfoUnknownReplacement); 080 replaceInFile(p, listInfoServer, listInfoServerReplacement); 081 replaceInFile(p, listInfoEveryone, listInfoEveryoneReplacement); 082 }); 083 } 084 } 085 086 private static void replaceInFile(Path path, String searchText, String replacementText) { 087 try { 088 String content = Files.readString(path); 089 if (content.contains(searchText)) { 090 content = content.replaceAll(searchText, replacementText); 091 Files.writeString(path, content); 092 } 093 } catch (IOException e) { 094 e.printStackTrace(); 095 } 096 } 097 098}