public static List<XUPacketSplit> splitPacket(final FMLProxyPacket packet) { final List<XUPacketSplit> out = new ArrayList<XUPacketSplit>(); final ByteBuf buf = packet.payload().copy(); int n = buf.readableBytes() / 2007136; if (n * 2007136 < buf.readableBytes()) { ++n; } ++PacketSplitter.curSendingIndex; LogHelper.debug("Splitting packet to " + n + " packets", new Object[0]); for (int i = 0; i < n; ++i) { final int s = (buf.readableBytes() < 2007136) ? buf.readableBytes() : 2007136; final ByteBuf o = Unpooled.buffer(s); buf.readBytes(o, s); out.add(new XUPacketSplit(buf, PacketSplitter.curSendingIndex, i, n)); } return out; }
@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.func_147298_b(), msg); } else if (msg.handler() instanceof NetHandlerPlayServer) { NetHandlerPlayServer server = (NetHandlerPlayServer) msg.handler(); event = new FMLNetworkEvent.ServerCustomPacketEvent(server.func_147362_b(), msg); } return event; }
@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); } }
@SuppressWarnings("unchecked") @Override public List<NetworkDispatcher> selectNetworks(Object args, ChannelHandlerContext context, FMLProxyPacket packet) { int dimension = (Integer)args; ImmutableList.Builder<NetworkDispatcher> builder = ImmutableList.<NetworkDispatcher>builder(); for (EntityPlayerMP player : (List<EntityPlayerMP>)FMLCommonHandler.instance().getMinecraftServerInstance().func_71203_ab().field_72404_b) { if (dimension == player.field_71093_bK) { NetworkDispatcher dispatcher = player.field_71135_a.field_147371_a.channel().attr(NetworkDispatcher.FML_DISPATCHER).get(); // Null dispatchers may exist for fake players - skip them if (dispatcher != null) builder.add(dispatcher); } } return builder.build(); }
@SuppressWarnings("unchecked") @Override public List<NetworkDispatcher> selectNetworks(Object args, ChannelHandlerContext context, FMLProxyPacket packet) { TargetPoint tp = (TargetPoint)args; ImmutableList.Builder<NetworkDispatcher> builder = ImmutableList.<NetworkDispatcher>builder(); for (EntityPlayerMP player : (List<EntityPlayerMP>)FMLCommonHandler.instance().getMinecraftServerInstance().func_71203_ab().field_72404_b) { if (player.field_71093_bK == tp.dimension) { double d4 = tp.x - player.field_70165_t; double d5 = tp.y - player.field_70163_u; double d6 = tp.z - player.field_70161_v; if (d4 * d4 + d5 * d5 + d6 * d6 < tp.range * tp.range) { NetworkDispatcher dispatcher = player.field_71135_a.field_147371_a.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 { ByteBuf buffer = Unpooled.buffer(); @SuppressWarnings("unchecked") // Stupid unnecessary cast I can't seem to kill Class<? extends A> clazz = (Class<? extends A>) msg.getClass(); byte discriminator = types.get(clazz); 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(); 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); }
protected void handlePacket(FMLProxyPacket proxyPacket, EntityPlayerMP player) { final ByteBuf payload = proxyPacket.payload(); if( payload.readableBytes() > 0 ) { final PacketBuffer buf = new PacketBuffer(payload); final byte id = buf.readByte(); final PacketType type = PacketType.get(id); if( type == null ) { FairyFactions.LOGGER.error("Got unknown packet type "+id); return; } final FairyPacket packet; try { packet = type.packet_class.newInstance(); } catch (Exception e) { // ERROR, unable to actually process e.printStackTrace(); return; } packet.init(buf); packet.handle(proxyPacket.getOrigin()); } }
@SideOnly(Side.CLIENT) public static FMLProxyPacket createPacket(WorldView worldview) { // This line may look like black magic (and, well, it is), but it's actually just returning a class reference for this class. Copy-paste safe. ByteBuf data = PacketHandlerBase.createDataBuffer((Class<? extends PacketHandlerBase>) new Object() {}.getClass().getEnclosingClass()); int x = 0; int y = -1; int z = 0; if (worldview.coords != null) { x = worldview.coords.posX >> 4; y = worldview.coords.posY >> 4; z = worldview.coords.posZ >> 4; } data.writeInt(worldview.getWorldObj().provider.dimensionId); data.writeInt(x); data.writeInt(y); data.writeInt(z); data.writeByte(Math.min(ModConfigs.renderDistance, Minecraft.getMinecraft().gameSettings.renderDistanceChunks)); return buildPacket(data); }
@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.func_147362_b(), msg); } return event; }
@SuppressWarnings("unchecked") @Override public List<NetworkDispatcher> selectNetworks(Object args, ChannelHandlerContext context, FMLProxyPacket packet) { int dimension = (Integer)args; ImmutableList.Builder<NetworkDispatcher> builder = ImmutableList.<NetworkDispatcher>builder(); for (EntityPlayerMP player : (List<EntityPlayerMP>)FMLCommonHandler.instance().getMinecraftServerInstance().getConfigurationManager().playerEntityList) { if (dimension == player.dimension) { NetworkDispatcher dispatcher = player.playerNetServerHandler.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(); }
@SuppressWarnings("unchecked") @Override public List<NetworkDispatcher> selectNetworks(Object args, ChannelHandlerContext context, FMLProxyPacket packet) { TargetPoint tp = (TargetPoint)args; ImmutableList.Builder<NetworkDispatcher> builder = ImmutableList.<NetworkDispatcher>builder(); for (EntityPlayerMP player : (List<EntityPlayerMP>)FMLCommonHandler.instance().getMinecraftServerInstance().getConfigurationManager().playerEntityList) { 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.playerNetServerHandler.netManager.channel().attr(NetworkDispatcher.FML_DISPATCHER).get(); if (dispatcher != null) builder.add(dispatcher); } } } return builder.build(); }
public static FMLProxyPacket createConfigPacket() { ByteBuf payload = Unpooled.buffer(); payload.writeByte(ID_CONFIG_OVERRIDE); NBTTagCompound nbt = AmunRa.config.getServerOverrideData(); try { NetworkUtil.writeNBTTagCompound(nbt, payload); } catch (IOException e) { e.printStackTrace(); } return new FMLProxyPacket(payload, CHANNEL); }
public static FMLProxyPacket createMothershipPacket() { ByteBuf payload = Unpooled.buffer(); payload.writeByte(ID_MOTHERSHIP_LIST); NBTTagCompound nbt = new NBTTagCompound (); TickHandlerServer.mothershipData.writeToNBT(nbt); try { NetworkUtil.writeNBTTagCompound(nbt, payload); } catch (IOException e) { e.printStackTrace(); } return new FMLProxyPacket(payload, CHANNEL); }
@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(buffer.copy(), ctx.channel().attr(NetworkRegistry.FML_CHANNEL).get()); out.add(proxyPacket); }
public static void removeCorruptionEffectForPlayer(EntityPlayer entityPlayer, String name) { NBTTagCompound corruptionData = getCorruptionDataForPlayer(entityPlayer); NBTTagList corrEffects = corruptionData.getTagList("CorruptionEffects", 8); if (corrEffects != null && corrEffects.tagCount() > 0) { NBTTagList copy = (NBTTagList) corrEffects.copy(); for (int i = 0; i < copy.tagCount(); i++) { if (name.equals(copy.getStringTagAt(i))) { corrEffects.removeTag(i); break; } } } corruptionData.setTag("CorruptionEffects", corrEffects); FMLProxyPacket packet = PacketEventHandler.getCorruptionEffectPacket(entityPlayer.getCommandSenderName(), name, false, false); Corruption.eventChannel.sendToAll(packet); }
@Override protected void channelRead0(ChannelHandlerContext ctx, FMLProxyPacket packet) throws Exception { if(count == 0) first = System.currentTimeMillis(); count++; if(count % 1000 == 0){ double delta = System.currentTimeMillis() - last; last = System.currentTimeMillis(); if(first != last && delta != 0) System.out.println(count + "packets received. approx " + (1000000D/delta) + "/sec. Avg:" + (count/(last-first)) + "/sec"); } Side side = FMLCommonHandler.instance().getEffectiveSide(); if (side == Side.CLIENT) { onClientPacketData(packet); } else { onServerPacketData(packet); } }
@Override protected void encode(ChannelHandlerContext ctx, ASMessage msg, List<Object> out) throws Exception { ByteBuf buffer = Unpooled.buffer(); Class<? extends ASMessage> 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); try { msg.encodeInto(ctx, buffer); } catch (IOException e) { e.printStackTrace(); throw e; } FMLProxyPacket proxyPacket = new FMLProxyPacket(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 AbstractPacket> clazz = this.packets.get(discriminator); AbstractPacket pkt = clazz.newInstance(); pkt.decodeInto(ctx, payload.slice()); switch (FMLCommonHandler.instance().getEffectiveSide()) { case CLIENT: pkt.handleClientSide(this.getClientPlayer()); break; case SERVER: INetHandler netHandler = ctx.channel().attr(NetworkRegistry.NET_HANDLER).get(); pkt.handleServerSide(((NetHandlerPlayServer) netHandler).playerEntity); break; default: break; } out.add(pkt); }
public static FMLProxyPacket createDimPacket(Integer[] dims) { ArrayList<Integer> data = new ArrayList(); for (int i = 0; i < dims.length; i++) data.add(dims[i]); return createPacket((byte) 101, data); }
public static FMLProxyPacket createSSPacket(Integer[] dims) { ArrayList<Integer> data = new ArrayList(); for (int i = 0; i < dims.length; i++) data.add(dims[i]); return createPacket((byte) 102, data); }
public static FMLProxyPacket createPacket(byte packetId, Collection<Integer> data) { ByteBuf payload = Unpooled.buffer(); payload.writeByte(packetId); payload.writeInt(data.size()); for (Integer i : data) { payload.writeInt(i.intValue()); } payload.writeInt(3519); //signature return new FMLProxyPacket(payload, CHANNEL); }
public static FMLProxyPacket createConfigPacket(List<Object> data) { ByteBuf payload = Unpooled.buffer(); payload.writeByte(103); try { NetworkUtil.encodeData(payload, data); } catch (IOException e) { e.printStackTrace(); } payload.writeInt(3519); //signature return new FMLProxyPacket(payload, CHANNEL); }
@SubscribeEvent @SideOnly(Side.CLIENT) public void onPacketData(FMLNetworkEvent.ClientCustomPacketEvent event) { FMLProxyPacket pkt = event.packet; onFMLProxyPacketData(event.manager, pkt, Minecraft.getMinecraft().thePlayer); }
@SubscribeEvent public void onPacketData(FMLNetworkEvent.ServerCustomPacketEvent event) { FMLProxyPacket pkt = event.packet; onFMLProxyPacketData(event.manager, pkt, ((NetHandlerPlayServer)event.handler).playerEntity); }
public void onFMLProxyPacketData(NetworkManager manager, FMLProxyPacket packet, EntityPlayer player) { try { if ((packet == null) || (packet.payload() == null)) throw new RuntimeException("Empty packet sent to Galacticraft channel"); ByteBuf data = packet.payload(); this.handle(data, player); } catch (Exception e) { GCLog.severe("GC login packet handler: Failed to read packet"); GCLog.severe(e.toString()); e.printStackTrace(); } }
@Override protected void channelRead0(ChannelHandlerContext ctx, FMLProxyPacket msg) throws Exception { handlers.get(ctx.channel().attr(NetworkRegistry.CHANNEL_SOURCE).get()) .handle(ctx.channel().attr(NetworkRegistry.NET_HANDLER).get(), ctx.channel().attr(NetworkRegistry.FML_CHANNEL).get(), new PacketCustom(msg.payload())); }
public FMLProxyPacket toPacket() { if (incoming()) throw new IllegalStateException("Tried to write an incoming packet"); if (byteBuf.readableBytes() > 32000 || (type & 0x80) != 0) do_compress(); //FML packet impl returns the whole of the backing array, copy used portion of array to another ByteBuf return new FMLProxyPacket(byteBuf.copy(), channel); }
@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.func_147362_b(), msg); } return event; }