PacketListener annotation
This commit is contained in:
@@ -4,6 +4,7 @@
|
||||
*/
|
||||
package mc.core.network.proto_1_12_2.netty;
|
||||
|
||||
import com.google.common.collect.ImmutableMap;
|
||||
import io.netty.channel.Channel;
|
||||
import io.netty.channel.ChannelFutureListener;
|
||||
import io.netty.channel.ChannelHandlerContext;
|
||||
@@ -24,9 +25,11 @@ import mc.core.text.TextColor;
|
||||
import mc.core.world.World;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
|
||||
import javax.annotation.PostConstruct;
|
||||
import java.lang.reflect.Method;
|
||||
import java.util.Arrays;
|
||||
import java.util.Optional;
|
||||
import java.util.function.Function;
|
||||
import java.util.stream.Stream;
|
||||
|
||||
import static mc.core.network.proto_1_12_2.netty.NettyServer.ATTR_STATE;
|
||||
|
||||
@@ -42,6 +45,19 @@ public class PacketHandler extends SimpleChannelInboundHandler<CSPacket> {
|
||||
private World world;
|
||||
@Autowired
|
||||
private ChatProcessor chatProcessor;
|
||||
private ImmutableMap<Class<? /*extends CSPacket*/>, Method> methods = ImmutableMap.of();
|
||||
|
||||
@PostConstruct
|
||||
public void init() {
|
||||
this.methods = Stream.of(this.getClass().getDeclaredMethods())
|
||||
.filter(method -> method.isAnnotationPresent(PacketListener.class)
|
||||
&& method.getParameterCount() == 2
|
||||
&& method.getParameterTypes()[0].isAssignableFrom(Channel.class)
|
||||
&& CSPacket.class.isAssignableFrom(method.getParameterTypes()[1]))
|
||||
.collect(ImmutableMap.toImmutableMap(
|
||||
method -> method.getParameterTypes()[1],
|
||||
Function.identity()));
|
||||
}
|
||||
|
||||
@Override
|
||||
public void channelInactive(ChannelHandlerContext ctx) throws Exception {
|
||||
@@ -56,25 +72,21 @@ public class PacketHandler extends SimpleChannelInboundHandler<CSPacket> {
|
||||
|
||||
@Override
|
||||
protected void channelRead0(ChannelHandlerContext ctx, CSPacket packet) throws Exception {
|
||||
Optional<Method> optionalMethod = Arrays.stream(this.getClass().getDeclaredMethods())
|
||||
.filter(method -> method.getName().equals("on" + packet.getClass().getSimpleName().replace("Packet", ""))
|
||||
&& method.getParameterCount() == 2
|
||||
&& method.getParameterTypes()[0].isAssignableFrom(Channel.class)
|
||||
&& method.getParameterTypes()[1].isAssignableFrom(packet.getClass()))
|
||||
.findFirst();
|
||||
|
||||
if (optionalMethod.isPresent()) {
|
||||
Method method = optionalMethod.get();
|
||||
method.invoke(this, ctx.channel(), packet);
|
||||
if (this.methods.containsKey(packet.getClass())) {
|
||||
this.methods.get(packet.getClass()).invoke(this, ctx.channel(), packet);
|
||||
} else {
|
||||
log.trace("No def listener of \"{}\"", packet.getClass().getSimpleName());
|
||||
}
|
||||
}
|
||||
|
||||
@PacketListener
|
||||
private void onHandshake(Channel channel, HandshakePacket packet) {
|
||||
if (packet.getNextState().equals(State.UNKNOWN)) return;
|
||||
log.debug("New state: {}", packet.getNextState());
|
||||
channel.attr(ATTR_STATE).set(packet.getNextState());
|
||||
}
|
||||
|
||||
@PacketListener
|
||||
private void onStatusRequest(Channel channel, StatusRequestPacket packet) {
|
||||
if (!channel.attr(ATTR_STATE).get().equals(State.STATUS)) return;
|
||||
|
||||
@@ -87,11 +99,13 @@ public class PacketHandler extends SimpleChannelInboundHandler<CSPacket> {
|
||||
channel.writeAndFlush(responsePacket);
|
||||
}
|
||||
|
||||
@PacketListener
|
||||
private void onPing(Channel channel, PingPacket packet) {
|
||||
if (!channel.attr(ATTR_STATE).get().equals(State.STATUS)) return;
|
||||
channel.writeAndFlush(packet);
|
||||
}
|
||||
|
||||
@PacketListener
|
||||
private void onLoginStart(Channel channel, LoginStartPacket packet) {
|
||||
if (!channel.attr(ATTR_STATE).get().equals(State.LOGIN)) return;
|
||||
|
||||
|
||||
@@ -0,0 +1,15 @@
|
||||
/*
|
||||
* DmitriyMX <dimon550@gmail.com>
|
||||
* 2018-06-16
|
||||
*/
|
||||
package mc.core.network.proto_1_12_2.netty;
|
||||
|
||||
import java.lang.annotation.ElementType;
|
||||
import java.lang.annotation.Retention;
|
||||
import java.lang.annotation.RetentionPolicy;
|
||||
import java.lang.annotation.Target;
|
||||
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
@Target(ElementType.METHOD)
|
||||
public @interface PacketListener {
|
||||
}
|
||||
Reference in New Issue
Block a user