Compare commits

...
4 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
26 changed files with 379 additions and 300 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.1.3</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,28 +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;
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;
@@ -62,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);
@@ -208,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.
@@ -222,6 +225,7 @@ public class PacketEvents implements PacketListener, Listener {
/**
* Called after the PlayerJoinEvent ONLY if the player has been injected!
*
* @param e
*/
@PacketHandler
@@ -230,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())) {
@@ -254,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) {
@@ -263,6 +251,7 @@ public class PacketEvents implements PacketListener, Listener {
/**
* Version independant player injection
*
* @param player
*/
public static void uninjectPlayer(final Player player) {
@@ -271,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;
@@ -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,172 +0,0 @@
package io.github.retrooper.packetevents.example;
import io.github.retrooper.packetevents.PacketEvents;
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;
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.packetwrappers.out.kickdisconnect.WrappedPacketOutKickDisconnect;
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;
}
}
@@ -77,6 +77,8 @@ 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"};
}
@@ -1,12 +1,12 @@
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 final class WrappedPacketInEntityAction extends WrappedPacket {
@@ -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);
}
}
@@ -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) {