From c93c271bd493c6c6d42102a50d62932fa78a4291 Mon Sep 17 00:00:00 2001 From: Frank Bauer Date: Mon, 26 Jul 2021 12:27:44 +0200 Subject: [PATCH] Added Patch-Function for *level.dat * --- .../ru/bclib/api/datafixer/DataFixerAPI.java | 35 +++++++++++++------ .../bclib/api/datafixer/MigrationProfile.java | 18 +++++++++- .../java/ru/bclib/api/datafixer/Patch.java | 14 ++++++++ .../api/datafixer/PatchDidiFailException.java | 10 ++++++ .../ru/bclib/api/datafixer/PatchFunction.java | 6 ++++ 5 files changed, 71 insertions(+), 12 deletions(-) create mode 100644 src/main/java/ru/bclib/api/datafixer/PatchDidiFailException.java create mode 100644 src/main/java/ru/bclib/api/datafixer/PatchFunction.java diff --git a/src/main/java/ru/bclib/api/datafixer/DataFixerAPI.java b/src/main/java/ru/bclib/api/datafixer/DataFixerAPI.java index ff7add7d..3f0abcc7 100644 --- a/src/main/java/ru/bclib/api/datafixer/DataFixerAPI.java +++ b/src/main/java/ru/bclib/api/datafixer/DataFixerAPI.java @@ -22,6 +22,9 @@ import java.util.regex.Pattern; public class DataFixerAPI { static final Logger LOGGER = new Logger("DataFixerAPI"); + static class State { + public boolean didFail = false; + } public static void fixData(File dir) { if (!Configs.MAIN_CONFIG.getBoolean(Configs.MAIN_PATCH_CATEGORY, "applyPatches", true)) { @@ -36,22 +39,22 @@ public class DataFixerAPI { return; } - boolean[] allOk = {true}; + State state = new State(); List regions = getAllRegions(dir, null); - regions.parallelStream().forEach((file) -> fixRegion(data, allOk, file)); + regions.parallelStream().forEach((file) -> fixRegion(data, state, file)); List players = getAllPlayers(dir); - players.parallelStream().forEach((file) -> fixPlayer(data, allOk, file)); + players.parallelStream().forEach((file) -> fixPlayer(data, state, file)); - fixLevel(data, allOk, new File(dir, "level.dat")); + fixLevel(data, state, new File(dir, "level.dat")); - if (allOk[0]) { + if (!state.didFail) { data.markApplied(); WorldDataAPI.saveFile(BCLib.MOD_ID); } } - private static void fixLevel(MigrationProfile data, boolean[] allOk, File file) { + private static void fixLevel(MigrationProfile data, State state, File file) { try { LOGGER.info("Inspecting " + file); CompoundTag level = NbtIo.readCompressed(file); @@ -64,6 +67,13 @@ public class DataFixerAPI { fixPlayerNbt(player, changed, data); } } + + try { + changed[0] |= data.patchLevelDat(level); + } catch (PatchDidiFailException e){ + state.didFail = true; + BCLib.LOGGER.error(e.getMessage()); + } if (changed[0]) { LOGGER.warning("Writing '{}'", file); @@ -71,12 +81,13 @@ public class DataFixerAPI { } } catch (Exception e) { - allOk[0] = false; + BCLib.LOGGER.error("Failed fixing Level-Data."); + state.didFail = true; e.printStackTrace(); } } - private static void fixPlayer(MigrationProfile data, boolean[] allOk, File file) { + private static void fixPlayer(MigrationProfile data, State state, File file) { try { LOGGER.info("Inspecting " + file); CompoundTag player = NbtIo.readCompressed(file); @@ -89,7 +100,8 @@ public class DataFixerAPI { } } catch (Exception e) { - allOk[0] = false; + BCLib.LOGGER.error("Failed fixing Player-Data."); + state.didFail = true; e.printStackTrace(); } } @@ -104,7 +116,7 @@ public class DataFixerAPI { fixInventory(enderitems, changed, data, true); } - private static void fixRegion(MigrationProfile data, boolean[] allOk, File file) { + private static void fixRegion(MigrationProfile data, State state, File file) { try { LOGGER.info("Inspecting " + file); boolean[] changed = new boolean[1]; @@ -155,7 +167,8 @@ public class DataFixerAPI { region.close(); } catch (Exception e) { - allOk[0] = false; + BCLib.LOGGER.error("Failed fixing Player Data."); + state.didFail = true; e.printStackTrace(); } } diff --git a/src/main/java/ru/bclib/api/datafixer/MigrationProfile.java b/src/main/java/ru/bclib/api/datafixer/MigrationProfile.java index 644b9d6c..8ad695b6 100644 --- a/src/main/java/ru/bclib/api/datafixer/MigrationProfile.java +++ b/src/main/java/ru/bclib/api/datafixer/MigrationProfile.java @@ -5,6 +5,8 @@ import org.jetbrains.annotations.NotNull; import java.util.Collections; import java.util.HashMap; +import java.util.LinkedList; +import java.util.List; import java.util.Map; import java.util.Set; import java.util.stream.Collectors; @@ -12,6 +14,8 @@ import java.util.stream.Collectors; class MigrationProfile { final Set mods; final Map idReplacements; + final List> levelPatchers; + private final CompoundTag config; MigrationProfile(CompoundTag config) { @@ -23,6 +27,7 @@ class MigrationProfile { .collect(Collectors.toSet())); HashMap replacements = new HashMap(); + List> levelPatches = new LinkedList<>(); for (String modID : mods) { Patch.getALL() .stream() @@ -30,6 +35,8 @@ class MigrationProfile { .forEach(patch -> { if (currentPatchLevel(modID) < patch.level) { replacements.putAll(patch.getIDReplacements()); + if (patch.getLevelDatPatcher()!=null) + levelPatches.add(patch.getLevelDatPatcher()); DataFixerAPI.LOGGER.info("Applying " + patch); } else { @@ -39,6 +46,7 @@ class MigrationProfile { } this.idReplacements = Collections.unmodifiableMap(replacements); + this.levelPatchers = Collections.unmodifiableList(levelPatches); } final public void markApplied() { @@ -58,7 +66,7 @@ class MigrationProfile { } public boolean hasAnyFixes() { - return idReplacements.size() > 0; + return idReplacements.size() > 0 || levelPatchers.size() > 0; } public boolean replaceStringFromIDs(@NotNull CompoundTag tag, @NotNull String key) { @@ -75,4 +83,12 @@ class MigrationProfile { return false; } + + public boolean patchLevelDat(@NotNull CompoundTag level) throws PatchDidiFailException { + boolean changed = false; + for (PatchFunction f : levelPatchers) { + changed |= f.apply(level); + } + return changed; + } } diff --git a/src/main/java/ru/bclib/api/datafixer/Patch.java b/src/main/java/ru/bclib/api/datafixer/Patch.java index 1be18145..30db9b4c 100644 --- a/src/main/java/ru/bclib/api/datafixer/Patch.java +++ b/src/main/java/ru/bclib/api/datafixer/Patch.java @@ -9,6 +9,7 @@ import java.util.List; import java.util.Map; public abstract class Patch { + private static List ALL = new ArrayList<>(10); /** @@ -119,6 +120,19 @@ public abstract class Patch { return new HashMap(); } + /** + * Return a {@link PatchFunction} that is called with the content of level.dat. + *

+ * The function needs to return {@code true}, if changes were made to the data. + * If an error occurs, the method shoudl throw a {@link PatchDidiFailException} + * + * The default implementation of this method returns null. + * + * @return The returned function is called a {@code CompoundTag} that contains the + * contents of level.dat + */ + public PatchFunction getLevelDatPatcher() { return null; } + /** * Generates ready to use data for all currently registered patches. The list of * patches is selected by the current patch-level of the world. diff --git a/src/main/java/ru/bclib/api/datafixer/PatchDidiFailException.java b/src/main/java/ru/bclib/api/datafixer/PatchDidiFailException.java new file mode 100644 index 00000000..737ba402 --- /dev/null +++ b/src/main/java/ru/bclib/api/datafixer/PatchDidiFailException.java @@ -0,0 +1,10 @@ +package ru.bclib.api.datafixer; + +public class PatchDidiFailException extends Exception { + public PatchDidiFailException(){ + super(); + } + public PatchDidiFailException(Exception e){ + super(e); + } +} diff --git a/src/main/java/ru/bclib/api/datafixer/PatchFunction.java b/src/main/java/ru/bclib/api/datafixer/PatchFunction.java new file mode 100644 index 00000000..77c70f6b --- /dev/null +++ b/src/main/java/ru/bclib/api/datafixer/PatchFunction.java @@ -0,0 +1,6 @@ +package ru.bclib.api.datafixer; + +@FunctionalInterface +public interface PatchFunction { + R apply(T t) throws PatchDidiFailException; +}