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.base.Position; 022import net.tridentsdk.server.netty.ClientConnection; 023import net.tridentsdk.server.netty.Codec; 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 029import javax.annotation.Nullable; 030 031public class PacketPlayInEntityInteract extends InPacket { 032 /** 033 * Entity id of the target interacted 034 */ 035 protected int target; 036 /** 037 * Type of interation, reference InteractType 038 * 039 * @see InteractType 040 */ 041 protected InteractType type; 042 043 /** 044 * Location of the target, sent as 3 floats x, y, z 045 */ 046 @Nullable 047 protected Position location; 048 049 @Override 050 public int id() { 051 return 0x0A; 052 } 053 054 @Override 055 public Packet decode(ByteBuf buf) { 056 this.target = Codec.readVarInt32(buf); 057 this.type = InteractType.fromId(Codec.readVarInt32(buf)); 058 059 if (type == InteractType.INTERACT_AT) { 060 double x = (double) buf.readFloat(); 061 double y = (double) buf.readFloat(); 062 double z = (double) buf.readFloat(); 063 this.location = Position.create(null, x, y, z); 064 } 065 066 return this; 067 } 068 069 public int target() { 070 return this.target; 071 } 072 073 public InteractType interactType() { 074 return this.type; 075 } 076 077 public Position location() { 078 return this.location; 079 } 080 081 @Override 082 public void handleReceived(ClientConnection connection) { 083 TridentPlayer player = ((PlayerConnection) connection).player(); 084 085 if (location != null) { 086 location.setWorld(player.world()); 087 } 088 089 // TODO: call event and process interact 090 } 091 092 public enum InteractType { 093 INTERACT(0), 094 ATTACK(1), 095 INTERACT_AT(2); 096 097 private final int id; 098 099 InteractType(int id) { 100 this.id = id; 101 } 102 103 public static InteractType fromId(int id) { 104 for (InteractType type : InteractType.values()) { 105 if (type.getId() == id) 106 return type; 107 } 108 109 return null; 110 } 111 112 public int getId() { 113 return this.id; 114 } 115 } 116}