Call DataFixer on WorldData

This commit is contained in:
Frank 2021-08-05 18:43:44 +02:00
parent 2ae840634a
commit 589151af81
4 changed files with 41 additions and 7 deletions

View file

@ -254,6 +254,13 @@ public class DataFixerAPI {
players.parallelStream().forEach((file) -> fixPlayer(profile, state, file));
fixLevel(profile, state, new File(dir, "level.dat"));
try {
profile.patchWorldData();
} catch (PatchDidiFailException e){
state.didFail = true;
BCLib.LOGGER.error(e.getMessage());
}
if (!state.didFail) {
profile.markApplied();

View file

@ -2,6 +2,7 @@ package ru.bclib.api.datafixer;
import net.minecraft.nbt.CompoundTag;
import org.jetbrains.annotations.NotNull;
import ru.bclib.api.WorldDataAPI;
import java.util.Collections;
import java.util.HashMap;
@ -11,10 +12,11 @@ import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;
class MigrationProfile {
public class MigrationProfile {
final Set<String> mods;
final Map<String, String> idReplacements;
final List<PatchFunction<CompoundTag, Boolean>> levelPatchers;
final List<Patch> worldDataPatchers;
private final CompoundTag config;
@ -28,6 +30,7 @@ class MigrationProfile {
HashMap<String, String> replacements = new HashMap<String, String>();
List<PatchFunction<CompoundTag, Boolean>> levelPatches = new LinkedList<>();
List<Patch> worldDataPatches = new LinkedList<>();
for (String modID : mods) {
Patch.getALL()
.stream()
@ -37,6 +40,8 @@ class MigrationProfile {
replacements.putAll(patch.getIDReplacements());
if (patch.getLevelDatPatcher()!=null)
levelPatches.add(patch.getLevelDatPatcher());
if (patch.getWorldDataPatcher()!=null)
worldDataPatches.add(patch);
DataFixerAPI.LOGGER.info("Applying " + patch);
}
else {
@ -47,6 +52,7 @@ class MigrationProfile {
this.idReplacements = Collections.unmodifiableMap(replacements);
this.levelPatchers = Collections.unmodifiableList(levelPatches);
this.worldDataPatchers = Collections.unmodifiableList(worldDataPatches);
}
final public void markApplied() {
@ -66,7 +72,7 @@ class MigrationProfile {
}
public boolean hasAnyFixes() {
return idReplacements.size() > 0 || levelPatchers.size() > 0;
return idReplacements.size() > 0 || levelPatchers.size() > 0 || worldDataPatchers.size() > 0;
}
public boolean replaceStringFromIDs(@NotNull CompoundTag tag, @NotNull String key) {
@ -87,8 +93,18 @@ class MigrationProfile {
public boolean patchLevelDat(@NotNull CompoundTag level) throws PatchDidiFailException {
boolean changed = false;
for (PatchFunction<CompoundTag, Boolean> f : levelPatchers) {
changed |= f.apply(level);
changed |= f.apply(level, this);
}
return changed;
}
public void patchWorldData() throws PatchDidiFailException {
for (Patch patch : worldDataPatchers) {
CompoundTag root = WorldDataAPI.getRootTag(patch.modID);
boolean changed = patch.getWorldDataPatcher().apply(root, this);
if (changed) {
WorldDataAPI.saveFile(patch.modID);
}
}
}
}

View file

@ -124,14 +124,25 @@ public abstract class Patch {
* 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}
* If an error occurs, the method should 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>
* @return {@code true} if changes were applied and we need to save the data
*/
public PatchFunction<CompoundTag, Boolean> getLevelDatPatcher() { return null; }
/**
* Return a {@link PatchFunction} that is called with the content from the
* {@link ru.bclib.api.WorldDataAPI} for this Mod.
* The function needs to return {@code true}, if changes were made to the data.
* If an error occurs, the method should throw a {@link PatchDidiFailException}
*
* The default implementation of this method returns null.
*
* @return {@code true} if changes were applied and we need to save the data
*/
public PatchFunction<CompoundTag, Boolean> getWorldDataPatcher() { return null; }
/**
* Generates ready to use data for all currently registered patches. The list of

View file

@ -2,5 +2,5 @@ package ru.bclib.api.datafixer;
@FunctionalInterface
public interface PatchFunction<T, R> {
R apply(T t) throws PatchDidiFailException;
R apply(T t, MigrationProfile profile) throws PatchDidiFailException;
}