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.base.Position;
022
023/**
024 * Represents a writable form of a {@link Position}
025 *
026 * @author The TridentSDK Team
027 */
028public class PositionWritable implements Writable {
029    private Position loc;
030
031    /**
032     * Creates a new position based from an existing location
033     *
034     * @param loc the location to wrap with writable format
035     */
036    public PositionWritable(Position loc) {
037        this.loc = loc;
038    }
039
040    /**
041     * Gets the wrapped, original location
042     *
043     * @return the location passed in by constructor or by {@link #setLoc(Position)}
044     */
045    public Position location() {
046        return this.loc;
047    }
048
049    /**
050     * Sets the wrapped position
051     *
052     * <p>This does not change the value of already written locations. This is purely for purposes of performance,
053     * but removes concurrency.</p>
054     *
055     * @param loc the location to wrap with writable format
056     */
057    public void setLoc(Position loc) {
058        this.loc = loc;
059    }
060
061    @Override
062    public void write(ByteBuf buf) {
063        buf.writeLong((long) ((int) this.loc.x() & 0x3FFFFFF) << 38 |
064                (long) ((int) this.loc.y() & 0xFFF) << 26 |
065                (long) ((int) this.loc.z() & 0x3FFFFFF));
066    }
067}