From da0d1221662615b1bfa000042165589fe1b8a1d8 Mon Sep 17 00:00:00 2001 From: Frank Date: Tue, 19 Oct 2021 09:58:41 +0200 Subject: [PATCH] Added Function that allows developers to manually fix nbts --- .../ru/bclib/api/datafixer/DataFixerAPI.java | 2 +- .../bclib/api/datafixer/MigrationProfile.java | 57 +++++++++++++++++-- .../java/ru/bclib/api/datafixer/Patch.java | 14 ++++- 3 files changed, 65 insertions(+), 8 deletions(-) diff --git a/src/main/java/ru/bclib/api/datafixer/DataFixerAPI.java b/src/main/java/ru/bclib/api/datafixer/DataFixerAPI.java index c0116bdb..6f52ad33 100644 --- a/src/main/java/ru/bclib/api/datafixer/DataFixerAPI.java +++ b/src/main/java/ru/bclib/api/datafixer/DataFixerAPI.java @@ -463,7 +463,7 @@ public class DataFixerAPI { e.printStackTrace(); } } - + static CompoundTag patchConfTag = null; static CompoundTag getPatchData(){ if (patchConfTag==null) { diff --git a/src/main/java/ru/bclib/api/datafixer/MigrationProfile.java b/src/main/java/ru/bclib/api/datafixer/MigrationProfile.java index 24e5e8ad..2cf92fdd 100644 --- a/src/main/java/ru/bclib/api/datafixer/MigrationProfile.java +++ b/src/main/java/ru/bclib/api/datafixer/MigrationProfile.java @@ -12,6 +12,7 @@ import ru.bclib.util.ModUtil; import java.io.File; import java.io.IOException; +import java.util.ArrayList; import java.util.Collections; import java.util.HashMap; import java.util.LinkedList; @@ -34,7 +35,7 @@ public class MigrationProfile { private boolean didRunPrePatch; private Exception prePatchException; - MigrationProfile(CompoundTag config) { + MigrationProfile(CompoundTag config, boolean applyAll) { this.config = config; this.mods = Collections.unmodifiableSet(Patch.getALL() @@ -55,7 +56,7 @@ public class MigrationProfile { List paths = patch.getWorldDataIDPaths(); if (paths!=null) worldDataIDPaths.put(modID, paths); - if (currentPatchLevel(modID) < patch.level) { + if (applyAll || currentPatchLevel(modID) < patch.level) { replacements.putAll(patch.getIDReplacements()); if (patch.getLevelDatPatcher()!=null) levelPatches.add(patch.getLevelDatPatcher()); @@ -75,6 +76,53 @@ public class MigrationProfile { this.worldDataPatchers = Collections.unmodifiableList(worldDataPatches); } + /** + * This method is supposed to be used by developers to apply id-patches to custom nbt structures. It is only + * available in Developer-Mode + * + */ + public static void fixCustomFolder(File dir){ + if (!BCLib.isDevEnvironment()) return; + MigrationProfile profile = Patch.createMigrationData(); + List nbts = getAllNbts(dir, null); + nbts.parallelStream().forEach((file) -> { + DataFixerAPI.LOGGER.info("Loading NBT " + file); + try { + CompoundTag root = NbtIo.readCompressed(file); + boolean[] changed = {false}; + if (root.contains("palette")){ + ListTag items = root.getList("palette", Tag.TAG_COMPOUND); + items.forEach(inTag -> { + CompoundTag tag = (CompoundTag)inTag; + changed[0] |= profile.replaceStringFromIDs(tag, "Name"); + }); + } + + if (changed[0]){ + DataFixerAPI.LOGGER.info("Writing NBT " + file); + NbtIo.writeCompressed(root, file); + } + } + catch (IOException e) { + e.printStackTrace(); + } + }); + } + + private static List getAllNbts(File dir, List list) { + if (list == null) { + list = new ArrayList<>(); + } + for (File file : dir.listFiles()) { + if (file.isDirectory()) { + getAllNbts(file, list); + } else if (file.isFile() && file.getName().endsWith(".nbt")) { + list.add(file); + } + } + return list; + } + final public CompoundTag getLevelDat(File levelBaseDir){ if (level == null || this.levelBaseDir==null || !this.levelBaseDir.equals(levelBaseDir)){ runPrePatches(levelBaseDir); @@ -125,12 +173,13 @@ public class MigrationProfile { final public void markApplied() { for (String modID : mods) { DataFixerAPI.LOGGER.info("Updating Patch-Level for '{}' from {} to {}", modID, ModUtil.convertModVersion(currentPatchLevel(modID)), ModUtil.convertModVersion(Patch.maxPatchLevel(modID))); - config.putString(modID, Patch.maxPatchVersion(modID)); + if (config!=null) + config.putString(modID, Patch.maxPatchVersion(modID)); } } public String currentPatchVersion(@NotNull String modID) { - if (!config.contains(modID)) return "0.0.0"; + if (config==null || !config.contains(modID)) return "0.0.0"; return config.getString(modID); } diff --git a/src/main/java/ru/bclib/api/datafixer/Patch.java b/src/main/java/ru/bclib/api/datafixer/Patch.java index 4b742f5a..fe025d9e 100644 --- a/src/main/java/ru/bclib/api/datafixer/Patch.java +++ b/src/main/java/ru/bclib/api/datafixer/Patch.java @@ -151,12 +151,20 @@ public abstract class Patch { *

* A {@link #Patch} with a given {@link #level} is only included if the patch-level of the * world is less - * @param config The current patch-level configuration - * @param levelBaseDir The location of the level + * @param config The current patch-level configuration* * @return a new {@link MigrationProfile} Object. */ static MigrationProfile createMigrationData(CompoundTag config) { - return new MigrationProfile(config); + return new MigrationProfile(config, false); + } + + /** + * This method is supposed to be used by developers to apply id-patches to custom nbt structures. It is only + * available in Developer-Mode + * + */ + static MigrationProfile createMigrationData() { + return new MigrationProfile(null, true); } /**