下面列出了怎么用net.minecraft.util.io.netty.channel.ChannelHandlerContext的API类实例代码及写法,或者点击链接到github查看源代码。
/**
* 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);
}
/**
* 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;
}
/**
* 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);
}
/**
* 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;
}
/**
* 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);
}
/**
* 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;
}
@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);
}
@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);
}
@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);
}
@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
}