Compare commits

...
2 Commits
Author SHA1 Message Date
purplexdev 297d261ca9 1.3.1.3 Beta
* ClientVersion added isHigherThan(ClientVersion version) and isLowerThan(ClientVersion version) methods for simple client version comparison.

* Packet.isInstanceOf(fatherPacketName, childPacketName) has now been deprecated, will be removed in the next version!

It has been replaced by Packet.isFlyingPacket(nmsPacketObject), as the flying packet is the only packet that has subclasses.
The flying packet is also used a lot, so I made an efficient way to check if the nms packet object is an instanceof of it!
If you are on a 1.9+ server, it will just check if the packet is an instance of "PacketPlayInPosition" or "PacketPlayInPositionLook" or "PacketPlayInLook".
2020-06-16 14:21:05 +02:00
purplexdev 89a67e7ee4 1.3.1.2 Stable!
* All wrapper classes are now FINAL classes as they are not meant to be extended.

* New wrapper class -> WrappedPacketOutKickDisconnect.
You can send this packet to any player if you want to kick them, or even read the message from such a packet.
2020-06-16 13:43:39 +02:00
21 changed files with 194 additions and 47 deletions

No files matched your search

+1 -1
View File
@@ -6,7 +6,7 @@
<groupId>io.github.retrooper</groupId>
<artifactId>packetevents</artifactId>
<version>1.3.0</version>
<version>1.3.1.3</version>
<packaging>jar</packaging>
<name>packetevents</name>
<properties>
@@ -26,8 +26,6 @@ import org.bukkit.scheduler.BukkitTask;
import javax.annotation.Nullable;
import java.lang.reflect.InvocationTargetException;
import java.util.HashMap;
import java.util.HashSet;
import java.util.UUID;
public class PacketEvents implements PacketListener, Listener {
@@ -64,4 +64,35 @@ public enum ClientVersion {
}
return INVALID;
}
/**
* Returns if the client's version is more up to date than the argument passed version
*
* @param version
* @return
*/
public boolean isHigherThan(final ClientVersion version) {
if (this == version) return false;
return !isLowerThan(version);
}
/**
* Returns if the client's version is more outdated than the argument passed version
* @param version
* @return
*/
public boolean isLowerThan(final ClientVersion version) {
if(version == this) return false;
byte len = (byte) values().length;
for (byte i = 0; i < len; i++) {
final ClientVersion v = values()[i];
if(v == this) {
return true;
}
else if(v == version) {
return false;
}
}
return false;
}
}
@@ -1,10 +1,7 @@
package io.github.retrooper.packetevents.example;
import io.github.retrooper.packetevents.PacketEvents;
import io.github.retrooper.packetevents.enums.Direction;
import io.github.retrooper.packetevents.enums.EntityUseAction;
import io.github.retrooper.packetevents.enums.PlayerAction;
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;
@@ -21,6 +18,7 @@ import io.github.retrooper.packetevents.packetwrappers.in.useentity.WrappedPacke
import io.github.retrooper.packetevents.packetwrappers.login.WrappedPacketLoginHandshake;
import io.github.retrooper.packetevents.packetwrappers.out.chat.WrappedPacketOutChat;
import io.github.retrooper.packetevents.packetwrappers.out.entityvelocity.WrappedPacketOutEntityVelocity;
import io.github.retrooper.packetevents.packetwrappers.out.kickdisconnect.WrappedPacketOutKickDisconnect;
import io.github.retrooper.packetevents.utils.vector.Vector3i;
import org.bukkit.Location;
import org.bukkit.entity.Entity;
@@ -1,15 +1,17 @@
package io.github.retrooper.packetevents.packet;
import io.github.retrooper.packetevents.enums.ServerVersion;
import io.github.retrooper.packetevents.utils.NMSUtils;
public abstract class Packet {
public static class Client {
private static final String c = "PacketPlayIn";
@Deprecated
public static final String FLYING = c + "Flying";
public static final String POSITION = c + "Position";
public static final String POSITION_LOOK = c + "PositionLook";
@@ -28,7 +30,8 @@ public abstract class Packet {
/**
* The packet name of the block place depending on your server version, "PacketPlayInBlockPlace" or "PacketPlayInUseItem"
*/
public static final String BLOCK_PLACE = c + ((ServerVersion.getVersion().isHigherThan(ServerVersion.v_1_8_8)) ? "UseItem" : "BlockPlace");;
public static final String BLOCK_PLACE = c + ((ServerVersion.getVersion().isHigherThan(ServerVersion.v_1_8_8)) ? "UseItem" : "BlockPlace");
public static final String ABILITIES = c + "Abilities";
public static final String HELD_ITEM_SLOT = c + "HeldItemSlot";
public static final String CLOSE_WINDOW = c + "CloseWindow";
@@ -37,6 +40,7 @@ public abstract class Packet {
public static final String SET_CREATIVE_SLOT = c + "SetCreativeSlot";
public static final String KEEP_ALIVE = c + "KeepAlive";
}
public static class Server {
@@ -74,9 +78,35 @@ public abstract class Packet {
public static final String START = "PacketStatusInStart";
public static final String SUCCESS = "PacketLoginOutSuccess";
public static final String[] LOGIN_PACKETS = new String[] {"PacketHandshakingInSetProtocol","PacketLoginInStart", "PacketLoginInEncryptionBegin"};
public static final String[] LOGIN_PACKETS = new String[]{"PacketHandshakingInSetProtocol", "PacketLoginInStart", "PacketLoginInEncryptionBegin"};
}
/**
* Is this NMS packet an instanceof the "PacketPlayInFlying" packet in the 1.8 protocol?
* 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!
*/
public static boolean isInstanceOfFlyingPacket(Object nmsPacket) {
//1.7->1.8.8
if (packetPlayInFlying != null) {
return packetPlayInFlying.isInstance(nmsPacket);
} else {
//1.9->1.15.2
return packetPlayInPosition.isInstance(nmsPacket)
|| packetPlayInPositionLook.isInstance(nmsPacket)
|| packetPlayInLook.isInstance(nmsPacket);
}
}
/**
* Use isInstanceOfFlyingPacket(nmsPacketObject) instead of this, as the flying packet is the only packet that has subclasses (in 1.8 atleast).
* Will be removed in newer versions of PacketEvents!
* @param fatherPacket
* @param childPacket
* @return isInstanceOf
*/
@Deprecated
public static boolean isInstanceOf(String fatherPacket, String childPacket) {
if (fatherPacket.equals(Client.FLYING) && childPacket.equals(Client.POSITION) || childPacket.equals(Client.POSITION_LOOK) || childPacket.equals(Client.LOOK)) {
return true;
@@ -84,5 +114,21 @@ public abstract class Packet {
return fatherPacket.equals(childPacket);
}
private static Class<?> packetPlayInFlying, packetPlayInPosition, packetPlayInPositionLook, packetPlayInLook;
static {
try {
packetPlayInFlying = NMSUtils.getNMSClass(Client.FLYING);
} catch (ClassNotFoundException e) {
//That is fine, they are on 1.9+ so we just initiate the rest of the variables
try {
packetPlayInPosition = NMSUtils.getNMSClass(Client.POSITION);
packetPlayInPositionLook = NMSUtils.getNMSClass(Client.POSITION_LOOK);
packetPlayInLook = NMSUtils.getNMSClass(Client.LOOK);
} catch (ClassNotFoundException e2) {
e2.printStackTrace();
}
}
}
}
@@ -5,7 +5,7 @@ import io.github.retrooper.packetevents.utils.NMSUtils;
import java.lang.reflect.Field;
public class WrappedPacketInAbilities extends WrappedPacket {
public final class WrappedPacketInAbilities extends WrappedPacket {
private boolean isVulnerable;
private boolean isFlying;
private boolean allowFly;
@@ -10,7 +10,7 @@ import io.github.retrooper.packetevents.utils.vector.Vector3i;
import java.lang.reflect.Field;
public class WrappedPacketInBlockDig extends WrappedPacket {
public final class WrappedPacketInBlockDig extends WrappedPacket {
private Vector3i blockPosition;
private Direction direction;
private PlayerDigType digType;
@@ -8,7 +8,7 @@ import org.bukkit.entity.Player;
import org.bukkit.inventory.ItemStack;
public class WrappedPacketInBlockPlace extends WrappedPacket {
public final class WrappedPacketInBlockPlace extends WrappedPacket {
private Vector3i blockPosition;
private ItemStack itemStack;
@@ -6,7 +6,7 @@ import net.minecraft.server.v1_7_R4.PacketPlayInBlockPlace;
import org.bukkit.entity.Player;
import org.bukkit.inventory.ItemStack;
class WrappedPacketInBlockPlace_1_7_10 extends WrappedPacket {
final class WrappedPacketInBlockPlace_1_7_10 extends WrappedPacket {
private Vector3i blockPosition;
private ItemStack itemStack;
private int blockFace;
@@ -6,7 +6,7 @@ import io.github.retrooper.packetevents.utils.NMSUtils;
import io.github.retrooper.packetevents.utils.vector.Vector3i;
import org.bukkit.inventory.ItemStack;
class WrappedPacketInBlockPlace_1_8 extends WrappedPacket {
final class WrappedPacketInBlockPlace_1_8 extends WrappedPacket {
private Vector3i blockPosition;
private ItemStack itemStack;
WrappedPacketInBlockPlace_1_8(final Object packet) {
@@ -9,7 +9,7 @@ import org.bukkit.entity.Player;
import java.util.HashSet;
class WrappedPacketInBlockPlace_1_9 extends WrappedPacket {
final class WrappedPacketInBlockPlace_1_9 extends WrappedPacket {
private Block block;
public WrappedPacketInBlockPlace_1_9(final Player player, final Object packet) {
@@ -6,7 +6,7 @@ import io.github.retrooper.packetevents.utils.NMSUtils;
import java.lang.reflect.Field;
public class WrappedPacketInChat extends WrappedPacket {
public final class WrappedPacketInChat extends WrappedPacket {
private String message;
public WrappedPacketInChat(Object packet) {
@@ -9,7 +9,7 @@ import org.bukkit.entity.Entity;
import javax.annotation.Nullable;
import java.lang.reflect.Field;
public class WrappedPacketInEntityAction extends WrappedPacket {
public final class WrappedPacketInEntityAction extends WrappedPacket {
private Entity entity;
private int entityId;
private PlayerAction action;
@@ -8,7 +8,6 @@ import io.github.retrooper.packetevents.utils.NMSUtils;
import java.lang.reflect.Field;
public class WrappedPacketInFlying extends WrappedPacket {
private double x;
private double y;
private double z;
@@ -73,27 +72,27 @@ public class WrappedPacketInFlying extends WrappedPacket {
}
}
public double getX() {
public final double getX() {
return x;
}
public double getY() {
public final double getY() {
return y;
}
public double getZ() {
public final double getZ() {
return z;
}
public float getYaw() {
public final float getYaw() {
return yaw;
}
public float getPitch() {
public final float getPitch() {
return pitch;
}
public boolean isOnGround() {
public final boolean isOnGround() {
return onGround;
}
@@ -6,7 +6,7 @@ import io.github.retrooper.packetevents.utils.NMSUtils;
import java.lang.reflect.Field;
public class WrappedPacketInKeepAlive extends WrappedPacket {
public final class WrappedPacketInKeepAlive extends WrappedPacket {
public WrappedPacketInKeepAlive(final Object packet) {
super(packet);
}
@@ -7,7 +7,7 @@ import org.bukkit.entity.Entity;
import java.lang.reflect.Field;
public class WrappedPacketInUseEntity extends WrappedPacket {
public final class WrappedPacketInUseEntity extends WrappedPacket {
private int entityId;
private Entity entity;
private EntityUseAction action;
@@ -6,7 +6,7 @@ import io.github.retrooper.packetevents.tinyprotocol.Reflection.FieldAccessor;
import io.github.retrooper.packetevents.utils.NMSUtils;
public class WrappedPacketLoginHandshake extends WrappedPacket {
public final class WrappedPacketLoginHandshake extends WrappedPacket {
private int protocolVersion, port;
private String hostname;
@@ -10,7 +10,7 @@ import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
public class WrappedPacketOutAnimation extends WrappedPacket implements Sendable {
public final class WrappedPacketOutAnimation extends WrappedPacket implements Sendable {
private Entity entity;
private int entityID;
private EntityAnimationType type;
@@ -40,15 +40,15 @@ public class WrappedPacketOutAnimation extends WrappedPacket implements Sendable
}
public final Entity getEntity() {
public Entity getEntity() {
return this.entity;
}
public final int getEntityId() {
public int getEntityId() {
return this.entityID;
}
public final EntityAnimationType getAnimationType() {
public EntityAnimationType getAnimationType() {
return type;
}
@@ -10,7 +10,7 @@ import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
public class WrappedPacketOutChat extends WrappedPacket implements Sendable {
public final class WrappedPacketOutChat extends WrappedPacket implements Sendable {
private String message;
public WrappedPacketOutChat(final Object packet) {
@@ -39,15 +39,8 @@ public class WrappedPacketOutChat extends WrappedPacket implements Sendable {
@Override
public Object asNMSPacket() {
Object iChatBaseComponentObj = null;
try {
iChatBaseComponentObj = serialize.invoke(null, this.message);
} catch (IllegalAccessException | InvocationTargetException e) {
e.printStackTrace();
}
try {
return chatClassConstructor.newInstance(iChatBaseComponentObj);
return chatClassConstructor.newInstance(toIChatBaseComponent(this.message));
} catch (InstantiationException | InvocationTargetException | IllegalAccessException e) {
e.printStackTrace();
}
@@ -55,6 +48,24 @@ public class WrappedPacketOutChat extends WrappedPacket implements Sendable {
return null;
}
public static String toStringFromIChatBaseComponent(Object obj) {
try {
return getStringOfIChatBase.invoke(obj).toString();
} catch (IllegalAccessException | InvocationTargetException e) {
e.printStackTrace();
}
return null;
}
public static Object toIChatBaseComponent(String msg) {
try {
return serialize.invoke(null, msg);
} catch (IllegalAccessException | InvocationTargetException e) {
e.printStackTrace();
}
return null;
}
public String getMessage() {
return this.message;
}
@@ -10,7 +10,7 @@ import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
public class WrappedPacketOutEntityVelocity extends WrappedPacket implements Sendable {
public final class WrappedPacketOutEntityVelocity extends WrappedPacket implements Sendable {
private Entity entity;
private int entityId;
private double velocityX, velocityY, velocityZ;
@@ -49,23 +49,23 @@ public class WrappedPacketOutEntityVelocity extends WrappedPacket implements Sen
}
}
public final int getEntityId() {
public int getEntityId() {
return entityId;
}
public final Entity getEntity() {
public Entity getEntity() {
return entity;
}
public final double getVelocityX() {
public double getVelocityX() {
return velocityX;
}
public final double getVelocityY() {
public double getVelocityY() {
return velocityY;
}
public final double getVelocityZ() {
public double getVelocityZ() {
return velocityZ;
}
@@ -0,0 +1,64 @@
package io.github.retrooper.packetevents.packetwrappers.out.kickdisconnect;
import io.github.retrooper.packetevents.packetwrappers.Sendable;
import io.github.retrooper.packetevents.packetwrappers.api.WrappedPacket;
import io.github.retrooper.packetevents.packetwrappers.out.chat.WrappedPacketOutChat;
import io.github.retrooper.packetevents.tinyprotocol.Reflection;
import io.github.retrooper.packetevents.utils.NMSUtils;
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
public final class WrappedPacketOutKickDisconnect extends WrappedPacket implements Sendable {
private String kickMessage;
public WrappedPacketOutKickDisconnect(final Object packet) {
super(packet);
}
public WrappedPacketOutKickDisconnect(final String kickMessage) {
super(null);
this.kickMessage = kickMessage;
}
@Override
protected void setup() {
this.kickMessage = WrappedPacketOutChat.toStringFromIChatBaseComponent(iChatBaseComponentAccessor.get(packet));
}
public String getKickMessage() {
return kickMessage;
}
@Override
public Object asNMSPacket() {
try {
return kickDisconnectConstructor.newInstance(WrappedPacketOutChat.toIChatBaseComponent("{\"text\": \"" + kickMessage + "\"}"));
} catch (InstantiationException | InvocationTargetException | IllegalAccessException e) {
e.printStackTrace();
}
return null;
}
private static Class<?> packetClass, iChatBaseComponentClass;
private static Constructor<?> kickDisconnectConstructor;
private static final Reflection.FieldAccessor<?> iChatBaseComponentAccessor;
static {
try {
packetClass = NMSUtils.getNMSClass("PacketPlayOutKickDisconnect");
iChatBaseComponentClass = NMSUtils.getNMSClass("IChatBaseComponent");
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
try {
kickDisconnectConstructor = packetClass.getConstructor(iChatBaseComponentClass);
} catch (NoSuchMethodException e) {
e.printStackTrace();
}
iChatBaseComponentAccessor = Reflection.getField(packetClass, iChatBaseComponentClass, 0);
}
}