Handle Player and Level data in Fixer
This commit is contained in:
parent
c64c6eca7b
commit
9e42fe7294
1 changed files with 109 additions and 19 deletions
|
@ -40,12 +40,69 @@ public class DataFixerAPI {
|
||||||
|
|
||||||
List<File> regions = getAllRegions(dir, null);
|
List<File> regions = getAllRegions(dir, null);
|
||||||
regions.parallelStream().forEach((file) -> fixRegion(data, allOk, file));
|
regions.parallelStream().forEach((file) -> fixRegion(data, allOk, file));
|
||||||
|
|
||||||
|
List<File> players = getAllPlayers(dir);
|
||||||
|
players.parallelStream().forEach((file) -> fixPlayer(data, allOk, file));
|
||||||
|
|
||||||
|
fixLevel(data, allOk, new File(dir, "level.dat"));
|
||||||
|
|
||||||
if (allOk[0]) {
|
if (allOk[0]) {
|
||||||
data.markApplied();
|
data.markApplied();
|
||||||
WorldDataAPI.saveFile(BCLib.MOD_ID);
|
WorldDataAPI.saveFile(BCLib.MOD_ID);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
private static void fixLevel(MigrationProfile data, boolean[] allOk, File file) {
|
||||||
|
try {
|
||||||
|
LOGGER.info("Inspecting " + file);
|
||||||
|
CompoundTag level = NbtIo.readCompressed(file);
|
||||||
|
boolean[] changed = { false };
|
||||||
|
|
||||||
|
if (level.contains("Data")) {
|
||||||
|
CompoundTag dataTag = (CompoundTag)level.get("Data");
|
||||||
|
if (dataTag.contains("Player")) {
|
||||||
|
CompoundTag player = (CompoundTag)dataTag.get("Player");
|
||||||
|
fixPlayerNbt(player, changed, data);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (changed[0]) {
|
||||||
|
LOGGER.warning("Writing '{}'", file);
|
||||||
|
NbtIo.writeCompressed(level, file);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
catch (Exception e) {
|
||||||
|
allOk[0] = false;
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private static void fixPlayer(MigrationProfile data, boolean[] allOk, File file) {
|
||||||
|
try {
|
||||||
|
LOGGER.info("Inspecting " + file);
|
||||||
|
CompoundTag player = NbtIo.readCompressed(file);
|
||||||
|
boolean[] changed = { false };
|
||||||
|
fixPlayerNbt(player, changed, data);
|
||||||
|
|
||||||
|
if (changed[0]) {
|
||||||
|
LOGGER.warning("Writing '{}'", file);
|
||||||
|
NbtIo.writeCompressed(player, file);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
catch (Exception e) {
|
||||||
|
allOk[0] = false;
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private static void fixPlayerNbt(CompoundTag player, boolean[] changed, MigrationProfile data) {
|
||||||
|
//Checking Inventory
|
||||||
|
ListTag inventory = player.getList("Inventory", 10);
|
||||||
|
fixInventory(inventory, changed, data, true);
|
||||||
|
|
||||||
|
//Checking EnderChest
|
||||||
|
ListTag enderitems = player.getList("EnderItems", 10);
|
||||||
|
fixInventory(enderitems, changed, data, true);
|
||||||
|
}
|
||||||
|
|
||||||
private static void fixRegion(MigrationProfile data, boolean[] allOk, File file) {
|
private static void fixRegion(MigrationProfile data, boolean[] allOk, File file) {
|
||||||
try {
|
try {
|
||||||
|
@ -59,9 +116,9 @@ public class DataFixerAPI {
|
||||||
if (region.hasChunk(pos)) {
|
if (region.hasChunk(pos)) {
|
||||||
DataInputStream input = region.getChunkDataInputStream(pos);
|
DataInputStream input = region.getChunkDataInputStream(pos);
|
||||||
CompoundTag root = NbtIo.read(input);
|
CompoundTag root = NbtIo.read(input);
|
||||||
if ((root.toString().contains("betternether:chest") || root.toString().contains("bclib:chest"))) {
|
// if ((root.toString().contains("betternether:chest") || root.toString().contains("bclib:chest"))) {
|
||||||
NbtIo.write(root, new File(file.toString() + "-" + x + "-" + z + ".nbt"));
|
// NbtIo.write(root, new File(file.toString() + "-" + x + "-" + z + ".nbt"));
|
||||||
}
|
// }
|
||||||
input.close();
|
input.close();
|
||||||
|
|
||||||
//Checking TileEntities
|
//Checking TileEntities
|
||||||
|
@ -73,7 +130,6 @@ public class DataFixerAPI {
|
||||||
ListTag entities = root.getList("Entities", 10);
|
ListTag entities = root.getList("Entities", 10);
|
||||||
fixItemArrayWithID(entities, changed, data, true);
|
fixItemArrayWithID(entities, changed, data, true);
|
||||||
|
|
||||||
|
|
||||||
//Checking Block Palette
|
//Checking Block Palette
|
||||||
ListTag sections = root.getCompound("Level")
|
ListTag sections = root.getCompound("Level")
|
||||||
.getList("Sections", 10);
|
.getList("Sections", 10);
|
||||||
|
@ -87,7 +143,7 @@ public class DataFixerAPI {
|
||||||
|
|
||||||
if (changed[0]) {
|
if (changed[0]) {
|
||||||
LOGGER.warning("Writing '{}': {}/{}", file, x, z);
|
LOGGER.warning("Writing '{}': {}/{}", file, x, z);
|
||||||
NbtIo.write(root, new File(file.toString() + "-" + x + "-" + z + "-changed.nbt"));
|
// NbtIo.write(root, new File(file.toString() + "-" + x + "-" + z + "-changed.nbt"));
|
||||||
DataOutputStream output = region.getChunkDataOutputStream(pos);
|
DataOutputStream output = region.getChunkDataOutputStream(pos);
|
||||||
NbtIo.write(root, output);
|
NbtIo.write(root, output);
|
||||||
output.close();
|
output.close();
|
||||||
|
@ -111,22 +167,56 @@ public class DataFixerAPI {
|
||||||
}
|
}
|
||||||
return patchConfTag;
|
return patchConfTag;
|
||||||
}
|
}
|
||||||
|
|
||||||
private static boolean fixItemArrayWithID(ListTag items, boolean[] changed, MigrationProfile data, boolean recursive) {
|
private static void fixInventory(ListTag inventory, boolean[] changed, MigrationProfile data, boolean recursive) {
|
||||||
items.forEach(inTag -> {
|
inventory.forEach(item -> {
|
||||||
final CompoundTag tag = (CompoundTag) inTag;
|
changed[0] |= data.replaceStringFromIDs((CompoundTag)item, "id");
|
||||||
|
|
||||||
changed[0] |= data.replaceStringFromIDs(tag, "id");
|
if (((CompoundTag) item).contains("tag")) {
|
||||||
|
CompoundTag tag = (CompoundTag)((CompoundTag) item).get("tag");
|
||||||
if (recursive && tag.contains("Items")) {
|
if (tag.contains("BlockEntityTag")){
|
||||||
changed[0] |= fixItemArrayWithID(tag.getList("Items", 10), changed, data, true);
|
CompoundTag blockEntityTag = (CompoundTag)((CompoundTag) tag).get("BlockEntityTag");
|
||||||
}
|
ListTag items = blockEntityTag.getList("Items", 10);
|
||||||
if (recursive && tag.contains("Inventory")) {
|
fixItemArrayWithID(items, changed, data, recursive);
|
||||||
changed[0] |= fixItemArrayWithID(tag.getList("Inventory", 10), changed, data, true);
|
}
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
}
|
||||||
return changed[0];
|
|
||||||
|
private static void fixItemArrayWithID(ListTag items, boolean[] changed, MigrationProfile data, boolean recursive) {
|
||||||
|
items.forEach(inTag -> {
|
||||||
|
fixID((CompoundTag) inTag, changed, data, recursive);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
private static void fixID(CompoundTag inTag, boolean[] changed, MigrationProfile data, boolean recursive) {
|
||||||
|
final CompoundTag tag = inTag;
|
||||||
|
|
||||||
|
changed[0] |= data.replaceStringFromIDs(tag, "id");
|
||||||
|
if (tag.contains("Item")) {
|
||||||
|
CompoundTag item = (CompoundTag)tag.get("Item");
|
||||||
|
fixID(item, changed, data, recursive);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (recursive && tag.contains("Items")) {
|
||||||
|
fixItemArrayWithID(tag.getList("Items", 10), changed, data, true);
|
||||||
|
}
|
||||||
|
if (recursive && tag.contains("Inventory")) {
|
||||||
|
ListTag inventory = tag.getList("Inventory", 10);
|
||||||
|
fixInventory(inventory, changed, data, recursive);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private static List<File> getAllPlayers(File dir) {
|
||||||
|
List<File> list = new ArrayList<>();
|
||||||
|
dir = new File(dir, "playerdata");
|
||||||
|
for (File file : dir.listFiles()) {
|
||||||
|
if (file.isFile() && file.getName().endsWith(".dat")) {
|
||||||
|
list.add(file);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return list;
|
||||||
}
|
}
|
||||||
|
|
||||||
private static List<File> getAllRegions(File dir, List<File> list) {
|
private static List<File> getAllRegions(File dir, List<File> list) {
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue