Added Patch-Function for *level.dat *
This commit is contained in:
parent
f853ac75d9
commit
c93c271bd4
5 changed files with 71 additions and 12 deletions
|
@ -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<File> regions = getAllRegions(dir, null);
|
||||
regions.parallelStream().forEach((file) -> fixRegion(data, allOk, file));
|
||||
regions.parallelStream().forEach((file) -> fixRegion(data, state, file));
|
||||
|
||||
List<File> 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();
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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<String> mods;
|
||||
final Map<String, String> idReplacements;
|
||||
final List<PatchFunction<CompoundTag, Boolean>> levelPatchers;
|
||||
|
||||
private final CompoundTag config;
|
||||
|
||||
MigrationProfile(CompoundTag config) {
|
||||
|
@ -23,6 +27,7 @@ class MigrationProfile {
|
|||
.collect(Collectors.toSet()));
|
||||
|
||||
HashMap<String, String> replacements = new HashMap<String, String>();
|
||||
List<PatchFunction<CompoundTag, Boolean>> 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<CompoundTag, Boolean> f : levelPatchers) {
|
||||
changed |= f.apply(level);
|
||||
}
|
||||
return changed;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -9,6 +9,7 @@ import java.util.List;
|
|||
import java.util.Map;
|
||||
|
||||
public abstract class Patch {
|
||||
|
||||
private static List<Patch> ALL = new ArrayList<>(10);
|
||||
|
||||
/**
|
||||
|
@ -119,6 +120,19 @@ public abstract class Patch {
|
|||
return new HashMap<String, String>();
|
||||
}
|
||||
|
||||
/**
|
||||
* Return a {@link PatchFunction} that is called with the content of <i>level.dat</i>.
|
||||
* <p>
|
||||
* 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 <i>level.dat</i>
|
||||
*/
|
||||
public PatchFunction<CompoundTag, Boolean> 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.
|
||||
|
|
|
@ -0,0 +1,10 @@
|
|||
package ru.bclib.api.datafixer;
|
||||
|
||||
public class PatchDidiFailException extends Exception {
|
||||
public PatchDidiFailException(){
|
||||
super();
|
||||
}
|
||||
public PatchDidiFailException(Exception e){
|
||||
super(e);
|
||||
}
|
||||
}
|
6
src/main/java/ru/bclib/api/datafixer/PatchFunction.java
Normal file
6
src/main/java/ru/bclib/api/datafixer/PatchFunction.java
Normal file
|
@ -0,0 +1,6 @@
|
|||
package ru.bclib.api.datafixer;
|
||||
|
||||
@FunctionalInterface
|
||||
public interface PatchFunction<T, R> {
|
||||
R apply(T t) throws PatchDidiFailException;
|
||||
}
|
Loading…
Reference in a new issue