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.packets.play.in;
019
020import io.netty.buffer.ByteBuf;
021import net.tridentsdk.server.netty.ClientConnection;
022import net.tridentsdk.server.netty.Codec;
023import net.tridentsdk.server.netty.packet.InPacket;
024import net.tridentsdk.server.netty.packet.Packet;
025import net.tridentsdk.server.player.PlayerConnection;
026import net.tridentsdk.server.player.TridentPlayer;
027
028import java.util.Locale;
029
030public class PacketPlayInClientSettings extends InPacket {
031    /**
032     * Locale of the client, reference to Locale#forLanguageTag(String) to read said locale
033     *
034     * @see java.util.Locale#forLanguageTag(String)
035     */
036    protected Locale locale;
037    /**
038     * View distance set by the client
039     */
040    protected short viewDistance;
041    /**
042     * Chat settings:  Bits 0-1. 00: Enabled. 01: Commands only. 10: Hidden.
043     */
044    protected byte chatFlags;
045    /**
046     * If the client has colours enabled for chat
047     */
048    protected boolean chatColors;
049    /**
050     * Displayed skin parts also packs several values into one byte.  Bit 0: Cape enabled Bit 1: Jacket enabled Bit
051     * 2: Left Sleeve enabled Bit 3: Right Sleeve enabled Bit 4: Left Pants Leg enabled Bit 5: Right Pants Leg enabled
052     * Bit 6: Hat enabled The most significant bit (bit 7) appears to be unused.
053     */
054    protected byte skinParts;
055
056    @Override
057    public int id() {
058        return 0x04;
059    }
060
061    @Override
062    public Packet decode(ByteBuf buf) {
063        this.locale = Locale.forLanguageTag(Codec.readString(buf));
064        this.viewDistance = (short) buf.readByte();
065        this.chatFlags = buf.readByte();
066        this.chatColors = buf.readBoolean();
067
068        this.skinParts = (byte) buf.readUnsignedByte();
069
070        return this;
071    }
072
073    @Override
074    public void handleReceived(ClientConnection connection) {
075        TridentPlayer player = ((PlayerConnection) connection).player();
076
077        player.setLocale(locale);
078        player.setSkinFlags(skinParts);
079        player.setViewDistance(viewDistance);
080    }
081}