From cfa240892f4223805ea054652df7081e9e2d114b Mon Sep 17 00:00:00 2001 From: Frank Bauer Date: Tue, 20 Jul 2021 00:42:01 +0200 Subject: [PATCH] Removed original `DataFixerAPI` --- src/main/java/ru/bclib/api/DataFixerAPI.java | 152 ------------------- src/main/java/ru/bclib/api/WorldDataAPI.java | 1 + 2 files changed, 1 insertion(+), 152 deletions(-) delete mode 100644 src/main/java/ru/bclib/api/DataFixerAPI.java diff --git a/src/main/java/ru/bclib/api/DataFixerAPI.java b/src/main/java/ru/bclib/api/DataFixerAPI.java deleted file mode 100644 index e2eaff5b..00000000 --- a/src/main/java/ru/bclib/api/DataFixerAPI.java +++ /dev/null @@ -1,152 +0,0 @@ -package ru.bclib.api; - -import com.google.common.collect.Lists; -import com.google.common.collect.Maps; -import net.fabricmc.loader.api.FabricLoader; -import net.fabricmc.loader.api.ModContainer; -import net.minecraft.nbt.CompoundTag; -import net.minecraft.nbt.ListTag; -import net.minecraft.nbt.NbtIo; -import net.minecraft.world.level.ChunkPos; -import net.minecraft.world.level.chunk.storage.RegionFile; - -import java.io.DataInputStream; -import java.io.DataOutputStream; -import java.io.File; -import java.util.Collection; -import java.util.List; -import java.util.Locale; -import java.util.Map; - -public class DataFixerAPI { - private static final Map REPLACEMENT = Maps.newHashMap(); - private static final Map FIX_VERSIONS = Maps.newHashMap(); - - public static void fixData(File dir) { - REPLACEMENT.clear(); // API is not finished yet! - if (REPLACEMENT.isEmpty()) { - return; - } - - boolean shoudFix = false; - Collection mods = FabricLoader.getInstance().getAllMods(); - for (ModContainer mod : mods) { - String name = mod.getMetadata().getId(); - int preVersion = WorldDataAPI.getIntModVersion(name); - int version = getModVersion(mod.getMetadata().getVersion().toString()); - if (version > preVersion) { - int fixVersion = FIX_VERSIONS.getOrDefault(name, version); - shoudFix |= fixVersion < version && fixVersion >= preVersion; - } - } - ; - if (!shoudFix) { - return; - } - - List regions = getAllRegions(dir, null); - regions.parallelStream().forEach((file) -> { - try { - System.out.println("Fixing " + file); - boolean[] changed = new boolean[1]; - RegionFile region = new RegionFile(file, file.getParentFile(), true); - for (int x = 0; x < 32; x++) { - for (int z = 0; z < 32; z++) { - ChunkPos pos = new ChunkPos(x, z); - changed[0] = false; - if (region.hasChunk(pos)) { - DataInputStream input = region.getChunkDataInputStream(pos); - CompoundTag root = NbtIo.read(input); - input.close(); - ListTag sections = root.getCompound("Level").getList("Sections", 10); - sections.forEach((tag) -> { - ListTag palette = ((CompoundTag) tag).getList("Palette", 10); - palette.forEach((blockTag) -> { - CompoundTag blockTagCompound = ((CompoundTag) blockTag); - String name = blockTagCompound.getString("Name"); - String replace = REPLACEMENT.get(name); - if (replace != null) { - blockTagCompound.putString("Name", replace); - changed[0] = true; - } - }); - }); - if (changed[0]) { - System.out.println("Write!"); - DataOutputStream output = region.getChunkDataOutputStream(pos); - NbtIo.write(root, output); - output.close(); - } - } - } - } - region.close(); - } - catch (Exception e) { - e.printStackTrace(); - } - }); - } - - /** - * Register block data fix. Fix will be applied on world load if current mod version will be newer than specified one. - * - * @param modID - {@link String} mod id; - * @param modVersion - {@link String} mod version, should be in format: %d.%d.%d - * @param result - {@link String} new block name; - * @param names - array of {@link String}, old block names to convert. - */ - protected static void addFix(String modID, String modVersion, String result, String... names) { - FIX_VERSIONS.put(modID, getModVersion(modVersion)); - for (String name : names) { - REPLACEMENT.put(name, result); - } - } - - private static List getAllRegions(File dir, List list) { - if (list == null) { - list = Lists.newArrayList(); - } - for (File file : dir.listFiles()) { - if (file.isDirectory()) { - getAllRegions(file, list); - } - else if (file.isFile() && file.getName().endsWith(".mca")) { - list.add(file); - } - } - return list; - } - - /** - * Get mod version from string. String should be in format: %d.%d.%d - * - * @param version - {@link String} mod version. - * @return int mod version. - */ - public static int getModVersion(String version) { - if (version.isEmpty()) { - return 0; - } - try { - String[] values = version.split("\\."); - return Integer.parseInt(values[0]) << 12 | Integer.parseInt(values[1]) << 6 | Integer.parseInt(values[2]); - } - catch (Exception e) { - return 0; - } - } - - /** - * Get mod version from integer. String will be in format %d.%d.%d - * - * @param version - mod version in integer form. - * @return {@link String} mod version. - */ - public static String getModVersion(int version) { - int a = (version >> 12) & 63; - int b = (version >> 6) & 63; - int c = version & 63; - return String.format(Locale.ROOT, "%d.%d.%d", a, b, c); - } -} diff --git a/src/main/java/ru/bclib/api/WorldDataAPI.java b/src/main/java/ru/bclib/api/WorldDataAPI.java index fda603d7..1ce22f35 100644 --- a/src/main/java/ru/bclib/api/WorldDataAPI.java +++ b/src/main/java/ru/bclib/api/WorldDataAPI.java @@ -7,6 +7,7 @@ import net.fabricmc.loader.api.ModContainer; import net.minecraft.nbt.CompoundTag; import net.minecraft.nbt.NbtIo; import ru.bclib.BCLib; +import ru.bclib.api.datafixer.DataFixerAPI; import java.io.File; import java.io.IOException;