Compare commits

..
3 Commits
Author SHA1 Message Date
purplexdev dc4dc80bdc Merge remote-tracking branch 'origin/master' 2020-06-05 20:17:47 +02:00
purplexdev 808a7b69d8 1.2.7 release,
Optimizations & bug fixes

* WrappedPacketInUseEntity getHand() null bug, so for now I just removed the getHand() method.

* WrappedPacketInEntityAction added as a couple of you suggested

* Optimization to getting entities by their id(used in use entity and entity action packet)

* After optimizing getEntity, file size has reduced by about 10%
2020-06-05 20:17:00 +02:00
retrooper 5b3b6ff509 jitpack version updated to 1.2.6 and link updated 2020-06-03 16:43:39 +02:00
21 changed files with 225 additions and 281 deletions

No files matched your search

+1 -1
View File
@@ -19,6 +19,6 @@ Make sure to replace **Tag** with a proper release version.
I recommend you to use the latest released version!
[![](http://jitpack.io/v/explored/packetevents.svg)](http://jitpack.io/#explored/packetevents)
[![](https://jitpack.io/v/retrooper/packetevents.svg)](https://jitpack.io/#retrooper/packetevents)
If clicking the badge doesn't work paste this link into your browser http://jitpack.io/#explored/packetevents
+1 -1
View File
@@ -6,7 +6,7 @@
<groupId>io.github.retrooper</groupId>
<artifactId>packetevents</artifactId>
<version>1.2.6</version>
<version>1.2.7</version>
<packaging>jar</packaging>
<name>packetevents</name>
<properties>
@@ -1,7 +1,7 @@
package io.github.retrooper.packetevents.example;
import io.github.retrooper.packetevents.PacketEvents;
import io.github.retrooper.packetevents.enums.PlayerDigType;
import io.github.retrooper.packetevents.enums.*;
import io.github.retrooper.packetevents.event.PacketEvent;
import io.github.retrooper.packetevents.event.PacketHandler;
import io.github.retrooper.packetevents.event.PacketListener;
@@ -9,14 +9,12 @@ import io.github.retrooper.packetevents.packetwrappers.in.abilities.WrappedPacke
import io.github.retrooper.packetevents.packetwrappers.in.blockdig.WrappedPacketInBlockDig;
import io.github.retrooper.packetevents.packetwrappers.in.blockplace.WrappedPacketInBlockPlace;
import io.github.retrooper.packetevents.packetwrappers.in.chat.WrappedPacketInChat;
import io.github.retrooper.packetevents.packetwrappers.in.entityaction.WrappedPacketInEntityAction;
import io.github.retrooper.packetevents.packetwrappers.in.flying.WrappedPacketInFlying;
import io.github.retrooper.packetevents.packetwrappers.in.keepalive.WrappedPacketInKeepAlive;
import io.github.retrooper.packetevents.packetwrappers.in.useentity.WrappedPacketInUseEntity;
import io.github.retrooper.packetevents.packet.Packet;
import io.github.retrooper.packetevents.utils.vector.Vector3i;
import io.github.retrooper.packetevents.enums.Direction;
import io.github.retrooper.packetevents.enums.EntityUseAction;
import io.github.retrooper.packetevents.enums.Hand;
import io.github.retrooper.packetevents.event.impl.*;
import org.bukkit.Location;
import org.bukkit.entity.Entity;
@@ -45,11 +43,10 @@ public class RegisteredListener implements PacketListener, Listener {
//ONLY CLIENT PACKETS ALLOWED HERE!
switch (e.getPacketName()) {
case Packet.Client.USE_ENTITY:
final WrappedPacketInUseEntity useEntity = new WrappedPacketInUseEntity(e.getPlayer(), e.getPacket());
final WrappedPacketInUseEntity useEntity = new WrappedPacketInUseEntity(e.getPacket());
final Entity entity = useEntity.getEntity();
final double distanceSquared = entity.getLocation().distanceSquared(p.getLocation());
final Hand hand = useEntity.getHand();
final EntityUseAction action = useEntity.getAction();
final EntityUseAction useAction = useEntity.getAction();
break;
case Packet.Client.ABILITIES:
final WrappedPacketInAbilities abilities = new WrappedPacketInAbilities(e.getPacket());
@@ -97,6 +94,11 @@ public class RegisteredListener implements PacketListener, Listener {
final WrappedPacketInKeepAlive keepAlive = new WrappedPacketInKeepAlive(e.getPacket());
final long responseID = keepAlive.getId();
break;
case Packet.Client.ENTITY_ACTION:
final WrappedPacketInEntityAction entityAction = new WrappedPacketInEntityAction(e.getPacket());
final PlayerAction action = entityAction.getAction();
final int jumpBoost = entityAction.getJumpBoost();
break;
//System.out.println("YOU SAID: " + message);
}
@@ -13,12 +13,12 @@ public class WrappedPacket {
"and tell him what version your server is running on! " +
"Make sure you are using spigot!";
public WrappedPacket(Object packet) {
public WrappedPacket(final Object packet) {
this.player = null;
this.packet = packet;
try {
setup();
} catch (Exception e) {
} catch (final Exception e) {
e.printStackTrace();
}
}
@@ -28,10 +28,8 @@ public class WrappedPacket {
this.packet = packet;
try {
setup();
} catch (Exception e) {
if (!(e instanceof NullPointerException)) {
e.printStackTrace();
}
} catch (final Exception e) {
e.printStackTrace();
}
}
@@ -0,0 +1,108 @@
package io.github.retrooper.packetevents.packetwrappers.in.entityaction;
import io.github.retrooper.packetevents.enums.PlayerAction;
import io.github.retrooper.packetevents.enums.ServerVersion;
import io.github.retrooper.packetevents.packetwrappers.api.WrappedPacket;
import io.github.retrooper.packetevents.utils.NMSUtils;
import org.bukkit.entity.Entity;
import javax.annotation.Nullable;
import java.lang.reflect.Field;
public class WrappedPacketInEntityAction extends WrappedPacket {
private Entity entity;
private int entityId;
private PlayerAction action;
private int jumpBoost;
public WrappedPacketInEntityAction(final Object packet) {
super(packet);
}
@Override
protected void setup() throws Exception {
final int entityId = fields[0].getInt(packet);
final int jumpBoost = fields[2].getInt(packet);
int animationIndex = -1;
//1.7.10
if (version.isLowerThan(ServerVersion.v_1_8)) {
animationIndex = fields[1].getInt(packet);
} else {
final Object enumObj = fields[1].get(packet);
final Object[] enumValues = enumPlayerActionConstants;
final int len = enumValues.length;
for (int i = 0; i < len; i++) {
final Object val = enumValues[i];
if (val.toString().equals(enumObj.toString())) {
animationIndex = i;
break;
}
}
}
this.entityId = entityId;
this.action = action;
this.jumpBoost = jumpBoost;
this.action = PlayerAction.get((byte) animationIndex);
this.entity = NMSUtils.getEntityById(this.entityId);
}
public Entity getEntity() {
return entity;
}
public int getEntityId() {
return entityId;
}
public PlayerAction getAction() {
return action;
}
public int getJumpBoost() {
return jumpBoost;
}
private static Class<?> entityActionClass;
@Nullable
private static Class<?> enumPlayerActionClass;
private static Field[] fields = new Field[3];
@Nullable
private static Object[] enumPlayerActionConstants;
static {
try {
entityActionClass = NMSUtils.getNMSClass("PacketPlayInEntityAction");
if (version.isHigherThan(ServerVersion.v_1_7_10)) {
enumPlayerActionClass = NMSUtils.getNMSClass("PacketPlayInEntityAction$EnumPlayerAction");
}
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
try {
//ints in 1.7.10, EnumPlayerAction in 1.8+
fields[0] = entityActionClass.getDeclaredField("a");
fields[1] = entityActionClass.getDeclaredField("animation");
fields[2] = entityActionClass.getDeclaredField("c");
if (version.isHigherThan(ServerVersion.v_1_7_10)) {
enumPlayerActionConstants = enumPlayerActionClass.getEnumConstants();
}
} catch (NoSuchFieldException e) {
e.printStackTrace();
}
for (final Field f : fields) {
f.setAccessible(true);
}
}
}
@@ -1,54 +1,30 @@
package io.github.retrooper.packetevents.packetwrappers.in.useentity;
import io.github.retrooper.packetevents.enums.EntityUseAction;
import io.github.retrooper.packetevents.enums.ServerVersion;
import io.github.retrooper.packetevents.packetwrappers.api.WrappedPacket;
import io.github.retrooper.packetevents.utils.NMSUtils;
import io.github.retrooper.packetevents.enums.Hand;
import org.bukkit.entity.Entity;
import org.bukkit.entity.Player;
import javax.annotation.Nullable;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
public class WrappedPacketInUseEntity extends WrappedPacket {
private int entityId;
private Entity entity;
private EntityUseAction action;
private Hand hand;
public WrappedPacketInUseEntity(final Player player, final Object packet) {
super(player, packet);
public WrappedPacketInUseEntity(final Object packet) {
super(packet);
}
@Override
protected void setup() throws IllegalAccessException, InvocationTargetException {
protected void setup() throws IllegalAccessException {
this.entityId = fields[0].getInt(packet);
this.entity = NMSUtils.getNearByEntityById(getPlayer().getWorld(), entityId);
this.entity = NMSUtils.getEntityById(this.entityId);
final Object useActionEnum = fields[1].get(packet);
this.action = EntityUseAction.valueOf(useActionEnum.toString());
final Object handObj;
if (fields[2] == null) {
handObj = Hand.MAIN_HAND.name();
} else {
handObj = fields[2].get(packet);
}
this.hand = Hand.valueOf(handObj.toString());
}
@Nullable
/**
* It is possible for the entity to be null, if the player attacks from way too far,
* if it is null, please find the entity yourself with the entity ID
*/
public Entity getEntity() {
return entity;
}
@@ -61,23 +37,19 @@ public class WrappedPacketInUseEntity extends WrappedPacket {
return action;
}
public Hand getHand() {
return hand;
}
private static Class<?> useEntityClass;
private static Class<?> entityClass;
private static Field[] fields = new Field[3];
private static Field[] fields = new Field[2];
static {
try {
useEntityClass = NMSUtils.getNMSClass("PacketPlayInUseEntity");
entityClass = NMSUtils.getNMSClass("Entity");
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
@@ -86,9 +58,6 @@ public class WrappedPacketInUseEntity extends WrappedPacket {
try {
fields[0] = useEntityClass.getDeclaredField("a");
fields[1] = useEntityClass.getDeclaredField("action");
if (version.isHigherThan(ServerVersion.v_1_8_8)) {
fields[2] = useEntityClass.getDeclaredField("d");
}
} catch (NoSuchFieldException e) {
e.printStackTrace();
@@ -2,9 +2,12 @@ package io.github.retrooper.packetevents.utils;
import io.github.retrooper.packetevents.enums.ServerVersion;
import io.github.retrooper.packetevents.utils.nms_entityfinder.EntityFinderUtils;
import org.bukkit.Bukkit;
import org.bukkit.World;
import org.bukkit.entity.Entity;
import org.bukkit.entity.Player;
import javax.annotation.Nullable;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
@@ -22,8 +25,6 @@ public class NMSUtils {
private static Method getServerMethod;
private static Field recentTPSField;
private static Class<?> worldServerClass;
private static Method getCraftWorldHandleMethod;
private static Method entityMethod;
@@ -34,14 +35,14 @@ public class NMSUtils {
private static Method getCraftPlayerHandle;
private static Field entityPlayerPingField;
static {
try {
minecraftServerClass = getNMSClass("MinecraftServer");
worldServerClass = getNMSClass("WorldServer");
craftWorldsClass = getOBCClass("CraftWorld");
craftPlayerClass = getOBCClass("entity.CraftPlayer");
entityPlayerClass= getNMSClass("EntityPlayer");
entityPlayerClass = getNMSClass("EntityPlayer");
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
@@ -50,7 +51,6 @@ public class NMSUtils {
try {
getServerMethod = minecraftServerClass.getMethod("getServer");
getCraftWorldHandleMethod = craftWorldsClass.getMethod("getHandle");
// entityMethod = worldServerClass.getMethod(is_1_8() ? "a" : "getEntity");
getCraftPlayerHandle = craftPlayerClass.getMethod("getHandle");
} catch (NoSuchMethodException e) {
e.printStackTrace();
@@ -59,7 +59,7 @@ public class NMSUtils {
try {
recentTPSField = minecraftServerClass.getField("recentTps");
entityPlayerPingField =entityPlayerClass.getField("ping");
entityPlayerPingField = entityPlayerClass.getField("ping");
} catch (NoSuchFieldException e) {
e.printStackTrace();
}
@@ -87,11 +87,17 @@ public class NMSUtils {
return Class.forName(nettyPrefix + "." + name);
}
public static Entity getNearByEntityById(final World w, final int id) throws InvocationTargetException, IllegalAccessException {
return EntityFinderUtils.getEntityById(w, id);
@Nullable
public static Entity getEntityById(final int id) {
return EntityFinderUtils.getEntityById(id);
}
public static int getPlayerPing(final Player player){
@Nullable
public static Entity getEntityByIdWithWorld(final World world, final int id) {
return EntityFinderUtils.getEntityByIdWithWorld(world, id);
}
public static int getPlayerPing(final Player player) {
final Object craftPlayer = craftPlayerClass.cast(player);
Object entityPlayer = null;
try {
@@ -2,50 +2,93 @@ package io.github.retrooper.packetevents.utils.nms_entityfinder;
import io.github.retrooper.packetevents.PacketEvents;
import io.github.retrooper.packetevents.enums.ServerVersion;
import io.github.retrooper.packetevents.utils.NMSUtils;
import org.bukkit.Bukkit;
import org.bukkit.World;
import org.bukkit.entity.Entity;
import javax.annotation.Nullable;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
public class EntityFinderUtils {
@Nullable
private static ServerVersion version = PacketEvents.getServerVersion();
public static Entity getEntityById(final World w, final int id) {
if (w == null) return null;
if(version == ServerVersion.v_1_7_10) {
return EntityFinder_1_7_10.getEntityById(w, id);
}
else if(version == ServerVersion.v_1_8) {
return EntityFinder_1_8.getEntityById(w, id);
}
else if(version == ServerVersion.v_1_8_3) {
return EntityFinder_1_8_3.getEntityById(w, id);
}
else if(version==ServerVersion.v_1_8_8) {
return EntityFinder_1_8_8.getEntityById(w, id);
}
else if(version==ServerVersion.v_1_9) {
return EntityFinder_1_9.getEntityById(w, id);
}
else if(version == ServerVersion.v_1_10) {
return EntityFinder_1_10.getEntityById(w, id);
}
else if(version == ServerVersion.v_1_11) {
return EntityFinder_1_11.getEntityById(w, id);
}
else if(version == ServerVersion.v_1_12) {
return EntityFinder_1_12.getEntityById(w, id);
}
else if(version == ServerVersion.v_1_13) {
return EntityFinder_1_13.getEntityById(w, id);
}
else if(version == ServerVersion.v_1_13_2) {
return EntityFinder_1_13_2.getEntityById(w, id);
}
else if(version == ServerVersion.v_1_14) {
return EntityFinder_1_14.getEntityById(w, id);
}
else if(version == ServerVersion.v_1_15) {
return EntityFinder_1_15.getEntityById(w, id);
public static Entity getEntityById(final int id) {
for (final World world : Bukkit.getWorlds()) {
final Entity entity = getEntityByIdWithWorld(world, id);
if (entity != null) {
return entity;
}
}
return null;
}
@Nullable
public static Entity getEntityByIdWithWorld(final World world, final int id) {
Object craftWorld = craftWorldClass.cast(world);
Object worldServer = null;
try {
worldServer = craftWorldGetHandle.invoke(craftWorld);
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
}
Object nmsEntity = null;
try {
nmsEntity = getEntityByIdMethod.invoke(worldServer, id);
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
}
try {
return (Entity) getBukkitEntity.invoke(nmsEntity);
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
}
return null;
}
private static Class<?> worldServerClass;
private static Class<?> craftWorldClass;
private static Class<?> entityClass;
private static Method getEntityByIdMethod;
private static Method craftWorldGetHandle;
private static Method getBukkitEntity;
static {
try {
worldServerClass = NMSUtils.getNMSClass("WorldServer");
craftWorldClass = NMSUtils.getOBCClass("CraftWorld");
entityClass = NMSUtils.getNMSClass("Entity");
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
try {
getEntityByIdMethod = worldServerClass.getMethod(getEntityByNameMethodName(), int.class);
craftWorldGetHandle = craftWorldClass.getMethod("getHandle");
getBukkitEntity = entityClass.getMethod("getBukkitEntity");
} catch (NoSuchMethodException e) {
e.printStackTrace();
}
}
public static String getEntityByNameMethodName() {
if (version.isHigherThan(ServerVersion.v_1_7_10) && version.isLowerThan(ServerVersion.v_1_9)) {
return "a";
}
return "getEntity";
}
}
@@ -1,14 +0,0 @@
package io.github.retrooper.packetevents.utils.nms_entityfinder;
import net.minecraft.server.v1_10_R1.WorldServer;
import org.bukkit.World;
import org.bukkit.craftbukkit.v1_10_R1.CraftWorld;
import org.bukkit.entity.Entity;
class EntityFinder_1_10 {
public static Entity getEntityById(final World world, final int id) {
WorldServer worldServer = ((CraftWorld) world).getHandle();
return worldServer.getEntity(id).getBukkitEntity();
}
}
@@ -1,14 +0,0 @@
package io.github.retrooper.packetevents.utils.nms_entityfinder;
import net.minecraft.server.v1_11_R1.WorldServer;
import org.bukkit.World;
import org.bukkit.craftbukkit.v1_11_R1.CraftWorld;
import org.bukkit.entity.Entity;
class EntityFinder_1_11 {
public static Entity getEntityById(final World world, final int id) {
WorldServer worldServer = ((CraftWorld) world).getHandle();
return worldServer.getEntity(id).getBukkitEntity();
}
}
@@ -1,14 +0,0 @@
package io.github.retrooper.packetevents.utils.nms_entityfinder;
import net.minecraft.server.v1_12_R1.WorldServer;
import org.bukkit.World;
import org.bukkit.craftbukkit.v1_12_R1.CraftWorld;
import org.bukkit.entity.Entity;
class EntityFinder_1_12 {
public static Entity getEntityById(final World world, final int id) {
WorldServer worldServer = ((CraftWorld) world).getHandle();
return worldServer.getEntity(id).getBukkitEntity();
}
}
@@ -1,14 +0,0 @@
package io.github.retrooper.packetevents.utils.nms_entityfinder;
import net.minecraft.server.v1_13_R1.WorldServer;
import org.bukkit.World;
import org.bukkit.craftbukkit.v1_13_R1.CraftWorld;
import org.bukkit.entity.Entity;
class EntityFinder_1_13 {
public static Entity getEntityById(final World world, final int id) {
WorldServer worldServer = ((CraftWorld) world).getHandle();
return worldServer.getEntity(id).getBukkitEntity();
}
}
@@ -1,14 +0,0 @@
package io.github.retrooper.packetevents.utils.nms_entityfinder;
import net.minecraft.server.v1_13_R2.WorldServer;
import org.bukkit.World;
import org.bukkit.craftbukkit.v1_13_R2.CraftWorld;
import org.bukkit.entity.Entity;
class EntityFinder_1_13_2 {
public static Entity getEntityById(final World world, final int id) {
WorldServer worldServer = ((CraftWorld) world).getHandle();
return worldServer.getEntity(id).getBukkitEntity();
}
}
@@ -1,14 +0,0 @@
package io.github.retrooper.packetevents.utils.nms_entityfinder;
import net.minecraft.server.v1_14_R1.WorldServer;
import org.bukkit.World;
import org.bukkit.craftbukkit.v1_14_R1.CraftWorld;
import org.bukkit.entity.Entity;
class EntityFinder_1_14 {
public static Entity getEntityById(final World world, final int id) {
WorldServer worldServer = ((CraftWorld) world).getHandle();
return worldServer.getEntity(id).getBukkitEntity();
}
}
@@ -1,14 +0,0 @@
package io.github.retrooper.packetevents.utils.nms_entityfinder;
import net.minecraft.server.v1_15_R1.WorldServer;
import org.bukkit.World;
import org.bukkit.craftbukkit.v1_15_R1.CraftWorld;
import org.bukkit.entity.Entity;
class EntityFinder_1_15 {
public static Entity getEntityById(final World world, final int id) {
WorldServer worldServer = ((CraftWorld) world).getHandle();
return worldServer.getEntity(id).getBukkitEntity();
}
}
@@ -1,14 +0,0 @@
package io.github.retrooper.packetevents.utils.nms_entityfinder;
import net.minecraft.server.v1_7_R4.*;
import org.bukkit.World;
import org.bukkit.craftbukkit.v1_7_R4.*;
import org.bukkit.entity.Entity;
class EntityFinder_1_7_10 {
public static Entity getEntityById(final World world, final int id) {
WorldServer worldServer = ((CraftWorld)world).getHandle();
net.minecraft.server.v1_7_R4.Entity e = worldServer.getEntity(id);
return e.getBukkitEntity();
}
}
@@ -1,14 +0,0 @@
package io.github.retrooper.packetevents.utils.nms_entityfinder;
import net.minecraft.server.v1_8_R1.WorldServer;
import org.bukkit.World;
import org.bukkit.craftbukkit.v1_8_R1.CraftWorld;
import org.bukkit.entity.Entity;
class EntityFinder_1_8 {
public static Entity getEntityById(final World world, final int id) {
WorldServer worldServer = ((CraftWorld)world).getHandle();
return worldServer.a(id).getBukkitEntity();
}
}
@@ -1,14 +0,0 @@
package io.github.retrooper.packetevents.utils.nms_entityfinder;
import net.minecraft.server.v1_8_R2.WorldServer;
import org.bukkit.World;
import org.bukkit.craftbukkit.v1_8_R2.CraftWorld;
import org.bukkit.entity.Entity;
class EntityFinder_1_8_3 {
public static Entity getEntityById(final World world, final int id) {
WorldServer worldServer = ((CraftWorld) world).getHandle();
return worldServer.a(id).getBukkitEntity();
}
}
@@ -1,14 +0,0 @@
package io.github.retrooper.packetevents.utils.nms_entityfinder;
import net.minecraft.server.v1_8_R3.WorldServer;
import org.bukkit.World;
import org.bukkit.craftbukkit.v1_8_R3.CraftWorld;
import org.bukkit.entity.Entity;
class EntityFinder_1_8_8 {
public static Entity getEntityById(final World world, final int id) {
WorldServer worldServer = ((CraftWorld) world).getHandle();
return worldServer.a(id).getBukkitEntity();
}
}
@@ -1,14 +0,0 @@
package io.github.retrooper.packetevents.utils.nms_entityfinder;
import net.minecraft.server.v1_9_R1.WorldServer;
import org.bukkit.World;
import org.bukkit.craftbukkit.v1_9_R1.CraftWorld;
import org.bukkit.entity.Entity;
class EntityFinder_1_9 {
public static Entity getEntityById(final World world, final int id) {
WorldServer worldServer = ((CraftWorld) world).getHandle();
return worldServer.getEntity(id).getBukkitEntity();
}
}
@@ -1,14 +0,0 @@
package io.github.retrooper.packetevents.utils.nms_entityfinder;
import net.minecraft.server.v1_9_R2.WorldServer;
import org.bukkit.World;
import org.bukkit.craftbukkit.v1_9_R2.CraftWorld;
import org.bukkit.entity.Entity;
class EntityFinder_1_9_4 {
public static Entity getEntityById(final World world, final int id) {
WorldServer worldServer = ((CraftWorld) world).getHandle();
return worldServer.getEntity(id).getBukkitEntity();
}
}