Skip to content

Commit

Permalink
Issue with netty, receive and send event were mixed
Browse files Browse the repository at this point in the history
  • Loading branch information
purplexdev committed Jun 25, 2020
1 parent c73d88a commit b753a98
Show file tree
Hide file tree
Showing 7 changed files with 90 additions and 136 deletions.
117 changes: 8 additions & 109 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -64,127 +64,18 @@


<dependencies>
<dependency>
<groupId>org.github.spigot</groupId>
<artifactId>1.14.1</artifactId>
<version>1.14.1</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.github.spigot</groupId>
<artifactId>1.15.1</artifactId>
<version>1.15.1</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.github.spigot</groupId>
<artifactId>1.14.3</artifactId>
<version>1.14.3</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.github.spigot</groupId>
<artifactId>1.14.2</artifactId>
<version>1.14.2</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.github.spigot</groupId>
<artifactId>1.15</artifactId>
<version>1.15</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.github.spigot</groupId>
<artifactId>1.15.2</artifactId>
<version>1.15.2</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.github.spigot</groupId>
<artifactId>1.13.2</artifactId>
<version>1.13.2</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.github.spigot</groupId>
<artifactId>1.7.10</artifactId>
<version>1.7.10</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.github.spigot</groupId>
<artifactId>1.8</artifactId>
<version>1.8</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.github.spigot</groupId>
<artifactId>1.8.3</artifactId>
<version>1.8.3</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.github.spigot</groupId>
<artifactId>1.8.8</artifactId>
<version>1.8.8</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.github.spigot</groupId>
<artifactId>1.9</artifactId>
<version>1.9</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.github.spigot</groupId>
<artifactId>1.9.4</artifactId>
<version>1.9.4</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.github.spigot</groupId>
<artifactId>1.10.2</artifactId>
<version>1.10.2</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.github.spigot</groupId>
<artifactId>1.11.1</artifactId>
<version>1.11.1</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.github.spigot</groupId>
<artifactId>1.11.2</artifactId>
<version>1.11.2</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.github.spigot</groupId>
<artifactId>1.12.1</artifactId>
<version>1.12.1</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.github.spigot</groupId>
<artifactId>1.12.2</artifactId>
<version>1.12.2</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.github.spigot</groupId>
<artifactId>1.13</artifactId>
<version>1.13</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.github.spigot</groupId>
<artifactId>1.14</artifactId>
<version>1.14</version>
<scope>provided</scope>
</dependency>

<dependency>
<groupId>us.myles</groupId>
<artifactId>viaversion</artifactId>
Expand All @@ -198,5 +89,13 @@
<version>4fdd683</version>
<scope>provided</scope>
</dependency>

<!-- https://mvnrepository.com/artifact/io.netty/netty-all -->
<dependency>
<groupId>io.netty</groupId>
<artifactId>netty-all</artifactId>
<version>4.1.50.Final</version>
<scope>provided</scope>
</dependency>
</dependencies>
</project>
Original file line number Diff line number Diff line change
Expand Up @@ -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;


Expand Down
Original file line number Diff line number Diff line change
@@ -1,21 +1,45 @@
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
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());
}
}

}
}
}
Original file line number Diff line number Diff line change
@@ -1,17 +1,20 @@
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;

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;
}
Expand All @@ -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;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -34,17 +32,23 @@ 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());
}
return false;
}

@Override
public boolean equals(Object o) {
if (this == o) {
return true;
Expand All @@ -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;
}
}
Original file line number Diff line number Diff line change
@@ -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 {
Expand All @@ -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
Expand Down Expand Up @@ -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();
}
}
Expand Down Expand Up @@ -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 {
Expand Down
Loading

0 comments on commit b753a98

Please sign in to comment.