From b753a989711421be173ea64aaefaeffc96fd7c65 Mon Sep 17 00:00:00 2001 From: purplexdev Date: Thu, 25 Jun 2020 17:37:05 +0200 Subject: [PATCH] Issue with netty, receive and send event were mixed --- pom.xml | 117 ++---------------- .../packetevents/enums/ServerVersion.java | 2 +- .../packetevents/example/MainExample.java | 28 ++++- .../handler/NettyPacketHandler_8.java | 7 +- .../packetevents/mojang/GameProfile.java | 24 ++-- .../packetevents/packet/PacketType.java | 31 +++-- .../WrappedPacketInBlockPlace_1_9.java | 17 ++- 7 files changed, 90 insertions(+), 136 deletions(-) diff --git a/pom.xml b/pom.xml index b4c20a4b50..879e982039 100644 --- a/pom.xml +++ b/pom.xml @@ -64,127 +64,18 @@ - - org.github.spigot - 1.14.1 - 1.14.1 - provided - - - org.github.spigot - 1.15.1 - 1.15.1 - provided - - - org.github.spigot - 1.14.3 - 1.14.3 - provided - - - org.github.spigot - 1.14.2 - 1.14.2 - provided - - - org.github.spigot - 1.15 - 1.15 - provided - - - org.github.spigot - 1.15.2 - 1.15.2 - provided - - - org.github.spigot - 1.13.2 - 1.13.2 - provided - org.github.spigot 1.7.10 1.7.10 provided - - org.github.spigot - 1.8 - 1.8 - provided - - - org.github.spigot - 1.8.3 - 1.8.3 - provided - org.github.spigot 1.8.8 1.8.8 provided - - org.github.spigot - 1.9 - 1.9 - provided - - - org.github.spigot - 1.9.4 - 1.9.4 - provided - - - org.github.spigot - 1.10.2 - 1.10.2 - provided - - - org.github.spigot - 1.11.1 - 1.11.1 - provided - - - org.github.spigot - 1.11.2 - 1.11.2 - provided - - - org.github.spigot - 1.12.1 - 1.12.1 - provided - - - org.github.spigot - 1.12.2 - 1.12.2 - provided - - - org.github.spigot - 1.13 - 1.13 - provided - - - org.github.spigot - 1.14 - 1.14 - provided - - us.myles viaversion @@ -198,5 +89,13 @@ 4fdd683 provided + + + + io.netty + netty-all + 4.1.50.Final + provided + diff --git a/src/main/java/io/github/retrooper/packetevents/enums/ServerVersion.java b/src/main/java/io/github/retrooper/packetevents/enums/ServerVersion.java index b7e77175bb..168192dcb5 100644 --- a/src/main/java/io/github/retrooper/packetevents/enums/ServerVersion.java +++ b/src/main/java/io/github/retrooper/packetevents/enums/ServerVersion.java @@ -5,7 +5,7 @@ public enum ServerVersion { - v_1_7_10, v_1_8, v_1_8_3, v_1_8_8, v_1_9, v_1_9_4, v_1_10, v_1_11, v_1_12, v_1_13, v_1_13_2, v_1_14, v_1_15, + v_1_7_10, v_1_8, v_1_8_3, v_1_8_8, v_1_9, v_1_9_4, v_1_10, v_1_11, v_1_12, v_1_13, v_1_13_2, v_1_14, v_1_15, v_1_16, ERROR; diff --git a/src/main/java/io/github/retrooper/packetevents/example/MainExample.java b/src/main/java/io/github/retrooper/packetevents/example/MainExample.java index 6da0a5b58c..27d64aafbb 100644 --- a/src/main/java/io/github/retrooper/packetevents/example/MainExample.java +++ b/src/main/java/io/github/retrooper/packetevents/example/MainExample.java @@ -1,15 +1,25 @@ package io.github.retrooper.packetevents.example; import io.github.retrooper.packetevents.PacketEvents; +import io.github.retrooper.packetevents.annotations.PacketHandler; +import io.github.retrooper.packetevents.event.PacketEvent; +import io.github.retrooper.packetevents.event.PacketListener; +import io.github.retrooper.packetevents.event.impl.PacketReceiveEvent; +import io.github.retrooper.packetevents.event.impl.PacketSendEvent; +import io.github.retrooper.packetevents.packet.PacketType; +import io.github.retrooper.packetevents.packetwrappers.in.flying.WrappedPacketInFlying; +import io.github.retrooper.packetevents.packetwrappers.in.useentity.WrappedPacketInUseEntity; import org.bukkit.plugin.java.JavaPlugin; -public class MainExample extends JavaPlugin { +public class MainExample extends JavaPlugin implements PacketListener { @Override public void onEnable() { PacketEvents.start(this); PacketEvents.getServerTickTask().cancel(); + + PacketEvents.getEventManager().registerListener(this); } @Override @@ -17,5 +27,19 @@ public void onDisable() { PacketEvents.stop(); } + @PacketHandler + public void onPacketReceive(PacketEvent event) { + if (event instanceof PacketReceiveEvent) { + PacketReceiveEvent e = (PacketReceiveEvent) event; + System.out.println(e.getPacketName() + " is NAME"); + if (PacketType.Util.isInstanceOfFlyingPacket(e.getNMSPacket())) { + WrappedPacketInFlying flying = new WrappedPacketInFlying(e.getNMSPacket()); + System.out.println(flying.isOnGround() + " and " + flying.isPosition() + " and " + flying.isLook() + " and " + flying.getX() + flying.getZ() + flying.getYaw()); + } else if (PacketType.Util.isInstanceOfUseEntity(e.getNMSPacket())) { + WrappedPacketInUseEntity useEntity = new WrappedPacketInUseEntity(e.getNMSPacket()); + System.out.println("a" + useEntity.getAction().name() + " and " + useEntity.getEntityId()); + } + } -} + } +} \ No newline at end of file diff --git a/src/main/java/io/github/retrooper/packetevents/handler/NettyPacketHandler_8.java b/src/main/java/io/github/retrooper/packetevents/handler/NettyPacketHandler_8.java index e42dfac960..b7541ef6cd 100644 --- a/src/main/java/io/github/retrooper/packetevents/handler/NettyPacketHandler_8.java +++ b/src/main/java/io/github/retrooper/packetevents/handler/NettyPacketHandler_8.java @@ -1,5 +1,7 @@ package io.github.retrooper.packetevents.handler; +import io.github.retrooper.packetevents.PacketEvents; +import io.github.retrooper.packetevents.enums.ServerVersion; import io.github.retrooper.packetevents.utils.NMSUtils; import io.netty.channel.*; import org.bukkit.entity.Player; @@ -7,11 +9,12 @@ import java.util.concurrent.Future; final class NettyPacketHandler_8 { + private static final ServerVersion version = PacketEvents.getServerVersion(); public static void injectPlayer(final Player player) { final ChannelDuplexHandler channelDuplexHandler = new ChannelDuplexHandler() { @Override public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception { - Object packet = NettyPacketHandler.read(player, msg); + Object packet = NettyPacketHandler.write(player, msg); if(packet == null) { return; } @@ -20,7 +23,7 @@ public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception @Override public void write(ChannelHandlerContext ctx, Object msg, ChannelPromise promise) throws Exception { - Object packet = NettyPacketHandler.write(player, msg); + Object packet = NettyPacketHandler.read(player, msg); if(packet == null) { return; } diff --git a/src/main/java/io/github/retrooper/packetevents/mojang/GameProfile.java b/src/main/java/io/github/retrooper/packetevents/mojang/GameProfile.java index b703630ec5..f7d200ae30 100644 --- a/src/main/java/io/github/retrooper/packetevents/mojang/GameProfile.java +++ b/src/main/java/io/github/retrooper/packetevents/mojang/GameProfile.java @@ -1,19 +1,17 @@ package io.github.retrooper.packetevents.mojang; import com.mojang.authlib.properties.PropertyMap; -import org.bukkit.craftbukkit.libs.org.apache.commons.lang3.StringUtils; -import org.bukkit.craftbukkit.libs.org.apache.commons.lang3.builder.ToStringBuilder; import java.util.UUID; -public class GameProfile { +public final class GameProfile { private final UUID id; private final String name; private final PropertyMap properties = new PropertyMap(); private boolean legacy; public GameProfile(UUID id, String name) { - if (id == null && StringUtils.isBlank(name)) { + if (id == null && name.isEmpty()) { throw new IllegalArgumentException("Name and ID cannot both be blank"); } else { this.id = id; @@ -34,10 +32,15 @@ public PropertyMap getProperties() { } public boolean isComplete() { - return this.id != null && StringUtils.isNotBlank(this.getName()); + return this.id != null && !getName().isEmpty(); } - public boolean equalsMojangGameProfile(com.mojang.authlib.GameProfile profile) { + + public boolean isLegacy() { + return this.legacy; + } + + public boolean equals(com.mojang.authlib.GameProfile profile) { if (profile != null) { return profile.getId() != null && getId() != null && profile.getId() == getId() && profile.getName() != null && getName() != null && profile.getName().equals(getName()); @@ -45,6 +48,7 @@ public boolean equalsMojangGameProfile(com.mojang.authlib.GameProfile profile) { return false; } + @Override public boolean equals(Object o) { if (this == o) { return true; @@ -66,17 +70,15 @@ public boolean equals(Object o) { } } + @Override public int hashCode() { int result = this.id != null ? this.id.hashCode() : 0; result = 31 * result + (this.name != null ? this.name.hashCode() : 0); return result; } + @Override public String toString() { - return (new ToStringBuilder(this)).append("id", this.id).append("name", this.name).append("properties", this.properties).append("legacy", this.legacy).toString(); - } - - public boolean isLegacy() { - return this.legacy; + return "id: " + id + ", name: " + name + ", legacy: " + legacy; } } diff --git a/src/main/java/io/github/retrooper/packetevents/packet/PacketType.java b/src/main/java/io/github/retrooper/packetevents/packet/PacketType.java index 9e59d1228b..aeb5a7c1ee 100644 --- a/src/main/java/io/github/retrooper/packetevents/packet/PacketType.java +++ b/src/main/java/io/github/retrooper/packetevents/packet/PacketType.java @@ -1,9 +1,11 @@ package io.github.retrooper.packetevents.packet; +import io.github.retrooper.packetevents.PacketEvents; import io.github.retrooper.packetevents.enums.ServerVersion; import io.github.retrooper.packetevents.utils.NMSUtils; public final class PacketType { + private static final ServerVersion version = PacketEvents.getServerVersion(); public static final class Util { @@ -12,7 +14,7 @@ public static final class Util { * Supports 1.9->1.15.2 too! * * @param nmsPacket - * @return If the nms packet provided is an instanceof the "PacketPlayInFlying" packet in the 1.8 protocol! + * @return If the nms packet provided is an instanceof the "PacketPlayInFlying" packet in the 1.8 protocol & 1.16 protocol! */ public static boolean isInstanceOfFlyingPacket(final Object nmsPacket) { //1.7->1.8.8 @@ -72,16 +74,32 @@ public static boolean isInstanceOfUseEntity(final Object nmsPacket) { static { try { + //Only exists in 1.7->1.8.8 & 1.16 protocol packetPlayInFlying = NMSUtils.getNMSClass(Client.FLYING); } catch (ClassNotFoundException e) { - //That is fine, they are on 1.9+ + //That is fine, they are on 1.9->1.15.2 } try { - packetPlayInPosition = NMSUtils.getNMSClass(Client.POSITION); - packetPlayInPositionLook = NMSUtils.getNMSClass(Client.POSITION_LOOK); - packetPlayInLook = NMSUtils.getNMSClass(Client.LOOK); - packetPlayInUseEntity = NMSUtils.getNMSClass(Client.USE_ENTITY); + packetPlayInPosition = NMSUtils.getNMSClass(Client.POSITION); + packetPlayInPositionLook = NMSUtils.getNMSClass(Client.POSITION_LOOK); + packetPlayInLook = NMSUtils.getNMSClass(Client.LOOK); + } catch (ClassNotFoundException e) { + for (final Class cls : packetPlayInFlying.getDeclaredClasses()) { + if (cls.getSimpleName().equals(Client.POSITION)) { + packetPlayInPosition = cls; + } else if (cls.getSimpleName().equals(Client.POSITION_LOOK)) { + packetPlayInPositionLook = cls; + } else if (cls.getSimpleName().equals(Client.LOOK)) { + packetPlayInLook = cls; + } + } + } + + try { + packetPlayInUseEntity = NMSUtils.getNMSClass(Client.USE_ENTITY); + } + catch(ClassNotFoundException e) { e.printStackTrace(); } } @@ -122,7 +140,6 @@ public static final class Client { public static final String SET_CREATIVE_SLOT = c + "SetCreativeSlot"; public static final String KEEP_ALIVE = c + "KeepAlive"; - } public static final class Server { diff --git a/src/main/java/io/github/retrooper/packetevents/packetwrappers/in/blockplace/WrappedPacketInBlockPlace_1_9.java b/src/main/java/io/github/retrooper/packetevents/packetwrappers/in/blockplace/WrappedPacketInBlockPlace_1_9.java index 8c7a93f58b..e7e0cb99f7 100644 --- a/src/main/java/io/github/retrooper/packetevents/packetwrappers/in/blockplace/WrappedPacketInBlockPlace_1_9.java +++ b/src/main/java/io/github/retrooper/packetevents/packetwrappers/in/blockplace/WrappedPacketInBlockPlace_1_9.java @@ -7,27 +7,36 @@ import org.bukkit.block.Block; import org.bukkit.entity.Player; +import java.lang.reflect.InvocationTargetException; +import java.lang.reflect.Method; import java.util.HashSet; final class WrappedPacketInBlockPlace_1_9 extends WrappedPacket { - private static Class blockPlaceClass_1_9; private Block block; + private static Method getTargetBlockMethod; + public WrappedPacketInBlockPlace_1_9(final Player player, final Object packet) { super(player, packet); } protected static void initStatic() { try { - blockPlaceClass_1_9 = NMSUtils.getNMSClass("PacketPlayInUseItem"); - } catch (ClassNotFoundException e) { + getTargetBlockMethod = Player.class.getMethod("getTargetBlock", HashSet.class, int.class); + } catch (NoSuchMethodException e) { e.printStackTrace(); } } @Override protected void setup() { - this.block = getPlayer().getTargetBlock((HashSet) null, 3); + try { + this.block = (Block) getTargetBlockMethod.invoke(getPlayer(), null, 3); + } catch (IllegalAccessException e) { + e.printStackTrace(); + } catch (InvocationTargetException e) { + e.printStackTrace(); + } }