下面列出了怎么用net.minecraft.util.io.netty.channel.Channel的API类实例代码及写法,或者点击链接到github查看源代码。
@Override
protected void initChannel(Channel channel) {
try {
channel.config().setOption(ChannelOption.IP_TOS, Integer.valueOf(24));
} catch (ChannelException channelexception) {
}
try {
channel.config().setOption(ChannelOption.TCP_NODELAY, Boolean.valueOf(false));
} catch (ChannelException channelexception1) {
}
channel.pipeline()
.addLast("timeout", new ReadTimeoutHandler(30))
.addLast("legacy_query", new LegacyPingHandler(connection))
.addLast("splitter", new PacketSplitter())
.addLast("decoder", new PacketDecoder(NetworkManager.h))
.addLast("prepender", new PacketPrepender())
.addLast("encoder", new PacketEncoder(NetworkManager.h));
NetworkManager networkmanager = new NetworkManager(false);
networkManagers.add(networkmanager);
channel.pipeline().addLast("packet_handler", networkmanager);
networkmanager.a(new HandshakeListener(MinecraftServer.getServer(), networkmanager));
}
/**
* Iterates through every open NetworkManager and adds my ChannelDuplexHandler subclass into the pipeline
* This allows you to listen for outgoing packets and modify them before they are sent
*
* The List of NetworkManager instances is converted to an array to avoid ConcurrentModificationExceptions
* NullPointerExceptions, IllegalArgumentExceptions, and NoSuchElementException only occur if there is a massive amount of ping requests being sent to the server.
* NullPointerExceptions are thrown when the pipeline has yet to be created.
* Since ping responses are handled on separate threads IllegalArgumentExceptions are thrown when this method is invoked at the same time on two different threads
* This means the null check will be passed and this method will attempt to create a duplicate handler which throws this exception
* NoSuchElementExceptions have a similar cause. They are caused when the "packet_handler" has yet to be added.
* The best solution I could find is simply ignoring these exceptions
*/
public void injectOpenConnections() {
try {
Field field = ReflectUtils.getFirstFieldByType(NetworkManager.class, Channel.class);
field.setAccessible(true);
for(Object manager : networkManagers.toArray()) {
Channel channel = (Channel) field.get(manager);
if(channel.pipeline().context("pingapi_handler") == null && (channel.pipeline().context("packet_handler") != null)) {
channel.pipeline().addBefore("packet_handler", "pingapi_handler", new DuplexHandler());
}
}
} catch(IllegalAccessException e) {
e.printStackTrace();
} catch(NullPointerException | IllegalArgumentException | NoSuchElementException ignored) {}
}
/**
* Iterates through every open NetworkManager and adds my ChannelDuplexHandler subclass into the pipeline
* This allows you to listen for outgoing packets and modify them before they are sent
*
* The List of NetworkManager instances is converted to an array to avoid ConcurrentModificationExceptions
* NullPointerExceptions, IllegalArgumentExceptions, and NoSuchElementException only occur if there is a massive amount of ping requests being sent to the server.
* NullPointerExceptions are thrown when the pipeline has yet to be created.
* Since ping responses are handled on separate threads IllegalArgumentExceptions are thrown when this method is invoked at the same time on two different threads
* This means the null check will be passed and this method will attempt to create a duplicate handler which throws this exception
* NoSuchElementExceptions have a similar cause. They are caused when the "packet_handler" has yet to be added.
* The best solution I could find is simply ignoring these exceptions
*/
public void injectOpenConnections() {
try {
Field field = ReflectUtils.getFirstFieldByType(NetworkManager.class, Channel.class);
field.setAccessible(true);
for(Object manager : networkManagers.toArray()) {
Channel channel = (Channel) field.get(manager);
if(channel.pipeline().context("pingapi_handler") == null && (channel.pipeline().context("packet_handler") != null)) {
channel.pipeline().addBefore("packet_handler", "pingapi_handler", new DuplexHandler());
}
}
} catch(IllegalAccessException e) {
e.printStackTrace();
} catch(NullPointerException | IllegalArgumentException | NoSuchElementException ignored) {}
}
/**
* Iterates through every open NetworkManager and adds my ChannelDuplexHandler subclass into the pipeline
* This allows you to listen for outgoing packets and modify them before they are sent
*
* The List of NetworkManager instances is converted to an array to avoid ConcurrentModificationExceptions
* NullPointerExceptions, IllegalArgumentExceptions, and NoSuchElementException only occur if there is a massive amount of ping requests being sent to the server.
* NullPointerExceptions are thrown when the pipeline has yet to be created.
* Since ping responses are handled on separate threads IllegalArgumentExceptions are thrown when this method is invoked at the same time on two different threads
* This means the null check will be passed and this method will attempt to create a duplicate handler which throws this exception
* NoSuchElementExceptions have a similar cause. They are caused when the "packet_handler" has yet to be added.
* The best solution I could find is simply ignoring these exceptions
*/
public void injectOpenConnections() {
try {
Field field = ReflectUtils.getFirstFieldByType(NetworkManager.class, Channel.class);
field.setAccessible(true);
for(Object manager : networkManagers.toArray()) {
Channel channel = (Channel) field.get(manager);
if(channel.pipeline().context("pingapi_handler") == null && (channel.pipeline().context("packet_handler") != null)) {
channel.pipeline().addBefore("packet_handler", "pingapi_handler", new DuplexHandler());
}
}
} catch(IllegalAccessException e) {
e.printStackTrace();
} catch(NullPointerException | IllegalArgumentException | NoSuchElementException ignored) {}
}
/**
* Gets the channel
*
* @param player player
* @return the Channel
*/
private static Channel getChannel(Player player) {
try {
Object entity = Reflections.getHandle(player);
return clientChannelField.get(networkManagerField.get(playerConnectionField.get(entity)));
}
catch(Exception e) {
LoggerUtils.exception(e);
return null;
}
}
@SuppressWarnings("unchecked")
public static void injectStreamSerializer() throws IllegalArgumentException, IllegalAccessException, NoSuchFieldException, SecurityException {
ServerConnection serverConnection = MinecraftServer.getServer().getServerConnection();
List<NetworkManager> networkManagersList = ((List<NetworkManager>) Utilities.setAccessible(Field.class, serverConnection.getClass().getDeclaredField("f"), true).get(serverConnection));
Channel channel = (Channel) ((List<ChannelFuture>) Utilities.setAccessible(Field.class, serverConnection.getClass().getDeclaredField("e"), true).get(serverConnection)).get(0).channel();
ChannelHandler serverHandler = channel.pipeline().first();
Utilities.setAccessible(Field.class, serverHandler.getClass().getDeclaredField("childHandler"), true).set(serverHandler, new ServerConnectionChannel(serverConnection, networkManagersList));
}
protected void swapFields() {
if (CHANNEL_FIELD == null) {
CHANNEL_FIELD = new Reflection().reflect(NetworkManager.class).getSafeFieldByType(Channel.class).getAccessor();
}
if (ADDRESS_FIELD == null) {
ADDRESS_FIELD = new Reflection().reflect(NetworkManager.class).getSafeFieldByType(InetSocketAddress.class).getAccessor();
}
CHANNEL_FIELD.set(this, new NullChannel(null));
ADDRESS_FIELD.set(this, null);
}
protected void swapFields() {
if (CHANNEL_FIELD == null) {
CHANNEL_FIELD = new Reflection().reflect(NetworkManager.class).getSafeFieldByType(Channel.class).getAccessor();
}
if (ADDRESS_FIELD == null) {
ADDRESS_FIELD = new Reflection().reflect(NetworkManager.class).getSafeFieldByType(InetSocketAddress.class).getAccessor();
}
CHANNEL_FIELD.set(this, new NullChannel(null));
ADDRESS_FIELD.set(this, null);
}
protected void swapFields() {
if (CHANNEL_FIELD == null) {
CHANNEL_FIELD = new Reflection().reflect(NetworkManager.class).getSafeFieldByType(Channel.class).getAccessor();
}
if (ADDRESS_FIELD == null) {
ADDRESS_FIELD = new Reflection().reflect(NetworkManager.class).getSafeFieldByType(SocketAddress.class).getAccessor();
}
CHANNEL_FIELD.set(this, new NullChannel(null));
ADDRESS_FIELD.set(this, new SocketAddress() {});
}
protected void swapFields() {
if (CHANNEL_FIELD == null) {
CHANNEL_FIELD = new Reflection().reflect(NetworkManager.class).getSafeFieldByType(Channel.class).getAccessor();
}
if (ADDRESS_FIELD == null) {
ADDRESS_FIELD = new Reflection().reflect(NetworkManager.class).getSafeFieldByType(SocketAddress.class).getAccessor();
}
CHANNEL_FIELD.set(this, new NullChannel(null));
ADDRESS_FIELD.set(this, new SocketAddress() {});
}
private Channel getChannel() {
if (this.channel == null)
throw new IllegalStateException("The Channel is NULL!");
return this.channel;
}