All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.sk89q.worldedit.world.storage.McRegionChunkStore Maven / Gradle / Ivy

Go to download

Blazingly fast Minecraft world manipulation for artists, builders and everyone else.

There is a newer version: 2.9.2
Show newest version
/*
 * WorldEdit, a Minecraft world manipulation toolkit
 * Copyright (C) sk89q 
 * Copyright (C) WorldEdit team and contributors
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see .
 */

package com.sk89q.worldedit.world.storage;

import com.sk89q.jnbt.CompoundTag;
import com.sk89q.worldedit.math.BlockVector2;
import com.sk89q.worldedit.world.DataException;
import com.sk89q.worldedit.world.World;

import javax.annotation.Nullable;
import java.io.IOException;
import java.io.InputStream;

public abstract class McRegionChunkStore extends ChunkStore {

    protected String curFilename = null;
    protected McRegionReader cachedReader = null;

    /**
     * Get the filename of a region file.
     *
     * @param position chunk position
     * @return the filename
     */
    public static String getFilename(BlockVector2 position) {
        int x = position.getBlockX();
        int z = position.getBlockZ();

        return "r." + (x >> 5) + "." + (z >> 5) + ".mca";
    }

    //FAWE start - biome and entity restore
    protected McRegionReader getReader(BlockVector2 pos, String worldname, @Nullable String folderOverride) throws DataException,
            IOException {
        //FAWE end
        String filename = getFilename(pos);
        if (curFilename != null) {
            if (curFilename.equals(filename)) {
                return cachedReader;
            } else {
                try {
                    cachedReader.close();
                } catch (IOException ignored) {
                }
            }
        }
        //FAWE start - biome and entity restore
        InputStream stream = getInputStream(filename, worldname, folderOverride);
        //FAWE end
        cachedReader = new McRegionReader(stream);
        //curFilename = filename;
        return cachedReader;
    }

    @Override
    public CompoundTag getChunkTag(BlockVector2 position, World world) throws DataException, IOException {
        return ChunkStoreHelper.readCompoundTag(() -> {
            McRegionReader reader = getReader(position, world.getName(), null);

            return reader.getChunkInputStream(position);
        });
    }

    //FAWE start - biome and entity restore
    @Override
    public CompoundTag getEntitiesTag(BlockVector2 position, World world) {
        try {
            return ChunkStoreHelper.readCompoundTag(() -> {
                McRegionReader reader = getReader(position, world.getName(), "entities");

                return reader.getChunkInputStream(position);
            });
        } catch (DataException | IOException e) {
            return null;
        }
    }
    //FAWE end

    /**
     * Get the input stream for a chunk file.
     *
     * @param name           the name of the chunk file
     * @param worldName      the world name
     * @param folderOverride override folder to check. "entities" used for getting entities in 1.17+
     * @return an input stream
     * @throws IOException if there is an error getting the chunk data
     */
    //FAWE start - biome and entity restore
    protected abstract InputStream getInputStream(String name, String worldName, @Nullable String folderOverride) throws
            IOException, DataException;
    //FAWE end

    @Override
    public void close() throws IOException {
        if (cachedReader != null) {
            cachedReader.close();
        }
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy