net.minecraftforge.fml.common.network.internal.FMLProxyPacket Java Examples

The following examples show how to use net.minecraftforge.fml.common.network.internal.FMLProxyPacket. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: PayloadSpoofer.java    From ForgeHax with MIT License 6 votes vote down vote up
@SubscribeEvent(priority = EventPriority.HIGHEST)
public void onIncomingPacket(PacketEvent.Incoming.Pre event) {
  String channel;
  ByteBuf packetBuffer;
  final Packet packet = event.getPacket();

  if (packet instanceof SPacketCustomPayload || packet instanceof FMLProxyPacket) {
    if (packet instanceof SPacketCustomPayload) {
      channel = ((SPacketCustomPayload) packet).getChannelName();
      packetBuffer = ((SPacketCustomPayload) packet).getBufferData();
    } else {
      channel = ((FMLProxyPacket) packet).channel();
      packetBuffer = ((FMLProxyPacket) packet).payload();
    }
    
    if (isBlockedPacket(channel, packetBuffer)) {
      event.setCanceled(true);
    }
  }
}
 
Example #2
Source File: PayloadSpoofer.java    From ForgeHax with MIT License 6 votes vote down vote up
@SubscribeEvent(priority = EventPriority.HIGHEST)
public void onOutgoingPacket(PacketEvent.Outgoing.Pre event) {
  String channel;
  ByteBuf packetBuffer;
  final Packet packet = event.getPacket();
  
  if (packet instanceof CPacketCustomPayload || packet instanceof FMLProxyPacket) {
    if (packet instanceof CPacketCustomPayload) {
      channel = ((CPacketCustomPayload) packet).getChannelName();
      packetBuffer = ((CPacketCustomPayload) packet).getBufferData();
    } else {
      channel = ((FMLProxyPacket) packet).channel();
      packetBuffer = ((FMLProxyPacket) packet).payload();
    }
    
    if (isBlockedPacket(channel, packetBuffer)) {
      event.setCanceled(true);
    }
  }
}
 
Example #3
Source File: RpcCallCodec.java    From OpenModsLib with MIT License 6 votes vote down vote up
@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);
}
 
Example #4
Source File: NetworkEventCodec.java    From OpenModsLib with MIT License 6 votes vote down vote up
@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);
}
 
Example #5
Source File: NoHandShakeModule.java    From seppuku with GNU General Public License v3.0 5 votes vote down vote up
@Listener
public void sendPacket(EventSendPacket event) {
    if (event.getStage() == EventStageable.EventStage.PRE) {
        if (event.getPacket() instanceof FMLProxyPacket && !Minecraft.getMinecraft().isSingleplayer()) {
            event.setCanceled(true);
        }
        if (event.getPacket() instanceof CPacketCustomPayload) {
            final CPacketCustomPayload packet = (CPacketCustomPayload) event.getPacket();
            if(packet.getChannelName().equals("MC|Brand")) {
                packet.data = new PacketBuffer(Unpooled.buffer()).writeString("vanilla");
            }
        }
    }
}
 
Example #6
Source File: NetworkHandler.java    From GregTech with GNU Lesser General Public License v3.0 5 votes vote down vote up
@SuppressWarnings("unchecked")
public static FMLProxyPacket packet2proxy(Packet packet) {
    PacketCodec<Packet> codec = (PacketCodec<Packet>) codecMap.get(packet.getClass());
    PacketBuffer buf = new PacketBuffer(Unpooled.buffer());
    buf.writeVarInt(packetMap.getId(packet.getClass()));
    codec.encoder.encode(packet, buf);
    return new FMLProxyPacket(buf, GTValues.MODID);
}
 
Example #7
Source File: NetworkHandler.java    From GregTech with GNU Lesser General Public License v3.0 5 votes vote down vote up
@SuppressWarnings("unchecked")
public static Packet proxy2packet(FMLProxyPacket packet) {
    PacketBuffer payload = (PacketBuffer) packet.payload();
    Class<Packet> packetClass = (Class<Packet>) packetMap.get(payload.readVarInt());
    PacketCodec<Packet> codec = (PacketCodec<Packet>) codecMap.get(packetClass);
    return codec.decoder.decode(payload);
}
 
Example #8
Source File: DescPacketHandler.java    From Signals with GNU General Public License v3.0 5 votes vote down vote up
@Override
protected void channelRead0(ChannelHandlerContext ctx, FMLProxyPacket msg) {

    PacketDescription packet = new PacketDescription();
    packet.fromBytes(msg.payload());
    packet.handleClientSide(Signals.proxy.getPlayer());
}
 
Example #9
Source File: PacketCustom.java    From CodeChickenLib with GNU Lesser General Public License v2.1 5 votes vote down vote up
@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()));
}
 
Example #10
Source File: PacketCustom.java    From CodeChickenLib with GNU Lesser General Public License v2.1 5 votes vote down vote up
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);
}
 
