/*
 * Trident - A Multithreaded Server Alternative
 * Copyright 2014 The TridentSDK Team
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package net.tridentsdk.server.packets.play.in;

import io.netty.buffer.ByteBuf;
import net.tridentsdk.Position;
import net.tridentsdk.server.netty.ClientConnection;
import net.tridentsdk.server.netty.Codec;
import net.tridentsdk.server.netty.packet.InPacket;
import net.tridentsdk.server.netty.packet.Packet;

public class PacketPlayInEntityInteract extends InPacket {
    /**
     * Entity id of the target interacted
     */
    protected int target;
    /**
     * Type of interation, reference InteractType
     *
     * @see InteractType
     */
    protected InteractType type;

    /**
     * Location of the target, sent as 3 floats x, y, z
     */
    protected Position location;

    @Override
    public int id() {
        return 0x02;
    }

    @Override
    public Packet decode(ByteBuf buf) {
        this.target = Codec.readVarInt32(buf);
        this.type = InteractType.fromId(Codec.readVarInt32(buf));

        double x = (double) buf.readFloat();
        double y = (double) buf.readFloat();
        double z = (double) buf.readFloat();

        this.location = Position.create(null, x, y, z); // TODO: Get the clients world
        return this;
    }

    public int target() {
        return this.target;
    }

    public InteractType interactType() {
        return this.type;
    }

    public Position location() {
        return this.location;
    }

    @Override
    public void handleReceived(ClientConnection connection) {
        // TODO: Respond to the client accordingly
    }

    public enum InteractType {
        INTERACT(0),
        ATTACK(1),
        INTERACT_AT(2);

        private final int id;

        InteractType(int id) {
            this.id = id;
        }

        public static InteractType fromId(int id) {
            for (InteractType type : InteractType.values()) {
                if (type.getId() == id)
                    return type;
            }

            return null;
        }

        public int getId() {
            return this.id;
        }
    }
}

@@ -954,16 +954,51 @@
 acket;%0A%0A
+import javax.annotation.Nullable;%0A%0A
 public c
@@ -1328,32 +1328,46 @@
 x, y, z%0A     */%0A
+    @Nullable%0A
     protected Po
@@ -1612,24 +1612,76 @@
 t32(buf));%0A%0A
+        if (type == InteractType.INTERACT_AT) %7B%0A    
         doub
@@ -1717,24 +1717,28 @@
 ();%0A        
+ 
+   
 double y = (
@@ -1766,24 +1766,28 @@
 ();%0A        
+ 
+   
 double z = (
@@ -1811,17 +1811,20 @@
 loat();%0A
-%0A
+    
         
@@ -1902,16 +1902,27 @@
 s world%0A
+        %7D%0A%0A
         

Fix small stability error