@Override
 public ChannelBuffer encode(EncryptionKeyRequestMessage message) {
   ChannelBuffer buffer = ChannelBuffers.dynamicBuffer();
   ChannelBufferUtils.writeString(buffer, message.getSessionId());
   byte[] publicKey = message.getSecretArray();
   buffer.writeShort((short) publicKey.length);
   buffer.writeBytes(publicKey);
   buffer.writeShort((short) message.getVerifyTokenArray().length);
   buffer.writeBytes(message.getVerifyTokenArray());
   return buffer;
 }
 @Override
 public EncryptionKeyRequestMessage decode(ChannelBuffer buffer) {
   String sessionId = ChannelBufferUtils.readString(buffer);
   int length = buffer.readShort() & 0xFFFF;
   byte[] publicKey = new byte[length];
   buffer.readBytes(publicKey);
   int tokenLength = buffer.readShort() & 0xFFFF;
   byte[] token = new byte[tokenLength];
   buffer.readBytes(token);
   return new EncryptionKeyRequestMessage(sessionId, false, publicKey, token);
 }
Esempio n. 3
0
  @Override
  public List<Message> getUpdateMessages(
      Entity entity, Transform liveTransform, RepositionManager rm, boolean force) {
    // Movement
    final Transform prevTransform = rm.convert(entity.getScene().getTransform());
    final Transform newTransform = rm.convert(liveTransform);

    final boolean looked = entity.getScene().isRotationDirty();

    final int lastX = protocolifyPosition(prevTransform.getPosition().getX());
    final int lastY = protocolifyPosition(prevTransform.getPosition().getY());
    final int lastZ = protocolifyPosition(prevTransform.getPosition().getZ());
    final int lastYaw = protocolifyYaw(prevTransform.getRotation().getYaw());
    final int lastPitch = protocolifyPitch(prevTransform.getRotation().getPitch());

    final int newX = protocolifyPosition(newTransform.getPosition().getX());
    final int newY = protocolifyPosition(newTransform.getPosition().getY());
    final int newZ = protocolifyPosition(newTransform.getPosition().getZ());
    final int newYaw = protocolifyYaw(newTransform.getRotation().getYaw());
    final int newPitch = protocolifyPitch(newTransform.getRotation().getPitch());

    final int deltaX = newX - lastX;
    final int deltaY = newY - lastY;
    final int deltaZ = newZ - lastZ;
    final int deltaYaw = newYaw - lastYaw;
    final int deltaPitch = newPitch - lastPitch;

    final List<Message> messages = new ArrayList<Message>();

    /*
     * Two scenarios:
     * - The entity moves more than 4 blocks and maybe changes rotation.
     * - The entity moves less than 4 blocks and maybe changes rotation.
     */
    if (force
        || deltaX > 128
        || deltaX < -128
        || deltaY > 128
        || deltaY < -128
        || deltaZ > 128
        || deltaZ < -128) {
      messages.add(new EntityTeleportMessage(entity.getId(), newX, newY, newZ, newYaw, newPitch));
      if (force || looked) {
        messages.add(new EntityYawMessage(entity.getId(), newYaw, newPitch));
      }
    } else if (deltaX != 0 || deltaY != 0 || deltaZ != 0 || deltaYaw != 0 || deltaPitch != 0) {
      if (looked) {
        messages.add(
            new EntityRelativePositionYawMessage(
                entity.getId(), deltaX, deltaY, deltaZ, newYaw, newPitch));
      } else if (!prevTransform.getPosition().equals(newTransform.getPosition())) {
        messages.add(new EntityRelativePositionMessage(entity.getId(), deltaX, deltaY, deltaZ));
      }
    }

    // Head movement
    HeadComponent head = entity.get(HeadComponent.class);
    if (head != null && head.isDirty()) {
      final int headYawProt = ChannelBufferUtils.protocolifyYaw(head.getRotation().getYaw());
      messages.add(new EntityHeadYawMessage(entity.getId(), headYawProt));
    }

    // Physics
    // TODO: Actually not used?
    /*if (physics != null && physics.isLinearVelocityDirty()) {
    	messages.add(new EntityVelocityMessage(entity.getId(), new Vector3(0, 0, 0)));
    }*/

    // Extra metadata
    List<Parameter<?>> params = getUpdateParameters(entity);
    if (lastMeta == null || !lastMeta.equals(params)) {
      messages.add(new EntityMetadataMessage(entity.getId(), params));
      lastMeta = params;
    }

    return messages;
  }