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.concurrent.ScheduledRunnable; 023import net.tridentsdk.meta.block.SignMeta; 024import net.tridentsdk.registry.Registered; 025import net.tridentsdk.server.netty.ClientConnection; 026import net.tridentsdk.server.netty.Codec; 027import net.tridentsdk.server.netty.packet.InPacket; 028import net.tridentsdk.server.netty.packet.Packet; 029import net.tridentsdk.server.player.PlayerConnection; 030import net.tridentsdk.server.player.TridentPlayer; 031import net.tridentsdk.util.TridentLogger; 032 033import java.util.concurrent.atomic.AtomicInteger; 034 035/** 036 * Packet is sent when a player wishes to update a sign 037 */ 038public class PacketPlayInUpdateSign extends InPacket { 039 /** 040 * Contents of the sign, represented in JSON 041 */ 042 protected final String[] jsonContents = new String[4]; 043 /** 044 * Location of the sign 045 */ 046 protected Position signLocation; 047 048 @Override 049 public int id() { 050 return 0x2A; 051 } 052 053 @Override 054 public Packet decode(ByteBuf buf) { 055 long encoded = buf.readLong(); 056 double x = (double) (encoded >> 38); 057 double y = (double) (encoded << 26 >> 52); 058 double z = (double) (encoded << 38 >> 38); 059 060 this.signLocation = Position.create(null, x, y, z); 061 062 for (int i = 0; i < 4; i++) { 063 this.jsonContents[i] = Codec.readString(buf); 064 } 065 return this; 066 } 067 068 public Position signLocation() { 069 return this.signLocation; 070 } 071 072 public String[] jsonContents() { 073 return this.jsonContents; 074 } 075 076 @Override 077 public void handleReceived(ClientConnection connection) { 078 TridentPlayer player = ((PlayerConnection) connection).player(); 079 signLocation.setWorld(player.world()); 080 081 Registered.tasks().asyncRepeat(null, new ScheduledRunnable() { 082 private final AtomicInteger integer = new AtomicInteger(); 083 084 @Override 085 public void run() { 086 SignMeta meta = signLocation.block().obtainMeta(SignMeta.class); 087 System.out.println("Meta at " + signLocation + " is " + signLocation.block().substance()); 088 integer.incrementAndGet(); 089 if (meta != null) { 090 for (int i = 0; i < 4; i++) { 091 meta.setTextAt(i, jsonContents[i]); 092 } 093 cancel(); 094 } else { 095 if (integer.get() > 10) { 096 TridentLogger.get().warn("Could not find sign at " + signLocation); 097 cancel(); 098 } 099 } 100 } 101 }, 1L, 1L); 102 } 103}