Call DataFixer on WorldData
This commit is contained in:
parent
2ae840634a
commit
589151af81
4 changed files with 41 additions and 7 deletions
|
@ -254,6 +254,13 @@ public class DataFixerAPI {
|
||||||
players.parallelStream().forEach((file) -> fixPlayer(profile, state, file));
|
players.parallelStream().forEach((file) -> fixPlayer(profile, state, file));
|
||||||
|
|
||||||
fixLevel(profile, state, new File(dir, "level.dat"));
|
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) {
|
if (!state.didFail) {
|
||||||
profile.markApplied();
|
profile.markApplied();
|
||||||
|
|
|
@ -2,6 +2,7 @@ package ru.bclib.api.datafixer;
|
||||||
|
|
||||||
import net.minecraft.nbt.CompoundTag;
|
import net.minecraft.nbt.CompoundTag;
|
||||||
import org.jetbrains.annotations.NotNull;
|
import org.jetbrains.annotations.NotNull;
|
||||||
|
import ru.bclib.api.WorldDataAPI;
|
||||||
|
|
||||||
import java.util.Collections;
|
import java.util.Collections;
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
|
@ -11,10 +12,11 @@ import java.util.Map;
|
||||||
import java.util.Set;
|
import java.util.Set;
|
||||||
import java.util.stream.Collectors;
|
import java.util.stream.Collectors;
|
||||||
|
|
||||||
class MigrationProfile {
|
public class MigrationProfile {
|
||||||
final Set<String> mods;
|
final Set<String> mods;
|
||||||
final Map<String, String> idReplacements;
|
final Map<String, String> idReplacements;
|
||||||
final List<PatchFunction<CompoundTag, Boolean>> levelPatchers;
|
final List<PatchFunction<CompoundTag, Boolean>> levelPatchers;
|
||||||
|
final List<Patch> worldDataPatchers;
|
||||||
|
|
||||||
private final CompoundTag config;
|
private final CompoundTag config;
|
||||||
|
|
||||||
|
@ -28,6 +30,7 @@ class MigrationProfile {
|
||||||
|
|
||||||
HashMap<String, String> replacements = new HashMap<String, String>();
|
HashMap<String, String> replacements = new HashMap<String, String>();
|
||||||
List<PatchFunction<CompoundTag, Boolean>> levelPatches = new LinkedList<>();
|
List<PatchFunction<CompoundTag, Boolean>> levelPatches = new LinkedList<>();
|
||||||
|
List<Patch> worldDataPatches = new LinkedList<>();
|
||||||
for (String modID : mods) {
|
for (String modID : mods) {
|
||||||
Patch.getALL()
|
Patch.getALL()
|
||||||
.stream()
|
.stream()
|
||||||
|
@ -37,6 +40,8 @@ class MigrationProfile {
|
||||||
replacements.putAll(patch.getIDReplacements());
|
replacements.putAll(patch.getIDReplacements());
|
||||||
if (patch.getLevelDatPatcher()!=null)
|
if (patch.getLevelDatPatcher()!=null)
|
||||||
levelPatches.add(patch.getLevelDatPatcher());
|
levelPatches.add(patch.getLevelDatPatcher());
|
||||||
|
if (patch.getWorldDataPatcher()!=null)
|
||||||
|
worldDataPatches.add(patch);
|
||||||
DataFixerAPI.LOGGER.info("Applying " + patch);
|
DataFixerAPI.LOGGER.info("Applying " + patch);
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
|
@ -47,6 +52,7 @@ class MigrationProfile {
|
||||||
|
|
||||||
this.idReplacements = Collections.unmodifiableMap(replacements);
|
this.idReplacements = Collections.unmodifiableMap(replacements);
|
||||||
this.levelPatchers = Collections.unmodifiableList(levelPatches);
|
this.levelPatchers = Collections.unmodifiableList(levelPatches);
|
||||||
|
this.worldDataPatchers = Collections.unmodifiableList(worldDataPatches);
|
||||||
}
|
}
|
||||||
|
|
||||||
final public void markApplied() {
|
final public void markApplied() {
|
||||||
|
@ -66,7 +72,7 @@ class MigrationProfile {
|
||||||
}
|
}
|
||||||
|
|
||||||
public boolean hasAnyFixes() {
|
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) {
|
public boolean replaceStringFromIDs(@NotNull CompoundTag tag, @NotNull String key) {
|
||||||
|
@ -87,8 +93,18 @@ class MigrationProfile {
|
||||||
public boolean patchLevelDat(@NotNull CompoundTag level) throws PatchDidiFailException {
|
public boolean patchLevelDat(@NotNull CompoundTag level) throws PatchDidiFailException {
|
||||||
boolean changed = false;
|
boolean changed = false;
|
||||||
for (PatchFunction<CompoundTag, Boolean> f : levelPatchers) {
|
for (PatchFunction<CompoundTag, Boolean> f : levelPatchers) {
|
||||||
changed |= f.apply(level);
|
changed |= f.apply(level, this);
|
||||||
}
|
}
|
||||||
return changed;
|
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);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -124,14 +124,25 @@ public abstract class Patch {
|
||||||
* Return a {@link PatchFunction} that is called with the content of <i>level.dat</i>.
|
* Return a {@link PatchFunction} that is called with the content of <i>level.dat</i>.
|
||||||
* <p>
|
* <p>
|
||||||
* The function needs to return {@code true}, if changes were made to the data.
|
* 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.
|
* The default implementation of this method returns null.
|
||||||
*
|
*
|
||||||
* @return The returned function is called a {@code CompoundTag} that contains the
|
* @return {@code true} if changes were applied and we need to save the data
|
||||||
* contents of <i>level.dat</i>
|
|
||||||
*/
|
*/
|
||||||
public PatchFunction<CompoundTag, Boolean> getLevelDatPatcher() { return null; }
|
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
|
* Generates ready to use data for all currently registered patches. The list of
|
||||||
|
|
|
@ -2,5 +2,5 @@ package ru.bclib.api.datafixer;
|
||||||
|
|
||||||
@FunctionalInterface
|
@FunctionalInterface
|
||||||
public interface PatchFunction<T, R> {
|
public interface PatchFunction<T, R> {
|
||||||
R apply(T t) throws PatchDidiFailException;
|
R apply(T t, MigrationProfile profile) throws PatchDidiFailException;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue