diff --git a/conversions/Backwards/src/main/java/com/agentdid127/resourcepack/backwards/impl/EnchantPathConverter.java b/conversions/Backwards/src/main/java/com/agentdid127/resourcepack/backwards/impl/EnchantPathConverter.java index 43f2ee9f..25333c85 100644 --- a/conversions/Backwards/src/main/java/com/agentdid127/resourcepack/backwards/impl/EnchantPathConverter.java +++ b/conversions/Backwards/src/main/java/com/agentdid127/resourcepack/backwards/impl/EnchantPathConverter.java @@ -17,18 +17,39 @@ public EnchantPathConverter(PackConverter packConverter) { @Override public void convert(Pack pack) throws IOException { Path miscPath = pack.getWorkingPath().resolve("assets/minecraft/textures/misc".replace("/", File.separator)); + if (miscPath.toFile().exists()) { + return; + } - if (miscPath.resolve("enchanted_glint_item.png").toFile().exists()) - Files.move(miscPath.resolve("enchanted_glint_item.png"), miscPath.resolve("enchanted_item_glint.png")); - else if (miscPath.resolve("enchanted_glint_entity.png").toFile().exists()) - Files.move(miscPath.resolve("enchanted_glint_entity.png"), miscPath.resolve("enchanted_item_glint.png")); + Path enchantGlintItemPath = miscPath.resolve("enchanted_glint_item.png"); + Path enchantGlintEntityPath = miscPath.resolve("enchanted_glint_entity.png"); + Path newEnchantGlintItemPath = miscPath.resolve("enchanted_item_glint.png"); + if (enchantGlintItemPath.toFile().exists()) { + if (newEnchantGlintItemPath.toFile().exists()) { + newEnchantGlintItemPath.toFile().delete(); + } + Files.move(enchantGlintItemPath, newEnchantGlintItemPath); + } else if (enchantGlintEntityPath.toFile().exists()) { + if (newEnchantGlintItemPath.toFile().exists()) { + newEnchantGlintItemPath.toFile().delete(); + } + Files.move(enchantGlintEntityPath, newEnchantGlintItemPath); + } - if (miscPath.resolve("enchanted_glint_item.png.mcmeta").toFile().exists()) - Files.move(miscPath.resolve("enchanted_glint_item.png.mcmeta"), - miscPath.resolve("enchanted_item_glint.png.mcmeta")); - else if (miscPath.resolve("enchanted_glint_entity.png.mcmeta").toFile().exists()) - Files.move(miscPath.resolve("enchanted_glint_entity.png.mcmeta"), - miscPath.resolve("enchanted_item_glint.png.mcmeta")); + Path enchantGlintItemMetaPath = miscPath.resolve("enchanted_glint_item.png.mcmeta"); + Path enchantGlintEntityMetaPath = miscPath.resolve("enchanted_glint_entity.png.mcmeta"); + Path newEnchantGlintItemMetaPath = miscPath.resolve("enchanted_item_glint.png.mcmeta"); + if (enchantGlintItemMetaPath.toFile().exists()) { + if (newEnchantGlintItemMetaPath.toFile().exists()) { + newEnchantGlintItemMetaPath.toFile().delete(); + } + Files.move(enchantGlintItemMetaPath, newEnchantGlintItemMetaPath); + } else if (enchantGlintEntityMetaPath.toFile().exists()) { + if (newEnchantGlintItemMetaPath.toFile().exists()) { + newEnchantGlintItemMetaPath.toFile().delete(); + } + Files.move(enchantGlintEntityMetaPath, newEnchantGlintItemMetaPath); + } Files.deleteIfExists(miscPath.resolve("enchanted_glint_entity.png.mcmeta")); Files.deleteIfExists(miscPath.resolve("enchanted_glint_item.png.mcmeta")); diff --git a/conversions/Backwards/src/main/java/com/agentdid127/resourcepack/backwards/impl/NameConverter.java b/conversions/Backwards/src/main/java/com/agentdid127/resourcepack/backwards/impl/NameConverter.java index 553c808e..5c8097ac 100644 --- a/conversions/Backwards/src/main/java/com/agentdid127/resourcepack/backwards/impl/NameConverter.java +++ b/conversions/Backwards/src/main/java/com/agentdid127/resourcepack/backwards/impl/NameConverter.java @@ -13,9 +13,10 @@ import java.io.IOException; import java.nio.file.Files; import java.nio.file.Path; -import java.nio.file.Paths; import java.util.Arrays; import java.util.List; +import java.util.Objects; +import java.util.stream.Stream; public class NameConverter extends Converter { private int to; @@ -71,48 +72,52 @@ public void convert(Pack pack) throws IOException { Logger.debug("MCPatcher exists, switching to optifine"); if (minecraftPath.resolve("optifine").toFile().exists()) { Logger.debug("OptiFine exists, merging directories"); - FileUtil.mergeDirectories(minecraftPath.resolve("optifine").toFile(), - minecraftPath.resolve("mcpatcher").toFile()); - } else + FileUtil.mergeDirectories(minecraftPath.resolve("optifine").toFile(), minecraftPath.resolve("mcpatcher").toFile()); + } else { Files.move(minecraftPath.resolve("mcpatcher"), minecraftPath.resolve("optifine")); - if (minecraftPath.resolve("mcpatcher").toFile().exists()) + } + + if (minecraftPath.resolve("mcpatcher").toFile().exists()) { FileUtil.deleteDirectoryAndContents(minecraftPath.resolve("mcpatcher")); + } } } Path modelsPath = minecraftPath.resolve("models"); - Path itemModelsPath = modelsPath.resolve("item"); Path blockModelsPath = modelsPath.resolve("block"); - if (modelsPath.toFile().exists()) { - // 1.20.3 Models - if (to < Util.getVersionProtocol(packConverter.getGson(), "1.20.3")) + if (to < Util.getVersionProtocol(packConverter.getGson(), "1.20.3")) { renameAll(blockMapping203, ".json", blockModelsPath); + } // 1.19 Models - if (to < Util.getVersionProtocol(packConverter.getGson(), "1.19")) + if (to < Util.getVersionProtocol(packConverter.getGson(), "1.19")) { renameAll(blockMapping19, ".json", blockModelsPath); + } // Update 1.14 items - if (to < Util.getVersionProtocol(packConverter.getGson(), "1.14")) + if (to < Util.getVersionProtocol(packConverter.getGson(), "1.14")) { renameAll(newItemMapping, ".json", itemModelsPath); + } // 1.13 Models if (to < Util.getVersionProtocol(packConverter.getGson(), "1.13")) { renameAll(itemMapping, ".json", itemModelsPath); renameAll(blockMapping, ".json", blockModelsPath); Path bannerModelPath = itemModelsPath.resolve("banner.json"); - if (bannerModelPath.toFile().exists()) + if (bannerModelPath.toFile().exists()) { bannerModelPath.toFile().delete(); + } } } // Update BlockStates Path blockStates = minecraftPath.resolve("blockstates"); - if (blockStates.toFile().exists()) + if (blockStates.toFile().exists()) { renameAll(blockMapping, ".json", blockStates); + } // Update textures Path texturesPath = minecraftPath.resolve("textures"); @@ -122,58 +127,74 @@ public void convert(Pack pack) throws IOException { // 1.20.3 if (to < Util.getVersionProtocol(packConverter.getGson(), "1.20.3") - && from >= Util.getVersionProtocol(packConverter.getGson(), "1.20.3")) + && from >= Util.getVersionProtocol(packConverter.getGson(), "1.20.3")) { renameAll(blockMapping203, ".png", texturesBlockPath); + } // 1.19 if (to < Util.getVersionProtocol(packConverter.getGson(), "1.19") - && from >= Util.getVersionProtocol(packConverter.getGson(), "1.19")) + && from >= Util.getVersionProtocol(packConverter.getGson(), "1.19")) { renameAll(blockMapping19, ".png", texturesBlockPath); + } // Entities Path entityPath = texturesPath.resolve("entity"); + if (entityPath.toFile().exists()) { + // 1.17 Squid + if (to < Util.getVersionProtocol(packConverter.getGson(), "1.17") + && from >= Util.getVersionProtocol(packConverter.getGson(), "1.17")) { + renameAll(blockMapping17, ".png", texturesBlockPath); + renameAll(itemMapping17, ".png", texturesItemPath); + renameAll(blockMapping17, ".png", blockModelsPath); + renameAll(itemMapping17, ".png", itemModelsPath); + Path squidPath = entityPath.resolve("squid/squid.png"); + if (squidPath.toFile().exists()) { + Path newSquidPath = entityPath.resolve("squid.png"); + if (newSquidPath.toFile().exists()) { + newSquidPath.toFile().delete(); + } + Files.move(squidPath, newSquidPath); + } + } - // 1.17 Squid - if (to < Util.getVersionProtocol(packConverter.getGson(), "1.17") - && from >= Util.getVersionProtocol(packConverter.getGson(), "1.17")) { - renameAll(blockMapping17, ".png", texturesBlockPath); - renameAll(itemMapping17, ".png", texturesItemPath); - renameAll(blockMapping17, ".png", blockModelsPath); - renameAll(itemMapping17, ".png", itemModelsPath); - Path newSquidPath = entityPath.resolve("squid/squid.png"); - if (newSquidPath.toFile().exists()) - Files.move(newSquidPath, entityPath.resolve("squid.png")); - } - - // 1.16 Iron golems - if (from >= Util.getVersionProtocol(packConverter.getGson(), "1.16") - && to < Util.getVersionProtocol(packConverter.getGson(), "1.16")) { - Path newIronGolemPath = entityPath - .resolve("iron_golem/iron_golem.png".replace("/", File.separator)); - if (newIronGolemPath.toFile().exists()) - Files.move(newIronGolemPath, entityPath.resolve("iron_golem.png")); - } - - if (to < Util.getVersionProtocol(packConverter.getGson(), "1.14")) { - renameAll(newBlockMapping, ".png", texturesBlockPath); - renameAll(newBlockMapping, ".png.mcmeta", texturesBlockPath); - renameAll(newItemMapping, ".png", texturesItemPath); - renameAll(newItemMapping, ".png.mcmeta", texturesItemPath); - } - - if (from >= Util.getVersionProtocol(packConverter.getGson(), "1.13") - && to < Util.getVersionProtocol(packConverter.getGson(), "1.13")) { - renameAll(blockMapping, ".png", texturesBlockPath); - renameAll(blockMapping, ".png.mcmeta", texturesBlockPath); + // 1.16 Iron golems + if (from >= Util.getVersionProtocol(packConverter.getGson(), "1.16") + && to < Util.getVersionProtocol(packConverter.getGson(), "1.16")) { + Path ironGolemPath = entityPath.resolve("iron_golem/iron_golem.png".replace("/", File.separator)); + if (ironGolemPath.toFile().exists()) { + Path newIronGolemPath = entityPath.resolve("iron_golem.png"); + if (newIronGolemPath.toFile().exists()) { + newIronGolemPath.toFile().delete(); + } + Files.move(ironGolemPath, newIronGolemPath); + } + } - renameAll(itemMapping, ".png", texturesItemPath); - renameAll(itemMapping, ".png.mcmeta", texturesItemPath); + if (to < Util.getVersionProtocol(packConverter.getGson(), "1.14")) { + renameAll(newBlockMapping, ".png", texturesBlockPath); + renameAll(newBlockMapping, ".png.mcmeta", texturesBlockPath); + renameAll(newItemMapping, ".png", texturesItemPath); + renameAll(newItemMapping, ".png.mcmeta", texturesItemPath); + } - // 1.13 End Crystals - if (entityPath.resolve("end_crystal").toFile().exists()) - Files.move(entityPath.resolve("end_crystal"), entityPath.resolve("endercrystal")); + if (from >= Util.getVersionProtocol(packConverter.getGson(), "1.13") + && to < Util.getVersionProtocol(packConverter.getGson(), "1.13")) { + renameAll(blockMapping, ".png", texturesBlockPath); + renameAll(blockMapping, ".png.mcmeta", texturesBlockPath); + renameAll(itemMapping, ".png", texturesItemPath); + renameAll(itemMapping, ".png.mcmeta", texturesItemPath); + + // 1.13 End Crystals + if (entityPath.resolve("end_crystal").toFile().exists()) { + Path newEnderCrystalFolderPath = entityPath.resolve("endcrystal"); + if (newEnderCrystalFolderPath.toFile().exists()) { + newEnderCrystalFolderPath.toFile().delete(); + } + Files.move(entityPath.resolve("end_crystal"), newEnderCrystalFolderPath); + } - findEntityFiles(texturesPath.resolve("entity")); + findEntityFiles(texturesPath.resolve("entity")); + } } } @@ -187,26 +208,29 @@ public void convert(Pack pack) throws IOException { /** * Finds files in entity folder - * + * * @param path * @throws IOException */ protected void findEntityFiles(Path path) throws IOException { - if (!path.toFile().exists()) - return; File directory = path.toFile(); - for (File file : directory.listFiles()) { - if (file.isDirectory()) - continue; - renameAll(entityMapping, ".png", file.toPath()); - renameAll(entityMapping, ".png.mcmeta", file.toPath()); - findEntityFiles(file.toPath()); + if (!directory.exists()) + return; + File[] files = directory.listFiles(); + if (files == null) + return; // why is this happening?? + for (File file : files) { + if (file.isDirectory()) { + renameAll(entityMapping, ".png", file.toPath()); + renameAll(entityMapping, ".png.mcmeta", file.toPath()); + findEntityFiles(file.toPath()); + } } } /** * Finds files in folders called - * + * * @param path * @throws IOException */ @@ -214,7 +238,7 @@ protected void findFiles(Path path) throws IOException { if (!path.toFile().exists()) return; File directory = path.toFile(); - for (File file : directory.listFiles()) { + for (File file : Objects.requireNonNull(directory.listFiles())) { if (file.isDirectory()) { if (file.getName().equals("item")) { Logger.debug("Found Items folder, renaming"); @@ -229,11 +253,13 @@ protected void findFiles(Path path) throws IOException { findFiles(file.toPath()); } - if (file.getName().contains("(")) + if (file.getName().contains("(")) { FileUtil.renameFile(path.resolve(file.getName()), file.getName().replaceAll("[()]", "")); + } - if (!file.getName().equals(file.getName().toLowerCase())) + if (!file.getName().equals(file.getName().toLowerCase())) { Logger.debug("Renamed: " + file.getName() + "->" + file.getName().toLowerCase()); + } FileUtil.renameFile(path.resolve(file.getName()), file.getName().toLowerCase()); } @@ -241,73 +267,101 @@ protected void findFiles(Path path) throws IOException { /** * Renames folder - * + * * @param mapping * @param extension * @param path * @throws IOException */ protected void renameAll(Mapping mapping, String extension, Path path) throws IOException { - if (path.toFile().exists()) { - // remap grass blocks in order due to the cyclical way their names have changed, - // i.e grass -> grass_block, tall_grass -> grass, double_grass -> tall_grass - List grasses = Arrays.asList("tall_grass", "grass", "grass_block"); - if (from <= Util.getVersionProtocol(packConverter.getGson(), "1.12.2") - && (path.endsWith("blockstates") || path.endsWith("textures/block"))) { - grasses.stream().forEach(name -> { - String newName = mapping.remap(name); - Boolean ret = FileUtil.renameFile(Paths.get(path + File.separator + name + extension), - newName + extension); - if (ret == null) - return; - if (ret) - Logger.debug("Renamed: " + name + extension + "->" + newName + extension); - else if (!ret) - System.err.println(" Failed to rename: " + name + extension + "->" + newName + extension); - }); - } + if (!path.toFile().exists()) { + return; + } - if (from > Util.getVersionProtocol(packConverter.getGson(), "1.12.2") && - to <= Util.getVersionProtocol(packConverter.getGson(), "1.12.2")) { - Arrays.asList("snow", "snow_block").stream().forEach(name -> { - String newName = mapping.remap(name); - if (!extension.equals(".png")) { - Boolean ret = FileUtil.renameFile(Paths.get(path + File.separator + name + extension), - newName + extension); - if (ret == null) - return; - if (ret) - Logger.debug("Renamed: " + name + extension + "->" + newName + extension); - else if (!ret) - Logger.log("Failed to rename: " + name + extension + "->" + newName + extension); - } - }); - } + // remap grass blocks in order due to the cyclical way their names have changed, + // i.e grass -> grass_block, tall_grass -> grass, double_grass -> tall_grass + List grasses = Arrays.asList("tall_grass", "grass", "grass_block"); + if (from <= Util.getVersionProtocol(packConverter.getGson(), "1.12.2") + && (path.endsWith("blockstates") || path.endsWith("textures/block"))) { + grasses.forEach(name -> { + Path basePath = path.resolve(name + extension); + String remappedName = mapping.remap(name); + + Path newPath = path.resolve(remappedName + extension); + if (newPath.toFile().exists()) { + // Same note from Slicer.java line 38 + newPath.toFile().delete(); + } - Files.list(path).forEach(path1 -> { - if (!path1.toString().endsWith(extension)) + Boolean renameSuccess = FileUtil.renameFile(basePath, newPath); + if (renameSuccess == null) { return; + } - String baseName = path1.getFileName().toString().substring(0, - path1.getFileName().toString().length() - extension.length()); - // skip the already renamed grass blocks - if (grasses.contains(baseName) - && (path.endsWith("blockstates") || path.endsWith("textures/block"))) - return; + if (renameSuccess) { + Logger.debug("Renamed: " + name + extension + "->" + newPath.getFileName()); + } else if (!renameSuccess) { + Logger.error(" Failed to rename: " + name + extension + "->" + newPath.getFileName()); + } + }); + } + + if (from > Util.getVersionProtocol(packConverter.getGson(), "1.12.2") && + to <= Util.getVersionProtocol(packConverter.getGson(), "1.12.2")) { + Stream.of("snow", "snow_block").forEach(name -> { + Path basePath = path.resolve(name + extension); + String remappedName = mapping.remap(name); + + if (!extension.equals(".png")) { + Path newPath = path.resolve(remappedName + extension); + if (newPath.toFile().exists()) { + // Same note from Slicer.java line 38 + newPath.toFile().delete(); + } - String newName = mapping.remap(baseName); - if (newName != null && !newName.equals(baseName)) { - Boolean ret = FileUtil.renameFile(path1, newName + extension); - if (ret == null) + Boolean renameSuccess = FileUtil.renameFile(basePath, newPath); + if (renameSuccess == null) { return; - if (ret) - Logger.debug("Renamed: " + path1.getFileName().toString() + "->" + newName + extension); - else if (!ret) - System.err.println(" Failed to rename: " + path1.getFileName().toString() + "->" + newName - + extension); + } + + if (renameSuccess) { + Logger.debug("Renamed: " + name + extension + "->" + newPath.getFileName()); + } else if (!renameSuccess) { + Logger.log("Failed to rename: " + name + extension + "->" + newPath.getFileName()); + } } }); } + + Files.list(path) + .filter(path1 -> path.toString().endsWith(extension)) + .forEach(path1 -> { + String baseName = path1.getFileName().toString().substring(0, path1.getFileName().toString().length() - extension.length()); + // NOTE: skip the already renamed grass blocks + if (grasses.contains(baseName) && (path.endsWith("blockstates") || path.endsWith("textures/block"))) { + return; + } + + String remappedName = mapping.remap(baseName); + if (remappedName != null && !remappedName.equals(baseName)) { + Path newPath = path1.getParent().resolve(remappedName + extension); + if (newPath.toFile().exists()) { + // Same note from Slicer.java line 38 + newPath.toFile().delete(); + } + + Boolean renameSuccess = FileUtil.renameFile(path1, newPath); + if (renameSuccess == null) { + return; + } + + if (renameSuccess) { + Logger.debug("Renamed: " + path1.getFileName().toString() + "->" + newPath.getFileName()); + } else if (!renameSuccess) { + Logger.error(" Failed to rename: " + path1.getFileName().toString() + "->" + newPath.getFileName()); + } + } + }); } public Mapping getBlockMapping() { diff --git a/conversions/Forwards/src/main/java/com/agentdid127/resourcepack/forwards/impl/NameConverter.java b/conversions/Forwards/src/main/java/com/agentdid127/resourcepack/forwards/impl/NameConverter.java index 835bde71..29f4234a 100644 --- a/conversions/Forwards/src/main/java/com/agentdid127/resourcepack/forwards/impl/NameConverter.java +++ b/conversions/Forwards/src/main/java/com/agentdid127/resourcepack/forwards/impl/NameConverter.java @@ -360,37 +360,36 @@ protected void renameAll(Mapping mapping, String extension, Path path) throws IO } Logger.addTab(); - Files.list(path).forEach(path1 -> { - if (!path1.toString().endsWith(extension)) - return; + Files.list(path) + .filter(path1 -> path.toString().endsWith(extension)) + .forEach(path1 -> { + String baseName = path1.getFileName().toString().substring(0, path1.getFileName().toString().length() - extension.length()); - String baseName = path1.getFileName().toString().substring(0, path1.getFileName().toString().length() - extension.length()); - - // skip the already renamed grass blocks - if (grasses.contains(baseName) && (path.endsWith("blockstates") || path.endsWith("textures/block"))) { - return; - } - - String remappedName = mapping.remap(baseName); - if (remappedName != null && !remappedName.equals(baseName)) { - Path newPath = path1.getParent().resolve(remappedName + extension); - if (newPath.toFile().exists()) { - // Same note from Slicer.java line 38 - newPath.toFile().delete(); - } - - Boolean renameSuccess = FileUtil.renameFile(path1, newPath); - if (renameSuccess == null) { - return; - } + // skip the already renamed grass blocks + if (grasses.contains(baseName) && (path.endsWith("blockstates") || path.endsWith("textures/block"))) { + return; + } - if (renameSuccess) { - Logger.debug("Renamed: " + path1.getFileName().toString() + "->" + newPath.getFileName()); - } else if (!renameSuccess) { - Logger.log("Failed to rename: " + path1.getFileName().toString() + "->" + newPath.getFileName()); - } - } - }); + String remappedName = mapping.remap(baseName); + if (remappedName != null && !remappedName.equals(baseName)) { + Path newPath = path1.getParent().resolve(remappedName + extension); + if (newPath.toFile().exists()) { + // Same note from Slicer.java line 38 + newPath.toFile().delete(); + } + + Boolean renameSuccess = FileUtil.renameFile(path1, newPath); + if (renameSuccess == null) { + return; + } + + if (renameSuccess) { + Logger.debug("Renamed: " + path1.getFileName().toString() + "->" + newPath.getFileName()); + } else if (!renameSuccess) { + Logger.log("Failed to rename: " + path1.getFileName().toString() + "->" + newPath.getFileName()); + } + } + }); Logger.subTab(); } } diff --git a/library/src/main/java/com/agentdid127/resourcepack/library/utilities/Logger.java b/library/src/main/java/com/agentdid127/resourcepack/library/utilities/Logger.java index 2334f9f8..9cc53ae5 100644 --- a/library/src/main/java/com/agentdid127/resourcepack/library/utilities/Logger.java +++ b/library/src/main/java/com/agentdid127/resourcepack/library/utilities/Logger.java @@ -14,7 +14,9 @@ public static void addTab() { public static void subTab() { tabs--; - if (tabs < 0) tabs = 0; + if (tabs < 0) { + tabs = 0; + } } public static void resetTab() { @@ -22,12 +24,11 @@ public static void resetTab() { } private static String getTabs() { - String out = ""; - + StringBuilder out = new StringBuilder(); for (int i = 0; i < tabs; i++) { - out += " "; + out.append(" "); } - return out; + return out.toString(); } public static void setStream(PrintStream stream) { @@ -39,15 +40,21 @@ public static void setDebug(boolean debug) { } public static void debug(String message) { - if (debug) log(message); + if (debug) { + log(message); + } } public static void log(String message) { stream.println(getTabs() + message); } + public static void error(String message) { + log(message); + } + private static void debug(Object thing) { - if (debug) log(thing); + debug(thing.toString()); } public static void log(Object thing) {