protected void streamACK(String identifier, int identifierACK) {
   byte[] buffer =
       Binary.appendBytes(
           RakNet.PACKET_ACK_NOTIFICATION,
           new byte[] {(byte) (identifier.length() & 0xff)},
           identifier.getBytes(StandardCharsets.UTF_8),
           Binary.writeInt(identifierACK));
   this.server.pushThreadToMainPacket(buffer);
 }
 public void streamRAW(String address, int port, byte[] payload) {
   byte[] buffer =
       Binary.appendBytes(
           RakNet.PACKET_RAW,
           new byte[] {(byte) (address.length() & 0xff)},
           address.getBytes(StandardCharsets.UTF_8),
           Binary.writeShort(port),
           payload);
   this.server.pushThreadToMainPacket(buffer);
 }
 protected void streamOpen(Session session) {
   String identifier = session.getAddress() + ":" + session.getPort();
   byte[] buffer =
       Binary.appendBytes(
           RakNet.PACKET_OPEN_SESSION,
           new byte[] {(byte) (identifier.length() & 0xff)},
           identifier.getBytes(StandardCharsets.UTF_8),
           new byte[] {(byte) (session.getAddress().length() & 0xff)},
           session.getAddress().getBytes(StandardCharsets.UTF_8),
           Binary.writeShort(session.getPort()),
           Binary.writeLong(session.getID()));
   this.server.pushThreadToMainPacket(buffer);
 }
 protected void streamInvalid(String identifier) {
   byte[] buffer =
       Binary.appendBytes(
           RakNet.PACKET_INVALID_SESSION,
           new byte[] {(byte) (identifier.length() & 0xff)},
           identifier.getBytes(StandardCharsets.UTF_8));
   this.server.pushThreadToMainPacket(buffer);
 }
 protected void streamOption(String name, String value) {
   byte[] buffer =
       Binary.appendBytes(
           RakNet.PACKET_SET_OPTION,
           new byte[] {(byte) (name.length() & 0xff)},
           name.getBytes(StandardCharsets.UTF_8),
           value.getBytes(StandardCharsets.UTF_8));
   this.server.pushThreadToMainPacket(buffer);
 }
 protected void streamClose(String identifier, String reason) {
   byte[] buffer =
       Binary.appendBytes(
           RakNet.PACKET_CLOSE_SESSION,
           new byte[] {(byte) (identifier.length() & 0xff)},
           identifier.getBytes(StandardCharsets.UTF_8),
           new byte[] {(byte) (reason.length() & 0xff)},
           reason.getBytes(StandardCharsets.UTF_8));
   this.server.pushThreadToMainPacket(buffer);
 }
 public void streamEncapsulated(Session session, EncapsulatedPacket packet, int flags) {
   String id = session.getAddress() + ":" + session.getPort();
   byte[] buffer =
       Binary.appendBytes(
           RakNet.PACKET_ENCAPSULATED,
           new byte[] {(byte) (id.length() & 0xff)},
           id.getBytes(StandardCharsets.UTF_8),
           new byte[] {(byte) (flags & 0xff)},
           packet.toBinary(true));
   this.server.pushThreadToMainPacket(buffer);
 }
