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.server.data;
019
020import io.netty.buffer.ByteBuf;
021import net.tridentsdk.server.netty.Codec;
022
023/**
024 * Records the state of a block as it was when the snapshot was taken
025 *
026 * <p>Note that this may not always represent the most accurate view of the block in question.</p>
027 *
028 * @author The TridentSDK Team
029 */
030public class RecordBuilder implements Writable {
031    private volatile byte x;
032    private volatile byte y;
033    private volatile byte z;
034    private volatile int blockId;
035    private volatile int data;
036
037    /**
038     * Get the X location of the block
039     *
040     * @return the block's X location
041     */
042    public byte x() {
043        return this.x;
044    }
045
046    public int data() {
047        return data;
048    }
049
050    public RecordBuilder setData(int data) {
051        this.data = data;
052        return this;
053    }
054
055    /**
056     * Sets the X location of the block
057     *
058     * @param x the new X value
059     * @return the current instance
060     */
061    public RecordBuilder setX(byte x) {
062        this.x = x;
063
064        return this;
065    }
066
067    /**
068     * Get the Y location of the block
069     *
070     * @return the block's Y location
071     */
072    public byte y() {
073        return this.y;
074    }
075
076    /**
077     * Sets the Y location of the block
078     *
079     * @param y the new Y value
080     * @return the current instance
081     */
082    public RecordBuilder setY(byte y) {
083        this.y = y;
084
085        return this;
086    }
087
088    /**
089     * Get the Z location of the block
090     *
091     * @return the block's Z location
092     */
093    public byte z() {
094        return this.z;
095    }
096
097    /**
098     * Sets the Z location of the block
099     *
100     * @param z the new Z value
101     * @return the current instance
102     */
103    public RecordBuilder setZ(byte z) {
104        this.z = z;
105
106        return this;
107    }
108
109    /**
110     * Gets the block ID number
111     *
112     * @return the ID number of the block
113     */
114    public int blockId() {
115        return this.blockId;
116    }
117
118    /**
119     * Sets the block ID number
120     *
121     * @param blockId the ID number of the block to be set
122     * @return the current instance
123     */
124    public RecordBuilder setBlockId(int blockId) {
125        this.blockId = blockId;
126
127        return this;
128    }
129
130    @Override
131    public void write(ByteBuf buf) {
132        buf.writeShort((((x % 16) & 0xF) << 12) |
133                (((z % 16) & 0xF) << 8) |
134                (y & 0xFF));
135        Codec.writeVarInt32(buf, blockId << 4 | data);
136    }
137}