@Override FMLNetworkEvent.CustomPacketEvent<?> make(FMLProxyPacket msg) { FMLNetworkEvent.CustomPacketEvent<?> event = null; if (msg.handler() instanceof NetHandlerPlayClient) { NetHandlerPlayClient client = (NetHandlerPlayClient) msg.handler(); event = new FMLNetworkEvent.ClientCustomPacketEvent(client.getNetworkManager(), msg); } else if (msg.handler() instanceof NetHandlerPlayServer) { NetHandlerPlayServer server = (NetHandlerPlayServer) msg.handler(); event = new FMLNetworkEvent.ServerCustomPacketEvent(server.getNetworkManager(), msg); } return event; }
@Override public void write(ChannelHandlerContext ctx, Object msg, ChannelPromise promise) throws Exception { if (msg instanceof FMLProxyPacket) { if (side == Side.CLIENT) { //Client to server large packets are not supported to prevent client being bad. ctx.write(((FMLProxyPacket) msg).toC17Packet(), promise); } else { List<Packet<INetHandlerPlayClient>> parts = ((FMLProxyPacket)msg).toS3FPackets(); for (Packet<INetHandlerPlayClient> pkt : parts) { ctx.write(pkt, promise); } } } else { ctx.write(msg, promise); } }
@Override protected void channelRead0(ChannelHandlerContext ctx, FMLProxyPacket msg) throws Exception { Side side = msg.getTarget(); NetworkManager manager = msg.getOrigin(); if (msg.channel().equals("REGISTER") || msg.channel().equals("UNREGISTER")) { byte[] data = new byte[msg.payload().readableBytes()]; msg.payload().readBytes(data); String channels = new String(data,Charsets.UTF_8); String[] split = channels.split("\0"); Set<String> channelSet = ImmutableSet.copyOf(split); FMLCommonHandler.instance().fireNetRegistrationEvent(manager, channelSet, msg.channel(), side); } else { ctx.fireChannelRead(msg); } }
@Override public List<NetworkDispatcher> selectNetworks(Object args, ChannelHandlerContext context, FMLProxyPacket packet) { int dimension = (Integer)args; ImmutableList.Builder<NetworkDispatcher> builder = ImmutableList.builder(); for (EntityPlayerMP player : FMLCommonHandler.instance().getMinecraftServerInstance().getPlayerList().getPlayerList()) { if (dimension == player.dimension) { NetworkDispatcher dispatcher = player.connection.netManager.channel().attr(NetworkDispatcher.FML_DISPATCHER).get(); // Null dispatchers may exist for fake players - skip them if (dispatcher != null) builder.add(dispatcher); } } return builder.build(); }
@Override public List<NetworkDispatcher> selectNetworks(Object args, ChannelHandlerContext context, FMLProxyPacket packet) { TargetPoint tp = (TargetPoint)args; ImmutableList.Builder<NetworkDispatcher> builder = ImmutableList.builder(); for (EntityPlayerMP player : FMLCommonHandler.instance().getMinecraftServerInstance().getPlayerList().getPlayerList()) { if (player.dimension == tp.dimension) { double d4 = tp.x - player.posX; double d5 = tp.y - player.posY; double d6 = tp.z - player.posZ; if (d4 * d4 + d5 * d5 + d6 * d6 < tp.range * tp.range) { NetworkDispatcher dispatcher = player.connection.netManager.channel().attr(NetworkDispatcher.FML_DISPATCHER).get(); if (dispatcher != null) builder.add(dispatcher); } } } return builder.build(); }
@Override protected final void encode(ChannelHandlerContext ctx, A msg, List<Object> out) throws Exception { PacketBuffer buffer = new PacketBuffer(Unpooled.buffer()); byte discriminator = types.get(msg.getClass()); buffer.writeByte(discriminator); encodeInto(ctx, msg, buffer); FMLProxyPacket proxy = new FMLProxyPacket(buffer/*.copy()*/, ctx.channel().attr(NetworkRegistry.FML_CHANNEL).get()); WeakReference<FMLProxyPacket> ref = ctx.attr(INBOUNDPACKETTRACKER).get().get(); FMLProxyPacket old = ref == null ? null : ref.get(); if (old != null) { proxy.setDispatcher(old.getDispatcher()); } out.add(proxy); }
@Override protected final void decode(ChannelHandlerContext ctx, FMLProxyPacket msg, List<Object> out) throws Exception { testMessageValidity(msg); ByteBuf payload = msg.payload().duplicate(); if (payload.readableBytes() < 1) { FMLLog.log(Level.ERROR, "The FMLIndexedCodec has received an empty buffer on channel %s, likely a result of a LAN server issue. Pipeline parts : %s", ctx.channel().attr(NetworkRegistry.FML_CHANNEL), ctx.pipeline().toString()); } byte discriminator = payload.readByte(); Class<? extends A> clazz = discriminators.get(discriminator); if(clazz == null) { throw new NullPointerException("Undefined message for discriminator " + discriminator + " in channel " + msg.channel()); } A newMsg = clazz.newInstance(); ctx.attr(INBOUNDPACKETTRACKER).get().set(new WeakReference<FMLProxyPacket>(msg)); decodeInto(ctx, payload.slice(), newMsg); out.add(newMsg); }
@SuppressWarnings( "unchecked" ) public void onPacketData( final FMLProxyPacket packet, final INetHandler handler, final EntityPlayerMP playerEntity ) { if ( playerEntity == null ) { return; } final PacketBuffer buffer = new PacketBuffer( packet.payload() ); final ModPacket innerPacket = parsePacket( buffer ); innerPacket.serverEntity = playerEntity; PacketThreadUtil.checkThreadAndEnqueue( innerPacket, handler, playerEntity.getServer() ); innerPacket.server( playerEntity ); }
void shareTransforms() { boolean needSync = (transformSynced.getGrandUnifiedDistance(transform) != 0 || velocitySynced.getGrandUnifiedDistance(velocity) != 0) && !hasOrders(); if (!needSync) return; try { ByteBuf output = Unpooled.buffer(); Core.network.prefixEntityPacket(output, this, SyncMessages.TRANSFORMS); putTransforms(new DataOutByteBuf(output, Side.SERVER)); FMLProxyPacket toSend = Core.network.entityPacket(output); Core.network.broadcastPacket(null, new Coord(this), toSend); } catch (IOException e) { e.printStackTrace(); return; } transformSynced = transform.copy(); velocitySynced = velocity.copy(); }
@Override public FMLProxyPacket getDescriptionPacket() { ByteBuf buf = Unpooled.buffer(); DataOutByteBuf data = new DataOutByteBuf(buf, Side.SERVER); try { NetworkFactorization.writeMessage(buf, FzNetEventHandler.TO_BLOCK, StandardMessageType.TileFzType); buf.writeInt(pos.getX()); buf.writeInt(pos.getY()); buf.writeInt(pos.getZ()); int ftId = getFactoryType().md; buf.writeByte(ftId); putData(data); return FzNetDispatch.generate(buf); } catch (IOException e) { e.printStackTrace(); return null; } }
@Override public boolean removedByPlayer(World world, BlockPos pos, EntityPlayer player, boolean willHarvest) { Coord here = new Coord(world, pos); TileEntityCommon tec = here.getTE(TileEntityCommon.class); if (tec == null) { if (!world.isRemote) { new Notice(here, "There was no TileEntity!").send(player); } return world.setBlockToAir(pos); } boolean ret = tec.removedByPlayer(player, willHarvest); if (!world.isRemote && !ret) { FMLProxyPacket description = tec.getDescriptionPacket(); Core.network.broadcastPacket(player, here, description); here.sendRedraw(); } return ret; }
@SubscribeEvent public void tickServer(TickEvent.ServerTickEvent event) { if (event.phase != TickEvent.Phase.START) return; MinecraftServer ms = MinecraftServer.getServer(); if (ms.getTickCounter() < ms.tickTimeArray.length) { //Ignore startup return; } if (measurements++ != FzConfig.tps_reporting_interval) { return; } measurements = 0; float tps = getTpsRatio(); if (tps != last_tps) { FMLProxyPacket packet = MiscellaneousNonsense.net.makeTpsReportPacket(getTpsRatio()); MiscNet.channel.sendToAll(packet); last_tps = tps; } }
@Override protected void encode (ChannelHandlerContext ctx, AbstractPacket msg, List<Object> out) throws Exception { ByteBuf buffer = Unpooled.buffer(); Class<? extends AbstractPacket> clazz = msg.getClass(); if (!this.packets.contains(msg.getClass())) { throw new NullPointerException("No Packet Registered for: " + msg.getClass().getCanonicalName()); } byte discriminator = (byte) this.packets.indexOf(clazz); buffer.writeByte(discriminator); msg.encodeInto(ctx, buffer); FMLProxyPacket proxyPacket = new FMLProxyPacket(new PacketBuffer(buffer.copy()), ctx.channel().attr(NetworkRegistry.FML_CHANNEL).get()); out.add(proxyPacket); }
@Override protected void encode(ChannelHandlerContext ctx, PacketAbstract msg, List<Object> out) throws Exception { ByteBuf buffer = Unpooled.buffer(); Class<? extends PacketAbstract> clazz = msg.getClass(); if (!packets.contains(msg.getClass())) { throw new NullPointerException("No Packet Registered for: " + msg.getClass().getCanonicalName()); } byte discriminator = (byte) this.packets.indexOf(clazz); buffer.writeByte(discriminator); msg.encodeInto(ctx, buffer); FMLProxyPacket proxyPacket = new FMLProxyPacket((PacketBuffer) buffer.copy(), ctx .channel().attr(NetworkRegistry.FML_CHANNEL).get()); out.add(proxyPacket); }
@Override protected void decode(ChannelHandlerContext ctx, FMLProxyPacket msg, List<Object> out) throws Exception { ByteBuf buf = msg.payload(); int packetId = buf.readByte(); Class pktCls = packets.get(packetId); SasaPacketBase pkt = (SasaPacketBase)pktCls.newInstance(); pkt.fromByteBuf(ctx, buf); if(FMLCommonHandler.instance().getEffectiveSide() == Side.CLIENT) { pkt.receiveClient(getClientPlayer()); } else if(FMLCommonHandler.instance().getEffectiveSide() == Side.SERVER) { INetHandler localINetHandler = (INetHandler)ctx.channel().attr(NetworkRegistry.NET_HANDLER).get(); EntityPlayer ep = ((NetHandlerPlayServer)localINetHandler).playerEntity; pkt.receiveServer(ep); } }
@Override protected void encode(ChannelHandlerContext ctx, ARKMessage msg, List<Object> out) throws Exception { ByteBuf buffer = Unpooled.buffer(); Class<? extends ARKMessage> clazz = msg.getClass(); if (!packets.contains(msg.getClass())) { throw new NullPointerException("No Packet Registered for: " + msg.getClass().getCanonicalName()); } byte discriminator = (byte) packets.indexOf(clazz); buffer.writeByte(discriminator); msg.encodeInto(ctx, buffer); FMLProxyPacket proxyPacket = new FMLProxyPacket((PacketBuffer) buffer, ctx.channel().attr(NetworkRegistry.FML_CHANNEL).get()); out.add(proxyPacket); }
@Override protected void encode(ChannelHandlerContext ctx, Packet msg, List<Object> out) throws Exception { ByteBuf buffer = Unpooled.buffer(); Class<? extends Packet> msgClass = msg.getClass(); if (!this.packets.contains(msg.getClass())) { throw new NullPointerException("No Packet Registered for: " + msg.getClass().getCanonicalName()); } byte discriminator = (byte) this.packets.indexOf(msgClass); buffer.writeByte(discriminator); msg.encode(ctx, buffer); FMLProxyPacket proxyPacket = new FMLProxyPacket((PacketBuffer) buffer.copy(), ctx.channel().attr(NetworkRegistry.FML_CHANNEL).get()); out.add(proxyPacket); }
@Override public void doSpecialAction(EntityPlayer entityPlayer, ItemStack itemStack) { FMLProxyPacket p; if(itemStack.getItem() instanceof IShield){ RayTraceResult mop = getMouseOver(4); if(mop != null && mop.entityHit instanceof EntityLivingBase){ p = new SpecialActionPacket(entityPlayer, mop.entityHit).generatePacket(); if(mop.entityHit instanceof EntityPlayerMP){ Battlegear.packetHandler.sendPacketToPlayer(p, (EntityPlayerMP) mop.entityHit); } Battlegear.packetHandler.sendPacketToServer(p); } } else{ p = new SpecialActionPacket(entityPlayer, null).generatePacket(); Battlegear.packetHandler.sendPacketToServer(p); } }
@Override protected void decode(ChannelHandlerContext ctx, FMLProxyPacket msg, List<Object> out) throws Exception { ByteBuf packetPayload = msg.payload(); byte indexOfClass = packetPayload.readByte(); ByteBufInputStream bbis = new ByteBufInputStream(packetPayload.slice()); Class<? extends CLLPacket> packetClass = registeredPackets.get(indexOfClass); if(packetClass != null){ CLLPacket packetCLL = packetClass.newInstance(); packetCLL.readDataFrom(bbis); switch(FMLCommonHandler.instance().getEffectiveSide()){ case CLIENT: handleClient(packetCLL); break; case SERVER: packetCLL.handleServerSide(((NetHandlerPlayServer)ctx.channel().attr(NetworkRegistry.NET_HANDLER).get()).player); break; } bbis.close(); out.add(packetCLL); } }
@Override protected void encode(ChannelHandlerContext ctx, RpcCall call, List<Object> out) throws Exception { final PacketBuffer output = new PacketBuffer(Unpooled.buffer()); { final IRpcTarget targetWrapper = call.target; int targetId = CommonRegistryCallbacks.mapObjectToId(targetRegistry, targetWrapper.getClass()); output.writeVarInt(targetId); targetWrapper.writeToStream(output); } { final BiMap<MethodEntry, Integer> eventIdMap = CommonRegistryCallbacks.getEntryIdMap(methodRegistry); int methodId = eventIdMap.get(call.method); output.writeVarInt(methodId); MethodParamsCodec paramsCodec = call.method.paramsCodec; paramsCodec.writeArgs(output, call.args); } FMLProxyPacket packet = new FMLProxyPacket(output, RpcCallDispatcher.CHANNEL_NAME); out.add(packet); }
@Override protected void encode(ChannelHandlerContext ctx, NetworkEvent msg, List<Object> out) throws IOException { final Channel channel = ctx.channel(); final Side side = channel.attr(NetworkRegistry.CHANNEL_SOURCE).get(); final NetworkEventEntry entry = CommonRegistryCallbacks.getObjectToEntryMap(registry).get(msg.getClass()); Preconditions.checkState(entry != null, "Can't find registration for class %s", msg.getClass()); final int id = CommonRegistryCallbacks.getEntryIdMap(registry).get(entry); final EventDirection validator = entry.getDirection(); Preconditions.checkState(validator != null && validator.validateSend(side), "Invalid direction: sending packet %s on side %s", msg.getClass(), side); final PacketBuffer buf = new PacketBuffer(Unpooled.buffer()); buf.writeVarInt(id); msg.writeToStream(buf); final FMLProxyPacket packet = new FMLProxyPacket(buf, NetworkEventDispatcher.CHANNEL_NAME); packet.setDispatcher(msg.dispatcher); out.add(packet); }
@Override protected void channelRead0(ChannelHandlerContext ctx, FMLProxyPacket msg) throws Exception { final PacketDescription packet = new PacketDescription(); packet.fromBytes(msg.payload()); PneumaticCraftRepressurized.proxy.addScheduledTask(new Runnable() { @Override public void run() { packet.handleClientSide(packet, PneumaticCraftRepressurized.proxy.getPlayer()); } }, false); }
@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; }
void fireRead(FMLProxyPacket msg, ChannelHandlerContext ctx) { FMLNetworkEvent.CustomPacketEvent<?> event = factory.make(msg); if (event != null) { this.eventBus.post(event); if (event.getReply() != null) { ctx.channel().attr(FMLOutboundHandler.FML_MESSAGETARGET).set(FMLOutboundHandler.OutboundTarget.REPLY); ctx.writeAndFlush(event.getReply()).addListener(ChannelFutureListener.FIRE_EXCEPTION_ON_FAILURE); } } }
/** * Send to a specific player * * @param pkt * @param player */ public void sendTo(FMLProxyPacket pkt, EntityPlayerMP player) { channels.get(Side.SERVER).attr(FMLOutboundHandler.FML_MESSAGETARGET).set(FMLOutboundHandler.OutboundTarget.PLAYER); channels.get(Side.SERVER).attr(FMLOutboundHandler.FML_MESSAGETARGETARGS).set(player); channels.get(Side.SERVER).writeAndFlush(pkt).addListener(ChannelFutureListener.FIRE_EXCEPTION_ON_FAILURE); }
/** * Send to all around a point * @param pkt * @param point */ public void sendToAllAround(FMLProxyPacket pkt, NetworkRegistry.TargetPoint point) { channels.get(Side.SERVER).attr(FMLOutboundHandler.FML_MESSAGETARGET).set(FMLOutboundHandler.OutboundTarget.ALLAROUNDPOINT); channels.get(Side.SERVER).attr(FMLOutboundHandler.FML_MESSAGETARGETARGS).set(point); channels.get(Side.SERVER).writeAndFlush(pkt).addListener(ChannelFutureListener.FIRE_EXCEPTION_ON_FAILURE); }
/** * Send to all in a dimension * @param pkt * @param dimensionId */ public void sendToDimension(FMLProxyPacket pkt, int dimensionId) { channels.get(Side.SERVER).attr(FMLOutboundHandler.FML_MESSAGETARGET).set(FMLOutboundHandler.OutboundTarget.DIMENSION); channels.get(Side.SERVER).attr(FMLOutboundHandler.FML_MESSAGETARGETARGS).set(dimensionId); channels.get(Side.SERVER).writeAndFlush(pkt).addListener(ChannelFutureListener.FIRE_EXCEPTION_ON_FAILURE); }
private MultiPartCustomPayload(PacketBuffer preamble) throws IOException { channel = preamble.readStringFromBuffer(20); part_count = preamble.readUnsignedByte(); int length = preamble.readInt(); if (length <= 0 || length >= FMLProxyPacket.MAX_LENGTH) { throw new IOException("The received FML MultiPart packet outside of valid length bounds, Max: " + FMLProxyPacket.MAX_LENGTH + ", Received: " + length); } data = new byte[length]; data_buf = new PacketBuffer(Unpooled.wrappedBuffer(data)); }
@Override public void write(ChannelHandlerContext ctx, Object msg, ChannelPromise promise) throws Exception { if (msg instanceof FMLProxyPacket) { this.dispatcher.sendProxy((FMLProxyPacket) msg); } }
@Override public List<NetworkDispatcher> selectNetworks(Object args, ChannelHandlerContext context, FMLProxyPacket packet) { EntityPlayerMP player = (EntityPlayerMP) args; NetworkDispatcher dispatcher = (player == null || player instanceof FakePlayer) ? null : player.connection.netManager.channel().attr(NetworkDispatcher.FML_DISPATCHER).get(); return dispatcher == null ? ImmutableList.<NetworkDispatcher>of() : ImmutableList.of(dispatcher); }
@Override public List<NetworkDispatcher> selectNetworks(Object args, ChannelHandlerContext context, FMLProxyPacket packet) { ImmutableList.Builder<NetworkDispatcher> builder = ImmutableList.builder(); for (EntityPlayerMP player : FMLCommonHandler.instance().getMinecraftServerInstance().getPlayerList().getPlayerList()) { NetworkDispatcher dispatcher = player.connection.netManager.channel().attr(NetworkDispatcher.FML_DISPATCHER).get(); if (dispatcher != null) builder.add(dispatcher); } return builder.build(); }
public FMLProxyPacket toPacket() { if (incoming()) { throw new IllegalStateException("Tried to write an incoming packet"); } if (readableBytes() > 32000 || (type & 0x80) != 0) { do_compress(); } return new FMLProxyPacket(new PacketBuffer(copy()), channel); }
@Override protected void encode(ChannelHandlerContext ctx, ARKMessage msg, List<Object> out) throws Exception { ByteBuf buffer = Unpooled.buffer(); Class<? extends ARKMessage> clazz = msg.getClass(); if (!packets.contains(msg.getClass())) { throw new NullPointerException("No Packet Registered for: " + msg .getClass().getCanonicalName()); } byte discriminator = (byte) packets.indexOf(clazz); buffer.writeByte(discriminator); msg.encodeInto(ctx, buffer); FMLProxyPacket proxyPacket = new FMLProxyPacket((PacketBuffer) buffer, ctx.channel().attr( NetworkRegistry.FML_CHANNEL).get()); out.add(proxyPacket); }
@Override protected void decode(ChannelHandlerContext ctx, FMLProxyPacket msg, List<Object> out) throws Exception { ByteBuf payload = msg.payload(); byte discriminator = payload.readByte(); Class<? extends ARKMessage> clazz = packets.get(discriminator); if (clazz == null) { throw new NullPointerException("No packet registered for discriminator: " + discriminator); } ARKMessage pkt = clazz.newInstance(); pkt.decodeInto(ctx, payload.slice()); EntityPlayer player; switch (FMLCommonHandler.instance().getEffectiveSide()) { case CLIENT: player = getClientPlayer(); pkt.handleClientSide(player); break; case SERVER: INetHandler netHandler = ctx.channel().attr(NetworkRegistry.NET_HANDLER).get(); player = ((NetHandlerPlayServer) netHandler).playerEntity; pkt.handleServerSide(player); break; default: } out.add(pkt); }
@Override protected void channelRead0(ChannelHandlerContext ctx, FMLProxyPacket msg) { PacketDescription packet = new PacketDescription(); packet.fromBytes(msg.payload()); packet.handleClientSide(Signals.proxy.getPlayer()); }
@SuppressWarnings( "unchecked" ) public void onPacketData( final FMLProxyPacket packet, final INetHandler handler ) { final PacketBuffer buffer = new PacketBuffer( packet.payload() ); final ModPacket innerPacket = parsePacket( buffer ); PacketThreadUtil.checkThreadAndEnqueue( innerPacket, handler, Minecraft.getMinecraft() ); innerPacket.client(); }
private FMLProxyPacket getProxyPacket( final ModPacket packet ) { final PacketBuffer buffer = new PacketBuffer( Unpooled.buffer() ); buffer.writeByte( ModPacketTypes.getID( packet.getClass() ) ); packet.getPayload( buffer ); return new FMLProxyPacket( buffer, channelName ); }
public static void fx(Coord at, EnumFacing side, FlatFace face, byte type) { ByteBuf buff = prepare(type); writeCoord(buff, at); writeSide(buff, side); writeFace(buff, face); FMLProxyPacket packet = build(buff); sendAround(at.getChunk(), packet); }
static FMLProxyPacket syncChunk(EntityPlayer player, Coord at) { IExtraChunkData ecd = (IExtraChunkData) at.getChunk(); FlatChunkLayer data = ecd.getFlatLayer(); SyncWrite writer = new SyncWrite(); data.iterate(writer); writer.finish(); return build(writer.buff); }
static void send(EntityPlayer player, FMLProxyPacket toSend) { if (player.worldObj.isRemote) { channel.sendToServer(toSend); } else { channel.sendTo(toSend, (EntityPlayerMP) player); } }