Merge remote-tracking branch 'origin/main'

This commit is contained in:
paulevsGitch 2021-10-25 16:20:15 +03:00
commit 9256faf0ba
4 changed files with 48 additions and 3 deletions

View file

@ -416,7 +416,7 @@ public class DataFixerAPI {
for (int z = 0; z < 32; z++) {
ChunkPos pos = new ChunkPos(x, z);
changed[0] = false;
if (region.hasChunk(pos)) {
if (region.hasChunk(pos) && !state.didFail) {
DataInputStream input = region.getChunkDataInputStream(pos);
CompoundTag root = NbtIo.read(input);
// if ((root.toString().contains("betternether:chest") || root.toString().contains("bclib:chest"))) {
@ -442,6 +442,16 @@ public class DataFixerAPI {
CompoundTag blockTagCompound = ((CompoundTag) blockTag);
changed[0] |= data.replaceStringFromIDs(blockTagCompound, "Name");
});
try {
changed[0] |= data.patchBlockState(palette, ((CompoundTag) tag).getList("BlockStates", Tag.TAG_LONG));
}
catch (PatchDidiFailException e) {
BCLib.LOGGER.error("Failed fixing BlockState in " + pos);
state.didFail = true;
changed[0] = false;
e.printStackTrace();
}
});
if (changed[0]) {
@ -450,7 +460,6 @@ public class DataFixerAPI {
DataOutputStream output = region.getChunkDataOutputStream(pos);
NbtIo.write(root, output);
output.close();
}
}
}
@ -458,7 +467,7 @@ public class DataFixerAPI {
region.close();
}
catch (Exception e) {
BCLib.LOGGER.error("Failed fixing Player Data.");
BCLib.LOGGER.error("Failed fixing Region.");
state.didFail = true;
e.printStackTrace();
}

View file

@ -25,6 +25,7 @@ public class MigrationProfile {
final Set<String> mods;
final Map<String, String> idReplacements;
final List<PatchFunction<CompoundTag, Boolean>> levelPatchers;
final List<PatchBiFunction<ListTag, ListTag, Boolean>> statePatchers;
final List<Patch> worldDataPatchers;
final Map<String, List<String>> worldDataIDPaths;
@ -46,6 +47,7 @@ public class MigrationProfile {
HashMap<String, String> replacements = new HashMap<String, String>();
List<PatchFunction<CompoundTag, Boolean>> levelPatches = new LinkedList<>();
List<Patch> worldDataPatches = new LinkedList<>();
List<PatchBiFunction<ListTag, ListTag, Boolean>> statePatches = new LinkedList<>();
HashMap<String, List<String>> worldDataIDPaths = new HashMap<>();
for (String modID : mods) {
@ -62,6 +64,8 @@ public class MigrationProfile {
levelPatches.add(patch.getLevelDatPatcher());
if (patch.getWorldDataPatcher()!=null)
worldDataPatches.add(patch);
if (patch.getBlockStatePatcher()!=null)
statePatches.add(patch.getBlockStatePatcher());
DataFixerAPI.LOGGER.info("Applying " + patch);
}
else {
@ -74,6 +78,7 @@ public class MigrationProfile {
this.idReplacements = Collections.unmodifiableMap(replacements);
this.levelPatchers = Collections.unmodifiableList(levelPatches);
this.worldDataPatchers = Collections.unmodifiableList(worldDataPatches);
this.statePatchers = Collections.unmodifiableList(statePatches);
}
/**
@ -305,4 +310,12 @@ public class MigrationProfile {
}
}
}
public boolean patchBlockState(ListTag palette, ListTag states) throws PatchDidiFailException{
boolean changed = false;
for (PatchBiFunction<ListTag, ListTag, Boolean> f : statePatchers) {
changed |= f.apply(palette, states, this);
}
return changed;
}
}

View file

@ -1,6 +1,7 @@
package ru.bclib.api.datafixer;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.nbt.ListTag;
import org.jetbrains.annotations.NotNull;
import ru.bclib.util.ModUtil;
@ -145,6 +146,22 @@ public abstract class Patch {
*/
public PatchFunction<CompoundTag, Boolean> getWorldDataPatcher() { return null; }
/**
* Return a {@link PatchBiFunction} that is called with pallette and blockstate of
* each chunk in every region. This method is called AFTER all ID replacements
* from {@link #getIDReplacements()} were applied to the pallete.
*
* The first parameter is the palette and the second is the blockstate.
*
* 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 PatchBiFunction<ListTag, ListTag, Boolean> getBlockStatePatcher() { 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.

View file

@ -0,0 +1,6 @@
package ru.bclib.api.datafixer;
@FunctionalInterface
public interface PatchBiFunction<U, V, R> {
R apply(U t, V v, MigrationProfile profile) throws PatchDidiFailException;
}