Esempio n. 8
0
  @Override
  public AsyncTask requestChunkTask(int x, int z) throws ChunkException {
    FullChunk chunk = this.getChunk(x, z, false);
    if (chunk == null) {
      throw new ChunkException("Invalid Chunk Set");
    }

    byte[] tiles = new byte[0];

    if (!chunk.getTiles().isEmpty()) {
      List<CompoundTag> tagList = new ArrayList<>();

      for (Tile tile : chunk.getTiles().values()) {
        if (tile instanceof Spawnable) {
          tagList.add(((Spawnable) tile).getSpawnCompound());
        }
      }

      try {
        tiles = NBTIO.write(tagList, ByteOrder.LITTLE_ENDIAN);
      } catch (IOException e) {
        throw new RuntimeException(e);
      }
    }

    BinaryStream extraData = new BinaryStream();
    extraData.putLInt(chunk.getBlockExtraDataArray().size());
    for (Integer key : chunk.getBlockExtraDataArray().values()) {
      extraData.putLInt(key);
      extraData.putLShort(chunk.getBlockExtraDataArray().get(key));
    }

    BinaryStream stream = new BinaryStream();
    stream.put(chunk.getBlockIdArray());
    stream.put(chunk.getBlockDataArray());
    stream.put(chunk.getBlockSkyLightArray());
    stream.put(chunk.getBlockLightArray());
    for (int height : chunk.getHeightMapArray()) {
      stream.putByte((byte) (height & 0xff));
    }
    for (int color : chunk.getBiomeColorArray()) {
      stream.put(Binary.writeInt(color));
    }
    stream.put(extraData.getBuffer());
    stream.put(tiles);

    this.getLevel()
        .chunkRequestCallback(x, z, stream.getBuffer(), FullChunkDataPacket.ORDER_LAYERED);

    return null;
  }
  public boolean receiveStream() throws Exception {
    byte[] packet = this.server.readMainToThreadPacket();
    if (packet != null && packet.length > 0) {
      byte id = packet[0];
      int offset = 1;
      switch (id) {
        case RakNet.PACKET_ENCAPSULATED:
          int len = packet[offset++];
          String identifier =
              new String(Binary.subBytes(packet, offset, len), StandardCharsets.UTF_8);
          offset += len;
          if (this.sessions.containsKey(identifier)) {
            byte flags = packet[offset++];
            byte[] buffer = Binary.subBytes(packet, offset);
            this.sessions
                .get(identifier)
                .addEncapsulatedToQueue(EncapsulatedPacket.fromBinary(buffer, true), flags);
          } else {
            this.streamInvalid(identifier);
          }
          break;
        case RakNet.PACKET_RAW:
          len = packet[offset++];
          String address = new String(Binary.subBytes(packet, offset, len), StandardCharsets.UTF_8);
          offset += len;
          int port = Binary.readShort(Binary.subBytes(packet, offset, 2));
          offset += 2;
          byte[] payload = Binary.subBytes(packet, offset);
          this.socket.writePacket(payload, address, port);
          break;
        case RakNet.PACKET_CLOSE_SESSION:
          len = packet[offset++];
          identifier = new String(Binary.subBytes(packet, offset, len), StandardCharsets.UTF_8);
          if (this.sessions.containsKey(identifier)) {
            this.removeSession(this.sessions.get(identifier));
          } else {
            this.streamInvalid(identifier);
          }
          break;
        case RakNet.PACKET_INVALID_SESSION:
          len = packet[offset++];
          identifier = new String(Binary.subBytes(packet, offset, len), StandardCharsets.UTF_8);
          if (this.sessions.containsKey(identifier)) {
            this.removeSession(this.sessions.get(identifier));
          }
          break;
        case RakNet.PACKET_SET_OPTION:
          len = packet[offset++];
          String name = new String(Binary.subBytes(packet, offset, len), StandardCharsets.UTF_8);
          offset += len;
          String value = new String(Binary.subBytes(packet, offset), StandardCharsets.UTF_8);
          switch (name) {
            case "name":
              this.name = value;
              break;
            case "portChecking":
              this.portChecking = Boolean.valueOf(value);
              break;
            case "packetLimit":
              this.packetLimit = Integer.valueOf(value);
              break;
          }
          break;
        case RakNet.PACKET_BLOCK_ADDRESS:
          len = packet[offset++];
          address = new String(Binary.subBytes(packet, offset, len), StandardCharsets.UTF_8);
          offset += len;
          int timeout = Binary.readInt(Binary.subBytes(packet, offset, 4));
          this.blockAddress(address, timeout);
          break;
        case RakNet.PACKET_SHUTDOWN:
          for (Session session : new ArrayList<>(this.sessions.values())) {
            this.removeSession(session);
          }

          this.socket.close();
          this.shutdown = true;
          break;
        case RakNet.PACKET_EMERGENCY_SHUTDOWN:
          this.shutdown = true;
        default:
          return false;
      }
      return true;
    }

    return false;
  }