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.server.netty.Codec;
022import net.tridentsdk.server.netty.packet.OutPacket;
023
024public class PacketPlayOutUpdateScore extends OutPacket {
025
026    protected String itemName;
027    protected UpdateType type;
028    protected String scoreName;
029    protected int value;
030
031    @Override
032    public int id() {
033        return 0x3C;
034    }
035
036    public String itemName() {
037        return this.itemName;
038    }
039
040    public UpdateType updateType() {
041        return this.type;
042    }
043
044    public String score() {
045        return this.scoreName;
046    }
047
048    public int value() {
049        return this.value;
050    }
051
052    @Override
053    public void encode(ByteBuf buf) {
054        Codec.writeString(buf, this.itemName);
055        buf.writeByte((int) this.type.asByte());
056
057        if (this.type.b == 1) {
058            return;
059        }
060
061        Codec.writeString(buf, this.scoreName);
062        Codec.writeVarInt32(buf, this.value);
063    }
064
065    public enum UpdateType {
066        CREATE(0),
067        UPDATE(0),
068        REMOVE(1);
069
070        protected final byte b;
071
072        UpdateType(int i) {
073            this.b = (byte) i;
074        }
075
076        public byte asByte() {
077            return this.b;
078        }
079    }
080}