Example #11
Source File: RpcCallCodec.java    From OpenModsLib with MIT License 5 votes vote down vote up
@Override
protected void decode(ChannelHandlerContext ctx, FMLProxyPacket msg, List<Object> out) throws Exception {
	final PacketBuffer input = new PacketBuffer(msg.payload());

	final Side side = ctx.channel().attr(NetworkRegistry.CHANNEL_SOURCE).get();

	final IRpcTarget target;
	final MethodEntry method;
	final Object[] args;

	{
		final int targetId = input.readVarInt();
		final BiMap<Integer, TargetTypeProvider> idToEntryMap = CommonRegistryCallbacks.getEntryIdMap(targetRegistry).inverse();
		final TargetTypeProvider entry = idToEntryMap.get(targetId);
		target = entry.createRpcTarget();
		EntityPlayer player = getPlayer(msg);
		target.readFromStreamStream(side, player, input);
	}

	{
		final BiMap<MethodEntry, Integer> eventIdMap = CommonRegistryCallbacks.getEntryIdMap(methodRegistry);
		final int methodId = input.readVarInt();
		method = eventIdMap.inverse().get(methodId);
		args = method.paramsCodec.readArgs(input);
	}

	int bufferJunkSize = input.readableBytes();
	Preconditions.checkState(bufferJunkSize == 0, "%s junk bytes left in buffer, method = %s", bufferJunkSize, method);

	out.add(new RpcCall(target, method, args));
	input.release();
}
 
Example #12
Source File: ExtendedOutboundHandler.java    From OpenModsLib with MIT License 5 votes vote down vote up
@Override
public void write(ChannelHandlerContext ctx, Object msg, ChannelPromise promise) throws Exception {
	if (!(msg instanceof FMLProxyPacket)) {
		ctx.write(msg);
		return;
	}

	final Channel channel = ctx.channel();

	final IPacketTargetSelector<?> target = channel.attr(MESSAGETARGET).get();
	if (target == null) {
		ctx.write(msg);
		return;
	}

	final FMLProxyPacket pkt = (FMLProxyPacket)msg;

	final Side channelSide = channel.attr(NetworkRegistry.CHANNEL_SOURCE).get();

	Preconditions.checkState(target.isAllowedOnSide(channelSide), "Packet not allowed on side");

	final String channelName = channel.attr(NetworkRegistry.FML_CHANNEL).get();

	Object arg = channel.attr(FMLOutboundHandler.FML_MESSAGETARGETARGS).get();

	try {
		final Collection<NetworkDispatcher> dispatchers = getDispatchers(target, arg);
		for (NetworkDispatcher dispatcher : dispatchers)
			dispatcher.sendProxy(pkt);

	} catch (Throwable t) {

		throw new IllegalStateException(String.format(
				"Failed to select and send message (selector %s, arg: %s, channel: %s, side: %s)",
				target, arg, channelName, channelSide), t);
	}

}
 
Example #13
Source File: NetworkEventCodec.java    From OpenModsLib with MIT License 5 votes vote down vote up
@Override
protected void decode(ChannelHandlerContext ctx, FMLProxyPacket msg, List<Object> out) throws Exception {
	final Channel channel = ctx.channel();
	final Side side = channel.attr(NetworkRegistry.CHANNEL_SOURCE).get();

	final PacketBuffer payload = new PacketBuffer(msg.payload());
	final int typeId = payload.readVarInt();
	final NetworkEventEntry type = CommonRegistryCallbacks.getEntryIdMap(registry).inverse().get(typeId);

	final EventDirection validator = type.getDirection();
	Preconditions.checkState(validator != null && validator.validateReceive(side),
			"Invalid direction: receiving packet %s on side %s", msg.getClass(), side);

	final NetworkEvent event = type.createPacket();
	event.readFromStream(payload);
	event.dispatcher = msg.getDispatcher();

	event.side = side;

	final INetHandler handler = msg.handler();
	if (handler != null) event.sender = OpenMods.proxy.getPlayerFromHandler(handler);

	final int bufferJunkSize = payload.readableBytes();
	if (bufferJunkSize > 0) Log.warn("%s junk bytes left in buffer, event %s", bufferJunkSize, event);

	out.add(event);
	payload.release();
}
 
Example #14
Source File: SyncChannelHolder.java    From OpenModsLib with MIT License 4 votes vote down vote up
public void sendPayloadToPlayers(PacketBuffer payload, Collection<EntityPlayerMP> players) {
	FMLProxyPacket packet = new FMLProxyPacket(payload, CHANNEL_NAME);
	senders.get(Side.SERVER).sendMessage(packet, players);
}
 
Example #15
Source File: SyncChannelHolder.java    From OpenModsLib with MIT License 4 votes vote down vote up
public static Packet<?> createPacket(PacketBuffer payload) {
	return new FMLProxyPacket(payload, CHANNEL_NAME);
}
 
Example #16
Source File: RpcCallCodec.java    From OpenModsLib with MIT License 4 votes vote down vote up
protected EntityPlayer getPlayer(FMLProxyPacket msg) {
	INetHandler handler = msg.handler();
	EntityPlayer player = OpenMods.proxy.getPlayerFromHandler(handler);
	Preconditions.checkNotNull(player, "Can't get player from handler %s", handler);
	return player;
}
 
Example #17
Source File: DescPacketHandler.java    From Signals with GNU General Public License v3.0 4 votes vote down vote up
public static FMLProxyPacket getPacket(PacketDescription packet){

        ByteBuf buf = Unpooled.buffer();
        packet.toBytes(buf);
        return new FMLProxyPacket(new PacketBuffer(buf), CHANNEL);
    }
 
Example #18
Source File: NetworkHandler.java    From GregTech with GNU Lesser General Public License v3.0 4 votes vote down vote up
default FMLProxyPacket toFMLPacket() {
    return packet2proxy(this);
}