net.minecraft.util.io.netty.channel.ChannelHandlerContext Java Examples

The following examples show how to use net.minecraft.util.io.netty.channel.ChannelHandlerContext. 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: DuplexHandler.java    From PingAPI with MIT License 6 votes vote down vote up
/**
 * The write() method sends packets to the client
 * It needs to be overrode in order to listen for outgoing packets
 */
@Override
public void write(ChannelHandlerContext ctx, Object msg, ChannelPromise promise) throws Exception {
	if(msg instanceof PacketStatusOutServerInfo) {
		PacketStatusOutServerInfo packet = (PacketStatusOutServerInfo) msg;
		PingReply reply = ServerInfoPacketHandler.constructReply(packet, ctx);
		this.event = new PingEvent(reply);
		for(PingListener listener : PingAPI.getListeners()) {
			listener.onPing(event);
		}
		if(!this.event.isCancelled()) {
			super.write(ctx, ServerInfoPacketHandler.constructPacket(reply), promise);
		}
		return;
	}
	else if(msg instanceof PacketStatusOutPong) {
		if(this.event != null && this.event.isPongCancelled()) {
			return;
		}
	}
	super.write(ctx, msg, promise);
}
 
Example #2
Source File: ServerInfoPacketHandler.java    From PingAPI with MIT License 6 votes vote down vote up
/**
 * Creates a new PingReply instance from the data found in a PacketStatusOutServerInfo packet
 * @param packet The PacketStatusOutServerInfo instance
 * @param ctx The ChannelHandlerContext instance
 * @return A PingReply instance
 */
public static PingReply constructReply(PacketStatusOutServerInfo packet, ChannelHandlerContext ctx) {
	try {
		ServerPing ping = (ServerPing) SERVER_PING_FIELD.get(packet);
		String motd = ChatSerializer.a(ping.a());
		int max = ping.b().a();
		int online = ping.b().b();
		int protocolVersion = ping.c().b();
		String protocolName = ping.c().a();
		GameProfile[] profiles = ping.b().c();
		List<String> list = new ArrayList<String>();
		for(int i = 0; i < profiles.length; i++) {
			list.add(profiles[i].getName());
		}
		PingReply reply = new PingReply(ctx, motd, online, max, protocolVersion, protocolName, list);
		return reply;
	} catch(IllegalAccessException e) {
		e.printStackTrace();
	}
	return null;
}
 
Example #3
Source File: DuplexHandler.java    From PingAPI with MIT License 6 votes vote down vote up
/**
 * The write() method sends packets to the client
 * It needs to be overrode in order to listen for outgoing packets
 */
@Override
public void write(ChannelHandlerContext ctx, Object msg, ChannelPromise promise) throws Exception {
	if(msg instanceof PacketStatusOutServerInfo) {
		PacketStatusOutServerInfo packet = (PacketStatusOutServerInfo) msg;
		PingReply reply = ServerInfoPacketHandler.constructReply(packet, ctx);
		this.event = new PingEvent(reply);
		for(PingListener listener : PingAPI.getListeners()) {
			listener.onPing(event);
		}
		if(!this.event.isCancelled()) {
			super.write(ctx, ServerInfoPacketHandler.constructPacket(reply), promise);
		}
		return;
	}
	else if(msg instanceof PacketStatusOutPong) {
		if(this.event != null && this.event.isPongCancelled()) {
			return;
		}
	}
	super.write(ctx, msg, promise);
}
 
Example #4
Source File: ServerInfoPacketHandler.java    From PingAPI with MIT License 6 votes vote down vote up
/**
 * Creates a new PingReply instance from the data found in a PacketStatusOutServerInfo packet
 * @param packet The PacketStatusOutServerInfo instance
 * @param ctx The ChannelHandlerContext instance
 * @return A PingReply instance
 */
public static PingReply constructReply(PacketStatusOutServerInfo packet, ChannelHandlerContext ctx) {
	try {
		ServerPing ping = (ServerPing) SERVER_PING_FIELD.get(packet);
		String motd = ChatSerializer.a(ping.a());
		int max = ping.b().a();
		int online = ping.b().b();
		int protocolVersion = ping.c().b();
		String protocolName = ping.c().a();
		GameProfile[] profiles = ping.b().c();
		List<String> list = new ArrayList<String>();
		for(int i = 0; i < profiles.length; i++) {
			list.add(profiles[i].getName());
		}
		PingReply reply = new PingReply(ctx, motd, online, max, protocolVersion, protocolName, list);
		return reply;
	} catch(IllegalAccessException e) {
		e.printStackTrace();
	}
	return null;
}
 
Example #5
Source File: DuplexHandler.java    From PingAPI with MIT License 6 votes vote down vote up
/**
 * The write() method sends packets to the client
 * It needs to be overrode in order to listen for outgoing packets
 */
@Override
public void write(ChannelHandlerContext ctx, Object msg, ChannelPromise promise) throws Exception {
	if(msg instanceof PacketStatusOutServerInfo) {
		PacketStatusOutServerInfo packet = (PacketStatusOutServerInfo) msg;
		PingReply reply = ServerInfoPacketHandler.constructReply(packet, ctx);
		this.event = new PingEvent(reply);
		for(PingListener listener : PingAPI.getListeners()) {
			listener.onPing(event);
		}
		if(!this.event.isCancelled()) {
			super.write(ctx, ServerInfoPacketHandler.constructPacket(reply), promise);
		}
		return;
	}
	else if(msg instanceof PacketStatusOutPong) {
		if(this.event != null && this.event.isPongCancelled()) {
			return;
		}
	}
	super.write(ctx, msg, promise);
}
 
Example #6
Source File: ServerInfoPacketHandler.java    From PingAPI with MIT License 6 votes vote down vote up
/**
 * Creates a new PingReply instance from the data found in a PacketStatusOutServerInfo packet
 * @param packet The PacketStatusOutServerInfo instance
 * @param ctx The ChannelHandlerContext instance
 * @return A PingReply instance
 */
public static PingReply constructReply(PacketStatusOutServerInfo packet, ChannelHandlerContext ctx) {
	try {
		ServerPing ping = (ServerPing) SERVER_PING_FIELD.get(packet);
		String motd = ChatSerializer.a(ping.a());
		int max = ping.b().a();
		int online = ping.b().b();
		int protocolVersion = ping.c().b();
		String protocolName = ping.c().a();
		GameProfile[] profiles = ping.b().c();
		List<String> list = new ArrayList<String>();
		for(int i = 0; i < profiles.length; i++) {
			list.add(profiles[i].getName());
		}
		PingReply reply = new PingReply(ctx, motd, online, max, protocolVersion, protocolName, list);
		return reply;
	} catch(IllegalAccessException e) {
		e.printStackTrace();
	}
	return null;
}
 
Example #7
Source File: NMUChannel.java    From PacketListenerAPI with MIT License 5 votes vote down vote up
@Override
public void write(ChannelHandlerContext ctx, Object msg, ChannelPromise promise) throws Exception {
	Cancellable cancellable = new Cancellable();
	Object pckt = msg;
	if (Packet.isAssignableFrom(msg.getClass())) {
		pckt = onPacketSend(this.owner, msg, cancellable);
	}
	if (cancellable.isCancelled()) { return; }
	super.write(ctx, pckt, promise);
}
 
Example #8
Source File: NMUChannel.java    From PacketListenerAPI with MIT License 5 votes vote down vote up
@Override
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
	Cancellable cancellable = new Cancellable();
	Object pckt = msg;
	if (Packet.isAssignableFrom(msg.getClass())) {
		pckt = onPacketReceive(this.owner, msg, cancellable);
	}
	if (cancellable.isCancelled()) { return; }
	super.channelRead(ctx, pckt);
}
 
Example #9
Source File: PlayerInjector.java    From HoloAPI with GNU General Public License v3.0 5 votes vote down vote up
@Override
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
    // Handle the packet
    final WrappedPacket packet = new WrappedPacket(msg);

    if (packet.getPacketType().equals(PacketType.Play.Client.USE_ENTITY))
        Bukkit.getScheduler().scheduleSyncDelayedTask(HoloAPI.getCore(), new Runnable() {
            @Override
            public void run() {
                PlayerInjector.this.injectionManager.handlePacket(packet, PlayerInjector.this);
            }
        });

    super.channelRead(ctx, msg);
}
 
Example #10
Source File: PacketEncoder.java    From Carbon with GNU Lesser General Public License v3.0 5 votes vote down vote up
@SuppressWarnings("unchecked")
@Override
protected void encode(ChannelHandlerContext channelhandlercontext, Object object, ByteBuf bytebuf) throws IOException {
	Packet packet = (Packet) object;
	Integer packetid = ((BiMap<Integer, Class<? extends Packet>>) channelhandlercontext.channel().attr(NetworkManager.f).get()).inverse().get(packet.getClass());
	if (logger.isDebugEnabled()) {
		logger.debug(logmarker, "OUT: [{}:{}] {}[{}]", channelhandlercontext.channel().attr(NetworkManager.d).get(), packetid, packet.getClass().getName(), packet.b());
	}
	if (packetid == null) {
		throw new IOException("Can't serialize unregistered packet");
	}
	//skip new packets for 1.7 client
	int version = NetworkManager.getVersion(channelhandlercontext.channel());
	if (version != Utilities.CLIENT_1_8_PROTOCOL_VERSION && channelhandlercontext.channel().attr(NetworkManager.d).get() == EnumProtocol.PLAY && newpackets[packetid.intValue()]) {
		return;
	}

	PacketDataSerializer packetdataserializer = new PacketDataSerializer(bytebuf, version);
	if (BlockedProtocols.is17Blocked() && packet instanceof PacketStatusOutServerInfo) {
		try {
			ServerPing serverPing = (ServerPing) Utilities.setAccessible(Field.class, PacketStatusOutServerInfo.class.getDeclaredField("b"), true).get(packet);
			serverPing.setServerInfo(new ServerPingServerData(serverPing.c().a(), 47));
		} catch (NoSuchFieldException | SecurityException | IllegalArgumentException | IllegalAccessException e) {
			e.printStackTrace(System.out);
		}
	}
	packetdataserializer.b(packetid.intValue());
	packet.b(packetdataserializer);
	//don't use packet statistic because it is a waste of resources
}