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.player;
019
020import net.tridentsdk.meta.nbt.NBTField;
021import net.tridentsdk.meta.nbt.NBTSerializable;
022import net.tridentsdk.meta.nbt.TagType;
023import net.tridentsdk.server.netty.packet.OutPacket;
024import net.tridentsdk.server.packets.play.out.PacketPlayOutPlayerAbilities;
025
026public class PlayerAbilities implements NBTSerializable {
027    @NBTField(name = "walkSpeed", type = TagType.FLOAT)
028    protected float walkingSpeed = 0F;
029    @NBTField(name = "flySpeed", type = TagType.FLOAT)
030    protected float flySpeed = 0F;
031    @NBTField(name = "canFly", type = TagType.BYTE)
032    protected byte canFly = 0;
033    @NBTField(name = "flying", type = TagType.BYTE)
034    protected byte flying = 0;
035    @NBTField(name = "invunerable", type = TagType.BYTE)
036    protected byte invulnerable = 0;
037    @NBTField(name = "canBuild", type = TagType.BYTE)
038    protected byte canBuild = 1;
039    @NBTField(name = "instantBuild", type = TagType.BYTE)
040    protected byte creative = 0;
041
042    protected PlayerAbilities() {
043    }
044
045    public float walkingSpeed() {
046        return walkingSpeed;
047    }
048
049    public float flyingSpeed() {
050        return flySpeed;
051    }
052
053    public boolean canFly() {
054        return canFly == 1;
055    }
056
057    public boolean isFlying() {
058        return flying == 1;
059    }
060
061    public boolean isInvulnerable() {
062        return flying == 1;
063    }
064
065    public boolean canBuild() {
066        return canBuild == 1;
067    }
068
069    public boolean isCreative() {
070        return creative == 1;
071    }
072
073    public OutPacket asPacket() {
074        OutPacket packet = new PacketPlayOutPlayerAbilities();
075        byte flags = (byte) ((isInvulnerable() ? 8 : 0) | (canFly() ? 4 : 0) | (isFlying() ? 2 : 0) |
076                (isCreative() ? 1 : 0));
077
078        packet.set("flags", flags);
079        packet.set("flyingSpeed", flySpeed / 2);
080        packet.set("walkingSpeed", walkingSpeed / 2);
081
082        return packet;
083    }
084}