Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package personthecat.modpackfixer;
- import java.io.File;
- import java.io.FileWriter;
- import java.io.IOException;
- import java.nio.charset.StandardCharsets;
- import java.nio.file.Files;
- import java.nio.file.Path;
- import java.util.ArrayList;
- import java.util.List;
- import java.util.stream.Stream;
- public class Main
- {
- static String path = System.getProperty("user.dir");
- static String filesToFixPath = path + "/assets/files to fix/";
- static File oldLangFile = new File(path + "/assets/old.lang");
- static File newLangFile = new File(path + "/assets/new.lang");
- static List<String> newLines = new ArrayList<>();
- public static void main(String[] args)
- {
- createKeyFile();
- fixAllFiles();
- }
- private static void createKeyFile()
- {
- File keyFile = new File(path + "/assets/keys.txt");
- try
- {
- keyFile.createNewFile();
- }
- catch (IOException e) {System.err.println("Could not create new file.");}
- String oldLangKeys = reformatString(getFullContent(oldLangFile));
- String newLangKeys = reformatString(getFullContent(newLangFile));
- String[] oldLangLines = ignoreComments(oldLangKeys.split(System.getProperty("line.separator")));
- String[] newLangLines = ignoreComments(newLangKeys.split(System.getProperty("line.separator")));
- addStringsToArray(oldLangLines, newLines, 1); //Add translation.
- addStringsToArray(oldLangLines, newLines, 0); //Add original keys.
- removeMatchlessAndNoneLines();
- removeDuplicateLines();
- addNewNamesToArrayElements(newLangLines);
- writeLinesToFile(keyFile, newLines.toArray(new String[newLines.size()]));
- }
- private static void fixAllFiles()
- {
- System.out.println("Preparing to fix all files...");
- try
- {
- Stream<Path> files = Files.walk(new File(filesToFixPath).toPath());
- files.forEach(path ->
- {
- File file = path.toFile();
- if (file.isFile() && !file.toString().contains(".nbt") && !file.toString().contains(".rcst"))
- {
- System.out.println("Fixing this file: " + file);
- String content = getFullContent(file);
- for (String s : newLines)
- {
- String[] split = s.split(",");
- if (split.length == 3)
- {
- content = content.replaceAll(split[1], split[2]);
- }
- }
- writeLinesToFile(file, content);
- }
- });
- files.close();
- }
- catch (IOException e) {}
- System.out.println("Done fixing files.");
- }
- private static void addNewNamesToArrayElements(String[] newElements)
- {
- for (int i = 0; i < newLines.size(); i++)
- {
- String x = newLines.get(i);
- String existingTranslationKey = x.split(",")[0];
- for (String y : newElements)
- {
- String newRegistryName = y.split("=")[0];
- String translationKey = y.split("=")[1];
- if (existingTranslationKey.equals(translationKey) && x.split(",").length < 4)
- {
- System.out.println("Finalizing this line: " + x);
- newLines.set(i, x + newRegistryName + ",");
- }
- if (x.split(",").length >= 4) System.out.println("Found a multiple entries for " + translationKey);
- }
- }
- }
- private static String getFullContent(File file)
- {
- String fullContent = null;
- try
- {
- fullContent = new String(Files.readAllBytes(file.toPath()), StandardCharsets.UTF_8);
- }
- catch (IOException e) {System.err.println("Could not load file: " + path);}
- return fullContent;
- }
- private static String reformatString(String input)
- {
- input = input.replaceAll("tile.", "").replaceAll(".name", "").replaceAll("item.", "");
- input = input.replaceAll(".alpha", ":0");
- input = input.replaceAll(".bravo", ":1");
- input = input.replaceAll(".charlie", ":2");
- input = input.replaceAll(".delta", ":3");
- input = input.replaceAll(".echo", ":4");
- input = input.replaceAll(".fox", ":5");
- input = input.replaceAll(".golf", ":6");
- input = input.replaceAll(".hotel", ":7");
- input = input.replaceAll(".india", ":8");
- input = input.replaceAll(".juliet", ":9");
- input = input.replaceAll(".kilo", ":10");
- input = input.replaceAll(".lima", ":11");
- input = input.replaceAll(".mike", ":12");
- input = input.replaceAll(".november", ":13");
- input = input.replaceAll(".oscar", ":14");
- input = input.replaceAll(".papa", ":15");
- return input;
- }
- private static void addStringsToArray(String[] strings, List<String> array, int index)
- {
- for (int i = 0; i < strings.length; i++)
- {
- String currentLine = strings[i].split("=")[index];
- try
- {
- array.set(i, array.get(i) + currentLine + ",");
- }
- catch (IndexOutOfBoundsException e) {array.add(currentLine + ",");}
- }
- }
- private static void writeLinesToFile(File file, String... lines)
- {
- try
- {
- FileWriter writer = new FileWriter(file);
- for (String line : lines)
- {
- if (!line.startsWith("none"))
- {
- writer.write(line + System.getProperty("line.separator"));
- }
- }
- writer.close();
- }
- catch (IOException e) {System.err.println("Could not write new file");}
- }
- private static String[] ignoreComments(String[] input)
- {
- List<String> newList = new ArrayList<>();
- for (String s : input)
- {
- if (s.contains("=") && s.split("=").length > 1)
- {
- newList.add(s);
- }
- }
- return newList.toArray(new String[newList.size()]);
- }
- private static void removeMatchlessAndNoneLines()
- {
- List<String> newList = new ArrayList<>();
- for (String s : newLines)
- {
- if (s.split(",").length > 1 && !s.startsWith("none")) newList.add(s);
- }
- newLines = newList;
- }
- private static void removeDuplicateLines()
- {
- List<String> newList = new ArrayList<>();
- for (int i = 0; i < newLines.size(); i++)
- {
- boolean canAdd = true;
- for (int j = 0; j < newLines.size(); j++)
- {
- if (!newLines.get(i).equals(newLines.get(j)))
- {
- if (newLines.get(i).split(",")[0].equals(newLines.get(j).split(",")[0]))
- {
- canAdd = false;
- }
- }
- }
- if (canAdd) newList.add(newLines.get(i));
- }
- newLines = newList;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment