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.world.ChunkLocation;
022
023/**
024 * Builds chunk metadata required for chunk packet data sent to the player
025 *
026 * @author The TridentSDK Team
027 */
028public class ChunkMetaBuilder implements Writable {
029    private ChunkLocation location;
030    private short bitmap;
031
032    /**
033     * Gets the chunk location
034     *
035     * @return the location of the chunk built into the metadata
036     */
037    public ChunkLocation location() {
038        return this.location;
039    }
040
041    /**
042     * Sets the chunk meta location
043     *
044     * @param location the location to set the chunk to
045     * @return the current instance
046     */
047    public ChunkMetaBuilder location(ChunkLocation location) {
048        this.location = location;
049
050        return this;
051    }
052
053    /**
054     * Reads the {@code short} value that maps the bits in the chunk
055     *
056     * @return the chunk bitmap
057     */
058    public short bitmap() {
059        return this.bitmap;
060    }
061
062    /**
063     * Sets the chunk bitmap
064     *
065     * @param bitmap the {@code short} that maps the chunk data
066     * @return the current instance
067     */
068    public ChunkMetaBuilder bitmap(short bitmap) {
069        this.bitmap = bitmap;
070
071        return this;
072    }
073
074    @Override
075    public void write(ByteBuf buf) {
076        buf.writeInt(this.location.x());
077        buf.writeInt(this.location.z());
078
079        buf.writeShort((int) this.bitmap);
080    }
081}