@SubscribeEvent
  public void onPacketReceive(ClientCustomPacketEvent event) {
    // This method receives the TerrainControl packet with the custom
    // biome colors and weather.

    FMLProxyPacket receivedPacket = event.getPacket();

    // We're on the client, receive the packet
    ByteBuf stream = receivedPacket.payload();
    try {
      int serverProtocolVersion = stream.readInt();
      int clientProtocolVersion = PluginStandardValues.ProtocolVersion;
      if (serverProtocolVersion == clientProtocolVersion) {
        // Server sent config
        WorldClient worldMC = FMLClientHandler.instance().getClient().theWorld;

        if (stream.readableBytes() > 4 && worldMC != null) {
          // If the packet wasn't empty, and the client world exists:
          // add the new biomes.
          // (If no client world exists yet, then we're on a local
          // server, and we can discard the packet.)

          DataInputStream wrappedStream = new DataInputStream(new ByteBufInputStream(stream));

          worldLoader.demandClientWorld(worldMC, wrappedStream);
        }

        TerrainControl.log(LogMarker.INFO, "Config received from server");
      } else {
        // Server or client is outdated
        if (serverProtocolVersion > PluginStandardValues.ProtocolVersion) {
          sendMessage(
              TextFormatting.GREEN,
              "The server is running a newer version of "
                  + PluginStandardValues.PLUGIN_NAME
                  + ". Please update!");
        } else {
          sendMessage(
              TextFormatting.YELLOW,
              "The server is running an outdated version of "
                  + PluginStandardValues.PLUGIN_NAME
                  + ". Cannot load custom biome colors and weather.");
        }
        TerrainControl.log(
            LogMarker.WARN,
            "Server has different protocol version. Client: {} Server: {}",
            PluginStandardValues.ProtocolVersion,
            serverProtocolVersion);
      }
    } catch (Exception e) {
      TerrainControl.log(LogMarker.FATAL, "Failed to receive packet");
      TerrainControl.printStackTrace(LogMarker.FATAL, e);
      TerrainControl.log(LogMarker.FATAL, "Packet contents: {}", Arrays.toString(stream.array()));
      sendMessage(TextFormatting.RED, "Error receiving packet.");
    }
  }
예제 #2
0
  @SubscribeEvent
  public void onPlayerLogin(PlayerEvent.PlayerLoggedInEvent event) {
    // Server-side - called whenever a player logs in
    // I couldn't find a way to detect if the client has TerrainControl,
    // so for now the configs are sent anyway.

    // Get the config
    if (!(event.player instanceof EntityPlayerMP)) {
      return;
    }

    EntityPlayerMP player = (EntityPlayerMP) event.player;

    LocalWorld worldTC = worldLoader.getWorld(player.getEntityWorld());
    if (worldTC == null) {
      // World not loaded
      return;
    }
    ConfigProvider configs = worldTC.getConfigs();

    // Serialize it
    ByteBuf nettyBuffer = Unpooled.buffer();
    PacketBuffer mojangBuffer = new PacketBuffer(nettyBuffer);

    DataOutput stream = new ByteBufOutputStream(nettyBuffer);
    try {
      stream.writeInt(PluginStandardValues.ProtocolVersion);
      ConfigToNetworkSender.send(configs, stream);
    } catch (IOException e) {
      TerrainControl.printStackTrace(LogMarker.FATAL, e);
    }

    // Make the packet
    SPacketCustomPayload packet =
        new SPacketCustomPayload(PluginStandardValues.ChannelName, mojangBuffer);

    // Send the packet
    player.connection.sendPacket(packet);
  }