Added API to fix id's on paths that can contain Lists/CompundTags

This commit is contained in:
Frank 2021-08-07 02:08:17 +02:00
parent b232c764e0
commit dffda00940
3 changed files with 54 additions and 2 deletions

View file

@ -323,11 +323,11 @@ public class DataFixerAPI {
private static void fixPlayerNbt(CompoundTag player, boolean[] changed, MigrationProfile data) { private static void fixPlayerNbt(CompoundTag player, boolean[] changed, MigrationProfile data) {
//Checking Inventory //Checking Inventory
ListTag inventory = player.getList("Inventory", 10); ListTag inventory = player.getList("Inventory", 10);
fixInventory(inventory, changed, data, true); fixItemArrayWithID(inventory, changed, data, true);
//Checking EnderChest //Checking EnderChest
ListTag enderitems = player.getList("EnderItems", 10); ListTag enderitems = player.getList("EnderItems", 10);
fixInventory(enderitems, changed, data, true); fixItemArrayWithID(enderitems, changed, data, true);
} }
private static void fixRegion(MigrationProfile data, State state, File file) { private static void fixRegion(MigrationProfile data, State state, File file) {

View file

@ -1,6 +1,8 @@
package ru.bclib.api.datafixer; package ru.bclib.api.datafixer;
import net.minecraft.nbt.CompoundTag; import net.minecraft.nbt.CompoundTag;
import net.minecraft.nbt.ListTag;
import net.minecraft.nbt.Tag;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
import ru.bclib.api.WorldDataAPI; import ru.bclib.api.WorldDataAPI;
@ -89,6 +91,46 @@ public class MigrationProfile {
return false; return false;
} }
private boolean replaceIDatPath(@NotNull ListTag list, @NotNull String[] parts, int level){
boolean[] changed = {false};
if (level == parts.length-1) {
DataFixerAPI.fixItemArrayWithID(list, changed, this, true);
} else {
list.forEach(inTag -> changed[0] |= replaceIDatPath((CompoundTag)inTag, parts, level));
}
return changed[0];
}
private boolean replaceIDatPath(@NotNull CompoundTag tag, @NotNull String[] parts, int level){
boolean changed = false;
for (int i=level; i<parts.length-1; i++) {
final String part = parts[i];
if (tag.contains(part)) {
final Tag subTag = tag.get(part);
if (subTag instanceof ListTag) {
ListTag list = (ListTag)subTag;
return replaceIDatPath(list, parts, i+1);
} else if (subTag instanceof CompoundTag) {
tag = (CompoundTag)tag;
}
} else {
return false;
}
}
if (tag!=null && parts.length>0) {
replaceStringFromIDs(tag, parts[parts.length-1]);
}
return false;
}
public boolean replaceIDatPath(@NotNull CompoundTag root, @NotNull String path){
String[] parts = path.split("\\.");
return replaceIDatPath(root, parts, 0);
}
public boolean patchLevelDat(@NotNull CompoundTag level) throws PatchDidiFailException { public boolean patchLevelDat(@NotNull CompoundTag level) throws PatchDidiFailException {
boolean changed = false; boolean changed = false;

View file

@ -156,5 +156,15 @@ public abstract class Patch {
static MigrationProfile createMigrationData(CompoundTag config) { static MigrationProfile createMigrationData(CompoundTag config) {
return new MigrationProfile(config); return new MigrationProfile(config);
} }
/**
* Returns a list of paths,where your mod stores IDs in your {@link ru.bclib.api.WorldDataAPI}-File.
* <p>
* {@link DataFixerAPI} will use information from the latest patch that returns a non-null-result.
* @return {@code null} if nothing changes or a list of Paths in your {@link ru.bclib.api.WorldDataAPI}-File
*/
public List<String> getIIDPathsInWorldDataAPI() {
return null;
}
} }