001/*
002 * Trident - A Multithreaded Server Alternative
003 * Copyright 2014 The TridentSDK Team
004 *
005 * Licensed under the Apache License, Version 2.0 (the "License");
006 * you may not use this file except in compliance with the License.
007 * You may obtain a copy of the License at
008 *
009 *    http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017
018package net.tridentsdk.base;
019
020import net.tridentsdk.docs.InternalUseOnly;
021
022import javax.annotation.concurrent.Immutable;
023
024/**
025 * Represents an instance or "snapshot" of a tile when viewed by the construction method
026 *
027 * <p>Snapshot using the {@link #of(Block)} method</p>
028 *
029 * @author The TridentSDK Team
030 * @since 0.3-alpha-DP
031 */
032@Immutable
033public final class BlockSnapshot {
034    private final Position position;
035    private final Substance substance;
036    private final byte meta;
037
038    private BlockSnapshot(Position position, Substance substance, byte data) {
039        this.position = position;
040        this.substance = substance;
041        this.meta = data;
042    }
043
044    /**
045     * Creates a view of the block at the time when taken
046     *
047     * @param block the tile to view
048     * @return the snapshot of the tile
049     */
050    public static BlockSnapshot of(Block block) {
051        return new BlockSnapshot(block.position(), block.substance(), block.meta());
052    }
053
054    @InternalUseOnly
055    public static BlockSnapshot from(Position position, Substance substance, byte data) {
056        return new BlockSnapshot(position, substance, data);
057    }
058
059    /**
060     * Places the data stored in the snapshot into the original block
061     *
062     * <p>Does not clear data from this snapshot</p>
063     */
064    public void load() {
065        Block block = position.world().blockAt(position);
066        block.setSubstance(substance);
067        block.setMeta(meta);
068    }
069
070    /**
071     * Obtains the position of the block
072     *
073     * @return the position
074     */
075    public Position position() {
076        return position;
077    }
078
079    /**
080     * Obtains the block substance
081     *
082     * @return the substance
083     */
084    public Substance type() {
085        return substance;
086    }
087
088    /**
089     * Obtains the block meta
090     *
091     * @return the block meta
092     */
093    public byte meta() {
094        return meta;
095    }
096}