@Override FMLNetworkEvent.CustomPacketEvent<?> make(FMLProxyPacket msg) { FMLNetworkEvent.CustomPacketEvent<?> event = null; if (msg.handler() instanceof NetHandlerPlayServer) { NetHandlerPlayServer server = (NetHandlerPlayServer) msg.handler(); event = new FMLNetworkEvent.ServerCustomPacketEvent(server.getNetworkManager(), msg); } return event; }
@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."); } }
@Override protected void decode( ChannelHandlerContext channelHandlerContext, FMLProxyPacket fmlProxyPacket, List<Object> out) throws Exception { ByteBuf payload = fmlProxyPacket.payload(); byte discriminator = payload.readByte(); Class<? extends AbstractPacketOld> clazz = this.packets.get(discriminator); if (clazz == null) { throw new NullPointerException("No packet registered for discriminator: " + discriminator); } AbstractPacketOld packet = clazz.newInstance(); packet.decodeInto(channelHandlerContext, payload.slice()); EntityPlayer player; switch (FMLCommonHandler.instance().getEffectiveSide()) { case CLIENT: player = PacketHandlerOld.getClientPlayer(); packet.handleClientSide(player); break; case SERVER: INetHandler netHandler = channelHandlerContext.channel().attr(NetworkRegistry.NET_HANDLER).get(); player = ((NetHandlerPlayServer) netHandler).playerEntity; packet.handleServerSide(player); break; default: } out.add(packet); }
private boolean handleClientSideCustomPacket( S3FPacketCustomPayload msg, ChannelHandlerContext context) { String channelName = msg.getChannelName(); if ("FML|MP".equals(channelName)) { try { if (multipart == null) { multipart = new MultiPartCustomPayload(msg.getBufferData()); } else { multipart.processPart(msg.getBufferData()); } } catch (IOException e) { this.kickWithMessage(e.getMessage()); multipart = null; return true; } if (multipart.isComplete()) { msg = multipart; channelName = msg.getChannelName(); multipart = null; } else { return true; // Haven't received all so return till we have. } } if ("FML|HS".equals(channelName) || "REGISTER".equals(channelName) || "UNREGISTER".equals(channelName)) { FMLProxyPacket proxy = new FMLProxyPacket(msg); proxy.setDispatcher(this); handshakeChannel.writeInbound(proxy); // forward any messages into the regular channel for (Object push : handshakeChannel.inboundMessages()) { List<FMLProxyPacket> messageResult = FMLNetworkHandler.forwardHandshake( (FMLMessage.CompleteHandshake) push, this, Side.CLIENT); for (FMLProxyPacket result : messageResult) { result.setTarget(Side.CLIENT); result.payload().resetReaderIndex(); context.fireChannelRead(result); } } handshakeChannel.inboundMessages().clear(); return true; } else if (NetworkRegistry.INSTANCE.hasChannel(channelName, Side.CLIENT)) { FMLProxyPacket proxy = new FMLProxyPacket(msg); proxy.setDispatcher(this); context.fireChannelRead(proxy); return true; } return false; }
private boolean handleServerSideCustomPacket( C17PacketCustomPayload msg, ChannelHandlerContext context) { if (state == ConnectionState.AWAITING_HANDSHAKE) { synchronized (this) { // guard from other threads changing the state on us if (state == ConnectionState.AWAITING_HANDSHAKE) { state = ConnectionState.HANDSHAKING; } } } String channelName = msg.getChannelName(); if ("FML|HS".equals(channelName) || "REGISTER".equals(channelName) || "UNREGISTER".equals(channelName)) { FMLProxyPacket proxy = new FMLProxyPacket(msg); proxy.setDispatcher(this); handshakeChannel.writeInbound(proxy); for (Object push : handshakeChannel.inboundMessages()) { List<FMLProxyPacket> messageResult = FMLNetworkHandler.forwardHandshake( (FMLMessage.CompleteHandshake) push, this, Side.SERVER); for (FMLProxyPacket result : messageResult) { result.setTarget(Side.SERVER); result.payload().resetReaderIndex(); context.fireChannelRead(result); } } handshakeChannel.inboundMessages().clear(); return true; } else if (NetworkRegistry.INSTANCE.hasChannel(channelName, Side.SERVER)) { FMLProxyPacket proxy = new FMLProxyPacket(msg); proxy.setDispatcher(this); context.fireChannelRead(proxy); return true; } return false; }