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.event.player.PlayerTabCompleteEvent; 023import net.tridentsdk.server.netty.ClientConnection; 024import net.tridentsdk.server.netty.Codec; 025import net.tridentsdk.server.netty.packet.InPacket; 026import net.tridentsdk.server.netty.packet.Packet; 027import net.tridentsdk.server.packets.play.out.PacketPlayOutTabComplete; 028import net.tridentsdk.server.player.PlayerConnection; 029 030/** 031 * Sent when the user presses tab while writing text. The payload contains all text behind the cursor. 032 */ 033public class PacketPlayInTabComplete extends InPacket { 034 /** 035 * Text currently written 036 */ 037 protected String text; 038 /** 039 * If player is looking at a specific block 040 */ 041 protected boolean hasPosition; 042 /** 043 * PositionWritable of the block the player is looking at, only sent if hasPosition is true 044 */ 045 protected Position lookedAtBlock; 046 047 @Override 048 public int id() { 049 return 0x01; 050 } 051 052 @Override 053 public Packet decode(ByteBuf buf) { 054 this.text = Codec.readString(buf); 055 this.hasPosition = buf.readBoolean(); 056 057 if (this.hasPosition) { 058 long encoded = buf.readLong(); 059 double x = (double) (encoded << 38); 060 double y = (double) (encoded << 26 >> 52); 061 double z = (double) (encoded << 38 >> 38); 062 063 this.lookedAtBlock = Position.create(null, x, y, z); 064 } 065 066 return this; 067 } 068 069 public String text() { 070 return this.text; 071 } 072 073 public boolean hasPosition() { 074 return this.hasPosition; 075 } 076 077 public Position lookedAtBlock() { 078 return this.lookedAtBlock; 079 } 080 081 @Override 082 public void handleReceived(ClientConnection connection) { 083 PlayerTabCompleteEvent event = new PlayerTabCompleteEvent(((PlayerConnection) connection).player(), 084 this.text); 085 086 if (event.suggestions().length > 0) { 087 connection.sendPacket(new PacketPlayOutTabComplete().set("matches", event.suggestions())); 088 } 089 } 090}