Compare commits

...
6 Commits
Author SHA1 Message Date
purplexdev 07692f87f2 1.3.3 Release STABLE
* EventManager small optimization
* Imports optimized
* Own GameProfile class added
* New wrappers, this version is going to go on spigotmc!
2020-06-17 17:29:51 +02:00
purplexdev 7f94b254a2 LoginInEncryptionBegin & LoginOutEncryptionBegin wrapper added,
LoginStart & LoginStatusPing wrapper added,
Bug fixed with PostPlayerInjectEvent not being called,
Custom GameProfile class coded,
Main example class readded
2020-06-17 13:15:35 +02:00
purplexdev ceb3581c28 PostPlayerInjectEvent not called bug fix! 2020-06-17 11:04:16 +02:00
purplexdev c3bf5d75d3 1.3.2 Release Stable!
* Code cleanup

* Custom NotNull and Nullable annotations added

* Example packages removed, please see https://github.com/retrooper/packetevents-example
2020-06-16 15:38:54 +02:00
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
42 changed files with 570 additions and 344 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.3</version>
<packaging>jar</packaging>
<name>packetevents</name>
<properties>
@@ -1,13 +1,12 @@
package io.github.retrooper.packetevents;
import io.github.retrooper.packetevents.annotations.Nullable;
import io.github.retrooper.packetevents.annotations.PacketHandler;
import io.github.retrooper.packetevents.enums.ClientVersion;
import io.github.retrooper.packetevents.enums.ServerVersion;
import io.github.retrooper.packetevents.event.PacketHandler;
import io.github.retrooper.packetevents.event.PacketEvent;
import io.github.retrooper.packetevents.event.PacketListener;
import io.github.retrooper.packetevents.event.impl.PacketLoginEvent;
import io.github.retrooper.packetevents.event.impl.PlayerInjectEvent;
import io.github.retrooper.packetevents.event.impl.PostPlayerInjectEvent;
import io.github.retrooper.packetevents.event.impl.ServerTickEvent;
import io.github.retrooper.packetevents.event.impl.*;
import io.github.retrooper.packetevents.event.manager.EventManager;
import io.github.retrooper.packetevents.handler.TinyProtocolHandler;
import io.github.retrooper.packetevents.packet.Packet;
@@ -19,30 +18,25 @@ import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.player.PlayerJoinEvent;
import org.bukkit.event.player.PlayerLoginEvent;
import org.bukkit.plugin.Plugin;
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 {
public final class PacketEvents implements PacketListener, Listener {
/*
* Login wrappers TODO
* PacketLoginInStart
* PacketLoginInEncryptionBegin
* Wrappers TODO:
* Spawn entity
*/
private static boolean hasRegistered;
private static final ServerVersion version = ServerVersion.getVersion();
private static PacketEvents instance;
private static EventManager eventManager = new EventManager();
private static final EventManager eventManager = new EventManager();
private static int currentTick;
@@ -64,8 +58,8 @@ public class PacketEvents implements PacketListener, Listener {
if (!hasRegistered) {
//Register Bukkit and PacketListener
getEventManager().registerListener(getInstance());
Bukkit.getPluginManager().registerEvents(getInstance(), plugin);
Bukkit.getPluginManager().registerEvents(getInstance(), plugin);
//Initialize the TinyProtocolHandler
TinyProtocolHandler.initTinyProtocol(plugin);
@@ -210,6 +204,13 @@ public class PacketEvents implements PacketListener, Listener {
}
}
@PacketHandler
public void onPacket(final PacketEvent e) {
if(e instanceof PacketReceiveEvent) {
// System.out.println("RECEIVED");
}
}
/**
* Do not check the client version in or before the PlayerInjectEvent, use the PostPlayerInjectEvent.
* It is not recommended to do much in the PlayerInjectEvent, as some fields in the Player object are be null.
@@ -224,6 +225,7 @@ public class PacketEvents implements PacketListener, Listener {
/**
* Called after the PlayerJoinEvent ONLY if the player has been injected!
*
* @param e
*/
@PacketHandler
@@ -232,22 +234,6 @@ public class PacketEvents implements PacketListener, Listener {
final ClientVersion clientVersion = PacketEvents.getClientVersion(e.getPlayer());
}
@EventHandler
public void onLogin(final PlayerLoginEvent e) {
final Plugin[] plugins = Bukkit.getPluginManager().getPlugins();
boolean canJoin = true;
for (final Plugin pl : plugins) {
if (!pl.isEnabled()) {
canJoin = false;
break;
}
}
if (!canJoin) {
e.setResult(PlayerLoginEvent.Result.KICK_OTHER);
e.setKickMessage("Please wait for the server to finish loading all plugins");
}
}
@EventHandler
public void onJoin(PlayerJoinEvent e) {
if (hasInjected(e.getPlayer())) {
@@ -256,7 +242,7 @@ public class PacketEvents implements PacketListener, Listener {
}
/**
* Version independant player injection
* Version independent player injection
* @param player
*/
public static void injectPlayer(final Player player) {
@@ -265,6 +251,7 @@ public class PacketEvents implements PacketListener, Listener {
/**
* Version independant player injection
*
* @param player
*/
public static void uninjectPlayer(final Player player) {
@@ -273,6 +260,7 @@ public class PacketEvents implements PacketListener, Listener {
/**
* Returns whether we have injected the player
*
* @param player
* @return hasInjected
*/
@@ -0,0 +1,15 @@
package io.github.retrooper.packetevents.annotations;
import java.lang.annotation.Documented;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
/**
* Is for sure not null.
*/
@Documented
@Retention(RetentionPolicy.RUNTIME)
public @interface NotNull {
}
@@ -0,0 +1,16 @@
package io.github.retrooper.packetevents.annotations;
import java.lang.annotation.Documented;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
/**
* Could be null.
*/
@Documented
@Retention(RetentionPolicy.RUNTIME)
public @interface Nullable {
}
@@ -1,4 +1,4 @@
package io.github.retrooper.packetevents.event;
package io.github.retrooper.packetevents.annotations;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
@@ -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;
}
}
@@ -3,7 +3,7 @@ package io.github.retrooper.packetevents.event.impl;
import io.github.retrooper.packetevents.event.PacketEvent;
import org.bukkit.entity.Player;
public class PlayerUninjectEvent extends PacketEvent {
public final class PlayerUninjectEvent extends PacketEvent {
private final Player player;
public PlayerUninjectEvent(final Player player) {
@@ -3,7 +3,7 @@ package io.github.retrooper.packetevents.event.impl;
import io.github.retrooper.packetevents.PacketEvents;
import io.github.retrooper.packetevents.event.PacketEvent;
public class ServerTickEvent extends PacketEvent {
public final class ServerTickEvent extends PacketEvent {
private final int tick;
private final double tps;
public ServerTickEvent(final int tick, final long timestamp) {
@@ -1,7 +1,7 @@
package io.github.retrooper.packetevents.event.manager;
import io.github.retrooper.packetevents.annotations.PacketHandler;
import io.github.retrooper.packetevents.event.PacketEvent;
import io.github.retrooper.packetevents.event.PacketHandler;
import io.github.retrooper.packetevents.event.PacketListener;
import java.lang.reflect.InvocationTargetException;
@@ -10,7 +10,7 @@ import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
public class EventManager {
public final class EventManager {
private HashMap<PacketListener, List<Method>> registeredMethods = new HashMap<PacketListener, List<Method>>();
@@ -19,8 +19,9 @@ public class EventManager {
//Annotated methods
final List<Method> methods = registeredMethods.get(listener);
for (final Method method : methods) {
if (method.getParameterTypes()[0].equals(PacketEvent.class)
|| method.getParameterTypes()[0].getSimpleName().equals(e.getClass().getSimpleName())) {
final Class<?> parameterType = method.getParameterTypes()[0];
if (parameterType.equals(PacketEvent.class)
|| parameterType.isInstance(e)) {
try {
method.invoke(listener, e);
} catch (IllegalAccessException | InvocationTargetException ex) {
@@ -33,13 +34,19 @@ public class EventManager {
public void registerListener(final PacketListener e) {
if (registeredMethods.containsKey(e)) {
return;
}
final List<Method> methods = new ArrayList<Method>();
for (final Method m : e.getClass().getMethods()) {
if (m.isAnnotationPresent(PacketHandler.class)) {
if (m.isAnnotationPresent(PacketHandler.class)
&& m.getParameterTypes().length == 1) {
methods.add(m);
}
}
registeredMethods.put(e, methods);
if (!methods.isEmpty()) {
registeredMethods.put(e, methods);
}
}
public void unregisterListener(final PacketListener e) {
@@ -1,59 +0,0 @@
package io.github.retrooper.packetevents.example;
import io.github.retrooper.packetevents.event.PacketEvent;
import org.bukkit.Location;
import org.bukkit.entity.Player;
/**
* Example packetevent
* Make sure to initiate the super timestamp variable,
* if you want to use the getTimestamp() function
*/
public class CustomMoveEvent extends PacketEvent {
private final Player player;
private Location to, from;
private boolean cancelled;
public CustomMoveEvent(final Player player, final Location to, final Location from) {
this.player = player;
this.to = to;
this.from = from;
this.cancelled = false;
//Timestamp is initialized automatically, so you don't need to initiate it, but you may modify it if you like
//this.timestamp = PacketEvents.currentTimeMS();
}
@Override
public long getTimestamp() {
return super.getTimestamp();
}
public void setCancelled(boolean cancelled) {
this.cancelled = cancelled;
}
public boolean isCancelled() {
return this.cancelled;
}
public Player getPlayer() {
return player;
}
public Location getTo() {
return to;
}
public Location getFrom() {
return from;
}
public void setTo(final Location loc) {
this.to = loc;
}
public void setFrom(final Location loc) {
this.from = from;
}
}
@@ -4,29 +4,14 @@ import io.github.retrooper.packetevents.PacketEvents;
import org.bukkit.plugin.java.JavaPlugin;
public class MainExample extends JavaPlugin {
@Override
public void onEnable() {
PacketEvents.start(this);
//If you do not need the server tick event you can just cancel its task like this
//PacketEvents.getServerTickTask().cancel();
/**
* Register TestExample's PacketListener
*/
PacketEvents.getEventManager().registerListener(new RegisteredListener());
}
@Override
public void onDisable() {
/**
* Stops server tick event task if enabled and unregisters packetlisteners
*/
PacketEvents.stop();
}
}
@@ -1,174 +0,0 @@
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.event.PacketEvent;
import io.github.retrooper.packetevents.event.PacketHandler;
import io.github.retrooper.packetevents.event.PacketListener;
import io.github.retrooper.packetevents.event.impl.*;
import io.github.retrooper.packetevents.packet.Packet;
import io.github.retrooper.packetevents.packetwrappers.in.abilities.WrappedPacketInAbilities;
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.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.utils.vector.Vector3i;
import org.bukkit.Location;
import org.bukkit.entity.Entity;
import org.bukkit.entity.Player;
import org.bukkit.inventory.ItemStack;
/**
* Implementing PacketListener to listen to packetevents' custom events,
*/
public class RegisteredListener implements PacketListener {
/**
* Listen to PacketReceiveEvent
*
* @param e PacketReceiveEvent
*/
@PacketHandler
public void onPacketReceive(PacketReceiveEvent e) {
final Player p = e.getPlayer();
final long timestamp = e.getTimestamp();
//The block place name field is not set at compilation time, so we cannot use it in the switch statement
//On 1.7->1.8.8 servers it is "PacketPlayInBlockPlace" and on 1.9+ servers it is "PacketPlayInUseItem"
if(e.getPacketName().equals(Packet.Client.BLOCK_PLACE)) {
final WrappedPacketInBlockPlace blockPlace = new WrappedPacketInBlockPlace(e.getPlayer(), e.getPacket());
final Vector3i blockPosition = blockPlace.getBlockPosition();
final ItemStack stack = blockPlace.getItemStack();
// e.getPlayer().sendMessage("pos: " + blockPosition.toString() + ", type: " + stack.getType());
}
switch (e.getPacketName()) {
case Packet.Client.USE_ENTITY:
final WrappedPacketInUseEntity useEntity = new WrappedPacketInUseEntity(e.getPacket());
final Entity entity = useEntity.getEntity();
final double distanceSquared = entity.getLocation().distanceSquared(p.getLocation());
final EntityUseAction useAction = useEntity.getAction();
break;
case Packet.Client.ABILITIES:
final WrappedPacketInAbilities abilities = new WrappedPacketInAbilities(e.getPacket());
final boolean isVulnerable = abilities.isVulnerable();
final boolean isFlying = abilities.isFlying();
final boolean canFly = abilities.canFly();
final boolean canInstantlyBuild = abilities.canInstantlyBuild();
final float flySpeed = abilities.getFlySpeed();
final float walkSpeed = abilities.getWalkSpeed();
// e.getPlayer().sendMessage("walkspeed: " + walkSpeed + ", flyspeed: " + flySpeed + ", canFly: " + canFly);
break;
case Packet.Client.BLOCK_DIG:
//Custom PlayerDigType enum
final WrappedPacketInBlockDig blockDig = new WrappedPacketInBlockDig(e.getPacket());
final PlayerDigType type = blockDig.getDigType();
final Direction direction = blockDig.getDirection();
break;
case Packet.Client.FLYING:
final WrappedPacketInFlying flying = new WrappedPacketInFlying(e.getPacket());
final float pitch = flying.getPitch();
final float yaw = flying.getYaw();
break;
case Packet.Client.POSITION:
final WrappedPacketInFlying.WrappedPacketInPosition position =
new WrappedPacketInFlying.WrappedPacketInPosition(e.getPacket());
final boolean isPos = position.isPosition(); //true
break;
case Packet.Client.POSITION_LOOK:
final WrappedPacketInFlying.WrappedPacketInPosition_Look position_look =
new WrappedPacketInFlying.WrappedPacketInPosition_Look(e.getPacket());
final boolean isLook = position_look.isLook(); //true
break;
case Packet.Client.CHAT:
final WrappedPacketInChat chat = new WrappedPacketInChat(e.getPacket());
final String message = chat.getMessage();
break;
case Packet.Client.KEEP_ALIVE:
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;
}
}
@PacketHandler
public void onInject(PlayerInjectEvent event) {
}
/**
* In this event you can safely register your players, unlike the PlayerInjectEvent.
* In the PlayerInjectEvent some fields in the player object might be null.
* So use this event!
* @param event
*/
@PacketHandler
public void onPostInject(PostPlayerInjectEvent event) {
}
/**
* You can use this event to unregister your players,
* This event is called when a player is uninjected
* @param event
*/
@PacketHandler
public void onUninject(PlayerUninjectEvent event) {
}
/**
* Another way to listen to packets
*
* @param e PacketEvent
*/
@PacketHandler
public void onPacket(PacketEvent e) {
if (e instanceof PacketSendEvent) {
final PacketSendEvent p = (PacketSendEvent) e;
final Player targetPlayer = p.getPlayer();
final Object rawNMSPacket = p.getPacket();
final long timestamp = p.getTimestamp();
if (p.getPacketName().equals(Packet.Server.ENTITY_VELOCITY)) {
final WrappedPacketOutEntityVelocity vel = new WrappedPacketOutEntityVelocity(p.getPacket());
final double velocityX = vel.getVelocityX();
final double velocityY = vel.getVelocityY();
final double velocityZ = vel.getVelocityZ();
} else if (p.getPacketName().equals(Packet.Server.CHAT)) {
final WrappedPacketOutChat chat = new WrappedPacketOutChat(p.getPacket());
}
} else if (e instanceof CustomMoveEvent) {
final CustomMoveEvent event = (CustomMoveEvent) e;
final Player player = event.getPlayer();
final Location to = event.getTo();
final Location from = event.getFrom();
} else if (e instanceof ServerTickEvent) {
final ServerTickEvent event = (ServerTickEvent) e;
final int currentTick = event.getCurrentTick();
final double tps = PacketEvents.getCurrentServerTPS();
}
else if(e instanceof PacketLoginEvent) {
final PacketLoginEvent event = (PacketLoginEvent)e;
if(event.getPacketName().equals(Packet.Login.HANDSHAKE)) {
final WrappedPacketLoginHandshake handshake = new WrappedPacketLoginHandshake(event.getPacket());
final int protocolVersion = handshake.getProtocolVersion();
final String hostname = handshake.getHostname();
final int port = handshake.getPort();
}
}
}
}
@@ -9,7 +9,7 @@ import io.github.retrooper.packetevents.utils.NMSUtils;
import org.bukkit.entity.Player;
import org.bukkit.plugin.Plugin;
public class TinyProtocolHandler {
public final class TinyProtocolHandler {
private static final ServerVersion version = PacketEvents.getServerVersion();
private static Object tinyProtocol;
@@ -1,6 +1,7 @@
package io.github.retrooper.packetevents.handler;
import io.github.retrooper.packetevents.PacketEvents;
import io.github.retrooper.packetevents.annotations.Nullable;
import io.github.retrooper.packetevents.event.impl.PacketLoginEvent;
import io.github.retrooper.packetevents.event.impl.PacketReceiveEvent;
import io.github.retrooper.packetevents.event.impl.PacketSendEvent;
@@ -10,9 +11,7 @@ import net.minecraft.util.io.netty.channel.Channel;
import org.bukkit.entity.Player;
import org.bukkit.plugin.Plugin;
import javax.annotation.Nullable;
public class TinyProtocolHandler_1_7 {
public final class TinyProtocolHandler_1_7 {
private final Plugin plugin;
public TinyProtocolHandler_1_7(final Plugin plugin) {
@@ -1,6 +1,7 @@
package io.github.retrooper.packetevents.handler;
import io.github.retrooper.packetevents.PacketEvents;
import io.github.retrooper.packetevents.annotations.Nullable;
import io.github.retrooper.packetevents.enums.ServerVersion;
import io.github.retrooper.packetevents.event.impl.PacketLoginEvent;
import io.github.retrooper.packetevents.event.impl.PacketReceiveEvent;
@@ -11,9 +12,7 @@ import io.netty.channel.Channel;
import org.bukkit.entity.Player;
import org.bukkit.plugin.Plugin;
import javax.annotation.Nullable;
public class TinyProtocolHandler_1_8 {
public final class TinyProtocolHandler_1_8 {
private static final ServerVersion version = PacketEvents.getServerVersion();
private final Plugin plugin;
@@ -0,0 +1,90 @@
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 {
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)) {
throw new IllegalArgumentException("Name and ID cannot both be blank");
} else {
this.id = id;
this.name = name;
}
}
public UUID getId() {
return this.id;
}
public String getName() {
return this.name;
}
public PropertyMap getProperties() {
return this.properties;
}
public boolean isComplete() {
return this.id != null && StringUtils.isNotBlank(this.getName());
}
public boolean equalsMojangGameProfile(com.mojang.authlib.GameProfile profile) {
if (profile != null) {
if (profile.getId() != null && getId() != null && profile.getId() == getId()
&& profile.getName() != null && getName() != null && profile.getName().equals(getName())) {
return true;
}
}
return false;
}
public boolean equals(Object o) {
if (this == o) {
return true;
} else if (o != null && this.getClass() == o.getClass()) {
GameProfile that = (GameProfile) o;
if (this.id != null) {
if (!this.id.equals(that.id)) {
return false;
}
} else if (that.id != null) {
return false;
}
if (this.name != null) {
if (this.name.equals(that.name)) {
return true;
}
} else if (that.name == null) {
return true;
}
return false;
} else {
return false;
}
}
public int hashCode() {
int result = this.id != null ? this.id.hashCode() : 0;
result = 31 * result + (this.name != null ? this.name.hashCode() : 0);
return result;
}
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;
}
}
@@ -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 {
@@ -73,10 +77,38 @@ public abstract class Packet {
public static final String PING = "PacketStatusInPing";
public static final String START = "PacketStatusInStart";
public static final String SUCCESS = "PacketLoginOutSuccess";
public static final String ENCRYPTION_BEGIN_IN = "PacketLoginInEncryptionBegin";
public static final String ENCRYPTION_BEGIN_OUT = "PacketLoginOutEncryptionBegin";
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 +116,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) {
@@ -1,15 +1,15 @@
package io.github.retrooper.packetevents.packetwrappers.in.entityaction;
import io.github.retrooper.packetevents.annotations.Nullable;
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 {
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;
@@ -0,0 +1,41 @@
package io.github.retrooper.packetevents.packetwrappers.login;
import io.github.retrooper.packetevents.packetwrappers.api.WrappedPacket;
import io.github.retrooper.packetevents.tinyprotocol.Reflection;
import io.github.retrooper.packetevents.utils.NMSUtils;
public final class WrappedPacketLoginInEncryptionBegin extends WrappedPacket {
private byte[] publicKey, verifyToken;
public WrappedPacketLoginInEncryptionBegin(final Object packet) {
super(packet);
}
@Override
protected void setup() {
this.publicKey = fields[0].get(packet);
this.verifyToken = fields[1].get(packet);
}
public byte[] getPublicKey() {
return publicKey;
}
public byte[] getVerifyToken() {
return verifyToken;
}
private static Class<?> packetClass;
private static final Reflection.FieldAccessor<byte[]>[] fields = new Reflection.FieldAccessor[2];
static {
try {
packetClass = NMSUtils.getNMSClass("PacketLoginInEncryptionBegin");
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
fields[0] = Reflection.getField(packetClass, byte[].class, 0);
fields[1] = Reflection.getField(packetClass, byte[].class, 1);
}
}
@@ -0,0 +1,58 @@
package io.github.retrooper.packetevents.packetwrappers.login;
import io.github.retrooper.packetevents.packetwrappers.api.WrappedPacket;
import io.github.retrooper.packetevents.tinyprotocol.Reflection;
import io.github.retrooper.packetevents.utils.NMSUtils;
import java.security.PublicKey;
public final class WrappedPacketLoginOutEncryptionBegin extends WrappedPacket {
private String encodedString;
private PublicKey encodedKey;
private byte[] byteArray;
public WrappedPacketLoginOutEncryptionBegin(final Object packet) {
super(packet);
}
@Override
protected void setup() {
//string - a (encodedstring)
//public key - b (encoded)
//byte array - c(byte array)
this.encodedString = fields[0].get(packet).toString();
this.encodedKey = (PublicKey) fields[1].get(packet);
this.byteArray = (byte[]) fields[2].get(packet);
}
public String getEncodedString() {
return encodedString;
}
public PublicKey getEncodedKey() {
return encodedKey;
}
public byte[] getByteArray() {
return byteArray;
}
private static Class<?> packetClass;
private static final Reflection.FieldAccessor<?>[] fields = new Reflection.FieldAccessor[3];
static {
try {
packetClass = NMSUtils.getNMSClass("PacketLoginOutEncryptionBegin");
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
fields[0] = Reflection.getField(packetClass, String.class, 0);
fields[1] = Reflection.getField(packetClass, PublicKey.class, 1);
fields[2] = Reflection.getField(packetClass, byte[].class, 2);
}
}
@@ -0,0 +1,36 @@
package io.github.retrooper.packetevents.packetwrappers.login;
import io.github.retrooper.packetevents.mojang.GameProfile;
import io.github.retrooper.packetevents.packetwrappers.api.WrappedPacket;
import io.github.retrooper.packetevents.tinyprotocol.Reflection;
import io.github.retrooper.packetevents.utils.NMSUtils;
public final class WrappedPacketLoginStart extends WrappedPacket {
private GameProfile gameProfile;
public WrappedPacketLoginStart(final Object packet) {
super(packet);
}
@Override
protected void setup() {
com.mojang.authlib.GameProfile gp = gameProfileAccessor.get(packet);
this.gameProfile = new GameProfile(gp.getId(), gp.getName());
}
public GameProfile getGameProfile() {
return gameProfile;
}
private static Class<?> packetClass;
private static final Reflection.FieldAccessor<com.mojang.authlib.GameProfile> gameProfileAccessor;
static {
try {
packetClass = NMSUtils.getNMSClass("PacketLoginInStart");
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
gameProfileAccessor = Reflection.getField(packetClass, com.mojang.authlib.GameProfile.class, 0);
}
}
@@ -0,0 +1,36 @@
package io.github.retrooper.packetevents.packetwrappers.login;
import io.github.retrooper.packetevents.packetwrappers.api.WrappedPacket;
import io.github.retrooper.packetevents.tinyprotocol.Reflection;
import io.github.retrooper.packetevents.utils.NMSUtils;
public final class WrappedPacketLoginStatusPing extends WrappedPacket {
private long number;
public WrappedPacketLoginStatusPing(final Object packet) {
super(packet);
}
@Override
protected void setup() {
this.number = numberAccessor.get(packet);
}
public long getNumber() {
return number;
}
private static Class<?> packetClass;
private static final Reflection.FieldAccessor<Long> numberAccessor;
static {
try {
packetClass = NMSUtils.getNMSClass("PacketStatusInPing");
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
numberAccessor = Reflection.getField(packetClass, long.class, 0);
}
}
@@ -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);
}
}
@@ -1,5 +1,6 @@
package io.github.retrooper.packetevents.utils;
import io.github.retrooper.packetevents.annotations.Nullable;
import io.github.retrooper.packetevents.enums.ServerVersion;
import io.github.retrooper.packetevents.packetwrappers.Sendable;
import io.github.retrooper.packetevents.utils.nms_entityfinder.EntityFinderUtils;
@@ -8,12 +9,11 @@ import org.bukkit.entity.Entity;
import org.bukkit.entity.Player;
import org.bukkit.inventory.ItemStack;
import javax.annotation.Nullable;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
public class NMSUtils {
public final class NMSUtils {
private static final ServerVersion version = ServerVersion.getVersion();
@@ -1,20 +1,21 @@
package io.github.retrooper.packetevents.utils.nms_entityfinder;
import io.github.retrooper.packetevents.PacketEvents;
import io.github.retrooper.packetevents.annotations.Nullable;
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 {
public final class EntityFinderUtils {
@Nullable
private static ServerVersion version = PacketEvents.getServerVersion();
public static Entity getEntityById(final int id) {
for (final World world : Bukkit.getWorlds()) {
final Entity entity = getEntityByIdWithWorld(world, id);
@@ -27,6 +28,9 @@ public class EntityFinderUtils {
@Nullable
public static Entity getEntityByIdWithWorld(final World world, final int id) {
if(world == null) {
return null;
}
Object craftWorld = craftWorldClass.cast(world);
Object worldServer = null;
@@ -0,0 +1,16 @@
package io.github.retrooper.packetevents.utils.vector;
public final class Vector3d {
public double x, y, z;
public Vector3d(final double x, final double y, final double z) {
this.x = x;
this.y = y;
this.z = z;
}
@Override
public String toString() {
return "X: " + x + ", Y: " + y + ", Z: " + z;
}
}
@@ -0,0 +1,16 @@
package io.github.retrooper.packetevents.utils.vector;
public final class Vector3f {
public float x, y, z;
public Vector3f(final float x, final float y, final float z) {
this.x = x;
this.y = y;
this.z = z;
}
@Override
public String toString() {
return "X: " + x + ", Y: " + y + ", Z: " + z;
}
}
@@ -1,6 +1,6 @@
package io.github.retrooper.packetevents.utils.vector;
public class Vector3i {
public final class Vector3i {
public int x, y, z;
public Vector3i(final int x, final int y, final int z) {