From 9e42fe72944a294a6708117137641fb5970d457e Mon Sep 17 00:00:00 2001 From: Frank Date: Thu, 22 Jul 2021 13:17:10 +0200 Subject: [PATCH] Handle Player and Level data in Fixer --- .../ru/bclib/api/datafixer/DataFixerAPI.java | 128 +++++++++++++++--- 1 file changed, 109 insertions(+), 19 deletions(-) diff --git a/src/main/java/ru/bclib/api/datafixer/DataFixerAPI.java b/src/main/java/ru/bclib/api/datafixer/DataFixerAPI.java index 0d8a8ccb..ff7add7d 100644 --- a/src/main/java/ru/bclib/api/datafixer/DataFixerAPI.java +++ b/src/main/java/ru/bclib/api/datafixer/DataFixerAPI.java @@ -40,12 +40,69 @@ public class DataFixerAPI { List regions = getAllRegions(dir, null); regions.parallelStream().forEach((file) -> fixRegion(data, allOk, file)); + + List players = getAllPlayers(dir); + players.parallelStream().forEach((file) -> fixPlayer(data, allOk, file)); + + fixLevel(data, allOk, new File(dir, "level.dat")); if (allOk[0]) { data.markApplied(); 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) { try { @@ -59,9 +116,9 @@ public class DataFixerAPI { if (region.hasChunk(pos)) { DataInputStream input = region.getChunkDataInputStream(pos); CompoundTag root = NbtIo.read(input); - if ((root.toString().contains("betternether:chest") || root.toString().contains("bclib:chest"))) { - NbtIo.write(root, new File(file.toString() + "-" + x + "-" + z + ".nbt")); - } + // if ((root.toString().contains("betternether:chest") || root.toString().contains("bclib:chest"))) { + // NbtIo.write(root, new File(file.toString() + "-" + x + "-" + z + ".nbt")); + // } input.close(); //Checking TileEntities @@ -73,7 +130,6 @@ public class DataFixerAPI { ListTag entities = root.getList("Entities", 10); fixItemArrayWithID(entities, changed, data, true); - //Checking Block Palette ListTag sections = root.getCompound("Level") .getList("Sections", 10); @@ -87,7 +143,7 @@ public class DataFixerAPI { if (changed[0]) { 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); NbtIo.write(root, output); output.close(); @@ -111,22 +167,56 @@ public class DataFixerAPI { } return patchConfTag; } - - private static boolean fixItemArrayWithID(ListTag items, boolean[] changed, MigrationProfile data, boolean recursive) { - items.forEach(inTag -> { - final CompoundTag tag = (CompoundTag) inTag; - - changed[0] |= data.replaceStringFromIDs(tag, "id"); - - if (recursive && tag.contains("Items")) { - changed[0] |= fixItemArrayWithID(tag.getList("Items", 10), changed, data, true); - } - if (recursive && tag.contains("Inventory")) { - changed[0] |= fixItemArrayWithID(tag.getList("Inventory", 10), changed, data, true); + + private static void fixInventory(ListTag inventory, boolean[] changed, MigrationProfile data, boolean recursive) { + inventory.forEach(item -> { + changed[0] |= data.replaceStringFromIDs((CompoundTag)item, "id"); + + if (((CompoundTag) item).contains("tag")) { + CompoundTag tag = (CompoundTag)((CompoundTag) item).get("tag"); + if (tag.contains("BlockEntityTag")){ + CompoundTag blockEntityTag = (CompoundTag)((CompoundTag) tag).get("BlockEntityTag"); + ListTag items = blockEntityTag.getList("Items", 10); + fixItemArrayWithID(items, changed, data, recursive); + } } }); - - 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 getAllPlayers(File dir) { + List 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 getAllRegions(File dir, List list) {