001/*
002 * PlotSquared, a land and world management plugin for Minecraft.
003 * Copyright (C) IntellectualSites <https://intellectualsites.com>
004 * Copyright (C) IntellectualSites team and contributors
005 *
006 * This program is free software: you can redistribute it and/or modify
007 * it under the terms of the GNU General Public License as published by
008 * the Free Software Foundation, either version 3 of the License, or
009 * (at your option) any later version.
010 *
011 * This program is distributed in the hope that it will be useful,
012 * but WITHOUT ANY WARRANTY; without even the implied warranty of
013 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
014 * GNU General Public License for more details.
015 *
016 * You should have received a copy of the GNU General Public License
017 * along with this program.  If not, see <https://www.gnu.org/licenses/>.
018 */
019package com.plotsquared.core.plot.schematic;
020
021import com.sk89q.jnbt.NBTOutputStream;
022import com.sk89q.jnbt.Tag;
023import com.sk89q.worldedit.WorldEditException;
024import com.sk89q.worldedit.extent.clipboard.Clipboard;
025import com.sk89q.worldedit.extent.clipboard.io.SpongeSchematicWriter;
026import com.sk89q.worldedit.math.BlockVector3;
027import com.sk89q.worldedit.world.block.BaseBlock;
028
029import java.io.File;
030import java.io.FileOutputStream;
031import java.io.IOException;
032import java.util.HashMap;
033import java.util.Map;
034
035public class Schematic {
036
037    // Lossy but fast
038    private final Clipboard clipboard;
039    private Map<String, Tag> flags = new HashMap<>();
040
041    public Schematic(final Clipboard clip) {
042        this.clipboard = clip;
043    }
044
045    public boolean setBlock(BlockVector3 position, BaseBlock block) throws WorldEditException {
046        if (clipboard.getRegion().contains(position)) {
047            BlockVector3 vector3 = position.subtract(clipboard.getRegion().getMinimumPoint());
048            clipboard.setBlock(vector3, block);
049            return true;
050        } else {
051            return false;
052        }
053    }
054
055    public void save(File file) throws IOException {
056        try (SpongeSchematicWriter schematicWriter = new SpongeSchematicWriter(
057                new NBTOutputStream(new FileOutputStream(file)))) {
058            schematicWriter.write(clipboard);
059        }
060    }
061
062    public Clipboard getClipboard() {
063        return this.clipboard;
064    }
065
066    public Map<String, Tag> getFlags() {
067        return this.flags;
068    }
069
070    public void setFlags(Map<String, Tag> flags) {
071        this.flags = flags == null ? new HashMap<>() : flags;
072    }
073
074}