Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
ee959b68dc | ||
|
|
b1d1201083 | ||
|
|
faff8fadc8 | ||
|
|
85a0f55b20 | ||
|
|
cf2e571d80 | ||
|
|
68596469eb | ||
|
|
181eda6ae4 | ||
|
|
31ed9c0e4c | ||
|
|
2916fb7112 | ||
|
|
1ca1a960b5 | ||
|
|
6c6a1c9465 | ||
|
|
0be7a21bab | ||
|
|
ca51988694 |
No files matched your search
@@ -21,6 +21,8 @@ Join my discord server(to talk to me): https://discord.gg/ZWTkg4v
|
||||
|
||||
[](https://jitpack.io/#retrooper/packetevents)
|
||||
|
||||
[](https://jitci.com/gh/retrooper/packetevents)
|
||||
|
||||
To learn how to setup this API please click on the link below
|
||||
[Setup Guide](https://github.com/retrooper/packetevents/wiki/PacketEvents-Setup-Guide)
|
||||
(or just paste this into your browser: https://github.com/retrooper/packetevents/wiki/PacketEvents-Setup-Guide)
|
||||
@@ -6,7 +6,7 @@
|
||||
|
||||
<groupId>io.github.retrooper</groupId>
|
||||
<artifactId>packetevents</artifactId>
|
||||
<version>1.3.7</version>
|
||||
<version>1.3.8</version>
|
||||
<packaging>jar</packaging>
|
||||
<name>packetevents</name>
|
||||
<properties>
|
||||
@@ -60,6 +60,10 @@
|
||||
<id>viaversion-repo</id>
|
||||
<url>https://repo.viaversion.com</url>
|
||||
</repository>
|
||||
<repository>
|
||||
<id>dmulloy2-repo</id>
|
||||
<url>https://repo.dmulloy2.net/nexus/repository/public/</url>
|
||||
</repository>
|
||||
</repositories>
|
||||
|
||||
|
||||
@@ -76,6 +80,15 @@
|
||||
<version>1.8.8</version>
|
||||
<scope>provided</scope>
|
||||
</dependency>
|
||||
|
||||
<!-- ProtocolLib API-->
|
||||
<dependency>
|
||||
<groupId>com.comphenix.protocol</groupId>
|
||||
<artifactId>ProtocolLib</artifactId>
|
||||
<version>4.5.0</version>
|
||||
<scope>provided</scope>
|
||||
</dependency>
|
||||
<!-- ViaVersion API-->
|
||||
<dependency>
|
||||
<groupId>us.myles</groupId>
|
||||
<artifactId>viaversion</artifactId>
|
||||
@@ -83,6 +96,7 @@
|
||||
<scope>provided</scope>
|
||||
</dependency>
|
||||
|
||||
<!-- ProtocolSupport API-->
|
||||
<dependency>
|
||||
<groupId>com.github.ProtocolSupport</groupId>
|
||||
<artifactId>ProtocolSupport</artifactId>
|
||||
|
||||
@@ -1,47 +1,25 @@
|
||||
package io.github.retrooper.packetevents;
|
||||
|
||||
import io.github.retrooper.packetevents.annotations.NotNull;
|
||||
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.PacketListener;
|
||||
import io.github.retrooper.packetevents.event.impl.BukkitMoveEvent;
|
||||
import io.github.retrooper.packetevents.event.impl.PacketReceiveEvent;
|
||||
import io.github.retrooper.packetevents.event.impl.PlayerInjectEvent;
|
||||
import io.github.retrooper.packetevents.event.impl.ServerTickEvent;
|
||||
import io.github.retrooper.packetevents.event.manager.EventManager;
|
||||
import io.github.retrooper.packetevents.handler.NettyPacketHandler;
|
||||
import io.github.retrooper.packetevents.packetwrappers.Sendable;
|
||||
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.PlayerMoveEvent;
|
||||
import org.bukkit.event.player.PlayerQuitEvent;
|
||||
import org.bukkit.plugin.Plugin;
|
||||
import org.bukkit.scheduler.BukkitTask;
|
||||
|
||||
import java.lang.reflect.InvocationTargetException;
|
||||
import java.util.HashMap;
|
||||
import java.util.UUID;
|
||||
|
||||
public final class PacketEvents implements PacketListener, Listener {
|
||||
|
||||
private static final ServerVersion version = ServerVersion.getVersion();
|
||||
private static final EventManager eventManager = new EventManager();
|
||||
private static final HashMap<UUID, ClientVersion> clientVersionsMap = new HashMap<UUID, ClientVersion>();
|
||||
private static final PacketEventsAPI packetEventsAPI = new PacketEventsAPI();
|
||||
private static final PacketEvents instance = new PacketEvents();
|
||||
private static boolean hasRegistered;
|
||||
private static PacketEvents instance;
|
||||
private static int currentTick;
|
||||
private static BukkitTask serverTickTask;
|
||||
|
||||
public static EventManager getEventManager() {
|
||||
return eventManager;
|
||||
}
|
||||
|
||||
private static Plugin plugin;
|
||||
|
||||
/**
|
||||
* Starts the server tick task and initiates the TinyProtocolHandler
|
||||
@@ -51,7 +29,7 @@ public final class PacketEvents implements PacketListener, Listener {
|
||||
public static void start(final Plugin plugin) {
|
||||
if (!hasRegistered) {
|
||||
//Register Bukkit and PacketListener
|
||||
getEventManager().registerListener(getInstance());
|
||||
getAPI().getEventManager().registerListener(getInstance());
|
||||
|
||||
Bukkit.getPluginManager().registerEvents(getInstance(), plugin);
|
||||
|
||||
@@ -59,11 +37,12 @@ public final class PacketEvents implements PacketListener, Listener {
|
||||
final Runnable tickRunnable = new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
getEventManager().callEvent(new ServerTickEvent(currentTick++, PacketEvents.highlyPreciseMillis()));
|
||||
getAPI().getEventManager().callEvent(new ServerTickEvent(currentTick++, PacketEvents.getAPI().currentPreciseMillis()));
|
||||
}
|
||||
};
|
||||
serverTickTask = Bukkit.getScheduler().runTaskTimerAsynchronously(plugin, tickRunnable, 0L, 1L);
|
||||
getAPI().setServerTickTask(Bukkit.getScheduler().runTaskTimerAsynchronously(plugin, tickRunnable, 0L, 1L));
|
||||
hasRegistered = true;
|
||||
PacketEvents.plugin = plugin;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -71,141 +50,28 @@ public final class PacketEvents implements PacketListener, Listener {
|
||||
* Stop all tasks and unregisters all PacketEvents' listeners
|
||||
*/
|
||||
public static void stop() {
|
||||
if (serverTickTask != null) {
|
||||
serverTickTask.cancel();
|
||||
if (getAPI().getServerTickTask() != null) {
|
||||
getAPI().getServerTickTask().cancel();
|
||||
}
|
||||
getEventManager().unregisterAllListeners();
|
||||
getAPI().getEventManager().unregisterAllListeners();
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the server's version
|
||||
*
|
||||
* @return version
|
||||
*/
|
||||
public static ServerVersion getServerVersion() {
|
||||
return version;
|
||||
public static PacketEventsAPI getAPI() {
|
||||
return packetEventsAPI;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the server tick task, you may cancel it if you wish.
|
||||
* To cancel it do PacketEvents.getServerTickTask().cancel();
|
||||
* <p>
|
||||
* This bukkit task runs asynchronously every tick and is started in the start(plugin) function
|
||||
*
|
||||
* @return serverTickTask
|
||||
*/
|
||||
public static BukkitTask getServerTickTask() {
|
||||
return serverTickTask;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get an instance of the PacketEvents API class
|
||||
*
|
||||
* @return instance
|
||||
*/
|
||||
public static PacketEvents getInstance() {
|
||||
return instance == null ? instance = new PacketEvents() : instance;
|
||||
private static PacketEvents getInstance() {
|
||||
return instance;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Get the server's recent TPS values
|
||||
* TPS stands for ticks per second.
|
||||
* See to learn more about TPS<a href="https://apexminecrafthosting.com/what-is-minecraft-tps/">"https://apexminecrafthosting.com/what-is-minecraft-tps/"</a>
|
||||
*
|
||||
* @return recentTPS[]
|
||||
*/
|
||||
public static double[] getRecentServerTPS() {
|
||||
double[] tpsArray = new double[0];
|
||||
try {
|
||||
tpsArray = NMSUtils.recentTPS();
|
||||
} catch (IllegalAccessException | InvocationTargetException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return tpsArray;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the server's current TPS
|
||||
* TPS stands for ticks per second.
|
||||
* See to learn more about TPS <a href="https://apexminecrafthosting.com/what-is-minecraft-tps/">"https://apexminecrafthosting.com/what-is-minecraft-tps/"</a>
|
||||
*
|
||||
* @return {@link PacketEvents#getRecentServerTPS()[0]}
|
||||
*/
|
||||
public static double getCurrentServerTPS() {
|
||||
return getRecentServerTPS()[0];
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the player's ping.
|
||||
* Learn more about ping <a href="https://en.wikipedia.org/wiki/Ping_(networking_utility)">https://en.wikipedia.org/wiki/Ping_(networking_utility)</a>
|
||||
*
|
||||
* @param player
|
||||
* @return ping
|
||||
*/
|
||||
public static int getPing(@NotNull final Player player) {
|
||||
return NMSUtils.getPlayerPing(player);
|
||||
}
|
||||
|
||||
/**
|
||||
* When to use this(nano / 1 million) vs {@link System#currentTimeMillis()}
|
||||
* This is preciser than System.currentTimeMillis(), System.currentTimeMillis() can be up to 50ms off on some operating systems, but using this isn't cheap.
|
||||
* Java documentation recommend using nano time if you are measuring elapsed time.
|
||||
* In this function nanoTime is divided by 1 million giving us milliseconds.
|
||||
* It is also important to mention that using this method doesn't guarantee thread safety.
|
||||
*
|
||||
* @return nanoTime / 1 million
|
||||
*/
|
||||
public static long highlyPreciseMillis() {
|
||||
return System.nanoTime() / 1000000;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Get the player's version.
|
||||
* Do not call this method in the PlayerInjectEvent, it is safe to call it in the PostPlayerInjectEvent.
|
||||
* The EntityPlayer object is null at that time, resulting in the version lookup to fail.
|
||||
*
|
||||
* @param player
|
||||
* @return ClientVersion
|
||||
*/
|
||||
|
||||
@Nullable
|
||||
public static ClientVersion getClientVersion(@NotNull final Player player) {
|
||||
return getClientVersion(player.getUniqueId());
|
||||
}
|
||||
|
||||
public static ClientVersion getClientVersion(@NotNull final UUID uuid) {
|
||||
return clientVersionsMap.get(uuid);
|
||||
}
|
||||
|
||||
/**
|
||||
* Version independent player injection
|
||||
*
|
||||
* @param player
|
||||
*/
|
||||
public static void injectPlayer(final Player player) {
|
||||
NettyPacketHandler.injectPlayer(player);
|
||||
}
|
||||
|
||||
/**
|
||||
* Version independant player injection
|
||||
*
|
||||
* @param player
|
||||
*/
|
||||
public static void uninjectPlayer(final Player player) {
|
||||
NettyPacketHandler.uninjectPlayer(player);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Send a wrapped sendable packet to a player
|
||||
*
|
||||
* @param player
|
||||
* @param sendable
|
||||
*/
|
||||
public static void sendPacket(final Player player, final Sendable sendable) {
|
||||
NMSUtils.sendSendableWrapper(player, sendable);
|
||||
public static Plugin getPlugin() {
|
||||
return plugin;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -217,7 +83,7 @@ public final class PacketEvents implements PacketListener, Listener {
|
||||
*/
|
||||
@PacketHandler
|
||||
public void onInject(final PlayerInjectEvent e) {
|
||||
clientVersionsMap.put(e.getPlayer().getUniqueId(), e.getClientVersion());
|
||||
getAPI().getPlayerUtilities().clientVersionsMap.put(e.getPlayer().getUniqueId(), e.getClientVersion());
|
||||
}
|
||||
|
||||
@PacketHandler
|
||||
@@ -226,16 +92,16 @@ public final class PacketEvents implements PacketListener, Listener {
|
||||
|
||||
@EventHandler
|
||||
public void onJoin(final PlayerJoinEvent e) {
|
||||
injectPlayer(e.getPlayer());
|
||||
getAPI().getPlayerUtilities().injectPlayer(e.getPlayer());
|
||||
}
|
||||
|
||||
@EventHandler
|
||||
public void onQuit(final PlayerQuitEvent e) {
|
||||
uninjectPlayer(e.getPlayer());
|
||||
getAPI().getPlayerUtilities().uninjectPlayer(e.getPlayer());
|
||||
}
|
||||
|
||||
@EventHandler
|
||||
public void onMove(final PlayerMoveEvent e) {
|
||||
PacketEvents.getEventManager().callEvent(new BukkitMoveEvent(e));
|
||||
getAPI().getEventManager().callEvent(new BukkitMoveEvent(e));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,78 @@
|
||||
package io.github.retrooper.packetevents;
|
||||
|
||||
import io.github.retrooper.packetevents.event.manager.EventManager;
|
||||
import org.bukkit.scheduler.BukkitTask;
|
||||
|
||||
public final class PacketEventsAPI {
|
||||
private final EventManager eventManager = new EventManager();
|
||||
private final PlayerUtils playerUtils = new PlayerUtils();
|
||||
private final ServerUtils serverUtils = new ServerUtils();
|
||||
|
||||
private BukkitTask serverTickTask;
|
||||
|
||||
/**
|
||||
* Not thread safe
|
||||
* Preciser than #currentMillis()
|
||||
* Expensive
|
||||
* Returns crazy values when you change system time
|
||||
* @return {@link System#nanoTime()} / 1000000(1 million)
|
||||
*/
|
||||
public long currentPreciseMillis() {
|
||||
return System.nanoTime() / 1000000;
|
||||
}
|
||||
|
||||
/**
|
||||
* Thread safe
|
||||
* Less precise compared to #currentPreciseMillis()
|
||||
* Cheap
|
||||
* Doesn't break when system time is changed
|
||||
*
|
||||
* @return {@link System#currentTimeMillis()}
|
||||
*/
|
||||
public long currentMillis() {
|
||||
return System.currentTimeMillis();
|
||||
}
|
||||
|
||||
/**
|
||||
* Get all utilities to do with the player
|
||||
* @return Player Utilities
|
||||
*/
|
||||
public PlayerUtils getPlayerUtilities() {
|
||||
return playerUtils;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get all utilities to do with the server
|
||||
* @return Server Utilities
|
||||
*/
|
||||
public ServerUtils getServerUtilities() {
|
||||
return serverUtils;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the event manager
|
||||
* Used to call events, register and unregister listeners
|
||||
* @return PacketEvents' event manager
|
||||
*/
|
||||
public EventManager getEventManager() {
|
||||
return eventManager;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the server tick task, called **asynchronously** every tick
|
||||
* @return server tick task
|
||||
*/
|
||||
public BukkitTask getServerTickTask() {
|
||||
return serverTickTask;
|
||||
}
|
||||
|
||||
/**
|
||||
* Modify the server tick task object
|
||||
* @param task
|
||||
*/
|
||||
public void setServerTickTask(final BukkitTask task) {
|
||||
this.serverTickTask = task;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@@ -0,0 +1,40 @@
|
||||
package io.github.retrooper.packetevents;
|
||||
|
||||
import io.github.retrooper.packetevents.enums.ClientVersion;
|
||||
import io.github.retrooper.packetevents.handler.NettyPacketHandler;
|
||||
import io.github.retrooper.packetevents.packetwrappers.Sendable;
|
||||
import io.github.retrooper.packetevents.utils.NMSUtils;
|
||||
import org.bukkit.entity.Player;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.UUID;
|
||||
|
||||
public class PlayerUtils {
|
||||
protected final HashMap<UUID, ClientVersion> clientVersionsMap = new HashMap<UUID, ClientVersion>();
|
||||
|
||||
public int getPlayerPing(final Player player) {
|
||||
return NMSUtils.getPlayerPing(player);
|
||||
}
|
||||
|
||||
public ClientVersion getClientVersion(final UUID uuid) {
|
||||
return clientVersionsMap.get(uuid);
|
||||
}
|
||||
|
||||
public ClientVersion getClientVersion(final Player player) {
|
||||
return getClientVersion(player.getUniqueId());
|
||||
}
|
||||
|
||||
public void injectPlayer(final Player player) {
|
||||
NettyPacketHandler.injectPlayer(player);
|
||||
}
|
||||
|
||||
public void uninjectPlayer(final Player player) {
|
||||
NettyPacketHandler.uninjectPlayer(player);
|
||||
}
|
||||
|
||||
public void sendPacket(final Player player, final Sendable sendable) {
|
||||
NMSUtils.sendSendableWrapper(player, sendable);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@@ -0,0 +1,31 @@
|
||||
package io.github.retrooper.packetevents;
|
||||
|
||||
import io.github.retrooper.packetevents.enums.ServerVersion;
|
||||
import io.github.retrooper.packetevents.enums.SystemOS;
|
||||
import io.github.retrooper.packetevents.utils.NMSUtils;
|
||||
|
||||
import java.lang.reflect.InvocationTargetException;
|
||||
|
||||
public class ServerUtils {
|
||||
public ServerVersion getServerVersion() {
|
||||
return ServerVersion.getVersion();
|
||||
}
|
||||
|
||||
public double[] getRecentServerTPS() {
|
||||
double[] tpsArray = new double[0];
|
||||
try {
|
||||
tpsArray = NMSUtils.recentTPS();
|
||||
} catch (IllegalAccessException | InvocationTargetException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return tpsArray;
|
||||
}
|
||||
|
||||
public double getCurrentServerTPS() {
|
||||
return getRecentServerTPS()[0];
|
||||
}
|
||||
|
||||
public SystemOS getPlatform(){
|
||||
return SystemOS.getOperatingSystem();
|
||||
}
|
||||
}
|
||||
@@ -1,5 +1,6 @@
|
||||
package io.github.retrooper.packetevents.annotations;
|
||||
|
||||
import io.github.retrooper.packetevents.annotations.data.EventSynchronization;
|
||||
import java.lang.annotation.ElementType;
|
||||
import java.lang.annotation.Retention;
|
||||
import java.lang.annotation.RetentionPolicy;
|
||||
@@ -8,10 +9,9 @@ import java.lang.annotation.Target;
|
||||
@Target(ElementType.METHOD)
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
/**
|
||||
* Just like Bukkit's @EventHandler, but for packets.
|
||||
* Make sure your class implements the PacketListener interface, and is registered!
|
||||
* Just like Bukkit's @EventHandler, but for PacketEvents' custom event system.
|
||||
* Make sure your class implements the PacketListener interface, and is registered when annotating your method with this annotation!
|
||||
*/
|
||||
public @interface PacketHandler {
|
||||
|
||||
|
||||
EventSynchronization synchronization() default EventSynchronization.NORMAL;
|
||||
}
|
||||
+5
@@ -0,0 +1,5 @@
|
||||
package io.github.retrooper.packetevents.annotations.data;
|
||||
|
||||
public enum EventSynchronization {
|
||||
FORCE_SYNC, FORCE_ASYNC, NORMAL
|
||||
}
|
||||
@@ -11,26 +11,29 @@ public enum ServerVersion {
|
||||
|
||||
private static final ServerVersion[] reversedValues;
|
||||
private static final String nmsVersionSuffix = Bukkit.getServer().getClass().getPackage().getName().replace(".", ",").split(",")[3];
|
||||
private static ServerVersion cachedVersion = ServerVersion.EMPTY;
|
||||
private static ServerVersion cachedVersion;
|
||||
|
||||
static {
|
||||
reversedValues = reverse(values());
|
||||
}
|
||||
|
||||
public static ServerVersion getVersion() {
|
||||
if (cachedVersion != ServerVersion.EMPTY) {
|
||||
return cachedVersion;
|
||||
}
|
||||
private static ServerVersion getVers() {
|
||||
for (final ServerVersion val : reversedValues) {
|
||||
String valName = val.name().substring(2).replace("_", ".");
|
||||
if (Bukkit.getBukkitVersion().contains(valName)) {
|
||||
cachedVersion = val;
|
||||
return val;
|
||||
}
|
||||
}
|
||||
return ERROR;
|
||||
}
|
||||
|
||||
public static ServerVersion getVersion() {
|
||||
if(cachedVersion == null) {
|
||||
cachedVersion = getVers();
|
||||
}
|
||||
return cachedVersion;
|
||||
}
|
||||
|
||||
private static ServerVersion[] reverse(final ServerVersion[] arr) {
|
||||
ServerVersion[] array = arr.clone();
|
||||
if (array == null) {
|
||||
|
||||
@@ -0,0 +1,32 @@
|
||||
package io.github.retrooper.packetevents.enums;
|
||||
|
||||
public enum SystemOS {
|
||||
WINDOWS, MACOS, LINUX, OTHER;
|
||||
|
||||
private static SystemOS value;
|
||||
|
||||
private static SystemOS getOS() {
|
||||
final String os = System.getProperty("os.name");
|
||||
for (final String osName : getOperatingSystemNames()) {
|
||||
if(os.toLowerCase().contains(osName.toLowerCase())) {
|
||||
return SystemOS.valueOf(osName);
|
||||
}
|
||||
}
|
||||
return OTHER;
|
||||
}
|
||||
|
||||
public static SystemOS getOperatingSystem() {
|
||||
if(value == null) {
|
||||
value = getOS();
|
||||
}
|
||||
return value;
|
||||
}
|
||||
|
||||
public static String[] getOperatingSystemNames() {
|
||||
final String[] arr = new String[values().length - 1];
|
||||
for (int i = 0; i < values().length - 1; i++) {
|
||||
arr[i] = values()[i].name();
|
||||
}
|
||||
return arr;
|
||||
}
|
||||
}
|
||||
@@ -22,4 +22,38 @@ public enum PlayerAction {
|
||||
public static PlayerAction get(final int i) {
|
||||
return values()[i];
|
||||
}
|
||||
|
||||
public int getIndex() {
|
||||
for (int i = 0; i < values().length; i++) {
|
||||
final PlayerAction action = values()[i];
|
||||
if (action == this) {
|
||||
return i;
|
||||
}
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
public enum UpdatedPlayerAction {
|
||||
PRESS_SHIFT_KEY, RELEASE_SHIFT_KEY, STOP_SLEEPING, START_SPRINTING, STOP_SPRINTING, START_RIDING_JUMP, STOP_RIDING_JUMP, OPEN_INVENTORY, START_FALL_FLYING;
|
||||
|
||||
public static UpdatedPlayerAction get(final int index) {
|
||||
for (int i = 0; i < index + 1; i++) {
|
||||
final UpdatedPlayerAction action = values()[i];
|
||||
if (i == index) {
|
||||
return action;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public int getIndex() {
|
||||
for (int i = 0; i < values().length; i++) {
|
||||
final UpdatedPlayerAction action = values()[i];
|
||||
if (action == this) {
|
||||
return i;
|
||||
}
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -9,8 +9,8 @@ import io.github.retrooper.packetevents.enums.ServerVersion;
|
||||
* recommended to avoid non thread safe bukkit operations
|
||||
*/
|
||||
public class PacketEvent {
|
||||
protected static ServerVersion version = PacketEvents.getServerVersion();
|
||||
protected long timestamp = PacketEvents.highlyPreciseMillis();
|
||||
protected static ServerVersion version = PacketEvents.getAPI().getServerUtilities().getServerVersion();
|
||||
protected long timestamp = PacketEvents.getAPI().currentPreciseMillis();
|
||||
|
||||
/**
|
||||
* Timestamp of the packet
|
||||
|
||||
@@ -34,7 +34,7 @@ public final class PacketReceiveEvent extends PacketEvent {
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the raw NMS packet object
|
||||
* Get the NMS packet object
|
||||
*
|
||||
* @return packet object
|
||||
*/
|
||||
@@ -42,6 +42,14 @@ public final class PacketReceiveEvent extends PacketEvent {
|
||||
return this.packet;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the class of the NMS packet object
|
||||
* @return packet object class
|
||||
*/
|
||||
public Class<?> getNMSPacketClass() {
|
||||
return getNMSPacket().getClass();
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns if the packet has been cancelled
|
||||
*
|
||||
|
||||
@@ -28,7 +28,7 @@ public final class PacketSendEvent extends PacketEvent {
|
||||
|
||||
|
||||
/**
|
||||
* Get the raw NMS packet object
|
||||
* Get the NMS packet object
|
||||
*
|
||||
* @return packet object
|
||||
*/
|
||||
@@ -36,6 +36,14 @@ public final class PacketSendEvent extends PacketEvent {
|
||||
return this.packet;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the class of the NMS packet object
|
||||
* @return packet object class
|
||||
*/
|
||||
public Class<?> getNMSPacketClass() {
|
||||
return getNMSPacket().getClass();
|
||||
}
|
||||
|
||||
public boolean isCancelled() {
|
||||
return cancelled;
|
||||
}
|
||||
|
||||
@@ -2,7 +2,7 @@ package io.github.retrooper.packetevents.event.impl;
|
||||
|
||||
import io.github.retrooper.packetevents.enums.ClientVersion;
|
||||
import io.github.retrooper.packetevents.event.PacketEvent;
|
||||
import io.github.retrooper.packetevents.utils.VersionLookupUtils;
|
||||
import io.github.retrooper.packetevents.utils.api.VersionLookupUtils;
|
||||
import org.bukkit.entity.Player;
|
||||
|
||||
public final class PlayerInjectEvent extends PacketEvent {
|
||||
|
||||
@@ -10,12 +10,12 @@ public final class ServerTickEvent extends PacketEvent {
|
||||
public ServerTickEvent(final int tick, final long timestamp) {
|
||||
this.tick = tick;
|
||||
this.timestamp = timestamp;
|
||||
this.tps = PacketEvents.getCurrentServerTPS();
|
||||
this.tps = PacketEvents.getAPI().getServerUtilities().getCurrentServerTPS();
|
||||
}
|
||||
|
||||
public ServerTickEvent(final int tick) {
|
||||
this.tick = tick;
|
||||
this.tps = PacketEvents.getCurrentServerTPS();
|
||||
this.tps = PacketEvents.getAPI().getServerUtilities().getCurrentServerTPS();
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -1,20 +1,33 @@
|
||||
package io.github.retrooper.packetevents.event.manager;
|
||||
|
||||
import io.github.retrooper.packetevents.PacketEvents;
|
||||
import io.github.retrooper.packetevents.annotations.PacketHandler;
|
||||
import io.github.retrooper.packetevents.annotations.data.EventSynchronization;
|
||||
import io.github.retrooper.packetevents.event.PacketEvent;
|
||||
import io.github.retrooper.packetevents.event.PacketListener;
|
||||
import org.bukkit.Bukkit;
|
||||
|
||||
import java.lang.reflect.InvocationTargetException;
|
||||
import java.lang.reflect.Method;
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.concurrent.ExecutorService;
|
||||
import java.util.concurrent.Executors;
|
||||
|
||||
public final class EventManager {
|
||||
|
||||
private final ExecutorService executorService;
|
||||
private final HashMap<PacketListener, List<Method>> registeredMethods = new HashMap<PacketListener, List<Method>>();
|
||||
|
||||
public EventManager(final int threadCount) {
|
||||
executorService = Executors.newFixedThreadPool(threadCount);
|
||||
}
|
||||
public EventManager() {
|
||||
this(Runtime.getRuntime().availableProcessors());
|
||||
}
|
||||
|
||||
public void callEvent(final PacketEvent e) {
|
||||
Integer i = 0;
|
||||
for (final PacketListener listener : registeredMethods.keySet()) {
|
||||
//Annotated methods
|
||||
final List<Method> methods = registeredMethods.get(listener);
|
||||
@@ -22,10 +35,23 @@ public final class EventManager {
|
||||
final Class<?> parameterType = method.getParameterTypes()[0];
|
||||
if (parameterType.equals(PacketEvent.class)
|
||||
|| parameterType.isInstance(e)) {
|
||||
try {
|
||||
method.invoke(listener, e);
|
||||
} catch (IllegalAccessException | InvocationTargetException ex) {
|
||||
ex.printStackTrace();
|
||||
final PacketHandler annotation = method.getAnnotation(PacketHandler.class);
|
||||
final Runnable invokeMethod = new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
try {
|
||||
method.invoke(listener, e);
|
||||
} catch (IllegalAccessException | InvocationTargetException ex) {
|
||||
ex.printStackTrace();
|
||||
}
|
||||
}
|
||||
};
|
||||
if (annotation.synchronization() == EventSynchronization.FORCE_ASYNC) {
|
||||
executorService.execute(invokeMethod);
|
||||
} else if (annotation.synchronization() == EventSynchronization.FORCE_SYNC) {
|
||||
Bukkit.getScheduler().runTask(PacketEvents.getPlugin(), invokeMethod);
|
||||
} else {
|
||||
invokeMethod.run();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -3,14 +3,16 @@ package io.github.retrooper.packetevents.example;
|
||||
import io.github.retrooper.packetevents.PacketEvents;
|
||||
import org.bukkit.plugin.java.JavaPlugin;
|
||||
|
||||
import static io.github.retrooper.packetevents.PacketEvents.getAPI;
|
||||
|
||||
public class MainExample extends JavaPlugin {
|
||||
|
||||
@Override
|
||||
public void onEnable() {
|
||||
PacketEvents.start(this);
|
||||
|
||||
PacketEvents.getServerTickTask().cancel();
|
||||
|
||||
getAPI().getServerTickTask().cancel();
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -12,11 +12,11 @@ import java.util.concurrent.Future;
|
||||
|
||||
public class NettyPacketHandler {
|
||||
public static final String handlerName = "packet_handler";
|
||||
private static final ServerVersion version = PacketEvents.getServerVersion();
|
||||
private static final ServerVersion version = PacketEvents.getAPI().getServerUtilities().getServerVersion();
|
||||
|
||||
public static void injectPlayer(final Player player) {
|
||||
final PlayerInjectEvent injectEvent = new PlayerInjectEvent(player);
|
||||
PacketEvents.getEventManager().callEvent(injectEvent);
|
||||
PacketEvents.getAPI().getEventManager().callEvent(injectEvent);
|
||||
if (!injectEvent.isCancelled()) {
|
||||
if (version.isLowerThan(ServerVersion.v_1_8)) {
|
||||
NettyPacketHandler_7.injectPlayer(player);
|
||||
@@ -28,7 +28,7 @@ public class NettyPacketHandler {
|
||||
|
||||
public static Future<?> uninjectPlayer(final Player player) {
|
||||
final PlayerUninjectEvent uninjectEvent = new PlayerUninjectEvent(player);
|
||||
PacketEvents.getEventManager().callEvent(uninjectEvent);
|
||||
PacketEvents.getAPI().getEventManager().callEvent(uninjectEvent);
|
||||
if (!uninjectEvent.isCancelled()) {
|
||||
if (version.isLowerThan(ServerVersion.v_1_8)) {
|
||||
return NettyPacketHandler_7.uninjectPlayer(player);
|
||||
@@ -42,7 +42,7 @@ public class NettyPacketHandler {
|
||||
public static Object write(final Player sender, final Object packet) {
|
||||
final String packetName = packet.getClass().getSimpleName();
|
||||
final PacketSendEvent packetSendEvent = new PacketSendEvent(sender, packetName, packet);
|
||||
PacketEvents.getEventManager().callEvent(packetSendEvent);
|
||||
PacketEvents.getAPI().getEventManager().callEvent(packetSendEvent);
|
||||
if (!packetSendEvent.isCancelled()) {
|
||||
return packet;
|
||||
}
|
||||
@@ -52,7 +52,7 @@ public class NettyPacketHandler {
|
||||
public static Object read(final Player receiver, final Object packet) {
|
||||
final String packetName = packet.getClass().getSimpleName();
|
||||
final PacketReceiveEvent packetReceiveEvent = new PacketReceiveEvent(receiver, packetName, packet);
|
||||
PacketEvents.getEventManager().callEvent(packetReceiveEvent);
|
||||
PacketEvents.getAPI().getEventManager().callEvent(packetReceiveEvent);
|
||||
if (!packetReceiveEvent.isCancelled()) {
|
||||
return packet;
|
||||
}
|
||||
|
||||
@@ -9,7 +9,7 @@ import org.bukkit.entity.Player;
|
||||
import java.util.concurrent.Future;
|
||||
|
||||
final class NettyPacketHandler_8 {
|
||||
private static final ServerVersion version = PacketEvents.getServerVersion();
|
||||
private static final ServerVersion version = PacketEvents.getAPI().getServerUtilities().getServerVersion();
|
||||
|
||||
public static void injectPlayer(final Player player) {
|
||||
final ChannelDuplexHandler channelDuplexHandler = new ChannelDuplexHandler() {
|
||||
|
||||
@@ -6,7 +6,7 @@ import io.github.retrooper.packetevents.reflectionutils.Reflection;
|
||||
import io.github.retrooper.packetevents.utils.NMSUtils;
|
||||
|
||||
public final class PacketType {
|
||||
private static final ServerVersion version = PacketEvents.getServerVersion();
|
||||
private static final ServerVersion version = PacketEvents.getAPI().getServerUtilities().getServerVersion();
|
||||
|
||||
|
||||
public static final class Util {
|
||||
|
||||
@@ -0,0 +1,48 @@
|
||||
package io.github.retrooper.packetevents.packet;
|
||||
|
||||
import io.github.retrooper.packetevents.annotations.Nullable;
|
||||
import io.github.retrooper.packetevents.utils.NMSUtils;
|
||||
|
||||
public class PacketTypeClasses {
|
||||
public static class Client {
|
||||
private static String c = "PacketPlayIn";
|
||||
|
||||
@Nullable
|
||||
public static final Class<?> FLYING = NMSUtils.getNMSClassWithoutException(c + "Flying");
|
||||
|
||||
public static Class<?> POSITION, POSITION_LOOK, LOOK, CLIENT_COMMAND,
|
||||
TRANSACTION, BLOCK_DIG, ENTITY_ACTION, USE_ENTITY,
|
||||
WINDOW_CLICK, STEER_VEHICLE, CUSTOM_PAYLOAD, ARM_ANIMATION,
|
||||
BLOCK_PLACE, ABILITIES, HELD_ITEM_SLOT, CLOSE_WINDOW,
|
||||
TAB_COMPLETE, CHAT, SET_CREATIVE_SLOT, KEEP_ALIVE;
|
||||
|
||||
static {
|
||||
try {
|
||||
POSITION = NMSUtils.getNMSClass(c + "Position");
|
||||
POSITION_LOOK = NMSUtils.getNMSClass(c + "PositionLook");
|
||||
LOOK = NMSUtils.getNMSClass(c + "Look");
|
||||
|
||||
CLIENT_COMMAND = NMSUtils.getNMSClass(c + "ClientCommand");
|
||||
TRANSACTION = NMSUtils.getNMSClass(c + "Transaction");
|
||||
BLOCK_DIG = NMSUtils.getNMSClass(c + "BlockDig");
|
||||
ENTITY_ACTION = NMSUtils.getNMSClass(c + "EntityAction");
|
||||
USE_ENTITY = NMSUtils.getNMSClass(c + "UseEntity");
|
||||
WINDOW_CLICK = NMSUtils.getNMSClass(c + "WindowClick");
|
||||
STEER_VEHICLE = NMSUtils.getNMSClass(c + "SteerVehicle");
|
||||
CUSTOM_PAYLOAD = NMSUtils.getNMSClass(c + "CustomPayload");
|
||||
ARM_ANIMATION = NMSUtils.getNMSClass(c + "ArmAnimation");
|
||||
ABILITIES = NMSUtils.getNMSClass(c + "Abilities");
|
||||
HELD_ITEM_SLOT = NMSUtils.getNMSClass(c + "HeldItemSlot");
|
||||
CLOSE_WINDOW = NMSUtils.getNMSClass(c + "CloseWindow");
|
||||
TAB_COMPLETE = NMSUtils.getNMSClass(c + "TabComplete");
|
||||
CHAT = NMSUtils.getNMSClass(c + "Chat");
|
||||
SET_CREATIVE_SLOT = NMSUtils.getNMSClass(c + "SetCreativeSlot");
|
||||
KEEP_ALIVE = NMSUtils.getNMSClass(c + "KeepAlive");
|
||||
} catch (ClassNotFoundException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
//Block place
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -3,6 +3,10 @@ package io.github.retrooper.packetevents.packetwrappers.api;
|
||||
import io.github.retrooper.packetevents.enums.ServerVersion;
|
||||
import org.bukkit.entity.Player;
|
||||
|
||||
import java.lang.reflect.Field;
|
||||
import java.lang.reflect.InvocationTargetException;
|
||||
import java.lang.reflect.Method;
|
||||
|
||||
public class WrappedPacket {
|
||||
protected static ServerVersion version = ServerVersion.getVersion();
|
||||
protected final Player player;
|
||||
|
||||
+1
-6
@@ -7,10 +7,6 @@ import io.github.retrooper.packetevents.packetwrappers.api.WrappedPacket;
|
||||
import io.github.retrooper.packetevents.reflectionutils.Reflection;
|
||||
import io.github.retrooper.packetevents.utils.NMSUtils;
|
||||
import io.github.retrooper.packetevents.utils.vector.Vector3i;
|
||||
import net.minecraft.server.v1_8_R3.PacketPlayInBlockDig;
|
||||
import org.bukkit.block.Block;
|
||||
|
||||
import java.lang.reflect.Field;
|
||||
|
||||
public final class WrappedPacketInBlockDig extends WrappedPacket {
|
||||
private static Class<?> blockDigClass, blockPositionClass, enumDirectionClass, digTypeClass;
|
||||
@@ -29,8 +25,7 @@ public final class WrappedPacketInBlockDig extends WrappedPacket {
|
||||
|
||||
try {
|
||||
digTypeClass = NMSUtils.getNMSClass("EnumPlayerDigType");
|
||||
}
|
||||
catch(ClassNotFoundException e) {
|
||||
} catch (ClassNotFoundException e) {
|
||||
//It is probably a subclass
|
||||
digTypeClass = Reflection.getSubClass(blockDigClass, "EnumPlayerDigType");
|
||||
}
|
||||
|
||||
+1
-7
@@ -2,16 +2,10 @@ package io.github.retrooper.packetevents.packetwrappers.in.blockplace;
|
||||
|
||||
|
||||
import io.github.retrooper.packetevents.packetwrappers.api.WrappedPacket;
|
||||
import io.github.retrooper.packetevents.reflectionutils.Reflection;
|
||||
import io.github.retrooper.packetevents.utils.BlockIteratorUtils;
|
||||
import org.bukkit.Material;
|
||||
import org.bukkit.block.Block;
|
||||
import org.bukkit.entity.Player;
|
||||
|
||||
import java.lang.reflect.InvocationTargetException;
|
||||
import java.lang.reflect.Method;
|
||||
import java.util.HashSet;
|
||||
|
||||
final class WrappedPacketInBlockPlace_1_9 extends WrappedPacket {
|
||||
private Block block;
|
||||
|
||||
@@ -22,7 +16,7 @@ final class WrappedPacketInBlockPlace_1_9 extends WrappedPacket {
|
||||
|
||||
@Override
|
||||
protected void setup() {
|
||||
this.block = BlockIteratorUtils.getBlockLookingAt(getPlayer(), 3);
|
||||
this.block = BlockIteratorUtils.getBlockLookingAt(getPlayer(), 3);
|
||||
}
|
||||
|
||||
public Player getPlayer() {
|
||||
|
||||
+19
-15
@@ -12,8 +12,6 @@ public final class WrappedPacketInEntityAction extends WrappedPacket {
|
||||
private static Class<?> entityActionClass;
|
||||
@Nullable
|
||||
private static Class<?> enumPlayerActionClass;
|
||||
@Nullable
|
||||
private static Object[] enumPlayerActionConstants;
|
||||
|
||||
static {
|
||||
try {
|
||||
@@ -25,6 +23,8 @@ public final class WrappedPacketInEntityAction extends WrappedPacket {
|
||||
break;
|
||||
}
|
||||
}
|
||||
} else {
|
||||
enumPlayerActionClass = NMSUtils.getNMSClass("EnumPlayerAction");
|
||||
}
|
||||
} catch (ClassNotFoundException e) {
|
||||
e.printStackTrace();
|
||||
@@ -44,7 +44,12 @@ public final class WrappedPacketInEntityAction extends WrappedPacket {
|
||||
protected void setup() {
|
||||
try {
|
||||
final int entityId = Reflection.getField(entityActionClass, int.class, 0).getInt(packet);
|
||||
final int jumpBoost = Reflection.getField(entityActionClass, int.class, 2).getInt(packet);
|
||||
final int jumpBoost;
|
||||
if (version.isLowerThan(ServerVersion.v_1_8)) {
|
||||
jumpBoost = Reflection.getField(entityActionClass, int.class, 2).getInt(packet);
|
||||
} else {
|
||||
jumpBoost = Reflection.getField(entityActionClass, int.class, 1).getInt(packet);
|
||||
}
|
||||
|
||||
int animationIndex = -1;
|
||||
|
||||
@@ -52,24 +57,23 @@ public final class WrappedPacketInEntityAction extends WrappedPacket {
|
||||
if (version.isLowerThan(ServerVersion.v_1_8)) {
|
||||
animationIndex = Reflection.getField(entityActionClass, int.class, 1).getInt(packet);
|
||||
} else {
|
||||
final Object enumObj = Reflection.getField(entityActionClass, enumPlayerActionClass, 0);
|
||||
|
||||
final Object[] enumValues = enumPlayerActionConstants;
|
||||
final int len = enumValues.length;
|
||||
for (int i = 0; i < len; i++) {
|
||||
final Object val = enumValues[i];
|
||||
if (val.toString().equals(enumObj.toString())) {
|
||||
animationIndex = i;
|
||||
break;
|
||||
}
|
||||
final Object enumObj = Reflection.getField(entityActionClass, enumPlayerActionClass, 0).get(packet);
|
||||
final String enumValue = enumObj.toString();
|
||||
try {
|
||||
this.action = PlayerAction.valueOf(enumValue);
|
||||
} catch (IllegalArgumentException e) {
|
||||
//They aren't on a legacy version and the enum has changed
|
||||
final PlayerAction.UpdatedPlayerAction updatedPlayerAction = PlayerAction.UpdatedPlayerAction.valueOf(enumValue);
|
||||
this.action = PlayerAction.get(updatedPlayerAction.getIndex());
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
this.entityId = entityId;
|
||||
this.jumpBoost = jumpBoost;
|
||||
this.action = PlayerAction.get(animationIndex);
|
||||
|
||||
if (animationIndex != -1) {
|
||||
this.action = PlayerAction.get(animationIndex);
|
||||
}
|
||||
this.entity = NMSUtils.getEntityById(this.entityId);
|
||||
} catch (IllegalAccessException e) {
|
||||
e.printStackTrace();
|
||||
|
||||
+1
@@ -139,6 +139,7 @@ public class WrappedPacketInFlying extends WrappedPacket {
|
||||
public WrappedPacketInLook(Object packet) {
|
||||
super(packet);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isPosition() {
|
||||
try {
|
||||
|
||||
+53
@@ -0,0 +1,53 @@
|
||||
package io.github.retrooper.packetevents.packetwrappers.in.steervehicle;
|
||||
|
||||
import io.github.retrooper.packetevents.packetwrappers.api.WrappedPacket;
|
||||
import io.github.retrooper.packetevents.reflectionutils.Reflection;
|
||||
import io.github.retrooper.packetevents.utils.NMSUtils;
|
||||
|
||||
public class WrappedPacketInSteerVehicle extends WrappedPacket {
|
||||
private float side, forward;
|
||||
private boolean jump, unmount;
|
||||
|
||||
public WrappedPacketInSteerVehicle(Object packet) {
|
||||
super(packet);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void setup() {
|
||||
try {
|
||||
this.side = Reflection.getField(packetClass, float.class, 0).getFloat(packet);
|
||||
this.forward = Reflection.getField(packetClass, float.class, 1).getFloat(packet);
|
||||
|
||||
this.jump = Reflection.getField(packetClass, boolean.class, 0).getBoolean(packet);
|
||||
this.unmount = Reflection.getField(packetClass, boolean.class, 1).getBoolean(packet);
|
||||
} catch (IllegalAccessException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
//Positive side value means left, negative means right
|
||||
public float getSideValue() {
|
||||
return side;
|
||||
}
|
||||
//Positive forward value means forward, negative is backwards
|
||||
public float getForwardValue() {
|
||||
return forward;
|
||||
}
|
||||
|
||||
public boolean isJump() {
|
||||
return jump;
|
||||
}
|
||||
|
||||
public boolean isUnmount() {
|
||||
return unmount;
|
||||
}
|
||||
|
||||
private static Class<?> packetClass;
|
||||
|
||||
static {
|
||||
try {
|
||||
packetClass = NMSUtils.getNMSClass("PacketPlayInSteerVehicle");
|
||||
} catch (ClassNotFoundException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
+1
-2
@@ -23,8 +23,7 @@ public final class WrappedPacketOutChat extends WrappedPacket implements Sendabl
|
||||
//In 1.8.3+ the ChatSerializer class is declared in the IChatBaseComponent class, so we have to handle that
|
||||
try {
|
||||
chatSerializerClass = NMSUtils.getNMSClass("ChatSerializer");
|
||||
}
|
||||
catch(ClassNotFoundException e) {
|
||||
} catch (ClassNotFoundException e) {
|
||||
//That is fine, it is probably a subclass
|
||||
chatSerializerClass = Reflection.getSubClass(iChatBaseComponentClass, "ChatSerializer");
|
||||
}
|
||||
|
||||
@@ -21,6 +21,10 @@ public final class Reflection {
|
||||
|
||||
|
||||
//FIELDS
|
||||
private static final HashMap<FieldWithName, Field> fieldWithNameHashMap = new HashMap<FieldWithName, Field>();
|
||||
private static final HashMap<FieldWithTypeAndIndex, Field> fieldWithTypeAndIndexHashMap = new HashMap<FieldWithTypeAndIndex, Field>();
|
||||
private static final HashMap<FieldWithIndex, Field> fieldWithIndexHashMap = new HashMap<FieldWithIndex, Field>();
|
||||
private static final HashMap<MethodWithIndexAndParams, Method> methodWithIndexAndParamsHashMap = new HashMap<MethodWithIndexAndParams, Method>();
|
||||
|
||||
public static Field[] getFields(final Class<?> cls) {
|
||||
if (cachedFields.containsKey(cls)) {
|
||||
@@ -35,8 +39,6 @@ public final class Reflection {
|
||||
}
|
||||
}
|
||||
|
||||
private static final HashMap<FieldWithName, Field> fieldWithNameHashMap = new HashMap<FieldWithName, Field>();
|
||||
|
||||
public static Field getField(final Class<?> cls, final String name) {
|
||||
final FieldWithName fwn = new FieldWithName(cls, name);
|
||||
if (!fieldWithNameHashMap.containsKey(fwn)) {
|
||||
@@ -52,8 +54,6 @@ public final class Reflection {
|
||||
return null;
|
||||
}
|
||||
|
||||
private static final HashMap<FieldWithTypeAndIndex, Field> fieldWithTypeAndIndexHashMap = new HashMap<FieldWithTypeAndIndex, Field>();
|
||||
|
||||
public static Field getField(final Class<?> cls, final Class<?> dataType, final int index) {
|
||||
final FieldWithTypeAndIndex fwtai = new FieldWithTypeAndIndex(cls, dataType, index);
|
||||
if (!fieldWithTypeAndIndexHashMap.containsKey(fwtai)) {
|
||||
@@ -72,8 +72,6 @@ public final class Reflection {
|
||||
return null;
|
||||
}
|
||||
|
||||
private static final HashMap<FieldWithIndex, Field> fieldWithIndexHashMap = new HashMap<FieldWithIndex, Field>();
|
||||
|
||||
public static Field getField(final Class<?> cls, final int index) {
|
||||
final FieldWithIndex fwi = new FieldWithIndex(cls, index);
|
||||
if (!fieldWithIndexHashMap.containsKey(fwi)) {
|
||||
@@ -104,8 +102,6 @@ public final class Reflection {
|
||||
}
|
||||
}
|
||||
|
||||
private static final HashMap<MethodWithIndexAndParams, Method> methodWithIndexAndParamsHashMap = new HashMap<MethodWithIndexAndParams, Method>();
|
||||
|
||||
public static Method getMethod(final Class<?> cls, final int index, final Class<?>... params) {
|
||||
final MethodWithIndexAndParams mwiap = new MethodWithIndexAndParams(cls, index, params);
|
||||
if (!methodWithIndexAndParamsHashMap.containsKey(mwiap)) {
|
||||
|
||||
+1
@@ -4,6 +4,7 @@ public class FieldWithName {
|
||||
|
||||
private final Class<?> cls;
|
||||
private final String name;
|
||||
|
||||
public FieldWithName(Class<?> cls, String name) {
|
||||
this.cls = cls;
|
||||
this.name = name;
|
||||
|
||||
@@ -10,7 +10,7 @@ public class BlockIteratorUtils {
|
||||
final Location location = player.getEyeLocation();
|
||||
final BlockIterator blocksToAdd = new BlockIterator(location, 0.0D, distance);
|
||||
Block block = null;
|
||||
while(blocksToAdd.hasNext()) {
|
||||
while (blocksToAdd.hasNext()) {
|
||||
block = blocksToAdd.next();
|
||||
}
|
||||
return block;
|
||||
|
||||
@@ -81,6 +81,15 @@ public final class NMSUtils {
|
||||
return Class.forName(nmsDir + "." + name);
|
||||
}
|
||||
|
||||
public static Class<?> getNMSClassWithoutException(String name) {
|
||||
try {
|
||||
return getNMSClass(name);
|
||||
} catch (ClassNotFoundException e) {
|
||||
return null;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public static Class<?> getOBCClass(String name) throws ClassNotFoundException {
|
||||
return Class.forName(obcDir + "." + name);
|
||||
}
|
||||
|
||||
@@ -1,15 +0,0 @@
|
||||
package io.github.retrooper.packetevents.utils;
|
||||
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.entity.Player;
|
||||
import protocolsupport.api.ProtocolSupportAPI;
|
||||
|
||||
public class ProtocolSupportUtils {
|
||||
public static boolean isProtocolSupportAvailable() {
|
||||
return Bukkit.getPluginManager().getPlugin("ProtocolSupport") != null;
|
||||
}
|
||||
|
||||
public static int getProtocolVersion(final Player player) {
|
||||
return ProtocolSupportAPI.getProtocolVersion(player).getId();
|
||||
}
|
||||
}
|
||||
@@ -1,14 +0,0 @@
|
||||
package io.github.retrooper.packetevents.utils;
|
||||
|
||||
import org.bukkit.entity.Player;
|
||||
|
||||
public class VersionLookupUtils {
|
||||
public static int getProtocolVersion(final Player player) {
|
||||
if (ViaUtils.isViaAPIAvaialable()) {
|
||||
return ViaUtils.getProtocolVersion(player);
|
||||
} else if (ProtocolSupportUtils.isProtocolSupportAvailable()) {
|
||||
return ProtocolSupportUtils.getProtocolVersion(player);
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,10 @@
|
||||
package io.github.retrooper.packetevents.utils.api;
|
||||
|
||||
import com.comphenix.protocol.ProtocolLibrary;
|
||||
import org.bukkit.entity.Player;
|
||||
|
||||
class ProtocolLibAPIAccessor {
|
||||
public static int getProtocolVersion(final Player player) {
|
||||
return ProtocolLibrary.getProtocolManager().getProtocolVersion(player);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
package io.github.retrooper.packetevents.utils.api;
|
||||
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.entity.Player;
|
||||
|
||||
public class ProtocolLibUtils {
|
||||
public static boolean isAvailable() {
|
||||
return Bukkit.getPluginManager().isPluginEnabled("ProtocolLib");
|
||||
}
|
||||
|
||||
public static int getProtocolVersion(final Player player) {
|
||||
return ProtocolLibAPIAccessor.getProtocolVersion(player);
|
||||
}
|
||||
}
|
||||
+1
-1
@@ -1,4 +1,4 @@
|
||||
package io.github.retrooper.packetevents.utils;
|
||||
package io.github.retrooper.packetevents.utils.api;
|
||||
|
||||
import org.bukkit.entity.Player;
|
||||
import protocolsupport.api.ProtocolSupportAPI;
|
||||
@@ -0,0 +1,14 @@
|
||||
package io.github.retrooper.packetevents.utils.api;
|
||||
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.entity.Player;
|
||||
|
||||
public class ProtocolSupportUtils {
|
||||
public static boolean isAvailable() {
|
||||
return Bukkit.getPluginManager().isPluginEnabled("ProtocolSupport");
|
||||
}
|
||||
|
||||
public static int getProtocolVersion(final Player player) {
|
||||
return ProtocolSupportAPIAccessor.getProtocolVersion(player);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,25 @@
|
||||
package io.github.retrooper.packetevents.utils.api;
|
||||
|
||||
import org.bukkit.entity.Player;
|
||||
|
||||
public class VersionLookupUtils {
|
||||
/**
|
||||
* If ViaVersion is present, we get the protocol version with the ViaVersion API.
|
||||
* If ProtocolSupport is present, we get the protocol version with the ProtocolSupport API.
|
||||
* If ProtocolLib is present, we get the protocol version with the ProtocolLib API.
|
||||
* Otherwise return -1
|
||||
*
|
||||
* @param player
|
||||
* @return protocol version of the player if ViaVersion or ProtocolSupport or ProtocolLib is present. Otherwise -1
|
||||
*/
|
||||
public static int getProtocolVersion(final Player player) {
|
||||
if (ViaUtils.isAvailable()) {
|
||||
return ViaUtils.getProtocolVersion(player);
|
||||
} else if (ProtocolSupportUtils.isAvailable()) {
|
||||
return ProtocolSupportUtils.getProtocolVersion(player);
|
||||
} else if (ProtocolLibUtils.isAvailable()) {
|
||||
return ProtocolLibUtils.getProtocolVersion(player);
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
+1
-1
@@ -1,4 +1,4 @@
|
||||
package io.github.retrooper.packetevents.utils;
|
||||
package io.github.retrooper.packetevents.utils.api;
|
||||
|
||||
import org.bukkit.entity.Player;
|
||||
import us.myles.ViaVersion.api.Via;
|
||||
+3
-3
@@ -1,4 +1,4 @@
|
||||
package io.github.retrooper.packetevents.utils;
|
||||
package io.github.retrooper.packetevents.utils.api;
|
||||
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.entity.Player;
|
||||
@@ -9,7 +9,7 @@ public class ViaUtils {
|
||||
return ViaAPIAccessor.getProtocolVersion(player);
|
||||
}
|
||||
|
||||
public static boolean isViaAPIAvaialable() {
|
||||
return Bukkit.getPluginManager().getPlugin("ViaVersion") != null;
|
||||
public static boolean isAvailable() {
|
||||
return Bukkit.getPluginManager().isPluginEnabled("ViaVersion");
|
||||
}
|
||||
}
|
||||
+1
-1
@@ -13,7 +13,7 @@ import java.lang.reflect.Method;
|
||||
|
||||
public final class EntityFinderUtils {
|
||||
@Nullable
|
||||
private static final ServerVersion version = PacketEvents.getServerVersion();
|
||||
private static final ServerVersion version = PacketEvents.getAPI().getServerUtilities().getServerVersion();
|
||||
private static Class<?> worldServerClass;
|
||||
private static Class<?> craftWorldClass;
|
||||
private static Class<?> entityClass;
|
||||
|
||||
Reference in new issue
Block a user