Search in sources :

Example 1 with RegionFile

use of net.minecraft.world.level.chunk.storage.RegionFile in project fabric-carpet by gnembon.

the class WorldTools method canHasChunk.

public static boolean canHasChunk(ServerLevel world, ChunkPos chpos, Map<String, RegionFile> regionCache, boolean deepcheck) {
    if (world.getChunk(chpos.x, chpos.z, ChunkStatus.STRUCTURE_STARTS, false) != null)
        return true;
    String currentRegionName = "r." + chpos.getRegionX() + "." + chpos.getRegionZ() + ".mca";
    if (regionCache != null && regionCache.containsKey(currentRegionName)) {
        RegionFile region = regionCache.get(currentRegionName);
        if (region == null)
            return false;
        return region.hasChunk(chpos);
    }
    Path regionPath = new File(((MinecraftServerInterface) world.getServer()).getCMSession().getDimensionPath(world.dimension()).toFile(), "region").toPath();
    Path regionFilePath = regionPath.resolve(currentRegionName);
    File regionFile = regionFilePath.toFile();
    if (!regionFile.exists()) {
        if (regionCache != null)
            regionCache.put(currentRegionName, null);
        return false;
    }
    // not using cache in this case.
    if (!deepcheck)
        return true;
    try {
        RegionFile region = new RegionFile(regionFile.toPath(), regionPath, true);
        if (regionCache != null)
            regionCache.put(currentRegionName, region);
        return region.hasChunk(chpos);
    } catch (IOException ignored) {
    }
    return true;
}
Also used : Path(java.nio.file.Path) RegionFile(net.minecraft.world.level.chunk.storage.RegionFile) IOException(java.io.IOException) RegionFile(net.minecraft.world.level.chunk.storage.RegionFile) File(java.io.File) MinecraftServerInterface(carpet.fakes.MinecraftServerInterface)

Example 2 with RegionFile

use of net.minecraft.world.level.chunk.storage.RegionFile in project fabric-carpet by gnembon.

the class ChunkMap_scarpetChunkCreationMixin method getExistingChunks.

@Unique
private Set<ChunkPos> getExistingChunks(final Set<ChunkPos> requestedChunks) {
    final Map<String, RegionFile> regionCache = new HashMap<>();
    final Set<ChunkPos> ret = new HashSet<>();
    for (final ChunkPos pos : requestedChunks) if (WorldTools.canHasChunk(this.level, pos, regionCache, true))
        ret.add(pos);
    return ret;
}
Also used : Object2IntOpenHashMap(it.unimi.dsi.fastutil.objects.Object2IntOpenHashMap) HashMap(java.util.HashMap) Long2ObjectLinkedOpenHashMap(it.unimi.dsi.fastutil.longs.Long2ObjectLinkedOpenHashMap) RegionFile(net.minecraft.world.level.chunk.storage.RegionFile) ChunkPos(net.minecraft.world.level.ChunkPos) HashSet(java.util.HashSet) Unique(org.spongepowered.asm.mixin.Unique)

Example 3 with RegionFile

use of net.minecraft.world.level.chunk.storage.RegionFile in project BCLib by paulevsGitch.

the class DataFixerAPI method fixRegion.

private static void fixRegion(MigrationProfile data, State state, File file) {
    try {
        Path path = file.toPath();
        LOGGER.info("Inspecting " + path);
        boolean[] changed = new boolean[1];
        RegionFile region = new RegionFile(path, path.getParent(), 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) && !state.didFail) {
                    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"));
                    // }
                    input.close();
                    // Checking TileEntities
                    ListTag tileEntities = root.getCompound("Level").getList("TileEntities", Tag.TAG_COMPOUND);
                    fixItemArrayWithID(tileEntities, changed, data, true);
                    // Checking Entities
                    ListTag entities = root.getList("Entities", Tag.TAG_COMPOUND);
                    fixItemArrayWithID(entities, changed, data, true);
                    // Checking Block Palette
                    ListTag sections = root.getCompound("Level").getList("Sections", Tag.TAG_COMPOUND);
                    sections.forEach((tag) -> {
                        ListTag palette = ((CompoundTag) tag).getList("Palette", Tag.TAG_COMPOUND);
                        palette.forEach((blockTag) -> {
                            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.addError("Failed fixing BlockState in " + pos + " (" + e.getMessage() + ")");
                            state.didFail = true;
                            changed[0] = false;
                            e.printStackTrace();
                        }
                    });
                    if (changed[0]) {
                        LOGGER.warning("Writing '{}': {}/{}", file, x, z);
                        // NbtIo.write(root, new File(file.toString() + "-" + x + "-" + z + "-changed.nbt"));
                        DataOutputStream output = region.getChunkDataOutputStream(pos);
                        NbtIo.write(root, output);
                        output.close();
                    }
                }
            }
        }
        region.close();
    } catch (Exception e) {
        BCLib.LOGGER.error("Failed fixing Region.");
        state.addError("Failed fixing Region in " + file.getName() + " (" + e.getMessage() + ")");
        state.didFail = true;
        e.printStackTrace();
    }
}
Also used : Path(java.nio.file.Path) RegionFile(net.minecraft.world.level.chunk.storage.RegionFile) DataOutputStream(java.io.DataOutputStream) ChunkPos(net.minecraft.world.level.ChunkPos) DataInputStream(java.io.DataInputStream) ListTag(net.minecraft.nbt.ListTag) CompoundTag(net.minecraft.nbt.CompoundTag) ZipException(java.util.zip.ZipException) IOException(java.io.IOException) EOFException(java.io.EOFException)

Aggregations

RegionFile (net.minecraft.world.level.chunk.storage.RegionFile)3 IOException (java.io.IOException)2 Path (java.nio.file.Path)2 ChunkPos (net.minecraft.world.level.ChunkPos)2 MinecraftServerInterface (carpet.fakes.MinecraftServerInterface)1 Long2ObjectLinkedOpenHashMap (it.unimi.dsi.fastutil.longs.Long2ObjectLinkedOpenHashMap)1 Object2IntOpenHashMap (it.unimi.dsi.fastutil.objects.Object2IntOpenHashMap)1 DataInputStream (java.io.DataInputStream)1 DataOutputStream (java.io.DataOutputStream)1 EOFException (java.io.EOFException)1 File (java.io.File)1 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1 ZipException (java.util.zip.ZipException)1 CompoundTag (net.minecraft.nbt.CompoundTag)1 ListTag (net.minecraft.nbt.ListTag)1 Unique (org.spongepowered.asm.mixin.Unique)1