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.out;
019
020import io.netty.buffer.ByteBuf;
021import net.tridentsdk.base.board.TagVisibility;
022import net.tridentsdk.server.netty.Codec;
023import net.tridentsdk.server.netty.packet.OutPacket;
024
025public class PacketPlayOutTeams extends OutPacket {
026
027    protected String teamName;
028    protected Mode mode;
029
030    protected String teamDisplay;
031    protected String teamPrefix;
032    protected String teamSuffix;
033
034    protected short friendlyFire;
035    protected TagVisibility tagVisibility;
036    protected short color;
037
038    protected String[] players;
039
040    @Override
041    public int id() {
042        return 0x3E;
043    }
044
045    public String team() {
046        return this.teamName;
047    }
048
049    public Mode mode() {
050        return this.mode;
051    }
052
053    public String teamDisplay() {
054        return this.teamDisplay;
055    }
056
057    public String teamPrefix() {
058        return this.teamPrefix;
059    }
060
061    public String teamSuffix() {
062        return this.teamSuffix;
063    }
064
065    public short friendlyFire() {
066        return this.friendlyFire;
067    }
068
069    public TagVisibility tagVisibility() {
070        return this.tagVisibility;
071    }
072
073    public short color() {
074        return this.color;
075    }
076
077    public String[] players() {
078        return this.players;
079    }
080
081    @Override
082    public void encode(ByteBuf buf) {
083        Codec.writeString(buf, this.teamName);
084        buf.writeByte((int) this.mode.asByte());
085
086        if (this.mode.b == 1 || this.mode.b == 2) {
087            Codec.writeString(buf, this.teamDisplay);
088            Codec.writeString(buf, this.teamPrefix);
089            Codec.writeString(buf, this.teamSuffix);
090
091            buf.writeByte((int) this.friendlyFire);
092            Codec.writeString(buf, this.tagVisibility.toString());
093            buf.writeByte((int) this.color);
094        }
095
096        if (this.mode.b == 3 || this.mode.b == 4) {
097            Codec.writeVarInt32(buf, this.players.length);
098
099            for (String s : this.players) {
100                Codec.writeString(buf, s);
101            }
102        }
103    }
104
105    public enum Mode {
106
107        CREATED(0),
108        REMOVED(1),
109        UPDATED(2),
110        ADD_PLAYER(3),
111        REMOVE_PLAYER(4);
112
113        protected final byte b;
114
115        Mode(int i) {
116            this.b = (byte) i;
117        }
118
119        public byte asByte() {
120            return this.b;
121        }
122    }
123}