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.packet.InPacket;
023import net.tridentsdk.server.netty.packet.Packet;
024import net.tridentsdk.server.packets.play.out.PacketPlayOutPlayerRespawn;
025import net.tridentsdk.server.packets.play.out.PacketPlayOutStatistics;
026import net.tridentsdk.server.player.PlayerConnection;
027import net.tridentsdk.server.player.TridentPlayer;
028import net.tridentsdk.util.TridentLogger;
029import net.tridentsdk.world.World;
030
031/**
032 * Sent by the client when it's ready to login or respawn after death
033 */
034public class PacketPlayInClientStatus extends InPacket {
035
036    /**
037     * Action ID values:  0 - Perform Respawn 1 - Request statistics 2 - Open inventory acheivement
038     */
039    protected short actionId;
040
041    @Override
042    public int id() {
043        return 0x03;
044    }
045
046    public short actionId() {
047        return this.actionId;
048    }
049
050    @Override
051    public Packet decode(ByteBuf buf) {
052        this.actionId = buf.readUnsignedByte();
053
054        return this;
055    }
056
057    @Override
058    public void handleReceived(ClientConnection connection) {
059        TridentPlayer player = ((PlayerConnection) connection).player();
060        World world = player.world();
061        StatusType type = StatusType.getStatus((int) this.actionId);
062
063        switch (type) {
064            case RESPAWN:
065                PacketPlayOutPlayerRespawn respawn = new PacketPlayOutPlayerRespawn();
066
067                respawn.set("dimension", (int) world.settings().dimension().asByte())
068                        .set("difficulity", (int) world.settings().difficulty().asByte())
069                        .set("gameMode", (int) world.settings().defaultGameMode().asByte()
070                        /* todo make this specific to the player */);
071
072                connection.sendPacket(respawn);
073                break;
074
075            case STATISTICS_REQUEST:
076                PacketPlayOutStatistics statistics = new PacketPlayOutStatistics();
077
078                // TODO prepare statistics for the player
079                statistics.set("entries", null);
080
081                connection.sendPacket(statistics); // inb4 NPE
082                break;
083
084            case OPEN_INVENTORY_ACHEIVEMENT:
085                // no packet existing for this, are we missing said packet?
086
087                break;
088
089            default:
090                TridentLogger.get().error(
091                        new IllegalArgumentException("Client sent invalid status, maybe modified?")); // catched by
092                // PacketHandler
093        }
094    }
095
096    public enum StatusType {
097        RESPAWN(0),
098        STATISTICS_REQUEST(1),
099        OPEN_INVENTORY_ACHEIVEMENT(2);
100
101        private final int id;
102
103        StatusType(int id) {
104            this.id = id;
105        }
106
107        public static StatusType getStatus(int id) {
108            for (StatusType type : StatusType.values()) {
109                if (type.getId() == id)
110                    return type;
111            }
112
113            return null;
114        }
115
116        public int getId() {
117            return this.id;
118        }
119    }
120}