Added Function that allows developers to manually fix nbts

This commit is contained in:
Frank 2021-10-19 09:58:41 +02:00
parent bd4e61f14f
commit da0d122166
3 changed files with 65 additions and 8 deletions

View file

@ -463,7 +463,7 @@ public class DataFixerAPI {
e.printStackTrace(); e.printStackTrace();
} }
} }
static CompoundTag patchConfTag = null; static CompoundTag patchConfTag = null;
static CompoundTag getPatchData(){ static CompoundTag getPatchData(){
if (patchConfTag==null) { if (patchConfTag==null) {

View file

@ -12,6 +12,7 @@ import ru.bclib.util.ModUtil;
import java.io.File; import java.io.File;
import java.io.IOException; import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections; import java.util.Collections;
import java.util.HashMap; import java.util.HashMap;
import java.util.LinkedList; import java.util.LinkedList;
@ -34,7 +35,7 @@ public class MigrationProfile {
private boolean didRunPrePatch; private boolean didRunPrePatch;
private Exception prePatchException; private Exception prePatchException;
MigrationProfile(CompoundTag config) { MigrationProfile(CompoundTag config, boolean applyAll) {
this.config = config; this.config = config;
this.mods = Collections.unmodifiableSet(Patch.getALL() this.mods = Collections.unmodifiableSet(Patch.getALL()
@ -55,7 +56,7 @@ public class MigrationProfile {
List<String> paths = patch.getWorldDataIDPaths(); List<String> paths = patch.getWorldDataIDPaths();
if (paths!=null) worldDataIDPaths.put(modID, paths); if (paths!=null) worldDataIDPaths.put(modID, paths);
if (currentPatchLevel(modID) < patch.level) { if (applyAll || currentPatchLevel(modID) < patch.level) {
replacements.putAll(patch.getIDReplacements()); replacements.putAll(patch.getIDReplacements());
if (patch.getLevelDatPatcher()!=null) if (patch.getLevelDatPatcher()!=null)
levelPatches.add(patch.getLevelDatPatcher()); levelPatches.add(patch.getLevelDatPatcher());
@ -75,6 +76,53 @@ public class MigrationProfile {
this.worldDataPatchers = Collections.unmodifiableList(worldDataPatches); 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<File> 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<File> getAllNbts(File dir, List<File> 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){ final public CompoundTag getLevelDat(File levelBaseDir){
if (level == null || this.levelBaseDir==null || !this.levelBaseDir.equals(levelBaseDir)){ if (level == null || this.levelBaseDir==null || !this.levelBaseDir.equals(levelBaseDir)){
runPrePatches(levelBaseDir); runPrePatches(levelBaseDir);
@ -125,12 +173,13 @@ public class MigrationProfile {
final public void markApplied() { final public void markApplied() {
for (String modID : mods) { for (String modID : mods) {
DataFixerAPI.LOGGER.info("Updating Patch-Level for '{}' from {} to {}", modID, ModUtil.convertModVersion(currentPatchLevel(modID)), ModUtil.convertModVersion(Patch.maxPatchLevel(modID))); 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) { 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); return config.getString(modID);
} }

View file

@ -151,12 +151,20 @@ public abstract class Patch {
* <p> * <p>
* A {@link #Patch} with a given {@link #level} is only included if the patch-level of the * A {@link #Patch} with a given {@link #level} is only included if the patch-level of the
* world is less * world is less
* @param config The current patch-level configuration * @param config The current patch-level configuration*
* @param levelBaseDir The location of the level
* @return a new {@link MigrationProfile} Object. * @return a new {@link MigrationProfile} Object.
*/ */
static MigrationProfile createMigrationData(CompoundTag config) { 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);
} }
/** /**