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.data.Slot;
022import net.tridentsdk.server.entity.TridentDroppedItem;
023import net.tridentsdk.server.netty.ClientConnection;
024import net.tridentsdk.server.netty.packet.InPacket;
025import net.tridentsdk.server.netty.packet.Packet;
026import net.tridentsdk.server.player.PlayerConnection;
027import net.tridentsdk.server.player.TridentPlayer;
028
029/**
030 * While the user is in the standard inventory (i.e., not a crafting bench) on a creative-mode server, then this packet
031 * will be sent:  If an item is dropped into the quick bar If an item is picked up from the quick bar (item id is
032 * -1)
033 */
034public class PacketPlayInPlayerCreativeAction extends InPacket {
035
036    /**
037     * Slot of the action
038     */
039    protected short slot;
040    /**
041     * Item used in the action
042     */
043    protected Slot item;
044
045    @Override
046    public int id() {
047        return 0x06;
048    }
049
050    public Slot item() {
051        return this.item;
052    }
053
054    @Override
055    public Packet decode(ByteBuf buf) {
056        this.slot = buf.readShort();
057        this.item = new Slot(buf);
058
059        return this;
060    }
061
062    public short slot() {
063        return this.slot;
064    }
065
066    @Override
067    public void handleReceived(ClientConnection connection) {
068        TridentPlayer player = ((PlayerConnection) connection).player();
069
070        if (slot <= 0) {
071            TridentDroppedItem item = new TridentDroppedItem(player.headLocation(), item().item());
072            item.spawn();
073            item.setVelocity(player.position().toDirection().normalize().multiply(2000));
074            // TODO set item type
075            // TODO this can also clear the inventory
076
077            return;
078        }
079
080        // TODO: Handle when the item is set in the Player's hand
081        player.window().setSlot(slot, item.item());
082    }
083}