Compare commits

...
3 Commits
Author SHA1 Message Date
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
37 changed files with 277 additions and 347 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.2</version>
<packaging>jar</packaging>
<name>packetevents</name>
<properties>
@@ -2,7 +2,7 @@ package io.github.retrooper.packetevents;
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.annotations.PacketHandler;
import io.github.retrooper.packetevents.event.PacketListener;
import io.github.retrooper.packetevents.event.impl.PacketLoginEvent;
import io.github.retrooper.packetevents.event.impl.PlayerInjectEvent;
@@ -17,19 +17,15 @@ import io.github.retrooper.packetevents.utils.NMSUtils;
import org.bukkit.Bukkit;
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 io.github.retrooper.packetevents.annotations.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{
/*
@@ -42,7 +38,7 @@ public class PacketEvents implements PacketListener, Listener {
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,7 +60,6 @@ public class PacketEvents implements PacketListener, Listener {
if (!hasRegistered) {
//Register Bukkit and PacketListener
getEventManager().registerListener(getInstance());
Bukkit.getPluginManager().registerEvents(getInstance(), plugin);
//Initialize the TinyProtocolHandler
TinyProtocolHandler.initTinyProtocol(plugin);
@@ -232,22 +227,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())) {
@@ -0,0 +1,13 @@
package io.github.retrooper.packetevents.annotations;
import java.lang.annotation.*;
/**
* 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.event.PacketEvent;
import io.github.retrooper.packetevents.event.PacketHandler;
import io.github.retrooper.packetevents.annotations.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>>();
@@ -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;
}
}
@@ -1,32 +0,0 @@
package io.github.retrooper.packetevents.example;
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;
@@ -10,9 +10,9 @@ import net.minecraft.util.io.netty.channel.Channel;
import org.bukkit.entity.Player;
import org.bukkit.plugin.Plugin;
import javax.annotation.Nullable;
import io.github.retrooper.packetevents.annotations.Nullable;
public class TinyProtocolHandler_1_7 {
public final class TinyProtocolHandler_1_7 {
private final Plugin plugin;
public TinyProtocolHandler_1_7(final Plugin plugin) {
@@ -11,9 +11,9 @@ import io.netty.channel.Channel;
import org.bukkit.entity.Player;
import org.bukkit.plugin.Plugin;
import javax.annotation.Nullable;
import io.github.retrooper.packetevents.annotations.Nullable;
public class TinyProtocolHandler_1_8 {
public final class TinyProtocolHandler_1_8 {
private static final ServerVersion version = PacketEvents.getServerVersion();
private final Plugin plugin;
@@ -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) {
@@ -6,10 +6,10 @@ 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 io.github.retrooper.packetevents.annotations.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);
}
}
@@ -8,12 +8,12 @@ import org.bukkit.entity.Entity;
import org.bukkit.entity.Player;
import org.bukkit.inventory.ItemStack;
import javax.annotation.Nullable;
import io.github.retrooper.packetevents.annotations.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) {